fusenapi/server/inventory/internal/logic/getcloudlistlogic.go
laodaming 84088a7a12 fix
2023-06-27 17:47:44 +08:00

155 lines
4.8 KiB
Go

package logic
import (
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"strings"
"context"
"fusenapi/server/inventory/internal/svc"
"fusenapi/server/inventory/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCloudListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCloudListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCloudListLogic {
return &GetCloudListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCloudListLogic) GetCloudList(req *types.GetCloudListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
}
if req.Page <= 0 {
req.Page = constants.DEFAULT_PAGE
}
if req.PageSize <= 0 || req.PageSize > 200 {
req.Page = constants.DEFAULT_PAGE_SIZE
}
//获取个人云仓列表
stockList, total, err := l.svcCtx.AllModels.FsUserStock.GetStockList(l.ctx, gmodel.GetStockListReq{
UserId: userinfo.UserId,
Page: int(req.Page),
Limit: int(req.PageSize),
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user stock list")
}
if len(stockList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCloudListRsp{
ListData: []types.ListDataItem{},
})
}
designIds := make([]int64, 0, len(stockList))
for _, v := range stockList {
designIds = append(designIds, *v.DesignId)
}
//获取设计数据
productDesignList, err := l.svcCtx.AllModels.FsProductDesign.GetAllByIds(l.ctx, designIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product design list")
}
//尺寸ids
sizeIds := make([]int64, 0, len(productDesignList))
//产品ids
productIds := make([]int64, 0, len(productDesignList))
//模板ids
templateIds := make([]int64, 0, len(productDesignList))
//配件ids
optionalIds := make([]int64, 0, len(productDesignList))
mapProductDesign := make(map[int64]int)
for k, v := range productDesignList {
sizeIds = append(sizeIds, *v.SizeId)
productIds = append(productIds, *v.ProductId)
templateIds = append(templateIds, *v.TemplateId)
optionalIds = append(optionalIds, *v.OptionalId)
mapProductDesign[v.Id] = k
}
//获取尺寸信息
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIdsWithoutStatus(l.ctx, sizeIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product size list")
}
mapSize := make(map[int64]int)
for k, v := range sizeList {
mapSize[v.Id] = k
}
//获取产品信息
productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIdsWithoutStatus(l.ctx, productIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list")
}
mapProduct := make(map[int64]int)
for k, v := range productList {
mapProduct[v.Id] = k
}
//获取模板信息
productTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByIdsWithoutStatus(l.ctx, templateIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product template list")
}
mapTemplate := make(map[int64]int)
for k, v := range productTemplateList {
mapTemplate[v.Id] = k
}
//获取配件列表
productModel3dList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsWithoutStatus(l.ctx, optionalIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product 3d model list")
}
mapProductModel := make(map[int64]int)
for k, v := range productModel3dList {
mapProductModel[v.Id] = k
}
//根据产品ids获取产品价格
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product price list")
}
for _, v := range priceList {
if *v.StepNum == "" || *v.StepPrice == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "price data`s step num or step price is empty")
}
stepNum, err := format.StrSlicToIntSlice(strings.Split(*v.StepNum, ","))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "parse step num err")
}
lenStepNum := len(stepNum)
stepPrice, err := format.StrSlicToIntSlice(strings.Split(*v.StepPrice, ","))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "parse step price err")
}
lenStepPrice := len(stepPrice)
for *v.MinBuyNum < int64(stepPrice[lenStepPrice-1]+5) {
//根据材质,尺寸,价格计算阶梯价
*v.MinBuyNum++
}
}
return resp.SetStatus(basic.CodeOK)
}