Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson 2023-07-21 15:20:56 +08:00
commit efde0005f9
9 changed files with 94 additions and 37 deletions

View File

@ -8,6 +8,7 @@ import (
type FsProductModel3d struct { type FsProductModel3d struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` //
ProductId *int64 `gorm:"index;default:0;" json:"product_id"` // 产品ID ProductId *int64 `gorm:"index;default:0;" json:"product_id"` // 产品ID
IsPopular *int64 `gorm:"default:0;" json:"is_popular"` // 是否热门 0否 1是
Tag *int64 `gorm:"default:1;" json:"tag"` // 类别1模型2配件3场景 Tag *int64 `gorm:"default:1;" json:"tag"` // 类别1模型2配件3场景
Title *string `gorm:"default:'';" json:"title"` // 标题 Title *string `gorm:"default:'';" json:"title"` // 标题
Name *string `gorm:"default:'';" json:"name"` // 详情页展示名称 Name *string `gorm:"default:'';" json:"name"` // 详情页展示名称

View File

@ -8,7 +8,7 @@ func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64) (resp *Fs
err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? ", id).First(&resp).Error err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? ", id).First(&resp).Error
return resp, err return resp, err
} }
func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64, fields ...string) (resp []FsProductModel3d, err error) { func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64, orderBy string, fields ...string) (resp []FsProductModel3d, err error) {
if len(ids) == 0 { if len(ids) == 0 {
return return
} }
@ -16,6 +16,9 @@ func (d *FsProductModel3dModel) GetAllByIds(ctx context.Context, ids []int64, fi
if len(fields) > 0 { if len(fields) > 0 {
db = db.Select(fields[0]) db = db.Select(fields[0])
} }
if orderBy != "" {
db = db.Order(orderBy)
}
err = db.Find(&resp).Error err = db.Find(&resp).Error
return resp, err return resp, err
} }

View File

@ -65,7 +65,7 @@ func (l *GetOrderDetailLogic) GetOrderDetail(req *types.GetOrderDetailReq, useri
} }
//获取配件列表 //获取配件列表
productModel3dModel := gmodel.NewFsProductModel3dModel(l.svcCtx.MysqlConn) productModel3dModel := gmodel.NewFsProductModel3dModel(l.svcCtx.MysqlConn)
productModel3dList, err := productModel3dModel.GetAllByIds(l.ctx, optionalIds) productModel3dList, err := productModel3dModel.GetAllByIds(l.ctx, optionalIds, "")
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product 3d models") return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product 3d models")

View File

@ -136,7 +136,7 @@ func (l *GetTemplatevDetailLogic) GetTemplatevDetail(req *types.GetTemplatevDeta
} }
//查询使用该选项的模板 //查询使用该选项的模板
if len(partIds) > 0 { if len(partIds) > 0 {
model3dList, err := productModel3dModel.GetAllByIds(l.ctx, partIds, "id,model_info,option_template") model3dList, err := productModel3dModel.GetAllByIds(l.ctx, partIds, "", "id,model_info,option_template")
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model list") return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model list")

View File

@ -73,7 +73,7 @@ func (l *GetFittingByPidLogic) GetFittingByPid(req *types.GetFittingByPidReq, us
partIds = append(partIds, *v.PartId) partIds = append(partIds, *v.PartId)
} }
//获取配件数据 //获取配件数据
fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, partIds) fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, partIds, "is_popular DESC,price ASC")
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get part list") return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get part list")
@ -110,6 +110,7 @@ func (l *GetFittingByPidLogic) GetFittingByPid(req *types.GetFittingByPidReq, us
Title: *fitting.Title, Title: *fitting.Title,
Price: *fitting.Price, Price: *fitting.Price,
ModelInfo: modelInfo, ModelInfo: modelInfo,
IsPopular: *fitting.IsPopular > 0,
}) })
} }
return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp) return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)

View File

@ -3,11 +3,13 @@ package logic
import ( import (
"errors" "errors"
"fmt" "fmt"
"fusenapi/model/gmodel"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"fusenapi/utils/format" "fusenapi/utils/format"
"fusenapi/utils/step_price" "fusenapi/utils/step_price"
"gorm.io/gorm" "gorm.io/gorm"
"sort"
"strings" "strings"
"context" "context"
@ -57,42 +59,76 @@ func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo
} }
//处理价格信息 //处理价格信息
mapRsp := make(map[string]*types.GetPriceByPidRsp) mapRsp := make(map[string]*types.GetPriceByPidRsp)
for _, price := range priceList { for _, priceInfo := range priceList {
stepNumSlice, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ",")) stepNumSlice, err := format.StrSlicToIntSlice(strings.Split(*priceInfo.StepNum, ","))
if err != nil { if err != nil {
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", price.Id)) return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", priceInfo.Id))
} }
stepPriceSlice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ",")) stepPriceSlice, err := format.StrSlicToIntSlice(strings.Split(*priceInfo.StepPrice, ","))
if err != nil { if err != nil {
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price,id = %d", price.Id)) return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price,id = %d", priceInfo.Id))
} }
lenStepNum := len(stepNumSlice) lenStepNum := len(stepNumSlice)
itemList := make([]types.PriceItem, 0, 10) itemList := make([]types.PriceItem, 0, 10)
for *price.MinBuyNum < (int64(stepNumSlice[lenStepNum-1]) + 5) { for *priceInfo.MinBuyNum < (int64(stepNumSlice[lenStepNum-1]) + 5) {
itemList = append(itemList, types.PriceItem{ itemList = append(itemList, types.PriceItem{
Num: *price.MinBuyNum, Num: *priceInfo.MinBuyNum,
TotalNum: (*price.MinBuyNum) * (*price.EachBoxNum), TotalNum: (*priceInfo.MinBuyNum) * (*priceInfo.EachBoxNum),
Price: step_price.GetCentStepPrice(int(*price.MinBuyNum), stepNumSlice, stepPriceSlice), Price: step_price.GetCentStepPrice(int(*priceInfo.MinBuyNum), stepNumSlice, stepPriceSlice),
}) })
*price.MinBuyNum++ *priceInfo.MinBuyNum++
} }
minPrice := float64(stepPriceSlice[len(stepPriceSlice)-1]) / 100 //组装阶梯数量范围价格
maxPrice := float64(stepPriceSlice[0]) / 100 stepListRsp := l.dealWithStepRange(stepNumSlice, stepPriceSlice, priceInfo)
mapKey := l.getSizePriceMapKey(*price.SizeId) //排序(必须放在其他逻辑之后)
if _, ok := mapRsp[mapKey]; ok { sort.Ints(stepPriceSlice)
mapRsp[mapKey].Items = append(mapRsp[mapKey].Items, itemList...) minPrice := float64(stepPriceSlice[0]) / 100
mapRsp[mapKey].MinPrice = minPrice maxPrice := float64(stepPriceSlice[len(stepPriceSlice)-1]) / 100
mapRsp[mapKey].MaxPrice = maxPrice mapKey := l.getSizePriceMapKey(*priceInfo.SizeId)
} else {
mapRsp[mapKey] = &types.GetPriceByPidRsp{ mapRsp[mapKey] = &types.GetPriceByPidRsp{
Items: itemList, Items: itemList,
MinPrice: minPrice, MinPrice: minPrice,
MaxPrice: maxPrice, MaxPrice: maxPrice,
} StepPrice: stepListRsp,
} }
} }
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp) return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
} }
// 组装阶梯价格范围
func (l *GetPriceByPidLogic) dealWithStepRange(stepNumSlice, stepPriceSlice []int, priceInfo gmodel.FsProductPrice) []types.StepPrice {
lenStepNum := len(stepNumSlice)
lenStepPrice := len(stepPriceSlice)
stepListRsp := make([]types.StepPrice, 0, lenStepNum)
for numKey, stepNum := range stepNumSlice {
//先取最后一个
tmpPrice := float64(stepPriceSlice[lenStepPrice-1]) / 100
//如果同下标下面有价格
if numKey < lenStepPrice {
tmpPrice = float64(stepPriceSlice[numKey]) / 100
}
num := int64(stepNum) * (*priceInfo.EachBoxNum)
rangeNum := ""
if numKey < lenStepNum-1 { //前面的
nextNum := int64(stepNumSlice[numKey+1]) * (*priceInfo.EachBoxNum)
//第一个
if numKey == 0 {
rangeNum = fmt.Sprintf("%d-%dPCS", num, nextNum-1)
} else {
rangeNum = fmt.Sprintf("%d-%dPCS", num, nextNum-1)
}
} else { //最后一个
rangeNum = fmt.Sprintf(">=%dPCS", num)
}
stepListRsp = append(stepListRsp, types.StepPrice{
Range: rangeNum,
Price: tmpPrice,
})
}
return stepListRsp
}
// 获取mapKey
func (l *GetPriceByPidLogic) getSizePriceMapKey(sizeId int64) string { func (l *GetPriceByPidLogic) getSizePriceMapKey(sizeId int64) string {
return fmt.Sprintf("_%d", sizeId) return fmt.Sprintf("_%d", sizeId)
} }

View File

@ -82,6 +82,7 @@ func (l *GetSizeByPidLogic) GetSizeByPid(req *types.GetSizeByPidReq, userinfo *a
Cover: *sizeInfo.Cover, Cover: *sizeInfo.Cover,
PartsCanDeleted: *sizeInfo.PartsCanDeleted > 0, PartsCanDeleted: *sizeInfo.PartsCanDeleted > 0,
ModelId: modelList[modelIndex].Id, ModelId: modelList[modelIndex].Id,
IsPopular: *sizeInfo.IsPopular > 0,
}) })
} }
return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp) return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)

View File

@ -228,8 +228,8 @@ type OtherProductListRsp struct {
} }
type GetRecommandProductListReq struct { type GetRecommandProductListReq struct {
Size uint32 `form:"size"` Size uint32 `form:"size,optional"`
Num int64 `form:"num"` Num int64 `form:"num,optional"`
Sn string `form:"sn"` Sn string `form:"sn"`
} }
@ -309,6 +309,12 @@ type GetPriceByPidRsp struct {
Items []PriceItem `json:"items"` Items []PriceItem `json:"items"`
MinPrice float64 `json:"min_price"` MinPrice float64 `json:"min_price"`
MaxPrice float64 `json:"max_price"` MaxPrice float64 `json:"max_price"`
StepPrice []StepPrice `json:"step_price"`
}
type StepPrice struct {
Range string `json:"range"`
Price float64 `json:"price"`
} }
type PriceItem struct { type PriceItem struct {
@ -328,6 +334,7 @@ type GetSizeByPidRsp struct {
Cover string `json:"cover"` //缩略图 Cover string `json:"cover"` //缩略图
PartsCanDeleted bool `json:"parts_can_deleted"` //用户可否删除配件 PartsCanDeleted bool `json:"parts_can_deleted"` //用户可否删除配件
ModelId int64 `json:"model_id"` //产品主模型id ModelId int64 `json:"model_id"` //产品主模型id
IsPopular bool `json:"is_popular"` //是否受欢迎
} }
type GetTemplateByPidReq struct { type GetTemplateByPidReq struct {
@ -344,6 +351,7 @@ type GetFittingByPidRsp struct {
MaterialImg string `json:"material_img"` MaterialImg string `json:"material_img"`
Title string `json:"title"` Title string `json:"title"`
Price int64 `json:"price"` Price int64 `json:"price"`
IsPopular bool `json:"is_popular"`
ModelInfo interface{} `json:"model_info"` ModelInfo interface{} `json:"model_info"`
} }

View File

@ -281,8 +281,8 @@ type OtherProductListRsp {
} }
//获取详情页推荐产品列表 //获取详情页推荐产品列表
type GetRecommandProductListReq { type GetRecommandProductListReq {
Size uint32 `form:"size"` Size uint32 `form:"size,optional"`
Num int64 `form:"num"` Num int64 `form:"num,optional"`
Sn string `form:"sn"` Sn string `form:"sn"`
} }
type GetRecommandProductListRsp { type GetRecommandProductListRsp {
@ -357,6 +357,11 @@ type GetPriceByPidRsp {
Items []PriceItem `json:"items"` Items []PriceItem `json:"items"`
MinPrice float64 `json:"min_price"` MinPrice float64 `json:"min_price"`
MaxPrice float64 `json:"max_price"` MaxPrice float64 `json:"max_price"`
StepPrice []StepPrice `json:"step_price"`
}
type StepPrice {
Range string `json:"range"`
Price float64 `json:"price"`
} }
type PriceItem { type PriceItem {
Num int64 `json:"num"` Num int64 `json:"num"`
@ -374,6 +379,7 @@ type GetSizeByPidRsp {
Cover string `json:"cover"` //缩略图 Cover string `json:"cover"` //缩略图
PartsCanDeleted bool `json:"parts_can_deleted"` //用户可否删除配件 PartsCanDeleted bool `json:"parts_can_deleted"` //用户可否删除配件
ModelId int64 `json:"model_id"` //产品主模型id ModelId int64 `json:"model_id"` //产品主模型id
IsPopular bool `json:"is_popular"` //是否受欢迎
} }
//获取产品模板列表 //获取产品模板列表
type GetTemplateByPidReq { type GetTemplateByPidReq {
@ -389,6 +395,7 @@ type GetFittingByPidRsp {
MaterialImg string `json:"material_img"` MaterialImg string `json:"material_img"`
Title string `json:"title"` Title string `json:"title"`
Price int64 `json:"price"` Price int64 `json:"price"`
IsPopular bool `json:"is_popular"`
ModelInfo interface{} `json:"model_info"` ModelInfo interface{} `json:"model_info"`
} }
//获取产品灯光数据 //获取产品灯光数据