From 3bba720f676766543cb61771accb90fcee9e67f7 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 10:58:24 +0800 Subject: [PATCH 01/15] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 7440e159..ad22851d 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -94,7 +94,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id != ? and `ltime` > ?` ", userId, 1, addressId, now.Add(time.Hour*24*365)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*365)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) From c12675698b815c35ce628c09327b86a41894e239 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:01:42 +0800 Subject: [PATCH 02/15] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index ad22851d..936843dc 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -87,7 +87,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() - err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id = ?` ", userId, 1, addressId). + err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). UpdateColumn("ltime", now.Add(time.Hour*24*365*100)). UpdateColumn("utime", now).Error if err != nil { From 2331979b657fa574eb5e3b8b9d4cd08888e7fbd0 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:04:12 +0800 Subject: [PATCH 03/15] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 936843dc..c633e89d 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,7 +88,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*100)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*50)). UpdateColumn("utime", now).Error if err != nil { return err From d0323646c5fa59ff522bf1ab9bc8205834a147d7 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:05:31 +0800 Subject: [PATCH 04/15] info address --- model/gmodel/fs_address_logic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index c633e89d..561e01c7 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,13 +88,13 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*50)). + UpdateColumn("ltime", now.Add(time.Hour*24*365)). UpdateColumn("utime", now).Error if err != nil { return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*365)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*128)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) From 0e282e741bfd0146079b7b8aad97a0e1332847e5 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:06:37 +0800 Subject: [PATCH 05/15] info address --- model/gmodel/fs_address_logic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 561e01c7..3bfeb8ed 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,13 +88,13 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*20)). UpdateColumn("utime", now).Error if err != nil { return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*128)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*365)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) From a832a6fa817e341c2df29409f5a58952e4d85743 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:07:45 +0800 Subject: [PATCH 06/15] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 3bfeb8ed..397ac55c 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,7 +88,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*20)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*10)). UpdateColumn("utime", now).Error if err != nil { return err From 87ff25a2e611bfeb553f6af30e0d516d61c0dc7a Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:08:32 +0800 Subject: [PATCH 07/15] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 397ac55c..c633e89d 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,7 +88,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*10)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*50)). UpdateColumn("utime", now).Error if err != nil { return err From f615e4fe7757e0abbc20c58970f8ec630898edd6 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:10:41 +0800 Subject: [PATCH 08/15] fix --- model/gmodel/fs_product_model3d_logic.go | 12 ++-- .../handler/caculateproductpricehandler.go | 35 ++++++++++++ server/product/internal/handler/routes.go | 5 ++ .../logic/caculateproductpricelogic.go | 57 +++++++++++++++++++ server/product/internal/types/types.go | 7 +++ server_api/product.api | 10 ++++ 6 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 server/product/internal/handler/caculateproductpricehandler.go create mode 100644 server/product/internal/logic/caculateproductpricelogic.go diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 9b01334a..dba92122 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -16,7 +16,7 @@ type StepPriceJsonStruct struct { } func (d *FsProductModel3dModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProductModel3d, err error) { - db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? ", id) + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` = ? and `status` =? ", id, 1) if len(fields) > 0 { db = db.Select(fields[0]) } @@ -41,7 +41,7 @@ func (d *FsProductModel3dModel) GetAllByIdsWithoutStatus(ctx context.Context, id if len(ids) == 0 { return } - db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?)", ids) + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ?", ids, 1) if len(fields) > 0 { db = db.Select(fields[0]) } @@ -80,13 +80,13 @@ func (d *FsProductModel3dModel) Get3dModelsByParam(ctx context.Context, req Get3 return resp, err } func (d *FsProductModel3dModel) Update(ctx context.Context, id int64, data *FsProductModel3d) error { - return d.db.WithContext(ctx).Where("`id` = ? ", id).Updates(&data).Error + return d.db.WithContext(ctx).Where("`id` = ? and `status` =? ", id, 1).Updates(&data).Error } func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds []int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) { if len(sizeIds) == 0 { return } - db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ?", sizeIds, tag) + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`size_id` in (?) and `tag` = ? and `status` = ?", sizeIds, tag, 1) if len(fields) != 0 { db = db.Select(fields[0]) } @@ -94,7 +94,7 @@ func (d *FsProductModel3dModel) GetAllBySizeIdsTag(ctx context.Context, sizeIds return resp, err } func (d *FsProductModel3dModel) GetAll(ctx context.Context) (resp []FsProductModel3d, err error) { - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Find(&resp).Error + err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`status` = ?", 1).Find(&resp).Error return resp, err } @@ -107,7 +107,7 @@ func (d *FsProductModel3dModel) GetGroupPartListByProductIds(ctx context.Context if len(productIds) == 0 { return } - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}). + err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`product_id` in(?) and `status` =? ", productIds, 1). Select("product_id,group_concat(part_list) as part_list"). Group("product_id").Find(&resp).Error return resp, err diff --git a/server/product/internal/handler/caculateproductpricehandler.go b/server/product/internal/handler/caculateproductpricehandler.go new file mode 100644 index 00000000..1624b5bc --- /dev/null +++ b/server/product/internal/handler/caculateproductpricehandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/product/internal/logic" + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" +) + +func CaculateProductPriceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.CaculateProductPriceReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewCaculateProductPriceLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.CaculateProductPrice(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index 260422e3..75311158 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -77,6 +77,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/product/get_product_step_price", Handler: GetProductStepPriceHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/product/caculate_product_price", + Handler: CaculateProductPriceHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/api/product/get_size_by_pid", diff --git a/server/product/internal/logic/caculateproductpricelogic.go b/server/product/internal/logic/caculateproductpricelogic.go new file mode 100644 index 00000000..2d31f963 --- /dev/null +++ b/server/product/internal/logic/caculateproductpricelogic.go @@ -0,0 +1,57 @@ +package logic + +import ( + "errors" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "gorm.io/gorm" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CaculateProductPriceLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCaculateProductPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaculateProductPriceLogic { + return &CaculateProductPriceLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *CaculateProductPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *CaculateProductPriceLogic) CaculateProductPrice(req *types.CaculateProductPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if req.ProductId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product id") + } + if req.SizeId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:size id") + } + //获取产品信息(只是获取id) + _, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId, "id") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") + } + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *CaculateProductPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index b9b96864..8d4c0e2d 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -335,6 +335,13 @@ type GetProductStepPriceReq struct { ProductId int64 `form:"product_id"` } +type CaculateProductPriceReq struct { + ProductId int64 `json:"product_id"` + SizeId int64 `json:"size_id"` + FittingId int64 `json:"fitting_id,optional"` + PurchaseQuantity int64 `json:"purchase_quantity"` +} + type GetSizeByPidReq struct { Pid string `form:"pid"` TemplateTag string `form:"template_tag"` diff --git a/server_api/product.api b/server_api/product.api index 94715b66..10007e2e 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -50,6 +50,9 @@ service product { //获取产品阶梯价格信息 @handler GetProductStepPriceHandler get /api/product/get_product_step_price(GetProductStepPriceReq) returns (response); + //根据产品+配件搭配计算价格 + @handler CaculateProductPriceHandler + post /api/product/caculate_product_price(CaculateProductPriceReq) returns (response); //获取产品尺寸列表 @handler GetSizeByPidHandler get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response); @@ -383,6 +386,13 @@ type PriceItem { type GetProductStepPriceReq { ProductId int64 `form:"product_id"` } +//根据产品+配件搭配计算价格 +type CaculateProductPriceReq { + ProductId int64 `json:"product_id"` + SizeId int64 `json:"size_id"` + FittingId int64 `json:"fitting_id,optional"` + PurchaseQuantity int64 `json:"purchase_quantity"` +} //获取产品尺寸列表 type GetSizeByPidReq { Pid string `form:"pid"` From 87c2d7bf96540bf5e2f7eb7a85148f5670ecf760 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:11:34 +0800 Subject: [PATCH 09/15] info address --- model/gmodel/fs_address_logic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index c633e89d..9b7d2ae9 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,13 +88,13 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*50)). + UpdateColumn("ltime", now.AddDate(50, 0, 0)). UpdateColumn("utime", now).Error if err != nil { return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*365)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.AddDate(1, 0, 0)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) From 7eab65be87b78b62696013522685c377ae1013ba Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:21:43 +0800 Subject: [PATCH 10/15] info address --- model/gmodel/fs_address_logic.go | 4 ++-- server/info/internal/logic/infologic_test.go | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 9b7d2ae9..e0bbce03 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,13 +88,13 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.AddDate(50, 0, 0)). + UpdateColumn("ltime", now.AddDate(250, 0, 0)). UpdateColumn("utime", now).Error if err != nil { return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.AddDate(1, 0, 0)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.AddDate(10, 0, 0)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) diff --git a/server/info/internal/logic/infologic_test.go b/server/info/internal/logic/infologic_test.go index 17a9d104..afc00177 100644 --- a/server/info/internal/logic/infologic_test.go +++ b/server/info/internal/logic/infologic_test.go @@ -175,6 +175,10 @@ func TestCaseJSON_EXTRACT(t *testing.T) { conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") // err = conn.Exec(updatesql, 6).Error + + var result []gmodel.FsAddress + conn.Model(&gmodel.FsAddress{}).Find(&result) + log.Println(conn.Model(&gmodel.FsChangeCode{}).Select("id").Where("id = 5").Take(nil).Error) log.Println(err) } From 47a1ecfc795a26dadbbe96abe6c747177203412a Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:32:43 +0800 Subject: [PATCH 11/15] info address --- model/gmodel/fs_address_logic.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index e0bbce03..b3826175 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -107,10 +107,8 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i } func (a *FsAddressModel) DeleteOne(ctx context.Context, addressId int64, userId int64) (err error) { - address := &FsAddress{ - AddressId: addressId, - UserId: &userId, - } - err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`address_id` = ? and `user_id` = ? and `status` = ? ", addressId, userId, 1).Delete(address).Error + err = a.db.WithContext(ctx).Model(&FsAddress{}). + Where("`address_id` = ? and `user_id` = ? and `status` = ? ", addressId, userId, 1). + UpdateColumn("status", 0).Error return err } From 01cc115a6065a1ed12c7f9b1d3c24f095aa5c6b3 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:40:27 +0800 Subject: [PATCH 12/15] fix --- model/gmodel/fs_product_model3d_logic.go | 10 ++ ...ler.go => calculateproductpricehandler.go} | 8 +- server/product/internal/handler/routes.go | 4 +- .../logic/caculateproductpricelogic.go | 57 ----------- .../logic/calculateproductpricelogic.go | 97 +++++++++++++++++++ server/product/internal/types/types.go | 8 +- server_api/product.api | 11 ++- utils/step_price/price.go | 30 +++--- 8 files changed, 143 insertions(+), 82 deletions(-) rename server/product/internal/handler/{caculateproductpricehandler.go => calculateproductpricehandler.go} (69%) delete mode 100644 server/product/internal/logic/caculateproductpricelogic.go create mode 100644 server/product/internal/logic/calculateproductpricelogic.go diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index dba92122..df5f7200 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -142,3 +142,13 @@ func (d *FsProductModel3dModel) GetAllByProductIdTag(ctx context.Context, produc err = db.Find(&resp).Error return resp, err } +func (d *FsProductModel3dModel) FindOneByProductIdSizeIdTag(ctx context.Context, productId, sizeId, tag int64, fields ...string) (resp *FsProductModel3d, err error) { + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}). + Where("`product_id` = ? and `size_id` = ? and `tag` = ? and `status` = ?", productId, sizeId, tag, 1). + Order("sort DESC") + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Take(&resp).Error + return resp, err +} diff --git a/server/product/internal/handler/caculateproductpricehandler.go b/server/product/internal/handler/calculateproductpricehandler.go similarity index 69% rename from server/product/internal/handler/caculateproductpricehandler.go rename to server/product/internal/handler/calculateproductpricehandler.go index 1624b5bc..e50829dd 100644 --- a/server/product/internal/handler/caculateproductpricehandler.go +++ b/server/product/internal/handler/calculateproductpricehandler.go @@ -11,22 +11,22 @@ import ( "fusenapi/server/product/internal/types" ) -func CaculateProductPriceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func CalculateProductPriceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.CaculateProductPriceReq + var req types.CalculateProductPriceReq userinfo, err := basic.RequestParse(w, r, svcCtx, &req) if err != nil { return } // 创建一个业务逻辑层实例 - l := logic.NewCaculateProductPriceLogic(r.Context(), svcCtx) + l := logic.NewCalculateProductPriceLogic(r.Context(), svcCtx) rl := reflect.ValueOf(l) basic.BeforeLogic(w, r, rl) - resp := l.CaculateProductPrice(&req, userinfo) + resp := l.CalculateProductPrice(&req, userinfo) if !basic.AfterLogic(w, r, rl, resp) { basic.NormalAfterLogic(w, r, resp) diff --git a/server/product/internal/handler/routes.go b/server/product/internal/handler/routes.go index 75311158..67db4302 100644 --- a/server/product/internal/handler/routes.go +++ b/server/product/internal/handler/routes.go @@ -79,8 +79,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodPost, - Path: "/api/product/caculate_product_price", - Handler: CaculateProductPriceHandler(serverCtx), + Path: "/api/product/calculate_product_price", + Handler: CalculateProductPriceHandler(serverCtx), }, { Method: http.MethodGet, diff --git a/server/product/internal/logic/caculateproductpricelogic.go b/server/product/internal/logic/caculateproductpricelogic.go deleted file mode 100644 index 2d31f963..00000000 --- a/server/product/internal/logic/caculateproductpricelogic.go +++ /dev/null @@ -1,57 +0,0 @@ -package logic - -import ( - "errors" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "gorm.io/gorm" - - "context" - - "fusenapi/server/product/internal/svc" - "fusenapi/server/product/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type CaculateProductPriceLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewCaculateProductPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaculateProductPriceLogic { - return &CaculateProductPriceLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -// 处理进入前逻辑w,r -// func (l *CaculateProductPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { -// } - -func (l *CaculateProductPriceLogic) CaculateProductPrice(req *types.CaculateProductPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { - if req.ProductId <= 0 { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product id") - } - if req.SizeId <= 0 { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:size id") - } - //获取产品信息(只是获取id) - _, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId, "id") - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists") - } - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") - } - return resp.SetStatus(basic.CodeOK) -} - -// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 -// func (l *CaculateProductPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { -// // httpx.OkJsonCtx(r.Context(), w, resp) -// } diff --git a/server/product/internal/logic/calculateproductpricelogic.go b/server/product/internal/logic/calculateproductpricelogic.go new file mode 100644 index 00000000..6c5cf0e4 --- /dev/null +++ b/server/product/internal/logic/calculateproductpricelogic.go @@ -0,0 +1,97 @@ +package logic + +import ( + "encoding/json" + "errors" + "fusenapi/constants" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/format" + "fusenapi/utils/step_price" + "gorm.io/gorm" + + "context" + + "fusenapi/server/product/internal/svc" + "fusenapi/server/product/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CalculateProductPriceLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCalculateProductPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CalculateProductPriceLogic { + return &CalculateProductPriceLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *CalculateProductPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateProductPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if req.ProductId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:product id") + } + if req.SizeId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:size id") + } + if req.PurchaseQuantity <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:purchase quantity") + } + //获取产品信息(只是获取id) + _, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId, "id") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") + } + //根据产品跟尺寸获取模型价格信息 + modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOneByProductIdSizeIdTag(l.ctx, req.ProductId, req.SizeId, constants.TAG_MODEL, "id,step_price") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "model info is not exists") + } + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model info") + } + var stepPrice gmodel.StepPriceJsonStruct + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price") + } + //配件 + fittingPrice := int64(0) + if req.FittingId > 0 { + fittingInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.FittingId, "id,price") + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "fitting info is not exists") + } + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting info") + fittingPrice = *fittingInfo.Price + } + totalPrice, itemPrice, err := step_price.GetNewCentStepPrice(req.PurchaseQuantity, stepPrice, fittingPrice) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to calculate product price ") + } + return resp.SetStatus(basic.CodeOK, "success", types.CalculateProductPriceRsp{ + ItemPrice: format.CentitoDollar(itemPrice, 3), + TotalPrice: format.CentitoDollarWithNoHalfAdjust(totalPrice, 2), + PurchaseQuantity: req.PurchaseQuantity, + }) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *CalculateProductPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 8d4c0e2d..61d79237 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -335,13 +335,19 @@ type GetProductStepPriceReq struct { ProductId int64 `form:"product_id"` } -type CaculateProductPriceReq struct { +type CalculateProductPriceReq struct { ProductId int64 `json:"product_id"` SizeId int64 `json:"size_id"` FittingId int64 `json:"fitting_id,optional"` PurchaseQuantity int64 `json:"purchase_quantity"` } +type CalculateProductPriceRsp struct { + ItemPrice string `json:"item_price"` + TotalPrice string `json:"total_price"` + PurchaseQuantity int64 `json:"purchase_quantity"` +} + type GetSizeByPidReq struct { Pid string `form:"pid"` TemplateTag string `form:"template_tag"` diff --git a/server_api/product.api b/server_api/product.api index 10007e2e..0fe9971e 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -51,8 +51,8 @@ service product { @handler GetProductStepPriceHandler get /api/product/get_product_step_price(GetProductStepPriceReq) returns (response); //根据产品+配件搭配计算价格 - @handler CaculateProductPriceHandler - post /api/product/caculate_product_price(CaculateProductPriceReq) returns (response); + @handler CalculateProductPriceHandler + post /api/product/calculate_product_price(CalculateProductPriceReq) returns (response); //获取产品尺寸列表 @handler GetSizeByPidHandler get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response); @@ -387,12 +387,17 @@ type GetProductStepPriceReq { ProductId int64 `form:"product_id"` } //根据产品+配件搭配计算价格 -type CaculateProductPriceReq { +type CalculateProductPriceReq { ProductId int64 `json:"product_id"` SizeId int64 `json:"size_id"` FittingId int64 `json:"fitting_id,optional"` PurchaseQuantity int64 `json:"purchase_quantity"` } +type CalculateProductPriceRsp { + ItemPrice string `json:"item_price"` + TotalPrice string `json:"total_price"` + PurchaseQuantity int64 `json:"purchase_quantity"` +} //获取产品尺寸列表 type GetSizeByPidReq { Pid string `form:"pid"` diff --git a/utils/step_price/price.go b/utils/step_price/price.go index ec3748cd..8230f39f 100644 --- a/utils/step_price/price.go +++ b/utils/step_price/price.go @@ -5,7 +5,7 @@ import ( "fusenapi/model/gmodel" ) -// 返回厘 +// 旧的返回厘(即将废弃) func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 { if minBuyNum > stepNum[len(stepNum)-1] { return int64(stepPrice[len(stepPrice)-1]) @@ -22,10 +22,10 @@ func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 { } // 新的阶梯价格(返回美元) -func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (price float64, err error) { +func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice float64, err error) { l := len(stepPrice.PriceRange) if l == 0 { - return 0, errors.New("price range is not set") + return 0, 0, errors.New("price range is not set") } //遍历查询合适的价格 for k, v := range stepPrice.PriceRange { @@ -33,34 +33,34 @@ func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruc if purchaseQuantity > v.StartQuantity { //最后一个 || 小于等于终点 if k == l-1 || purchaseQuantity <= v.EndQuantity { - return float64(v.Price+fittingPrice) / 1000, nil + itemPrice = float64(v.Price+fittingPrice) / 1000 + return itemPrice * float64(purchaseQuantity), itemPrice / 1000, nil } } } //遍历里面没有则返回第一个 - return float64(stepPrice.PriceRange[0].Price+fittingPrice) / 1000, nil + itemPrice = float64(stepPrice.PriceRange[0].Price+fittingPrice) / 1000 + return itemPrice * float64(purchaseQuantity), itemPrice, nil } // 新的阶梯价格(返回厘) -func GetNewCentStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (price int64, err error) { +func GetNewCentStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error) { l := len(stepPrice.PriceRange) if l == 0 { - return 0, errors.New("price range is not set") + return 0, 0, errors.New("price range is not set") } //遍历查询合适的价格 for k, v := range stepPrice.PriceRange { //购买数量>起点 if purchaseQuantity > v.StartQuantity { - //最后一个 - if k == l-1 { - return v.Price + fittingPrice, nil - } - //小于等于终点 - if purchaseQuantity <= v.EndQuantity { - return v.Price + fittingPrice, nil + //最后一个 || 小于等于终点 + if k == l-1 || purchaseQuantity <= v.EndQuantity { + itemPrice = v.Price + fittingPrice + return itemPrice * purchaseQuantity, itemPrice, nil } } } //遍历里面没有则返回第一个 - return stepPrice.PriceRange[0].Price + fittingPrice, nil + itemPrice = stepPrice.PriceRange[0].Price + fittingPrice + return itemPrice * purchaseQuantity, itemPrice, nil } From f7992279e227991500a0e7d19b393c578693f730 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:47:31 +0800 Subject: [PATCH 13/15] fix --- server/product/internal/logic/getsizebypidlogic.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/product/internal/logic/getsizebypidlogic.go b/server/product/internal/logic/getsizebypidlogic.go index 0515f61e..7f27f40e 100644 --- a/server/product/internal/logic/getsizebypidlogic.go +++ b/server/product/internal/logic/getsizebypidlogic.go @@ -59,7 +59,10 @@ func (l *GetSizeByPidLogic) GetSizeByPid(req *types.GetSizeByPidReq, userinfo *a } else { //根据模板找到模型sizeId defaultModel3d, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, *defaultTemplate.ModelId, "size_id") - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the template`s model is not exists ") + } logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get default model ") } From 6a41be4f3a12e8633b9d958d537a2d01697db19c Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 12:08:51 +0800 Subject: [PATCH 14/15] info address --- server/info/internal/handler/routes.go | 2 -- server/info/internal/types/types.go | 2 +- server_api/info.api | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/server/info/internal/handler/routes.go b/server/info/internal/handler/routes.go index 82d01e1c..8ab5d58b 100644 --- a/server/info/internal/handler/routes.go +++ b/server/info/internal/handler/routes.go @@ -2,7 +2,6 @@ package handler import ( - "net/http" "fusenapi/server/info/internal/svc" @@ -11,7 +10,6 @@ import ( ) func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { - server.AddRoutes( []rest.Route{ { diff --git a/server/info/internal/types/types.go b/server/info/internal/types/types.go index c3ca47c3..2b740955 100644 --- a/server/info/internal/types/types.go +++ b/server/info/internal/types/types.go @@ -23,7 +23,7 @@ type AddressNameRequest struct { } type AddressRequest struct { - AddressId int64 `json:"address_id,omitempty"` + AddressId int64 `json:"address_id,optional"` IsDefault int64 `json:"is_default"` //是否默认 AddressName string `json:"address_name"` //收货人 FirstName string `json:"first_name"` //first_name diff --git a/server_api/info.api b/server_api/info.api index dfadc0b2..094b8dba 100644 --- a/server_api/info.api +++ b/server_api/info.api @@ -17,7 +17,7 @@ service info { post /api/info/address/default(AddressIdRequest) returns (response); @handler AddressAddHandler - post /api/info/address/add(AddressNameRequest) returns (response); + post /api/info/address/add(AddressRequest) returns (response); @handler AddressUpdateHandler post /api/info/address/update(AddressObjectRequest) returns (response); @@ -48,7 +48,7 @@ type ( } AddressRequest { - AddressId int64 `json:"address_id,omitempty"` + AddressId int64 `json:"address_id,optional"` IsDefault int64 `json:"is_default"` //是否默认 AddressName string `json:"address_name"` //收货人 FirstName string `json:"first_name"` //first_name From e23ee6518d07cf50f5293ce207c79886271ad221 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 12:12:50 +0800 Subject: [PATCH 15/15] fix --- server/product/internal/logic/calculateproductpricelogic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/product/internal/logic/calculateproductpricelogic.go b/server/product/internal/logic/calculateproductpricelogic.go index 6c5cf0e4..2da76ec1 100644 --- a/server/product/internal/logic/calculateproductpricelogic.go +++ b/server/product/internal/logic/calculateproductpricelogic.go @@ -84,7 +84,7 @@ func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateP logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to calculate product price ") } - return resp.SetStatus(basic.CodeOK, "success", types.CalculateProductPriceRsp{ + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateProductPriceRsp{ ItemPrice: format.CentitoDollar(itemPrice, 3), TotalPrice: format.CentitoDollarWithNoHalfAdjust(totalPrice, 2), PurchaseQuantity: req.PurchaseQuantity,