fusenapi/server/product/internal/logic/gettemplatebypidlogic.go

96 lines
3.2 KiB
Go
Raw Normal View History

2023-07-17 06:40:24 +00:00
package logic
import (
"encoding/json"
"errors"
"fmt"
2023-08-11 06:52:16 +00:00
"fusenapi/model/gmodel"
2023-07-17 06:40:24 +00:00
"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")
}
2023-08-08 09:03:54 +00:00
if req.ProductTemplateTagId <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product_template_tag_id")
}
2023-08-11 06:52:16 +00:00
//获取产品信息(只获取id)
2023-07-17 06:40:24 +00:00
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")
}
2023-08-11 06:52:16 +00:00
//获取该产品该模板标签下的模板列表(只获取id)
templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.
GetListByProductAndTemplateTag(l.ctx, fmt.Sprintf("%d", req.ProductTemplateTagId), productInfo.Id, "id")
2023-07-17 06:40:24 +00:00
if err != nil {
logx.Error(err)
2023-08-11 06:52:16 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get templates")
2023-07-17 06:40:24 +00:00
}
2023-08-11 06:52:16 +00:00
if len(templateList) == 0 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "templates are not exists")
2023-07-17 06:40:24 +00:00
}
2023-08-11 06:52:16 +00:00
templateIds := make([]int64, 0, len(templateList))
for _, v := range templateList {
templateIds = append(templateIds, v.Id)
2023-07-17 06:40:24 +00:00
}
2023-08-11 06:52:16 +00:00
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)
2023-07-17 06:40:24 +00:00
}
if err != nil {
2023-08-11 06:52:16 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info not found")
2023-07-17 06:40:24 +00:00
}
logx.Error(err)
2023-08-11 06:52:16 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
2023-07-17 06:40:24 +00:00
}
2023-08-11 06:52:16 +00:00
if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
return resp.SetStatusWithMessage(basic.CodeJsonErr, "template info is not set")
2023-07-17 06:40:24 +00:00
}
2023-08-11 06:52:16 +00:00
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)
2023-07-17 06:40:24 +00:00
}
2023-08-11 06:52:16 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTemplateByPidRsp{
TemplateId: templateInfo.Id,
TemplateInfo: info,
})
2023-07-17 06:40:24 +00:00
}