package logic import ( "encoding/json" "errors" "fmt" "fusenapi/model/gmodel" "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 GetTemplateByPidLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetTemplateByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTemplateByPidLogic { return &GetTemplateByPidLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, 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") } if req.ProductTemplateTagId <= 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product_template_tag_id") } //获取产品信息(只获取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") } //获取该产品该模板标签下的模板列表(只获取id) templateList, err := l.svcCtx.AllModels.FsProductTemplateV2. GetListByProductAndTemplateTag(l.ctx, fmt.Sprintf("%d", req.ProductTemplateTagId), productInfo.Id, "id") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get templates") } if len(templateList) == 0 { return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "templates are not exists") } templateIds := make([]int64, 0, len(templateList)) for _, v := range templateList { templateIds = append(templateIds, v.Id) } var ( templateInfo *gmodel.FsProductTemplateV2 ) //传了指定物料,则获取该物料的第一个模板 if req.ProductSizeId > 0 { templateInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.GetSizeLatestTemplate(l.ctx, templateIds, productInfo.Id) } else { //没有指定物料则获取第一个物料的第一个模板 templateInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.GetProductLatestTemplate(l.ctx, templateIds, productInfo.Id) } if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info not found") } logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info") } if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" { return resp.SetStatusWithMessage(basic.CodeJsonErr, "template info is not set") } var info interface{} if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &info); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json product template info:", templateInfo.Id) } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTemplateByPidRsp{ TemplateId: templateInfo.Id, TemplateInfo: info, }) }