fusenapi/server/shopping-cart/internal/logic/calculatecartpricelogic.go

162 lines
5.5 KiB
Go
Raw Normal View History

2023-09-15 10:19:01 +00:00
package logic
import (
"context"
2023-09-26 07:28:17 +00:00
"encoding/json"
2023-09-19 09:52:23 +00:00
"errors"
2023-09-15 10:19:01 +00:00
"fmt"
"fusenapi/model/gmodel"
2023-09-20 04:27:40 +00:00
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
2023-09-15 10:19:01 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-09-18 04:31:51 +00:00
"fusenapi/utils/format"
2023-09-19 09:52:23 +00:00
"gorm.io/gorm"
2023-09-15 10:19:01 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type CalculateCartPriceLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCalculateCartPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CalculateCartPriceLogic {
return &CalculateCartPriceLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *CalculateCartPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
if len(req.CalculateList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{CalculateResultList: []types.CalculateResultItem{}})
}
cartIds := make([]int64, 0, len(req.CalculateList))
mapCalculateQuantity := make(map[int64]types.CalculateItem)
2023-09-15 10:19:01 +00:00
for _, v := range req.CalculateList {
cartIds = append(cartIds, v.CartId)
2023-09-18 04:31:51 +00:00
if v.PurchaseQuantity <= 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "purchase quantity must grater than 0")
}
mapCalculateQuantity[v.CartId] = v
2023-09-15 10:19:01 +00:00
}
//获取购物车列表
carts, _, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
Ids: cartIds,
2023-09-26 09:08:49 +00:00
Fields: "id,size_id,product_id,fitting_id,model_id",
2023-09-15 10:19:01 +00:00
UserId: userinfo.UserId,
Page: 1,
Limit: len(cartIds),
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get cart list")
}
2023-09-18 04:31:51 +00:00
if len(carts) < len(req.CalculateList) {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "please refresh page for the shopping cart has changed!!")
}
2023-09-15 10:19:01 +00:00
sizeIds := make([]int64, 0, len(carts))
productIds := make([]int64, 0, len(carts))
2023-09-26 07:28:17 +00:00
modelIds := make([]int64, 0, len(carts)) //模型+配件
2023-09-15 10:19:01 +00:00
for _, v := range carts {
sizeIds = append(sizeIds, *v.SizeId)
productIds = append(productIds, *v.ProductId)
2023-09-26 07:28:17 +00:00
modelIds = append(modelIds, *v.ModelId)
2023-09-15 10:19:01 +00:00
if *v.FittingId > 0 {
2023-09-26 07:28:17 +00:00
modelIds = append(modelIds, *v.FittingId)
2023-09-15 10:19:01 +00:00
}
}
2023-09-18 04:31:51 +00:00
//获取配件列表(只有id跟价格)
2023-09-26 07:28:17 +00:00
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "id,step_price,price")
2023-09-18 04:31:51 +00:00
if err != nil {
logx.Error(err)
2023-09-26 07:28:17 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
2023-09-18 04:31:51 +00:00
}
2023-09-26 07:28:17 +00:00
mapModel := make(map[int64]gmodel.FsProductModel3d)
for _, v := range modelList {
mapModel[v.Id] = v
2023-09-18 04:31:51 +00:00
}
//开始计算价格
calculateResultList := make([]types.CalculateResultItem, 0, len(req.CalculateList))
2023-09-18 07:18:55 +00:00
subTotalPrice := int64(0)
2023-09-19 09:52:23 +00:00
//开启事物
err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error {
shoppingCartModel := gmodel.NewFsShoppingCartModel(tx)
for _, cart := range carts {
2023-09-26 07:28:17 +00:00
modelInfo, ok := mapModel[*cart.ModelId]
2023-09-19 09:52:23 +00:00
if !ok {
2023-09-26 07:28:17 +00:00
return err
}
var stepPrice gmodel.StepPriceJsonStruct
2023-09-26 08:36:35 +00:00
if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 {
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
logx.Error(err)
return err
}
2023-09-19 09:52:23 +00:00
}
//请求的数量
reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity
2023-09-20 03:17:00 +00:00
isSelected := int64(0)
if mapCalculateQuantity[cart.Id].IsSelected {
isSelected = 1
}
2023-09-19 09:52:23 +00:00
//如果有配件,单价也要加入配件价格
2023-09-20 04:13:27 +00:00
fittingPrice := int64(0)
2023-09-19 09:52:23 +00:00
if *cart.FittingId > 0 {
2023-09-26 07:28:17 +00:00
if fittingInfo, ok := mapModel[*cart.FittingId]; ok {
fittingPrice = *fittingInfo.Price
2023-09-19 09:52:23 +00:00
} else {
2023-11-10 07:29:44 +00:00
logx.Error(fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId))
2023-09-19 09:52:23 +00:00
}
}
2023-09-20 04:13:27 +00:00
//计算价格
2023-09-26 08:20:53 +00:00
totalPrice, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(reqPurchaseQuantity, stepPrice, fittingPrice)
2023-09-20 04:27:40 +00:00
if err != nil {
return err
}
2023-09-19 09:52:23 +00:00
calculateResultList = append(calculateResultList, types.CalculateResultItem{
CartId: cart.Id,
2023-10-27 07:31:23 +00:00
ItemPrice: format.NumToStringWithThousandthPercentile(format.CentitoDollar(itemPrice, 3)),
TotalPrice: format.NumToStringWithThousandthPercentile(format.CentitoDollarWithNoHalfAdjust(totalPrice, 2)),
2023-09-19 09:52:23 +00:00
})
2023-09-20 03:17:00 +00:00
updData := &gmodel.FsShoppingCart{
PurchaseQuantity: &reqPurchaseQuantity,
2023-09-21 07:57:01 +00:00
IsSelected: &isSelected,
2023-09-20 03:17:00 +00:00
}
2023-09-21 06:31:24 +00:00
//如果是选中则累加总价
if isSelected == 1 {
subTotalPrice += totalPrice
}
2023-09-19 09:52:23 +00:00
//更新购物车购买数量
2023-09-20 03:17:00 +00:00
if err = shoppingCartModel.Update(l.ctx, cart.Id, userinfo.UserId, updData); err != nil {
2023-09-19 09:52:23 +00:00
logx.Error(err)
return errors.New(fmt.Sprintf("failed to update cart`s purchase quantity:%d", cart.Id))
2023-09-18 04:31:51 +00:00
}
}
2023-09-19 09:52:23 +00:00
return nil
})
if err != nil {
2023-09-26 07:28:17 +00:00
logx.Error(err)
2023-09-19 09:52:23 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error())
2023-09-18 04:31:51 +00:00
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{
2023-10-27 07:31:23 +00:00
SubTotalPrice: format.NumToStringWithThousandthPercentile(format.CentitoDollarWithNoHalfAdjust(subTotalPrice, 2)),
2023-09-18 04:31:51 +00:00
CalculateResultList: calculateResultList,
})
2023-09-15 10:19:01 +00:00
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *CalculateCartPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }