fusenapi/server/product-templatev2/internal/logic/gettemplatevdetaillogic.go
laodaming edeb2bb5f1 fix
2023-06-20 19:56:18 +08:00

173 lines
6.0 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")
if authKey == "" {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
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")
}
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")
}
//获取模型信息
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.CodeServiceErr, "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.CodeServiceErr, "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.CodeServiceErr, "failed to get product template tag info")
}
//配件ids
partIds, err := format.StrSlicToIntSlice(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.CodeServiceErr, "failed to get product model light list")
}
/*
//产品模型数据解析model_info
$productModel['model_info'] = $productModel['model_info'] ? json_decode($productModel['model_info']) : null;
//产品模板数据解析template_info
if ($productTemplate) {
$productTemplate['template_info'] = $productTemplate['template_info'] ? json_decode($productTemplate['template_info']) : '';
}
//获取灯光列表
$lightList = ProductModelLight::find()->where(['in', 'id', $lightListArr])
->select(['id', 'info'])
->asArray()->all();
//产品模型灯光数据解析
foreach ($lightList as $key => $val) {
$lightList[$key]['info'] = json_decode($val['info'], true);
}
//查询使用该选项的模板
if ($partIds) {
$optionModelInfo = ProductModel::find()
->where(['in', 'id', $partIds])
->select('id,model_info,option_template')
->asArray()
->all();
//公共模板数据
$optionIds = array_column($optionModelInfo, 'option_template');
$optionTemplates = ProductTemplateV2::find()->where(['in', 'id', $optionIds])
->asArray()
->all();
$optionTemplates = array_column($optionTemplates, null, 'id');
//处理数据
$optionModelInfoArr = [];
foreach ($optionModelInfo as $item => $row) {
$rowInfo = json_decode($row['model_info'], true);
if (isset($optionTemplates[$row['option_template']])) {
$rowInfo['material_img'] = $optionTemplates[$row['option_template']]['material_img'];
}
$rowInfo['id'] = $row['id'];
$optionModelInfoArr[] = $rowInfo;
}
}
//是否是公共模板
if ($productTemplate) {
if ($productTemplate['is_public'] == 1) {
$productTemplate['is_public'] = true;
} else {
$productTemplate['is_public'] = false;
}
}
//返回的数据组装
return [
'product_model_info' => $productModel['model_info'],
'product_template' => $productTemplate ?? null,
'light_list' => $lightList ?? [],
'option_model_info' => $optionModelInfoArr ?? [],
'tag' => $productModel['tag'],
];*/
return resp.SetStatus(basic.CodeOK)
}