fusenapi/service/repositories/shopping_cart.go

150 lines
5.3 KiB
Go
Raw Normal View History

2023-09-20 07:21:42 +00:00
package repositories
2023-09-15 02:23:40 +00:00
import (
"encoding/json"
2023-09-20 07:21:42 +00:00
"errors"
2023-09-15 02:23:40 +00:00
"fusenapi/model/gmodel"
"fusenapi/utils/hash"
2023-09-20 07:21:42 +00:00
"github.com/aws/aws-sdk-go/aws/session"
"gorm.io/gorm"
2023-09-15 02:23:40 +00:00
"strings"
)
2023-09-20 07:21:42 +00:00
func NewShoppingCart(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) ShoppingCart {
return &defaultShoppingCart{
MysqlConn: gormDB,
BLMServiceUrl: bLMServiceUrl,
AwsSession: awsSession,
}
}
type (
defaultShoppingCart struct {
MysqlConn *gorm.DB
BLMServiceUrl *string
AwsSession *session.Session
}
ShoppingCart interface {
// 校验订单
VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error
//计算购物车价格
2023-09-26 07:28:17 +00:00
CaculateStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error)
2023-09-20 07:21:42 +00:00
}
)
2023-09-15 02:23:40 +00:00
// 校验购物车快照数据跟目前是否一致
type VerifyShoppingCartSnapshotDataChangeReq struct {
Carts []gmodel.FsShoppingCart
MapSize map[int64]gmodel.FsProductSize
MapModel map[int64]gmodel.FsProductModel3d //模型跟配件都在
MapTemplate map[int64]gmodel.FsProductTemplateV2
MapCartChange map[int64]string
2023-09-20 07:21:42 +00:00
MapSnapshot map[int64]gmodel.CartSnapshot
2023-09-18 03:45:29 +00:00
MapProduct map[int64]gmodel.FsProduct
2023-09-15 02:23:40 +00:00
}
2023-09-20 07:21:42 +00:00
func (d *defaultShoppingCart) VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error {
2023-09-15 02:23:40 +00:00
for _, cartInfo := range req.Carts {
2023-09-15 08:30:01 +00:00
descrptionBuilder := strings.Builder{}
//产品下架/删除
if _, ok := req.MapProduct[*cartInfo.ProductId]; !ok {
descrptionBuilder.WriteString("<p>the product is off shelf or deleted </p>")
}
2023-09-20 07:21:42 +00:00
var snapShotParseInfo gmodel.CartSnapshot
2023-09-15 02:23:40 +00:00
if err := json.Unmarshal([]byte(*cartInfo.Snapshot), &snapShotParseInfo); err != nil {
return err
}
2023-09-15 06:19:14 +00:00
req.MapSnapshot[cartInfo.Id] = snapShotParseInfo
2023-09-15 02:23:40 +00:00
//快照中模板设计json数据哈希值
snapshotTemplateJsonHash := hash.JsonHashKey(snapShotParseInfo.TemplateInfo.TemplateJson)
//快照中模型设计json数据哈希值
snapshotModelJsonHash := hash.JsonHashKey(snapShotParseInfo.ModelInfo.ModelJson)
//快照中配件设计json数据哈希值
snapshotFittingJsonHash := hash.JsonHashKey(snapShotParseInfo.FittingInfo.FittingJson)
//有模板验证模板相关
if *cartInfo.TemplateId > 0 {
if curTemplateInfo, ok := req.MapTemplate[*cartInfo.TemplateId]; !ok {
descrptionBuilder.WriteString("<p>the template is lose</p>")
} else {
//当前模板设计json数据哈希值
curTemplateJsonHash := hash.JsonHashKey(*curTemplateInfo.TemplateInfo)
//模板设计信息改变了
if snapshotTemplateJsonHash != curTemplateJsonHash {
descrptionBuilder.WriteString("<p>the template design info has changed</p>")
}
//模板标签改变了
if snapShotParseInfo.TemplateInfo.TemplateTag != *curTemplateInfo.TemplateTag {
descrptionBuilder.WriteString("<p>the template`s template tag has changed</p>")
}
}
}
//有模型验证模型相关
if *cartInfo.ModelId > 0 {
if curModelInfo, ok := req.MapModel[*cartInfo.ModelId]; !ok { //不存在
descrptionBuilder.WriteString("<p>the model is lose</p>")
} else {
//当前模型设计json数据哈希值
curModelJsonHash := hash.JsonHashKey(*curModelInfo.ModelInfo)
if snapshotModelJsonHash != curModelJsonHash {
descrptionBuilder.WriteString("<p>the model design info has changed</p>")
}
}
}
//有配件验证配件相关
if *cartInfo.FittingId > 0 {
if curFittingInfo, ok := req.MapModel[*cartInfo.FittingId]; !ok { //不存在
descrptionBuilder.WriteString("<p>the fitting is lose</p>")
} else {
//当前配件设计json数据哈希值
curFittingJsonHash := hash.JsonHashKey(*curFittingInfo.ModelInfo)
if snapshotFittingJsonHash != curFittingJsonHash {
descrptionBuilder.WriteString("<p>the fitting design info has changed</p>")
}
}
}
//验证尺寸相关
if *cartInfo.SizeId > 0 {
curSize, ok := req.MapSize[*cartInfo.SizeId]
if !ok {
descrptionBuilder.WriteString("<p>the size is lose</p>")
} else {
2023-09-20 07:21:42 +00:00
var curSizeTitle gmodel.SizeInfo
2023-09-15 02:23:40 +00:00
if err := json.Unmarshal([]byte(*curSize.Title), &curSizeTitle); err != nil {
return err
}
if snapShotParseInfo.SizeInfo.Inch != curSizeTitle.Inch || snapShotParseInfo.SizeInfo.Cm != curSizeTitle.Cm {
descrptionBuilder.WriteString("<p>the size design info has changed</p>")
}
}
}
//收集错误
descrption := descrptionBuilder.String()
if descrption != "" {
req.MapCartChange[cartInfo.Id] = descrption
}
}
return nil
}
2023-09-20 07:21:42 +00:00
// 计算价格
2023-09-26 07:28:17 +00:00
func (d *defaultShoppingCart) CaculateStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error) {
l := len(stepPrice.PriceRange)
if l == 0 {
return 0, 0, errors.New("price range is not set")
2023-09-20 07:21:42 +00:00
}
2023-09-26 07:28:17 +00:00
//遍历查询合适的价格
for k, v := range stepPrice.PriceRange {
//购买数量>起点
if purchaseQuantity > v.StartQuantity {
//最后一个 || 小于等于终点
if k == l-1 || purchaseQuantity <= v.EndQuantity {
itemPrice = v.Price + fittingPrice
return itemPrice * purchaseQuantity, itemPrice, nil
}
}
2023-09-20 07:21:42 +00:00
}
2023-09-26 07:28:17 +00:00
//遍历里面没有则返回第一个
itemPrice = stepPrice.PriceRange[0].Price + fittingPrice
return itemPrice * purchaseQuantity, itemPrice, nil
2023-09-20 07:21:42 +00:00
}