From 86c39263be8a27e638b8e5e22b6dcf184eb75086 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 20 Sep 2023 10:31:36 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=AD=E7=89=A9?= =?UTF-8?q?=E8=BD=A6=E6=96=B0=E5=A2=9E=E7=81=AF=E5=85=89=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gmodel/fs_product_model3d_light_logic.go | 4 ++++ model/gmodel/fs_shopping_cart_gen.go | 2 ++ .../internal/logic/addtocartlogic.go | 22 +++++++++++++++++++ utils/shopping_cart/shopping_cart_snapshot.go | 5 +++++ 4 files changed, 33 insertions(+) diff --git a/model/gmodel/fs_product_model3d_light_logic.go b/model/gmodel/fs_product_model3d_light_logic.go index 445c26c0..76fb12b3 100755 --- a/model/gmodel/fs_product_model3d_light_logic.go +++ b/model/gmodel/fs_product_model3d_light_logic.go @@ -2,6 +2,10 @@ package gmodel import "context" +func (l *FsProductModel3dLightModel) FindOne(ctx context.Context, id int64) (resp *FsProductModel3dLight, err error) { + err = l.db.WithContext(ctx).Model(&FsProductModel3dLight{}).Where("`id` = ? and `status` = ?", id, 1).Take(&resp).Error + return resp, err +} func (l *FsProductModel3dLightModel) GetAllByIds(ctx context.Context, ids []int64) (resp []FsProductModel3dLight, err error) { if len(ids) == 0 { return diff --git a/model/gmodel/fs_shopping_cart_gen.go b/model/gmodel/fs_shopping_cart_gen.go index 04996ad2..88ecb052 100644 --- a/model/gmodel/fs_shopping_cart_gen.go +++ b/model/gmodel/fs_shopping_cart_gen.go @@ -13,9 +13,11 @@ type FsShoppingCart struct { TemplateId *int64 `gorm:"default:0;" json:"template_id"` // 模板id ModelId *int64 `gorm:"default:0;" json:"model_id"` // 模型id SizeId *int64 `gorm:"default:0;" json:"size_id"` // 尺寸id + LightId *int64 `gorm:"default:0;" json:"light_id"` // 灯光id FittingId *int64 `gorm:"default:0;" json:"fitting_id"` // 配件id PurchaseQuantity *int64 `gorm:"default:0;" json:"purchase_quantity"` // 购买数量 Snapshot *string `gorm:"default:'';" json:"snapshot"` // + IsSelected *int64 `gorm:"default:0;" json:"is_selected"` // 是否被选中 0非 1是 IsHighlyCustomized *int64 `gorm:"default:0;" json:"is_highly_customized"` // 是否高度定制 0非 1是(针对客人高度定制只能后台增加如购物车) Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // diff --git a/server/shopping-cart/internal/logic/addtocartlogic.go b/server/shopping-cart/internal/logic/addtocartlogic.go index e792eefe..43021ab3 100644 --- a/server/shopping-cart/internal/logic/addtocartlogic.go +++ b/server/shopping-cart/internal/logic/addtocartlogic.go @@ -66,6 +66,8 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI templateTag string //模板表的模板标签 fittingJson string //配件的json设计信息 fittingName string //配件名 + lightJson string //灯光设计数据 + lightName string //灯光名字 ) //有模板 if req.TemplateId > 0 { @@ -140,6 +142,21 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI if modelInfo.ModelInfo == nil || *modelInfo.ModelInfo == "" { return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model`s design info is empty") } + //获取灯光信息 + if *modelInfo.Light > 0 { + lightInfo, err := l.svcCtx.AllModels.FsProductModel3dLight.FindOne(l.ctx, *modelInfo.Light) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the model`s light info is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get light info") + } + lightName = *lightInfo.Name + if lightInfo.Info != nil && *lightInfo.Info != "" { + lightJson = *lightInfo.Info + } + } var sizeKeyInfo shopping_cart.SizeInfo if err = json.Unmarshal([]byte(*sizeInfo.Title), &sizeKeyInfo); err != nil { logx.Error(err) @@ -174,6 +191,10 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI Qrcode: req.DiyInfo.Qrcode, Slogan: req.DiyInfo.Slogan, }, + LightInfo: shopping_cart.LightInfo{ + LightJson: lightJson, + LightName: lightName, + }, } snapshotJsonBytes, _ := json.Marshal(snapshot) snapshotJsonStr := string(snapshotJsonBytes) @@ -183,6 +204,7 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI ProductId: &req.ProductId, TemplateId: &req.TemplateId, ModelId: &modelInfo.Id, + LightId: modelInfo.Light, SizeId: &req.SizeId, FittingId: &req.FittingId, PurchaseQuantity: &req.PurchaseQuantity, diff --git a/utils/shopping_cart/shopping_cart_snapshot.go b/utils/shopping_cart/shopping_cart_snapshot.go index 07df7c9c..45539065 100644 --- a/utils/shopping_cart/shopping_cart_snapshot.go +++ b/utils/shopping_cart/shopping_cart_snapshot.go @@ -11,6 +11,7 @@ type CartSnapshot struct { SizeInfo SizeInfo `json:"size_info"` //尺寸基本信息 ProductInfo ProductInfo `json:"product_info"` //产品基本信息(只记录不要使用) UserDiyInformation UserDiyInformation `json:"user_diy_information"` //用户diy数据 + LightInfo LightInfo `json:"light_info"` //灯光数据 } type ProductInfo struct { ProductName string `json:"product_name"` @@ -39,3 +40,7 @@ type UserDiyInformation struct { Qrcode string `json:"qrcode"` //二维码 Slogan string `json:"slogan"` //slogan } +type LightInfo struct { + LightJson string `json:"light_json"` //灯光设计json数据 + LightName string `json:"light_name"` //名称 +} From d8707e3a4af9aedce09f186a062ce9095337ebd7 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 20 Sep 2023 11:02:46 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=AD=E7=89=A9?= =?UTF-8?q?=E8=BD=A6=E6=96=B0=E5=A2=9E=E7=81=AF=E5=85=89=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/logic/calculatecartpricelogic.go | 12 ++++++++---- server/shopping-cart/internal/types/types.go | 1 + server_api/shopping-cart.api | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 3f34b490..31a4b317 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -46,13 +46,13 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{CalculateResultList: []types.CalculateResultItem{}}) } cartIds := make([]int64, 0, len(req.CalculateList)) - mapCalculateQuantity := make(map[int64]int64) + mapCalculateQuantity := make(map[int64]types.CalculateItem) for _, v := range req.CalculateList { cartIds = append(cartIds, v.CartId) if v.PurchaseQuantity <= 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "purchase quantity must grater than 0") } - mapCalculateQuantity[v.CartId] = v.PurchaseQuantity + mapCalculateQuantity[v.CartId] = v } //获取购物车列表 carts, _, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{ @@ -128,7 +128,8 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri return errors.New(fmt.Sprintf("step price or step number is not set:%d_%d", *cart.ProductId, *cart.SizeId)) } //请求的数量 - reqPurchaseQuantity := mapCalculateQuantity[cart.Id] + reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity + isSelected := mapCalculateQuantity[cart.Id].IsSelected //购买箱数 boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*sizePrice.EachBoxNum))) //根据数量获取阶梯价格中对应的价格 @@ -148,7 +149,10 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), }) - subTotalPrice += totalPrice + //如果是选中则累加总价 + if isSelected { + subTotalPrice += totalPrice + } //更新购物车购买数量 err = shoppingCartModel.Update(l.ctx, cart.Id, userinfo.UserId, &gmodel.FsShoppingCart{ PurchaseQuantity: &reqPurchaseQuantity, diff --git a/server/shopping-cart/internal/types/types.go b/server/shopping-cart/internal/types/types.go index 1ed8b661..65041e67 100644 --- a/server/shopping-cart/internal/types/types.go +++ b/server/shopping-cart/internal/types/types.go @@ -95,6 +95,7 @@ type CalculateCartPriceReq struct { type CalculateItem struct { CartId int64 `json:"cart_id"` //购物车id PurchaseQuantity int64 `json:"purchase_quantity"` //数量 + IsSelected bool `json:"is_selected"` //是否是选中的,选中的会统计到总价中 } type CalculateCartPriceRsp struct { diff --git a/server_api/shopping-cart.api b/server_api/shopping-cart.api index 216bdfe2..0c076fa2 100644 --- a/server_api/shopping-cart.api +++ b/server_api/shopping-cart.api @@ -109,6 +109,7 @@ type CalculateCartPriceReq { type CalculateItem { CartId int64 `json:"cart_id"` //购物车id PurchaseQuantity int64 `json:"purchase_quantity"` //数量 + IsSelected bool `json:"is_selected"` //是否是选中的,选中的会统计到总价中 } type CalculateCartPriceRsp { SubTotalPrice string `json:"sub_total_price"` From f2847024481c5b595ba9225c372b6dd4e29bb9ab Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 20 Sep 2023 11:05:59 +0800 Subject: [PATCH 3/4] 11 --- .../modifycartpurchasequantityhandler.go | 35 ---------- .../shopping-cart/internal/handler/routes.go | 5 -- .../logic/modifycartpurchasequantitylogic.go | 69 ------------------- server/shopping-cart/internal/types/types.go | 5 -- server_api/shopping-cart.api | 9 +-- 5 files changed, 1 insertion(+), 122 deletions(-) delete mode 100644 server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go delete mode 100644 server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go diff --git a/server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go b/server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go deleted file mode 100644 index b04427bf..00000000 --- a/server/shopping-cart/internal/handler/modifycartpurchasequantityhandler.go +++ /dev/null @@ -1,35 +0,0 @@ -package handler - -import ( - "net/http" - "reflect" - - "fusenapi/utils/basic" - - "fusenapi/server/shopping-cart/internal/logic" - "fusenapi/server/shopping-cart/internal/svc" - "fusenapi/server/shopping-cart/internal/types" -) - -func ModifyCartPurchaseQuantityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - - var req types.ModifyCartPurchaseQuantityReq - userinfo, err := basic.RequestParse(w, r, svcCtx, &req) - if err != nil { - return - } - - // 创建一个业务逻辑层实例 - l := logic.NewModifyCartPurchaseQuantityLogic(r.Context(), svcCtx) - - rl := reflect.ValueOf(l) - basic.BeforeLogic(w, r, rl) - - resp := l.ModifyCartPurchaseQuantity(&req, userinfo) - - if !basic.AfterLogic(w, r, rl, resp) { - basic.NormalAfterLogic(w, r, resp) - } - } -} diff --git a/server/shopping-cart/internal/handler/routes.go b/server/shopping-cart/internal/handler/routes.go index 3a4188d6..14f2c2d3 100644 --- a/server/shopping-cart/internal/handler/routes.go +++ b/server/shopping-cart/internal/handler/routes.go @@ -22,11 +22,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/shopping-cart/delete", Handler: DeleteCartHandler(serverCtx), }, - { - Method: http.MethodPost, - Path: "/api/shopping-cart/modify", - Handler: ModifyCartPurchaseQuantityHandler(serverCtx), - }, { Method: http.MethodGet, Path: "/api/shopping-cart/get_carts", diff --git a/server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go b/server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go deleted file mode 100644 index b78dd2e8..00000000 --- a/server/shopping-cart/internal/logic/modifycartpurchasequantitylogic.go +++ /dev/null @@ -1,69 +0,0 @@ -package logic - -import ( - "errors" - "fusenapi/model/gmodel" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "gorm.io/gorm" - - "context" - - "fusenapi/server/shopping-cart/internal/svc" - "fusenapi/server/shopping-cart/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type ModifyCartPurchaseQuantityLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewModifyCartPurchaseQuantityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyCartPurchaseQuantityLogic { - return &ModifyCartPurchaseQuantityLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -// 处理进入前逻辑w,r -// func (l *ModifyCartPurchaseQuantityLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { -// } - -func (l *ModifyCartPurchaseQuantityLogic) ModifyCartPurchaseQuantity(req *types.ModifyCartPurchaseQuantityReq, userinfo *auth.UserInfo) (resp *basic.Response) { - if !userinfo.IsUser() { - return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in") - } - if req.Id <= 0 { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "cart id is required") - } - if req.PurchaseQuantity <= 0 { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "purchase quantity can not less than 0 or equal to 0") - } - //查询购物车 - _, err := l.svcCtx.AllModels.FsShoppingCart.FineOneUserCart(l.ctx, req.Id, userinfo.UserId, "id") - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the shopping cart is not exists") - } - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get shopping cart info ") - } - //修改数量 - err = l.svcCtx.AllModels.FsShoppingCart.Update(l.ctx, req.Id, userinfo.UserId, &gmodel.FsShoppingCart{ - PurchaseQuantity: &req.PurchaseQuantity, - }) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to modify cart purchase quantity") - } - return resp.SetStatus(basic.CodeOK, "success") -} - -// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 -// func (l *ModifyCartPurchaseQuantityLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { -// // httpx.OkJsonCtx(r.Context(), w, resp) -// } diff --git a/server/shopping-cart/internal/types/types.go b/server/shopping-cart/internal/types/types.go index 65041e67..a1eb5ab2 100644 --- a/server/shopping-cart/internal/types/types.go +++ b/server/shopping-cart/internal/types/types.go @@ -29,11 +29,6 @@ type DeleteCartReq struct { Id int64 `json:"id"` //购物车id } -type ModifyCartPurchaseQuantityReq struct { - Id int64 `json:"id"` //购物车id - PurchaseQuantity int64 `json:"purchase_quantity"` //数量 -} - type GetCartsReq struct { CurrentPage int `form:"current_page"` //当前页 } diff --git a/server_api/shopping-cart.api b/server_api/shopping-cart.api index 0c076fa2..bb347743 100644 --- a/server_api/shopping-cart.api +++ b/server_api/shopping-cart.api @@ -15,9 +15,6 @@ service shopping-cart { //删除购物车 @handler DeleteCartHandler post /api/shopping-cart/delete(DeleteCartReq) returns (response); - //修改购物车购买数量 - @handler ModifyCartPurchaseQuantityHandler - post /api/shopping-cart/modify(ModifyCartPurchaseQuantityReq) returns (response); //获取购物车列表 @handler GetCartsHandler get /api/shopping-cart/get_carts(GetCartsReq) returns (response); @@ -49,11 +46,7 @@ type DiyInfo { type DeleteCartReq { Id int64 `json:"id"` //购物车id } -//修改购物车购买数量 -type ModifyCartPurchaseQuantityReq { - Id int64 `json:"id"` //购物车id - PurchaseQuantity int64 `json:"purchase_quantity"` //数量 -} + //获取购物车列表 type GetCartsReq { CurrentPage int `form:"current_page"` //当前页 From 9342ab8c24de2fe9d0ec2fe9ee4f0a3fe53b565b Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 20 Sep 2023 11:17:00 +0800 Subject: [PATCH 4/4] 11 --- .../internal/logic/calculatecartpricelogic.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 31a4b317..4eb6b7c0 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -129,7 +129,10 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri } //请求的数量 reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity - isSelected := mapCalculateQuantity[cart.Id].IsSelected + isSelected := int64(0) + if mapCalculateQuantity[cart.Id].IsSelected { + isSelected = 1 + } //购买箱数 boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*sizePrice.EachBoxNum))) //根据数量获取阶梯价格中对应的价格 @@ -149,15 +152,16 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), }) + updData := &gmodel.FsShoppingCart{ + PurchaseQuantity: &reqPurchaseQuantity, + } //如果是选中则累加总价 - if isSelected { + if isSelected == 1 { subTotalPrice += totalPrice + updData.IsSelected = &isSelected } //更新购物车购买数量 - err = shoppingCartModel.Update(l.ctx, cart.Id, userinfo.UserId, &gmodel.FsShoppingCart{ - PurchaseQuantity: &reqPurchaseQuantity, - }) - if err != nil { + if err = shoppingCartModel.Update(l.ctx, cart.Id, userinfo.UserId, updData); err != nil { logx.Error(err) return errors.New(fmt.Sprintf("failed to update cart`s purchase quantity:%d", cart.Id)) }