117 lines
3.7 KiB
Go
117 lines
3.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fusenapi/constants"
|
|
"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 GetFittingByPidLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetFittingByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFittingByPidLogic {
|
|
return &GetFittingByPidLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetFittingByPidLogic) GetFittingByPid(req *types.GetFittingByPidReq, 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")
|
|
}
|
|
//获取尺寸列表(只获取id)
|
|
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "", "id")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
|
|
}
|
|
if len(sizeList) == 0 {
|
|
return resp.SetStatusAddMessage(basic.CodeOK, "success:size list is empty")
|
|
}
|
|
sizeIds := make([]int64, 0, len(sizeList))
|
|
for _, v := range sizeList {
|
|
sizeIds = append(sizeIds, v.Id)
|
|
}
|
|
//获取配件id
|
|
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "part_id")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
|
|
}
|
|
if len(modelList) == 0 {
|
|
return resp.SetStatusAddMessage(basic.CodeOK, "success:model list is empty")
|
|
}
|
|
partIds := make([]int64, 0, len(modelList))
|
|
for _, v := range modelList {
|
|
partIds = append(partIds, *v.PartId)
|
|
}
|
|
//获取配件数据
|
|
fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, partIds, "price ASC")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get part list")
|
|
}
|
|
if len(fittingList) == 0 {
|
|
return resp.SetStatusAddMessage(basic.CodeOK, "success:fitting list is empty")
|
|
}
|
|
//处理组装数据返回
|
|
listRsp := make([]types.GetFittingByPidRsp, 0, len(fittingList))
|
|
for _, fitting := range fittingList {
|
|
materialImg := ""
|
|
var tInfo *gmodel.FsProductTemplateV2
|
|
//贴图,如果绑定了公共模板,则获取公共模板的贴图数据
|
|
if *fitting.OptionTemplate > 0 {
|
|
tInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, *fitting.OptionTemplate)
|
|
} else { //否则取该配件下的模板贴图
|
|
tInfo, err = l.svcCtx.AllModels.FsProductTemplateV2.FindOneByModelId(l.ctx, fitting.Id)
|
|
}
|
|
if err == nil {
|
|
materialImg = *tInfo.MaterialImg
|
|
} else {
|
|
logx.Error(err)
|
|
}
|
|
var modelInfo interface{}
|
|
if fitting.ModelInfo != nil && *fitting.ModelInfo != "" {
|
|
if err = json.Unmarshal([]byte(*fitting.ModelInfo), &modelInfo); err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse model json info")
|
|
}
|
|
}
|
|
listRsp = append(listRsp, types.GetFittingByPidRsp{
|
|
Id: fitting.Id,
|
|
MaterialImg: materialImg,
|
|
Title: *fitting.Title,
|
|
Price: *fitting.Price,
|
|
ModelInfo: modelInfo,
|
|
})
|
|
}
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)
|
|
}
|