package logic import ( "encoding/json" "errors" "fusenapi/constants" "fusenapi/utils/auth" "fusenapi/utils/basic" "gorm.io/gorm" "strings" "context" "fusenapi/server/product/internal/svc" "fusenapi/server/product/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetLightByPidLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetLightByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLightByPidLogic { return &GetLightByPidLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetLightByPidLogic) GetLightByPid(req *types.GetLightByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) { req.Pid = strings.Trim(req.Pid, " ") if req.Pid == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty") } //获取产品信息(只是获取id) productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id") if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists") } logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") } //获取尺寸ids sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list") } if len(sizeList) == 0 { return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty") } sizeIds := make([]int64, 0, len(sizeList)) for _, v := range sizeList { sizeIds = append(sizeIds, v.Id) } //获取模型列表 modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") } if len(modelList) == 0 { return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty") } lightIds := make([]int64, 0, len(modelList)) for _, v := range modelList { lightIds = append(lightIds, *v.Light) } //获取光源数据 lightList, err := l.svcCtx.AllModels.FsProductModel3dLight.GetAllByIds(l.ctx, lightIds) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get light list") } //组装数据 listRsp := make([]interface{}, 0, len(lightList)) for _, v := range lightList { var lightInfo map[string]interface{} if v.Info == nil || *v.Info == "" { continue } if err = json.Unmarshal([]byte(*v.Info), &lightInfo); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse light info") } lightInfo["id"] = v.Id listRsp = append(listRsp, lightInfo) } return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp) }