From dd42e1f3cb38912b6fd2c2784c52e9a5de1f21a1 Mon Sep 17 00:00:00 2001 From: sxy <3187870250@qq.com> Date: Wed, 12 Mar 2025 14:24:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=8EExcel=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=88=B0=E6=95=B0=E6=8D=AE=E5=BA=93=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/exhibition/exhibition.pb.go | 52 +++++++++++--------- api/exhibition/exhibition_triple.pb.go | 45 +++++++++++++++++ conf/config.yaml | 10 ++-- pkg/service/common/common.go | 37 ++++++++++---- pkg/service/register_record.go | 67 +++++++++++--------------- 5 files changed, 136 insertions(+), 75 deletions(-) diff --git a/api/exhibition/exhibition.pb.go b/api/exhibition/exhibition.pb.go index 8c8bd49..5ed738d 100644 --- a/api/exhibition/exhibition.pb.go +++ b/api/exhibition/exhibition.pb.go @@ -926,7 +926,7 @@ var file_pb_exhibition_proto_rawDesc = []byte{ 0x61, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x65, 0x78, 0x68, 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x45, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x32, 0x8a, 0x03, 0x0a, 0x0a, 0x45, 0x78, 0x68, 0x69, + 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x32, 0xdd, 0x03, 0x0a, 0x0a, 0x45, 0x78, 0x68, 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x42, 0x0a, 0x0a, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x50, 0x68, 0x6f, 0x6e, 0x65, 0x12, 0x18, 0x2e, 0x65, 0x78, 0x68, 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x1a, 0x1a, @@ -951,8 +951,14 @@ var file_pb_exhibition_proto_rawDesc = []byte{ 0x65, 0x78, 0x68, 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x49, 0x6e, 0x66, 0x6f, 0x1a, 0x1b, 0x2e, 0x65, 0x78, 0x68, 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x49, 0x64, 0x43, 0x61, 0x72, 0x64, - 0x52, 0x65, 0x73, 0x70, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x70, 0x62, 0x2f, 0x65, 0x78, 0x68, - 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x52, 0x65, 0x73, 0x70, 0x12, 0x51, 0x0a, 0x11, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x49, 0x6e, + 0x66, 0x6f, 0x42, 0x79, 0x45, 0x78, 0x63, 0x65, 0x6c, 0x12, 0x18, 0x2e, 0x65, 0x78, 0x68, 0x69, + 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x49, + 0x6e, 0x66, 0x6f, 0x1a, 0x22, 0x2e, 0x65, 0x78, 0x68, 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, + 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x63, + 0x6f, 0x72, 0x64, 0x52, 0x65, 0x73, 0x70, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x70, 0x62, 0x2f, + 0x65, 0x78, 0x68, 0x69, 0x62, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, } var ( @@ -980,25 +986,27 @@ var file_pb_exhibition_proto_goTypes = []any{ (*ExportRecordResp)(nil), // 8: exhibition.ExportRecordResp } var file_pb_exhibition_proto_depIdxs = []int32{ - 0, // 0: exhibition.SaveRegisterRecordResp.data:type_name -> exhibition.RegisterInfo - 0, // 1: exhibition.CheckPhoneResp.data:type_name -> exhibition.RegisterInfo - 0, // 2: exhibition.RecordListResp.data:type_name -> exhibition.RegisterInfo - 7, // 3: exhibition.ExportRecordResp.data:type_name -> exhibition.ExportInfo - 0, // 4: exhibition.Exhibition.CheckPhone:input_type -> exhibition.RegisterInfo - 0, // 5: exhibition.Exhibition.SaveRegisterRecord:input_type -> exhibition.RegisterInfo - 4, // 6: exhibition.Exhibition.RegisterRecordList:input_type -> exhibition.RecordListReq - 6, // 7: exhibition.Exhibition.ExportRegisterRecord:input_type -> exhibition.ExportRecordReq - 0, // 8: exhibition.Exhibition.CheckIdCard:input_type -> exhibition.RegisterInfo - 2, // 9: exhibition.Exhibition.CheckPhone:output_type -> exhibition.CheckPhoneResp - 1, // 10: exhibition.Exhibition.SaveRegisterRecord:output_type -> exhibition.SaveRegisterRecordResp - 5, // 11: exhibition.Exhibition.RegisterRecordList:output_type -> exhibition.RecordListResp - 8, // 12: exhibition.Exhibition.ExportRegisterRecord:output_type -> exhibition.ExportRecordResp - 3, // 13: exhibition.Exhibition.CheckIdCard:output_type -> exhibition.CheckIdCardResp - 9, // [9:14] is the sub-list for method output_type - 4, // [4:9] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name + 0, // 0: exhibition.SaveRegisterRecordResp.data:type_name -> exhibition.RegisterInfo + 0, // 1: exhibition.CheckPhoneResp.data:type_name -> exhibition.RegisterInfo + 0, // 2: exhibition.RecordListResp.data:type_name -> exhibition.RegisterInfo + 7, // 3: exhibition.ExportRecordResp.data:type_name -> exhibition.ExportInfo + 0, // 4: exhibition.Exhibition.CheckPhone:input_type -> exhibition.RegisterInfo + 0, // 5: exhibition.Exhibition.SaveRegisterRecord:input_type -> exhibition.RegisterInfo + 4, // 6: exhibition.Exhibition.RegisterRecordList:input_type -> exhibition.RecordListReq + 6, // 7: exhibition.Exhibition.ExportRegisterRecord:input_type -> exhibition.ExportRecordReq + 0, // 8: exhibition.Exhibition.CheckIdCard:input_type -> exhibition.RegisterInfo + 0, // 9: exhibition.Exhibition.ImportInfoByExcel:input_type -> exhibition.RegisterInfo + 2, // 10: exhibition.Exhibition.CheckPhone:output_type -> exhibition.CheckPhoneResp + 1, // 11: exhibition.Exhibition.SaveRegisterRecord:output_type -> exhibition.SaveRegisterRecordResp + 5, // 12: exhibition.Exhibition.RegisterRecordList:output_type -> exhibition.RecordListResp + 8, // 13: exhibition.Exhibition.ExportRegisterRecord:output_type -> exhibition.ExportRecordResp + 3, // 14: exhibition.Exhibition.CheckIdCard:output_type -> exhibition.CheckIdCardResp + 1, // 15: exhibition.Exhibition.ImportInfoByExcel:output_type -> exhibition.SaveRegisterRecordResp + 10, // [10:16] is the sub-list for method output_type + 4, // [4:10] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name } func init() { file_pb_exhibition_proto_init() } diff --git a/api/exhibition/exhibition_triple.pb.go b/api/exhibition/exhibition_triple.pb.go index 77eb822..c937be8 100644 --- a/api/exhibition/exhibition_triple.pb.go +++ b/api/exhibition/exhibition_triple.pb.go @@ -33,6 +33,7 @@ type ExhibitionClient interface { RegisterRecordList(ctx context.Context, in *RecordListReq, opts ...grpc_go.CallOption) (*RecordListResp, common.ErrorWithAttachment) ExportRegisterRecord(ctx context.Context, in *ExportRecordReq, opts ...grpc_go.CallOption) (*ExportRecordResp, common.ErrorWithAttachment) CheckIdCard(ctx context.Context, in *RegisterInfo, opts ...grpc_go.CallOption) (*CheckIdCardResp, common.ErrorWithAttachment) + ImportInfoByExcel(ctx context.Context, in *RegisterInfo, opts ...grpc_go.CallOption) (*SaveRegisterRecordResp, common.ErrorWithAttachment) } type exhibitionClient struct { @@ -45,6 +46,7 @@ type ExhibitionClientImpl struct { RegisterRecordList func(ctx context.Context, in *RecordListReq) (*RecordListResp, error) ExportRegisterRecord func(ctx context.Context, in *ExportRecordReq) (*ExportRecordResp, error) CheckIdCard func(ctx context.Context, in *RegisterInfo) (*CheckIdCardResp, error) + ImportInfoByExcel func(ctx context.Context, in *RegisterInfo) (*SaveRegisterRecordResp, error) } func (c *ExhibitionClientImpl) GetDubboStub(cc *triple.TripleConn) ExhibitionClient { @@ -89,6 +91,12 @@ func (c *exhibitionClient) CheckIdCard(ctx context.Context, in *RegisterInfo, op return out, c.cc.Invoke(ctx, "/"+interfaceKey+"/CheckIdCard", in, out) } +func (c *exhibitionClient) ImportInfoByExcel(ctx context.Context, in *RegisterInfo, opts ...grpc_go.CallOption) (*SaveRegisterRecordResp, common.ErrorWithAttachment) { + out := new(SaveRegisterRecordResp) + interfaceKey := ctx.Value(constant.InterfaceKey).(string) + return out, c.cc.Invoke(ctx, "/"+interfaceKey+"/ImportInfoByExcel", in, out) +} + // ExhibitionServer is the server API for Exhibition service. // All implementations must embed UnimplementedExhibitionServer // for forward compatibility @@ -98,6 +106,7 @@ type ExhibitionServer interface { RegisterRecordList(context.Context, *RecordListReq) (*RecordListResp, error) ExportRegisterRecord(context.Context, *ExportRecordReq) (*ExportRecordResp, error) CheckIdCard(context.Context, *RegisterInfo) (*CheckIdCardResp, error) + ImportInfoByExcel(context.Context, *RegisterInfo) (*SaveRegisterRecordResp, error) mustEmbedUnimplementedExhibitionServer() } @@ -121,6 +130,9 @@ func (UnimplementedExhibitionServer) ExportRegisterRecord(context.Context, *Expo func (UnimplementedExhibitionServer) CheckIdCard(context.Context, *RegisterInfo) (*CheckIdCardResp, error) { return nil, status.Errorf(codes.Unimplemented, "method CheckIdCard not implemented") } +func (UnimplementedExhibitionServer) ImportInfoByExcel(context.Context, *RegisterInfo) (*SaveRegisterRecordResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method ImportInfoByExcel not implemented") +} func (s *UnimplementedExhibitionServer) XXX_SetProxyImpl(impl protocol.Invoker) { s.proxyImpl = impl } @@ -294,6 +306,35 @@ func _Exhibition_CheckIdCard_Handler(srv interface{}, ctx context.Context, dec f return interceptor(ctx, in, info, handler) } +func _Exhibition_ImportInfoByExcel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc_go.UnaryServerInterceptor) (interface{}, error) { + in := new(RegisterInfo) + if err := dec(in); err != nil { + return nil, err + } + base := srv.(dubbo3.Dubbo3GrpcService) + args := []interface{}{} + args = append(args, in) + md, _ := metadata.FromIncomingContext(ctx) + invAttachment := make(map[string]interface{}, len(md)) + for k, v := range md { + invAttachment[k] = v + } + invo := invocation.NewRPCInvocation("ImportInfoByExcel", args, invAttachment) + if interceptor == nil { + result := base.XXX_GetProxyImpl().Invoke(ctx, invo) + return result, result.Error() + } + info := &grpc_go.UnaryServerInfo{ + Server: srv, + FullMethod: ctx.Value("XXX_TRIPLE_GO_INTERFACE_NAME").(string), + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + result := base.XXX_GetProxyImpl().Invoke(ctx, invo) + return result, result.Error() + } + return interceptor(ctx, in, info, handler) +} + // Exhibition_ServiceDesc is the grpc_go.ServiceDesc for Exhibition service. // It's only intended for direct use with grpc_go.RegisterService, // and not to be introspected or modified (even as a copy) @@ -321,6 +362,10 @@ var Exhibition_ServiceDesc = grpc_go.ServiceDesc{ MethodName: "CheckIdCard", Handler: _Exhibition_CheckIdCard_Handler, }, + { + MethodName: "ImportInfoByExcel", + Handler: _Exhibition_ImportInfoByExcel_Handler, + }, }, Streams: []grpc_go.StreamDesc{}, Metadata: "pb/exhibition.proto", diff --git a/conf/config.yaml b/conf/config.yaml index dca0efb..bc53b58 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -27,10 +27,10 @@ jwt: key: "3Ei*^!a6^6$w^wgK" expire: 24 bos: - AccessKeyId: "LTAI5tHfjSmWXHqfWgaL7Uo5" - AccessKeySecret: "kOPctFZ3DHsbdSSym1fLyDK39hkzPI" - BucketName: "erp-k8s-store" - Endpoint: "oss-cn-hangzhou-internal.aliyuncs.com" + AccessKeyId: "LTAI5tLz1fSK53FQAEC9uNSb" + AccessKeySecret: "oGB9chrQzQzITXR2IGv37Ji5WxZh4j" + BucketName: "fontree-test" + Endpoint: "oss-cn-hangzhou.aliyuncs.com" BosBaseDir: "exhibition" Host: "https://oss-cn-hangzhou.aliyuncs.com" - CdnHost: "https://e-cdn.fontree.cn" + CdnHost: "https://cdn-test.szjixun.cn" diff --git a/pkg/service/common/common.go b/pkg/service/common/common.go index c6e14cd..1de35f2 100644 --- a/pkg/service/common/common.go +++ b/pkg/service/common/common.go @@ -6,6 +6,16 @@ import ( "encoding/json" "errors" "fmt" + "io/ioutil" + "mime/multipart" + "net/url" + "os" + "path" + "path/filepath" + "strconv" + "strings" + "sync" + "github.com/disintegration/imaging" "github.com/dubbogo/gost/log/logger" "github.com/exhibition-main/internal/config" @@ -19,15 +29,6 @@ import ( uuid "github.com/satori/go.uuid" ffmpeg "github.com/u2takey/ffmpeg-go" "go.uber.org/zap" - "io/ioutil" - "mime/multipart" - "net/url" - "os" - "path" - "path/filepath" - "strconv" - "strings" - "sync" ) var ( @@ -305,3 +306,21 @@ func BaiduCheckImage(imageByte []byte) (err error) { } return nil } + +// 图片转url +func UploadFile(fileBytes []byte) (url string, err error) { + mask := "default" + source := "国展报名" + uuids := uuid.NewV4() + filePath := fmt.Sprintf("%s/%s/%s.png", mask, source, uuids) + + var objectName string = fmt.Sprintf("%s/%s/%s", config.Data.Bos.BosBaseDir, config.Data.System.Mode, filePath) + BOSClient, _ := objstorage.NewOSS(config.Data.Bos.AccessKeyId, config.Data.Bos.AccessKeySecret, config.Data.Bos.Endpoint) + _, err = BOSClient.PutObjectFromBytes(config.Data.Bos.BucketName, objectName, fileBytes) + if err != nil { + logger.Errorf("UploadImg PutObjectFromBytes err", err) + return "", err + } + url = fmt.Sprintf("%s/%s", config.Data.Bos.CdnHost, objectName) + return +} diff --git a/pkg/service/register_record.go b/pkg/service/register_record.go index c1e147f..ec224f7 100644 --- a/pkg/service/register_record.go +++ b/pkg/service/register_record.go @@ -15,6 +15,7 @@ import ( "github.com/exhibition-main/internal/model" "github.com/exhibition-main/internal/msg" "github.com/exhibition-main/internal/response" + "github.com/exhibition-main/pkg/service/common" "github.com/exhibition-main/pkg/utils" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" @@ -285,6 +286,8 @@ func OcrBase64(c *gin.Context) { response.ResponseQuickMsg(c, msg.Ok, "操作成功", res) return } + +// 导入数据到数据库(一次性) func ImportRecordByExcel(c *gin.Context) { file, err := c.FormFile("file") if err != nil { @@ -326,30 +329,19 @@ func ImportRecordByExcel(c *gin.Context) { continue } - if len(row) < 12 { // 确保列数足够 + if len(row) < 3 { // 确保列数足够 continue } - // artworkUrl := "" - // if pics, err := excelFile.GetPictures(sheetName, fmt.Sprintf("K%d", i+1)); err == nil && len(pics) > 0 { - // tmpFile, err := ioutil.TempFile("", fmt.Sprintf("oss_upload_%d*.jpg", i)) - // if err != nil { - // logger.Errorf("创建临时文件失败: %v", err) - // response.ResponseQuickMsg(c, msg.Fail, "创建临时文件失败", nil) - // return - // } - // defer os.Remove(tmpFile.Name()) - // if _, err := tmpFile.Write(pics[0].File); err != nil { - // logger.Errorf("写入临时文件失败: %v", err) - // response.ResponseQuickMsg(c, msg.Fail, "写入临时文件失败", nil) - // return - // } - // //objectKey := fmt.Sprintf("artwork/%s/%d.jpg", time.Now().Format("20060102"), time.Now().UnixNano()) - // if url, err := common.PutBos(tmpFile.Name(), "image", false); err == nil { - // artworkUrl = url - // } else { - // logger.Errorf("上传图片失败: %v", err) - // } - // } + artworkUrl := "" + if pics, err := excelFile.GetPictures(sheetName, fmt.Sprintf("K%d", i+1)); err == nil && len(pics) > 0 { + if url, err := common.UploadFile(pics[0].File); err != nil { + logger.Errorf("上传到服务器失败:%v", err) + response.ResponseQuickMsg(c, msg.Fail, "上传到服务器失败:"+err.Error(), nil) + return + } else { + artworkUrl = url + } + } record := &exhibition.RegisterInfo{ PreliminaryRatingNo: row[0], ReRatingNo: row[1], @@ -360,32 +352,27 @@ func ImportRecordByExcel(c *gin.Context) { ArtistName: row[6], PhoneNum: string(row[7]), Province: string(row[8]), - IdCard: string(row[9]), - //ArtworkFile: artworkUrl, + IdCard: row[9], + ArtworkFile: artworkUrl, + Gender: GetGenderByIdCard(row[9]), } - record.Gender = GetGenderByIdCard(record.IdCard) records = append(records, record) } - - for i, r := range records { - recordListReq := exhibition.RecordListReq{ - Keyword: r.ArtistName, - } - resp, err := GrpcExhibitionClientImpl.RegisterRecordList(context.Background(), &recordListReq) - if err != nil { - response.ResponseQuickMsg(c, msg.Fail, err.Error(), nil) - return - } - records[i].Uuid = resp.Data[i].Uuid + for k, v := range records { + fmt.Println(k, v) } for _, r := range records { - _, err := GrpcExhibitionClientImpl.SaveRegisterRecord(context.Background(), r) + // registerInfo := &exhibition.RegisterInfo{ + // ArtistName: r.ArtistName, + // ArtworkName: r.ArtworkName, + // } + _, err := GrpcExhibitionClientImpl.ImportInfoByExcel(context.Background(), r) if err != nil { response.ResponseQuickMsg(c, msg.Fail, err.Error(), nil) return } - } + } response.ResponseQuickMsg(c, msg.Ok, "ok", "") return @@ -395,6 +382,9 @@ func ImportRecordByExcel(c *gin.Context) { // 参数:idCard - 身份证号码字符串 // 返回值:1-男,2-女,0-未知 func GetGenderByIdCard(idCard string) int32 { + if idCard == "" { + return 0 + } if len(idCard) != 18 && len(idCard) != 15 { return 0 } @@ -409,7 +399,6 @@ func GetGenderByIdCard(idCard string) int32 { if err != nil { return 0 } - if genderNum%2 == 0 { return 2 // 女 }