2023-06-20 08:38:03 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-06-20 11:56:18 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fusenapi/model/gmodel"
|
2023-06-26 10:27:38 +00:00
|
|
|
"fusenapi/utils/auth"
|
2023-06-20 10:05:29 +00:00
|
|
|
"fusenapi/utils/basic"
|
2023-06-20 11:56:18 +00:00
|
|
|
"fusenapi/utils/format"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"context"
|
2023-06-20 08:38:03 +00:00
|
|
|
|
2023-06-25 06:11:47 +00:00
|
|
|
"fusenapi/server/product-template/internal/svc"
|
|
|
|
"fusenapi/server/product-template/internal/types"
|
2023-06-20 08:38:03 +00:00
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetTemplatevDetailLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGetTemplatevDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTemplatevDetailLogic {
|
|
|
|
return &GetTemplatevDetailLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 10:27:38 +00:00
|
|
|
func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDetailReq, userInfo *auth.BackendUserInfo) (resp *basic.Response) {
|
2023-06-21 07:06:09 +00:00
|
|
|
if req.TemplateId <= 0 {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param template_id")
|
|
|
|
}
|
|
|
|
if req.ModelId <= 0 {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param model_id")
|
|
|
|
}
|
2023-06-20 11:56:18 +00:00
|
|
|
//获取模型信息
|
2023-06-20 10:05:29 +00:00
|
|
|
productModel3dModel := gmodel.NewFsProductModel3dModel(l.svcCtx.MysqlConn)
|
|
|
|
model3dInfo, err := productModel3dModel.FindOne(l.ctx, req.ModelId)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product model info is not exists")
|
|
|
|
}
|
|
|
|
logx.Error(err)
|
2023-06-21 04:11:43 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model info")
|
2023-06-20 10:05:29 +00:00
|
|
|
}
|
2023-06-20 11:56:18 +00:00
|
|
|
//查询产品模板并检测数据完整
|
|
|
|
productTemplatev2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
|
|
|
|
fields := "cover_img,id,is_public,logo_height,logo_width,material_img,model_id,name,product_id,sort,tag,template_info,title"
|
|
|
|
templatev2Info, err := productTemplatev2Model.FindByParam(l.ctx, req.TemplateId, req.ModelId, fields)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info is not exists")
|
|
|
|
}
|
|
|
|
logx.Error(err)
|
2023-06-21 04:11:43 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template info")
|
2023-06-20 11:56:18 +00:00
|
|
|
}
|
|
|
|
//获取模板标签
|
2023-08-17 03:49:31 +00:00
|
|
|
templateTagInfo, err := l.svcCtx.AllModels.FsProductTemplateTags.FindOneByTagName(l.ctx, *templatev2Info.TemplateTag, "id,title")
|
2023-06-20 11:56:18 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template tag info is not exists")
|
|
|
|
}
|
|
|
|
logx.Error(err)
|
2023-06-21 04:11:43 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template tag info")
|
2023-06-20 11:56:18 +00:00
|
|
|
}
|
2023-06-20 10:05:29 +00:00
|
|
|
//配件ids
|
2023-06-21 04:11:43 +00:00
|
|
|
partIds := make([]int64, 0, 10)
|
|
|
|
if *model3dInfo.PartList != "" {
|
|
|
|
partIds, err = format.StrSlicToInt64Slice(strings.Split(*model3dInfo.PartList, ","))
|
|
|
|
}
|
2023-06-20 11:56:18 +00:00
|
|
|
//灯光ids
|
|
|
|
var lightIds []int64
|
|
|
|
if err = json.Unmarshal([]byte(*model3dInfo.LightList), &lightIds); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "parse light list err")
|
|
|
|
}
|
|
|
|
//获取灯光列表
|
|
|
|
productModel3dLightModel := gmodel.NewFsProductModel3dLightModel(l.svcCtx.MysqlConn)
|
|
|
|
model3dLightList, err := productModel3dLightModel.GetAllByIds(l.ctx, lightIds)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
2023-06-21 04:11:43 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model light list")
|
2023-06-20 11:56:18 +00:00
|
|
|
}
|
2023-06-21 04:11:43 +00:00
|
|
|
//组装灯光信息
|
|
|
|
lightListRsp := make([]*types.Light, 0, len(model3dLightList))
|
|
|
|
for _, v := range model3dLightList {
|
|
|
|
var info interface{}
|
|
|
|
if err = json.Unmarshal([]byte(*v.Info), &info); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse light info")
|
|
|
|
}
|
|
|
|
lightListRsp = append(lightListRsp, &types.Light{
|
|
|
|
Id: v.Id,
|
|
|
|
Info: info,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
//产品模型数据解析model_info
|
|
|
|
var productModelInfoRsp interface{}
|
|
|
|
if model3dInfo.ModelInfo != nil {
|
|
|
|
if err = json.Unmarshal([]byte(*model3dInfo.ModelInfo), &productModelInfoRsp); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse product 3d model info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//产品模板数据解析template_info
|
|
|
|
templateInfoRsp := make(map[string]interface{})
|
|
|
|
if templatev2Info.TemplateInfo != nil {
|
|
|
|
if err = json.Unmarshal([]byte(*templatev2Info.TemplateInfo), &templateInfoRsp); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse product v2 template info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
templateInfoRsp["tag"] = map[string]interface{}{
|
|
|
|
"id": templateTagInfo.Id,
|
2023-08-17 03:49:31 +00:00
|
|
|
"title": *templateTagInfo.TemplateTag,
|
2023-06-21 04:11:43 +00:00
|
|
|
}
|
|
|
|
response := types.GetTemplatevDetailRsp{
|
|
|
|
ProductModelInfo: productModelInfoRsp,
|
|
|
|
ProductTemplate: templateInfoRsp,
|
|
|
|
LightList: lightListRsp,
|
|
|
|
OptionModelInfo: nil,
|
|
|
|
Tag: *model3dInfo.Tag,
|
|
|
|
}
|
|
|
|
//查询使用该选项的模板
|
|
|
|
if len(partIds) > 0 {
|
2023-07-21 04:27:37 +00:00
|
|
|
model3dList, err := productModel3dModel.GetAllByIds(l.ctx, partIds, "", "id,model_info,option_template")
|
2023-06-21 04:11:43 +00:00
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model list")
|
|
|
|
}
|
|
|
|
optionIds := make([]int64, 0, len(model3dList))
|
|
|
|
for _, v := range model3dList {
|
|
|
|
if v.OptionTemplate != nil {
|
|
|
|
optionIds = append(optionIds, *v.OptionTemplate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//获取公共模板信息
|
|
|
|
optionTemplateList, err := productTemplatev2Model.FindAllByIds(l.ctx, optionIds)
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product v2 template list")
|
|
|
|
}
|
|
|
|
mapOptionTemplate := make(map[int64]int)
|
|
|
|
for k, v := range optionTemplateList {
|
|
|
|
mapOptionTemplate[v.Id] = k
|
|
|
|
}
|
|
|
|
//处理数据
|
|
|
|
optionModelInfoList := make([]interface{}, 0, len(model3dList))
|
|
|
|
for _, v := range model3dList {
|
|
|
|
info := make(map[string]interface{})
|
2023-07-05 10:08:18 +00:00
|
|
|
if v.ModelInfo != nil && *v.ModelInfo != "" {
|
2023-06-21 04:11:43 +00:00
|
|
|
if err = json.Unmarshal([]byte(*v.ModelInfo), &info); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model info")
|
|
|
|
}
|
|
|
|
if optionTemplateIndex, ok := mapOptionTemplate[*v.OptionTemplate]; ok {
|
|
|
|
info["material_img"] = optionTemplateList[optionTemplateIndex].MaterialImg
|
|
|
|
}
|
|
|
|
info["id"] = v.Id
|
|
|
|
}
|
|
|
|
optionModelInfoList = append(optionModelInfoList, info)
|
|
|
|
}
|
|
|
|
response.OptionModelInfo = optionModelInfoList
|
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", response)
|
2023-06-20 08:38:03 +00:00
|
|
|
}
|