This commit is contained in:
laodaming 2023-08-11 18:59:03 +08:00
parent ed8904ca5e
commit 48afd3105f
5 changed files with 65 additions and 59 deletions

View File

@ -101,3 +101,14 @@ func (d *FsProductModel3dModel) FindOneJoinSize(ctx context.Context, productId i
Order("s.sort ASC").Take(&resp).Error
return resp, err
}
func (d *FsProductModel3dModel) GetOneBySizeIdTag(ctx context.Context, sizeId int64, tag int64, fields ...string) (resp *FsProductModel3d, err error) {
db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).
Where("`size_id` = ? and `tag` = ? and `status` = ?", sizeId, tag, 1).
Order("sort DESC")
if len(fields) != 0 {
db = db.Select(fields[0])
}
err = db.Take(&resp).Error
return resp, err
}

View File

@ -22,6 +22,7 @@ type FsProductTemplateV2 struct {
Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间
Tag *string `gorm:"default:'';" json:"tag"` // 标签(用户自填)
IsDel *int64 `gorm:"default:0;" json:"is_del"` // 是否删除 1删除
SwitchInfo *string `gorm:"default:'';" json:"switch_info"` // 开关信息
GroupOptions *string `gorm:"default:'';" json:"group_options"` // 颜色分组
Version *int64 `gorm:"default:0;" json:"version"` //
}

View File

@ -140,22 +140,6 @@ func (t *FsProductTemplateV2Model) FindAllByModelIdsTemplateTag(ctx context.Cont
return resp, err
}
// 获取产品某个物料下的第一个模板
func (t *FsProductTemplateV2Model) GetSizeLatestTemplate(ctx context.Context, ids []int64, productId int64) (resp *FsProductTemplateV2, err error) {
err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).
Where("id in (?) and product_id = ? and status = ? and is_del = ?", ids, productId, 1, 0).
Order("sort ASC").Take(&resp).Error
return resp, err
}
// 获取产品第一个物料下的第一个模板
func (t *FsProductTemplateV2Model) GetProductLatestTemplate(ctx context.Context, ids []int64, productId int64) (resp *FsProductTemplateV2, err error) {
err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).
Where("id in (?) and product_id = ? and status = ? and is_del = ?", ids, productId, 1, 0).
Order("sort ASC").Take(&resp).Error
return resp, err
}
// 获取产品在指定模板标签下的所有模板
func (t *FsProductTemplateV2Model) GetListByProductAndTemplateTag(ctx context.Context, templateTagId string, productId int64, fields ...string) (resp []FsProductTemplateV2, err error) {
db := t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).

View File

@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
@ -40,7 +40,6 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq,
if req.ProductTemplateTagId <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product_template_tag_id")
}
//获取产品信息(只获取id)
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
if err != nil {
@ -50,46 +49,62 @@ func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq,
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
}
//获取该产品该模板标签下的模板列表(只获取id)
templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.
GetListByProductAndTemplateTag(l.ctx, fmt.Sprintf("%d", req.ProductTemplateTagId), productInfo.Id, "id")
//没有指定物料
sizeIds := make([]int64, 0, 10)
if req.ProductSizeId <= 0 {
//获取产品所有物料
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 templates")
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "failed to get product size list")
}
if len(templateList) == 0 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "templates are not exists")
if len(sizeList) == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product size list is empty")
}
templateIds := make([]int64, 0, len(templateList))
for _, v := range templateList {
templateIds = append(templateIds, v.Id)
for _, v := range sizeList {
sizeIds = append(sizeIds, v.Id)
}
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)
} else { //指定物料
sizeIds = append(sizeIds, req.ProductSizeId)
}
//根据尺寸id获取模型id
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "template info not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
}
if len(modelList) == 0 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "model list is empty")
}
modelIds := make([]int64, 0, len(modelList))
for _, v := range modelList {
modelIds = append(modelIds, v.Id)
}
//查询模型ids下对应tag标签的模板
templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIdsTemplateTag(l.ctx, modelIds, fmt.Sprintf("%d", req.ProductTemplateTagId), "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list")
}
rspList := make([]interface{}, 0, len(templateList))
for _, templateInfo := range templateList {
//没有设置模板据不要
if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
return resp.SetStatusWithMessage(basic.CodeJsonErr, "template info is not set")
continue
}
//基础模板信息
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)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTemplateByPidRsp{
TemplateId: templateInfo.Id,
TemplateInfo: info,
})
//后台隐藏/显示信息
var switchInfo interface{}
if templateInfo.SwitchInfo != nil && *templateInfo.SwitchInfo != "" {
_ = json.Unmarshal([]byte(*templateInfo.SwitchInfo), &switchInfo)
}
// todo 继续下周
rspList = append(rspList, map[string]interface{}{})
}
return nil
}

View File

@ -387,11 +387,6 @@ type GetSizeByPidRsp {
type GetTemplateByPidReq {
Pid string `form:"pid"`
ProductTemplateTagId int64 `form:"product_template_tag_id"`
ProductSizeId int64 `form:"product_size_id,optional"`
}
type GetTemplateByPidRsp {
TemplateId int64 `json:"template_id"` //模板id
TemplateInfo interface{} `json:"template_info"` //模板信息
}
//获取产品配件数据
type GetFittingByPidReq {