diff --git a/constants/blm_service.go b/constants/blm_service.go new file mode 100644 index 00000000..d4a36c9e --- /dev/null +++ b/constants/blm_service.go @@ -0,0 +1,5 @@ +package constants + +const ( + BLMServiceUrlLogoCombine string = "/LogoCombine" +) diff --git a/initalize/service.go b/initalize/service.go new file mode 100644 index 00000000..7e486360 --- /dev/null +++ b/initalize/service.go @@ -0,0 +1,24 @@ +package initalize + +import ( + "fusenapi/service/repositories" + + "github.com/aws/aws-sdk-go/aws/session" + "gorm.io/gorm" +) + +type Repositories struct { + ImageHandle repositories.ImageHandle +} + +type NewAllRepositorieData struct { + GormDB *gorm.DB + BLMServiceUrl *string + AwsSession *session.Session +} + +func NewAllRepositories(newData *NewAllRepositorieData) *Repositories { + return &Repositories{ + ImageHandle: repositories.NewImageHandle(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), + } +} diff --git a/server/resource/etc/resource.yaml b/server/resource/etc/resource.yaml index befe8caf..473ac3f3 100644 --- a/server/resource/etc/resource.yaml +++ b/server/resource/etc/resource.yaml @@ -15,6 +15,7 @@ AWS: Secret: sjCEv0JxATnPCxno2KNLm0X8oDc7srUR+4vkYhvm Token: BLMService: + Url: "http://18.119.109.254:8999" LogoCombine: #Url: "http://192.168.1.7:8999/LogoCombine" Url: "http://18.119.109.254:8999/LogoCombine" \ No newline at end of file diff --git a/server/resource/internal/config/config.go b/server/resource/internal/config/config.go index 51aad1c7..e8569f26 100644 --- a/server/resource/internal/config/config.go +++ b/server/resource/internal/config/config.go @@ -21,6 +21,7 @@ type Config struct { } } BLMService struct { + Url string LogoCombine struct { Url string } diff --git a/server/resource/internal/logic/logocombinelogic.go b/server/resource/internal/logic/logocombinelogic.go index 86da8d7c..bad59cb9 100644 --- a/server/resource/internal/logic/logocombinelogic.go +++ b/server/resource/internal/logic/logocombinelogic.go @@ -1,17 +1,9 @@ package logic import ( - "encoding/json" - "errors" - "fusenapi/model/gmodel" + "fusenapi/service/repositories" "fusenapi/utils/auth" "fusenapi/utils/basic" - "fusenapi/utils/curl" - "fusenapi/utils/file" - "fusenapi/utils/hash" - "io" - "strings" - "time" "context" @@ -19,7 +11,6 @@ import ( "fusenapi/server/resource/internal/types" "github.com/zeromicro/go-zero/core/logx" - "gorm.io/gorm" ) type LogoCombineLogic struct { @@ -65,125 +56,22 @@ func (l *LogoCombineLogic) LogoCombine(req *types.LogoCombineReq, userinfo *auth // 否则,使用用户ID和用户键名格式 userId = userinfo.UserId } - - // 根据hash 查询数据资源 - var resourceId string = hash.JsonHashKey(req.ResourceKey) - resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) - resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId) - if err == nil && resourceInfo.ResourceId != "" { - return resp.SetStatus(basic.CodeOK, map[string]interface{}{ - "resource_id": resourceId, - "resource_url": resourceInfo.ResourceUrl, - "resource_metadata": resourceInfo.Metadata, - }) - } else { - if err != nil { - if !errors.Is(err, gorm.ErrRecordNotFound) { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error") - } - } - } - - // 没有查到,先根据模版id 查询模版数据 请求算法数据 - productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn) - productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, req.TemplateId) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error") - } - var groupOptions map[string]interface{} - if productTemplateV2Info.GroupOptions != nil { - err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal groupOptions") - } - } - - var materialList []interface{} - if productTemplateV2Info.TemplateInfo != nil { - var templateInfo map[string]interface{} - err = json.Unmarshal([]byte(*productTemplateV2Info.TemplateInfo), &templateInfo) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal templateInfo") - } - materialList = templateInfo["materialList"].([]interface{}) - } - - var moduleDataMap = make(map[string]interface{}, 4) - moduleDataMap["id"] = productTemplateV2Info.Id - moduleDataMap["material"] = productTemplateV2Info.MaterialImg - moduleDataMap["groupOptions"] = groupOptions - moduleDataMap["materialList"] = materialList - - var combineParam map[string]interface{} - json.Unmarshal([]byte(req.CombineParam), &combineParam) - var postMap = make(map[string]interface{}, 2) - postMap["module_data"] = moduleDataMap - postMap["param_data"] = combineParam - postMapB, _ := json.Marshal(postMap) - - //result, err := http.Post(l.svcCtx.Config.BLMService.LogoCombine.Url, "application/json", strings.NewReader(string(postMapB))) - var headerData = make(map[string]string, 1) - headerData["Content-Type"] = "application/json" - result, err := curl.ApiCall(l.svcCtx.Config.BLMService.LogoCombine.Url, "POST", headerData, strings.NewReader(string(postMapB)), time.Second*20) - - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service post fail") - } - defer result.Body.Close() - b, err := io.ReadAll(result.Body) - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail") - } - ress := string(b) - - if ress == "Internal Server Error" { - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail") - } - - var resultData map[string]interface{} - err = json.Unmarshal(b, &resultData) - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail") - } - // { - // id: "", - // logo_url:"https://s3.amazon.com/xxxx", - // result: "$saa541afaldjaldjasldjsadjsapsaasda" - // } - var fileBase = resultData["result"] - - // 上传文件 - var upload = file.Upload{ - Ctx: l.ctx, - MysqlConn: l.svcCtx.MysqlConn, - AwsSession: l.svcCtx.AwsSession, - } - uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ - FileHash: resourceId, - FileData: fileBase.(string), - UploadBucket: 1, - ApiType: 2, + res, err := l.svcCtx.Repositories.ImageHandle.LogoCombine(l.ctx, &repositories.LogoCombineReq{ + ResourceKey: req.ResourceKey, + TemplateId: req.TemplateId, + CombineParam: req.CombineParam, UserId: userId, GuestId: guestId, }) + if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileLogoCombineErr, "LogoCombine error upload file failed") + return resp.SetStatus(basic.CodeServiceErr) } + // 返回成功的响应和上传URL return resp.SetStatus(basic.CodeOK, map[string]interface{}{ - "resource_id": resourceId, - "resource_url": uploadRes.ResourceUrl, + "resource_id": res.ResourceId, + "resource_url": res.ResourceUrl, "resource_metadata": "", }) } diff --git a/server/resource/internal/svc/servicecontext.go b/server/resource/internal/svc/servicecontext.go index a81a0767..e006fbce 100644 --- a/server/resource/internal/svc/servicecontext.go +++ b/server/resource/internal/svc/servicecontext.go @@ -19,10 +19,11 @@ import ( type ServiceContext struct { Config config.Config - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen - RabbitMq *initalize.RabbitMqHandle - AwsSession *session.Session + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle + AwsSession *session.Session + Repositories *initalize.Repositories } func NewServiceContext(c config.Config) *ServiceContext { @@ -35,6 +36,11 @@ func NewServiceContext(c config.Config) *ServiceContext { AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), AwsSession: session.Must(session.NewSession(&config)), + Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{ + GormDB: initalize.InitMysql(c.SourceMysql), + BLMServiceUrl: &c.BLMService.Url, + AwsSession: session.Must(session.NewSession(&config)), + }), } } diff --git a/service/repositories/image_handle.go b/service/repositories/image_handle.go new file mode 100644 index 00000000..5e873b3f --- /dev/null +++ b/service/repositories/image_handle.go @@ -0,0 +1,179 @@ +package repositories + +import ( + "context" + "encoding/json" + "errors" + "fusenapi/constants" + "fusenapi/model/gmodel" + "fusenapi/utils/curl" + "fusenapi/utils/file" + "fusenapi/utils/hash" + "io" + "strings" + "time" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +func NewImageHandle(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) ImageHandle { + return &defaultImageHandle{ + MysqlConn: gormDB, + BLMServiceUrl: bLMServiceUrl, + AwsSession: awsSession, + } +} + +type ( + defaultImageHandle struct { + MysqlConn *gorm.DB + BLMServiceUrl *string + AwsSession *session.Session + } + + ImageHandle = interface { + + // logo合图 + LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) + } +) + +/* logo合图 */ +type ( + LogoCombineReq struct { + ResourceKey string `json:"resource_key"` + TemplateId int64 `json:"template_id"` + CombineParam string `json:"combine_param"` + UserId int64 `json:"user_id"` + GuestId int64 `json:"guest_id"` + } + LogoCombineRes struct { + ResourceId string + ResourceUrl *string + Metadata *string + } +) + +func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) { + // 根据hash 查询数据资源 + var resourceId string = hash.JsonHashKey(in.ResourceKey) + resourceModel := gmodel.NewFsResourceModel(l.MysqlConn) + resourceInfo, err := resourceModel.FindOneById(ctx, resourceId) + if err == nil && resourceInfo.ResourceId != "" { + return &LogoCombineRes{ + ResourceId: resourceId, + ResourceUrl: resourceInfo.ResourceUrl, + }, nil + } else { + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + logx.Error(err) + return nil, err + } + } + } + + // 没有查到,先根据模版id 查询模版数据 请求算法数据 + productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.MysqlConn) + productTemplateV2Info, err := productTemplateV2Model.FindOne(ctx, in.TemplateId) + + if err != nil { + logx.Error(err) + return nil, err + } + var groupOptions map[string]interface{} + if productTemplateV2Info.GroupOptions != nil { + err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions) + + if err != nil { + logx.Error(err) + return nil, err + } + } + + var materialList []interface{} + if productTemplateV2Info.TemplateInfo != nil { + var templateInfo map[string]interface{} + err = json.Unmarshal([]byte(*productTemplateV2Info.TemplateInfo), &templateInfo) + + if err != nil { + logx.Error(err) + return nil, err + } + materialList = templateInfo["materialList"].([]interface{}) + } + + var moduleDataMap = make(map[string]interface{}, 4) + moduleDataMap["id"] = productTemplateV2Info.Id + moduleDataMap["material"] = productTemplateV2Info.MaterialImg + moduleDataMap["groupOptions"] = groupOptions + moduleDataMap["materialList"] = materialList + + var combineParam map[string]interface{} + json.Unmarshal([]byte(in.CombineParam), &combineParam) + var postMap = make(map[string]interface{}, 2) + postMap["module_data"] = moduleDataMap + postMap["param_data"] = combineParam + postMapB, _ := json.Marshal(postMap) + + var headerData = make(map[string]string, 1) + headerData["Content-Type"] = "application/json" + result, err := curl.ApiCall(*l.BLMServiceUrl+constants.BLMServiceUrlLogoCombine, "POST", headerData, strings.NewReader(string(postMapB)), time.Second*20) + + if err != nil { + logx.Error(err) + return nil, err + } + defer result.Body.Close() + b, err := io.ReadAll(result.Body) + if err != nil { + logx.Error(err) + return nil, err + } + ress := string(b) + + if ress == "Internal Server Error" { + logx.Error(errors.New("BLMService fail Internal Server Error")) + return nil, err + } + + var resultData map[string]interface{} + err = json.Unmarshal(b, &resultData) + if err != nil { + logx.Error(err) + return nil, err + } + // { + // id: "", + // logo_url:"https://s3.amazon.com/xxxx", + // result: "$saa541afaldjaldjasldjsadjsapsaasda" + // } + var fileBase = resultData["result"] + + // 上传文件 + var upload = file.Upload{ + Ctx: ctx, + MysqlConn: l.MysqlConn, + AwsSession: l.AwsSession, + } + uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ + FileHash: resourceId, + FileData: fileBase.(string), + UploadBucket: 1, + ApiType: 2, + UserId: in.UserId, + GuestId: in.GuestId, + }) + if err != nil { + logx.Error(err) + return nil, err + } + return &LogoCombineRes{ + ResourceId: uploadRes.ResourceId, + ResourceUrl: &uploadRes.ResourceUrl, + }, nil +} + +/* logo合图 */