From 6158779a9507980817a9fc07f36c8050d4bb208d Mon Sep 17 00:00:00 2001 From: Hiven Date: Thu, 3 Aug 2023 17:37:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/fs_order_logic.go | 12 +- model/gmodel/fs_pay_logic.go | 10 +- model/gmodel/fs_refund_reason_logic.go | 4 +- model/gmodel/fs_resource_logic.go | 4 +- model/gmodel/fs_resources_logic.go | 4 +- model/gmodel/fs_user_material_logic.go | 6 +- .../home-user-auth/internal/handler/routes.go | 5 + .../internal/handler/useragainorderhandler.go | 35 +++++ .../internal/logic/useragainorderlogic.go | 144 ++++++++++++++++++ .../internal/logic/userordercancellogic.go | 8 +- server/home-user-auth/internal/types/types.go | 7 + server_api/home-user-auth.api | 11 ++ utils/{handler => handlers}/gormHandler.go | 2 +- utils/{handler => handlers}/payHandler.go | 2 +- 14 files changed, 228 insertions(+), 26 deletions(-) create mode 100644 server/home-user-auth/internal/handler/useragainorderhandler.go create mode 100644 server/home-user-auth/internal/logic/useragainorderlogic.go rename utils/{handler => handlers}/gormHandler.go (99%) rename utils/{handler => handlers}/payHandler.go (99%) diff --git a/model/gmodel/fs_order_logic.go b/model/gmodel/fs_order_logic.go index b96e4d90..60ce74d8 100755 --- a/model/gmodel/fs_order_logic.go +++ b/model/gmodel/fs_order_logic.go @@ -6,7 +6,7 @@ import ( "reflect" "time" - "fusenapi/utils/handler" + "fusenapi/utils/handlers" "gorm.io/gorm" ) @@ -60,7 +60,7 @@ func (o *FsOrderModel) FindPageListByPage(ctx context.Context, rowBuilder *gorm. var resp []*FsOrderRel // 过滤 if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } // 排序 @@ -70,11 +70,11 @@ func (o *FsOrderModel) FindPageListByPage(ctx context.Context, rowBuilder *gorm. for i := 0; i < s.NumField(); i++ { fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{} } - rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap)) + rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap)) } // 分页 - rowBuilder = rowBuilder.Scopes(handler.Paginate(page, pageSize)) + rowBuilder = rowBuilder.Scopes(handlers.Paginate(page, pageSize)) // 结果 result := rowBuilder.Debug().WithContext(ctx).Find(&resp) @@ -125,7 +125,7 @@ func (m *FsOrderModel) FindCount(ctx context.Context, countBuilder *gorm.DB, fil // 过滤 if filterMap != nil { - countBuilder = countBuilder.Scopes(handler.FilterData(filterMap)) + countBuilder = countBuilder.Scopes(handlers.FilterData(filterMap)) } result := countBuilder.WithContext(ctx).Limit(1).Count(&count) @@ -140,7 +140,7 @@ func (m *FsOrderModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB, var resp FsOrderRel if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp) diff --git a/model/gmodel/fs_pay_logic.go b/model/gmodel/fs_pay_logic.go index fa4a6093..c23f3b36 100644 --- a/model/gmodel/fs_pay_logic.go +++ b/model/gmodel/fs_pay_logic.go @@ -2,7 +2,7 @@ package gmodel import ( "context" - "fusenapi/utils/handler" + "fusenapi/utils/handlers" "reflect" "gorm.io/gorm" @@ -55,7 +55,7 @@ func (m *FsPayModel) FindCount(ctx context.Context, countBuilder *gorm.DB, filte // 过滤 if filterMap != nil { - countBuilder = countBuilder.Scopes(handler.FilterData(filterMap)) + countBuilder = countBuilder.Scopes(handlers.FilterData(filterMap)) } result := countBuilder.WithContext(ctx).Limit(1).Count(&count) @@ -70,7 +70,7 @@ func (m *FsPayModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.DB, fi var resp FsPay if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp) @@ -85,7 +85,7 @@ func (m *FsPayModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap var resp []*FsPay // 过滤 if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } // 排序 @@ -95,7 +95,7 @@ func (m *FsPayModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap for i := 0; i < s.NumField(); i++ { fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{} } - rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap)) + rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap)) } result := rowBuilder.WithContext(ctx).Find(&resp) diff --git a/model/gmodel/fs_refund_reason_logic.go b/model/gmodel/fs_refund_reason_logic.go index 48e67d51..14acda7a 100644 --- a/model/gmodel/fs_refund_reason_logic.go +++ b/model/gmodel/fs_refund_reason_logic.go @@ -2,7 +2,7 @@ package gmodel import ( "context" - "fusenapi/utils/handler" + "fusenapi/utils/handlers" "gorm.io/gorm" ) @@ -40,7 +40,7 @@ func (m *FsRefundReasonModel) FindOneByQuery(ctx context.Context, rowBuilder *go var resp FsRefundReason if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp) diff --git a/model/gmodel/fs_resource_logic.go b/model/gmodel/fs_resource_logic.go index e72f5ae3..6dab100f 100644 --- a/model/gmodel/fs_resource_logic.go +++ b/model/gmodel/fs_resource_logic.go @@ -3,7 +3,7 @@ package gmodel import ( "context" "errors" - "fusenapi/utils/handler" + "fusenapi/utils/handlers" "gorm.io/gorm" ) @@ -36,7 +36,7 @@ func (m *FsResourceModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm.D var resp FsResource if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp) diff --git a/model/gmodel/fs_resources_logic.go b/model/gmodel/fs_resources_logic.go index 767af34a..3ae4657c 100644 --- a/model/gmodel/fs_resources_logic.go +++ b/model/gmodel/fs_resources_logic.go @@ -2,7 +2,7 @@ package gmodel import ( "context" - "fusenapi/utils/handler" + "fusenapi/utils/handlers" "gorm.io/gorm" ) @@ -23,7 +23,7 @@ func (m *FsResourcesModel) FindOneByQuery(ctx context.Context, rowBuilder *gorm. var resp FsResources if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } result := rowBuilder.WithContext(ctx).Limit(1).Find(&resp) diff --git a/model/gmodel/fs_user_material_logic.go b/model/gmodel/fs_user_material_logic.go index 73111e58..d8ecaaf4 100644 --- a/model/gmodel/fs_user_material_logic.go +++ b/model/gmodel/fs_user_material_logic.go @@ -2,7 +2,7 @@ package gmodel import ( "context" - "fusenapi/utils/handler" + "fusenapi/utils/handlers" "reflect" "gorm.io/gorm" @@ -24,7 +24,7 @@ func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, var resp []*FsUserMaterial // 过滤 if filterMap != nil { - rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap)) } // 排序 @@ -34,7 +34,7 @@ func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, for i := 0; i < s.NumField(); i++ { fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{} } - rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap)) + rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap)) } result := rowBuilder.WithContext(ctx).Find(&resp) diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index a06a6fea..607e6278 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -97,6 +97,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/logo-list", Handler: UserLogoListHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/user/one-more-order", + Handler: UserAgainOrderHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/useragainorderhandler.go b/server/home-user-auth/internal/handler/useragainorderhandler.go new file mode 100644 index 00000000..4bf584e7 --- /dev/null +++ b/server/home-user-auth/internal/handler/useragainorderhandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/home-user-auth/internal/logic" + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" +) + +func UserAgainOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserAgainOrderReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserAgainOrderLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserAgainOrder(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/home-user-auth/internal/logic/useragainorderlogic.go b/server/home-user-auth/internal/logic/useragainorderlogic.go new file mode 100644 index 00000000..c1edb6c4 --- /dev/null +++ b/server/home-user-auth/internal/logic/useragainorderlogic.go @@ -0,0 +1,144 @@ +package logic + +import ( + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "time" + + "context" + + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" + + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +type UserAgainOrderLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserAgainOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAgainOrderLogic { + return &UserAgainOrderLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserAgainOrderLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserAgainOrderLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *UserAgainOrderLogic) UserAgainOrder(req *types.UserAgainOrderReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + if userinfo == nil || userinfo.UserId == 0 { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found") + } + + // 查询订单数据 + orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn) + orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn) + fsOrderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn) + fsProductDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn) + + rsbOrder := orderModel.RowSelectBuilder(nil) + rsbOrder = rsbOrder.Where("sn =?", req.Sn).Preload("FsOrderDetails") + rsbOrder = rsbOrder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB { + return dbPreload.Table(fsOrderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB { + return dbPreload.Table(orderDetailTemplateModel.TableName()).Preload("FsProductDesignInfo", func(dbPreload *gorm.DB) *gorm.DB { + return dbPreload.Table(fsProductDesignModel.TableName()) + }) + }) + }) + + fsOrderRelInfo, err := orderModel.FindOneByQuery(l.ctx, rsbOrder, nil) + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info") + } + + if len(fsOrderRelInfo.FsOrderDetails) > 0 { + for _, fsOrderDetail := range fsOrderRelInfo.FsOrderDetails { + var isCheck int64 = 1 + productDesignInfo := fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo + if productDesignInfo.Id != 0 { + + // 查找是否有此材质、产品、大小id的阶梯价格 + productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn) + priceStatus := int64(1) + priceReq := gmodel.FindOneProductPriceByParamsReq{ + ProductId: productDesignInfo.ProductId, + MaterialId: productDesignInfo.MaterialId, + SizeId: productDesignInfo.SizeId, + Status: &priceStatus, + } + productPriceInfo, err := productPriceModel.FindOneProductPriceByParams(l.ctx, priceReq) + if err == nil && productPriceInfo.Id != 0 && *productPriceInfo.EachBoxNum > 0 { + + // 买的数量和每箱数量取余为0 且 份数大于等于最小购买数量才算满足条件 + if *fsOrderDetail.BuyNum%*productPriceInfo.EachBoxNum == 0 && int64(float64(*fsOrderDetail.BuyNum)/float64(*productPriceInfo.EachBoxNum)) >= *productPriceInfo.MinBuyNum { + + // 查询购物车 + cartModel := gmodel.NewFsCartModel(l.svcCtx.MysqlConn) + cartStatus := int64(1) + cartReq := gmodel.FindOneCartByParamsReq{ + UserId: &userinfo.UserId, + ProductId: productDesignInfo.ProductId, + TemplateId: productDesignInfo.TemplateId, + PriceId: &productPriceInfo.Id, + DesignId: &productDesignInfo.Id, + MaterialId: productDesignInfo.MaterialId, + Status: &cartStatus, + } + cartInfo, err := cartModel.FindOneCartByParams(l.ctx, cartReq) + if err == nil && (err != nil && errors.Is(err, gorm.ErrRecordNotFound)) { + now := time.Now().Unix() + nowTime := time.Now() + data := gmodel.FsCart{ + UserId: &userinfo.UserId, + ProductId: productPriceInfo.ProductId, + TemplateId: productDesignInfo.TemplateId, + PriceId: &productPriceInfo.Id, + MaterialId: productDesignInfo.MaterialId, + SizeId: productDesignInfo.SizeId, + BuyNum: fsOrderDetail.BuyNum, + Cover: productDesignInfo.Cover, + DesignId: &productDesignInfo.Id, + Ctime: &now, + Status: &cartStatus, + OptionalId: productDesignInfo.OptionalId, + IsCheck: &isCheck, + TsTime: &nowTime, + } + if cartInfo == nil { + err = cartModel.Create(l.ctx, data) + } else { + err = cartModel.Update(l.ctx, cartInfo.Id, data) + } + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to add to cart") + } + } + } + } + } + } + } + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/home-user-auth/internal/logic/userordercancellogic.go b/server/home-user-auth/internal/logic/userordercancellogic.go index ce64a5b2..3fad53bb 100644 --- a/server/home-user-auth/internal/logic/userordercancellogic.go +++ b/server/home-user-auth/internal/logic/userordercancellogic.go @@ -6,13 +6,13 @@ import ( "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "fusenapi/utils/handlers" "time" "context" "fusenapi/server/home-user-auth/internal/svc" "fusenapi/server/home-user-auth/internal/types" - handlerUtils "fusenapi/utils/handler" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" @@ -74,7 +74,7 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us orderInfo.RefundReason = &req.RefundReason var nowTime = time.Now().Unix() - var payList []handlerUtils.PayInfo + var payList []handlers.PayInfo // 事务处理 err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) { // 修改订单信息 @@ -106,7 +106,7 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us if *payInfo.PaymentMethod == int64(constants.PAYMETHOD_STRIPE) { key = l.svcCtx.Config.PayConfig.Stripe.Key } - payList = append(payList, handlerUtils.PayInfo{ + payList = append(payList, handlers.PayInfo{ TradeNo: *payInfo.TradeNo, PaymentMethod: *payInfo.PaymentMethod, Key: key, @@ -115,7 +115,7 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us return nil }) // 退款申请--调取第三方接口发起退款 - handlerUtils.PayRefundHandler(&handlerUtils.PayRefundHandlerReq{ + handlers.PayRefundHandler(&handlers.PayRefundHandlerReq{ PayInfoList: payList, }) if err != nil { diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index 7ab699ff..b183ef86 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,13 @@ import ( "fusenapi/utils/basic" ) +type UserAgainOrderReq struct { + Sn string `form:"sn"` // 订单编号 +} + +type UserAgainOrderRes struct { +} + type UserLogoListReq struct { } diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index c5684eb7..3b686163 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -71,8 +71,19 @@ service home-user-auth { // 用户logo列表 @handler UserLogoListHandler get /api/user/logo-list (UserLogoListReq) returns (response); + + // 再来一单 + @handler UserAgainOrderHandler + get /api/user/one-more-order (UserAgainOrderReq) returns (response); } +type ( + UserAgainOrderReq { + Sn string `form:"sn"` // 订单编号 + } + UserAgainOrderRes struct{} +) + type ( UserLogoListReq { } diff --git a/utils/handler/gormHandler.go b/utils/handlers/gormHandler.go similarity index 99% rename from utils/handler/gormHandler.go rename to utils/handlers/gormHandler.go index d239931e..e9b21633 100644 --- a/utils/handler/gormHandler.go +++ b/utils/handlers/gormHandler.go @@ -1,4 +1,4 @@ -package handler +package handlers import ( "encoding/json" diff --git a/utils/handler/payHandler.go b/utils/handlers/payHandler.go similarity index 99% rename from utils/handler/payHandler.go rename to utils/handlers/payHandler.go index f69412b7..66fd7590 100644 --- a/utils/handler/payHandler.go +++ b/utils/handlers/payHandler.go @@ -1,4 +1,4 @@ -package handler +package handlers import ( "fusenapi/constants"