190 lines
6.7 KiB
Go
190 lines
6.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/format"
|
|
"gorm.io/gorm"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/product-templatev2/internal/svc"
|
|
"fusenapi/server/product-templatev2/internal/types"
|
|
|
|
"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,
|
|
}
|
|
}
|
|
|
|
func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDetailReq, r *http.Request) (resp *basic.Response) {
|
|
authKey := r.Header.Get("Auth-Key")
|
|
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
|
|
_, err := genentModel.Find(l.ctx, authKey)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
|
|
}
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
|
|
}
|
|
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")
|
|
}
|
|
//获取模型信息
|
|
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)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model info")
|
|
}
|
|
//查询产品模板并检测数据完整
|
|
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)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template info")
|
|
}
|
|
//获取模板标签
|
|
templateTagModel := gmodel.NewFsProductTemplateTagsModel(l.svcCtx.MysqlConn)
|
|
tagId, err := strconv.ParseInt(*templatev2Info.Tag, 10, 64)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "parse int tag id err")
|
|
}
|
|
templateTagInfo, err := templateTagModel.FindOne(l.ctx, tagId, "id,title")
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template tag info is not exists")
|
|
}
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template tag info")
|
|
}
|
|
//配件ids
|
|
partIds := make([]int64, 0, 10)
|
|
if *model3dInfo.PartList != "" {
|
|
partIds, err = format.StrSlicToInt64Slice(strings.Split(*model3dInfo.PartList, ","))
|
|
}
|
|
//灯光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)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product model light list")
|
|
}
|
|
//组装灯光信息
|
|
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,
|
|
"title": *templateTagInfo.Title,
|
|
}
|
|
response := types.GetTemplatevDetailRsp{
|
|
ProductModelInfo: productModelInfoRsp,
|
|
ProductTemplate: templateInfoRsp,
|
|
LightList: lightListRsp,
|
|
OptionModelInfo: nil,
|
|
Tag: *model3dInfo.Tag,
|
|
}
|
|
//查询使用该选项的模板
|
|
if len(partIds) > 0 {
|
|
model3dList, err := productModel3dModel.GetAllByIds(l.ctx, partIds, "id,model_info,option_template")
|
|
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{})
|
|
if v.ModelInfo != nil {
|
|
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)
|
|
}
|