From ae2c41364ff191bc87579f976f3e8c905daddf48 Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Sat, 7 Oct 2023 18:36:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=88=A0=E9=99=A4=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constants/orders.go | 2 + .../internal/handler/deleteorderhandler.go | 35 +++++ server/order/internal/handler/routes.go | 5 + .../order/internal/logic/deleteorderlogic.go | 55 ++++++++ server/order/internal/types/types.go | 4 + server_api/order.api | 7 + service/repositories/order.go | 120 ++++++++++++++++-- utils/basic/basic.go | 1 + 8 files changed, 215 insertions(+), 14 deletions(-) create mode 100644 server/order/internal/handler/deleteorderhandler.go create mode 100644 server/order/internal/logic/deleteorderlogic.go diff --git a/constants/orders.go b/constants/orders.go index d955c38c..17c83e1a 100644 --- a/constants/orders.go +++ b/constants/orders.go @@ -59,6 +59,7 @@ const ( ORDER_STATUS_UNPAIDDEPOSIT OrderStatusCode = 0 // 0,未支付定金 ORDER_STATUS_CLOSE OrderStatusCode = 10 // 10,订单已关闭 ORDER_STATUS_COMPLETE OrderStatusCode = 20 // 20,订单已完成 + ORDER_STATUS_DELETE OrderStatusCode = 30 // 20,订单已删除 ORDER_STATUS_DIRECTMAIL_ORDERED OrderStatusCode = 10100 // 10100,直邮单--已下单 ORDER_STATUS_DIRECTMAIL_ORDEREDMAINING OrderStatusCode = 10100001 // 10100001,直邮单--已下单--尾款 @@ -111,6 +112,7 @@ func init() { OrderStatusMessage[ORDER_STATUS_UNPAIDDEPOSIT] = "未支付定金" OrderStatusMessage[ORDER_STATUS_CLOSE] = "订单已关闭" OrderStatusMessage[ORDER_STATUS_COMPLETE] = "订单已完成" + OrderStatusMessage[ORDER_STATUS_DELETE] = "订单已删除" OrderStatusMessage[ORDER_STATUS_DIRECTMAIL_ORDERED] = "直邮单--已下单" OrderStatusMessage[ORDER_STATUS_DIRECTMAIL_ORDEREDMAINING] = "直邮单--已下单--尾款" diff --git a/server/order/internal/handler/deleteorderhandler.go b/server/order/internal/handler/deleteorderhandler.go new file mode 100644 index 00000000..b967d77c --- /dev/null +++ b/server/order/internal/handler/deleteorderhandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/order/internal/logic" + "fusenapi/server/order/internal/svc" + "fusenapi/server/order/internal/types" +) + +func DeleteOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.DeleteOrderReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewDeleteOrderLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.DeleteOrder(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/order/internal/handler/routes.go b/server/order/internal/handler/routes.go index 8e1d47a9..c03bd1db 100644 --- a/server/order/internal/handler/routes.go +++ b/server/order/internal/handler/routes.go @@ -27,6 +27,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/order/create-prepayment-balance", Handler: CreatePrePaymentByBalanceHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/order/delete", + Handler: DeleteOrderHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/api/order/list", diff --git a/server/order/internal/logic/deleteorderlogic.go b/server/order/internal/logic/deleteorderlogic.go new file mode 100644 index 00000000..c2d63eca --- /dev/null +++ b/server/order/internal/logic/deleteorderlogic.go @@ -0,0 +1,55 @@ +package logic + +import ( + "fusenapi/service/repositories" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/order/internal/svc" + "fusenapi/server/order/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type DeleteOrderLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewDeleteOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteOrderLogic { + return &DeleteOrderLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *DeleteOrderLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *DeleteOrderLogic) DeleteOrder(req *types.DeleteOrderReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + if !userinfo.IsUser() { + // 如果是,返回未授权的错误码 + return resp.SetStatus(basic.CodeUnAuth) + } + res, err := l.svcCtx.Repositories.NewOrder.Delete(l.ctx, &repositories.DeleteReq{ + UserId: userinfo.UserId, + OrderSn: req.OrderSn, + }) + + if err != nil { + return resp.SetStatus(&res.ErrorCode) + } + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *DeleteOrderLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/order/internal/types/types.go b/server/order/internal/types/types.go index 6cc21a64..f868a96d 100644 --- a/server/order/internal/types/types.go +++ b/server/order/internal/types/types.go @@ -5,6 +5,10 @@ import ( "fusenapi/utils/basic" ) +type DeleteOrderReq struct { + OrderSn string `json:"order_sn"` +} + type OrderDetailReq struct { OrderSn string `form:"order_sn"` } diff --git a/server_api/order.api b/server_api/order.api index 5c53de53..48bb9a61 100644 --- a/server_api/order.api +++ b/server_api/order.api @@ -20,6 +20,9 @@ service order { @handler CreatePrePaymentByBalanceHandler post /api/order/create-prepayment-balance(CreatePrePaymentByBalanceReq) returns (response); + @handler DeleteOrderHandler + post /api/order/delete(DeleteOrderReq) returns (response); + @handler OrderListHandler get /api/order/list(OrderListReq) returns (response); @@ -28,6 +31,10 @@ service order { } +type DeleteOrderReq { + OrderSn string `json:"order_sn"` +} + type OrderDetailReq { OrderSn string `form:"order_sn"` } diff --git a/service/repositories/order.go b/service/repositories/order.go index 1192dc2d..bf56a1b0 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -48,6 +48,8 @@ type ( PaymentSuccessful(ctx context.Context, in *PaymentSuccessfulReq) (res *PaymentSuccessfulRes, err error) // 关闭 Close(ctx context.Context, in *CloseReq) (res *CloseRes, err error) + // 删除 + Delete(ctx context.Context, in *DeleteReq) (res *DeleteRes, err error) // 支付超时订单自动关闭 CloseList(ctx context.Context, in *CloseListReq) (res *CloseListRes, err error) @@ -83,6 +85,18 @@ type ( Amount int64 `json:"amount"` // 金额 Label string `json:"label"` // 标签 } + + /* 删除订单 */ + DeleteReq struct { + UserId int64 `json:"user_id"` + OrderSn string `json:"order_sn"` + } + DeleteRes struct { + ErrorCode basic.StatusResponse + OrderSn string + } + /* 删除订单 */ + /* 支付超时订单自动关闭 */ CloseListReq struct { Type int64 // type:1=关闭 @@ -195,6 +209,85 @@ type ( /* 列表 */ ) +// 订单删除 +func (d *defaultOrder) Delete(ctx context.Context, in *DeleteReq) (res *DeleteRes, err error) { + var errorCode basic.StatusResponse + err = d.MysqlConn.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + var orderInfo gmodel.FsOrder + model := tx + if in.UserId != 0 { + model = model.Where("user_id = ?", in.UserId) + } + if in.OrderSn != "" { + model = model.Where("order_sn = ?", in.OrderSn) + } + result := model.Take(&orderInfo) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + errorCode = *basic.CodeErrOrderCreatePrePaymentInfoNoFound + } else { + errorCode = *basic.CodeServiceErr + } + logc.Errorf(ctx, "order delete failed, err: %v", err) + return result.Error + } + ress, err := d.OrderDetailHandler(ctx, &orderInfo, 0) + if err != nil { + logc.Errorf(ctx, "order delete failed DetailOrderDetailHandler,OrderSn:%s, err: %v", in.OrderSn, err) + return err + } + + if !(*orderInfo.Status == int64(constants.ORDER_STATUS_CLOSE) || (*orderInfo.DeliveryMethod == int64(constants.DELIVERYMETHODDIRECTMAIL) && *orderInfo.Status == int64(constants.ORDER_STATUS_DIRECTMAIL_ARRIVED))) { + errorCode = *basic.CodeErrOrderDeleteStatusIllegality + err = errors.New("order delete failed, status is illegality") + logc.Errorf(ctx, "order delete failed, err: %v", err) + return err + } + + // 状态链路 + var ntime = time.Now().UTC() + var statusCode = constants.ORDER_STATUS_DELETE + var statusLink = append(ress.OrderDetailOriginal.StatusLink, gmodel.OrderStatus{ + Ctime: &ntime, + Utime: &ntime, + StatusCode: statusCode, + StatusTitle: constants.OrderStatusMessage[statusCode], + }) + statusLinkByte, err := json.Marshal(statusLink) + if err != nil { + logc.Errorf(ctx, "order delete failed Marshal statusLinkByte,OrderSn:%s, err: %v", in.OrderSn, err) + return err + } + + var table = gmodel.NewAllModels(tx).FsOrder.TableName() + var resUpdate *gorm.DB + var resUpdateSql string = fmt.Sprintf("UPDATE %s SET `utime` = '%s',`is_del` = 1", table, ntime) + resUpdate = tx.Exec(fmt.Sprintf("%s ,`status_link`= JSON_MERGE_PATCH(`status_link`,?) WHERE `id` = %d", resUpdateSql, orderInfo.Id), statusLinkByte) + + err = resUpdate.Error + if err != nil { + logc.Errorf(ctx, "order delete failed Update FsOrder,OrderSn:%s, err: %v", in.OrderSn, err) + return err + } + return nil + }) + if err != nil { + logc.Errorf(ctx, "order delete failed, err: %v", err) + if errorCode.Code == 0 { + errorCode.Code = basic.CodeApiErr.Code + errorCode.Message = basic.CodeApiErr.Message + } + return &DeleteRes{ + ErrorCode: errorCode, + }, err + } + + return &DeleteRes{ + ErrorCode: errorCode, + OrderSn: in.OrderSn, + }, err +} + // 订单关闭-支付超时 func (d *defaultOrder) CloseList(ctx context.Context, in *CloseListReq) (res *CloseListRes, err error) { var orderList []gmodel.FsOrder @@ -245,13 +338,12 @@ func (d *defaultOrder) CloseList(ctx context.Context, in *CloseListReq) (res *Cl // 关闭 func (d *defaultOrder) Close(ctx context.Context, in *CloseReq) (res *CloseRes, err error) { - fmt.Println(in) var errorCode basic.StatusResponse err = d.MysqlConn.WithContext(ctx).Transaction(func(tx *gorm.DB) error { var orderInfo gmodel.FsOrder model := tx.Where("status = ?", int64(constants.ORDER_STATUS_UNPAIDDEPOSIT)).Where("pay_status = ?", int(constants.ORDER_PAY_STATUS_UNPAIDDEPOSIT)) if in.OrderSn != "" { - model.Where("order_sn = ?", in.OrderSn) + model = model.Where("order_sn = ?", in.OrderSn) } result := model.Take(&orderInfo) if result.Error != nil { @@ -576,10 +668,10 @@ func (d *defaultOrder) CreatePrePaymentByBalance(ctx context.Context, in *Create var orderInfo gmodel.FsOrder model := d.MysqlConn.Where("is_del = ?", 0) if in.UserId != 0 { - model.Where("user_id = ?", in.UserId) + model = model.Where("user_id = ?", in.UserId) } if in.OrderSn != "" { - model.Where("order_sn = ?", in.OrderSn) + model = model.Where("order_sn = ?", in.OrderSn) } result := model.Take(&orderInfo) if result.Error != nil { @@ -662,10 +754,10 @@ func (d *defaultOrder) CreatePrePaymentByDeposit(ctx context.Context, in *Create var orderInfo gmodel.FsOrder model := d.MysqlConn.Where("is_del = ?", 0) if in.UserId != 0 { - model.Where("user_id = ?", in.UserId) + model = model.Where("user_id = ?", in.UserId) } if in.OrderSn != "" { - model.Where("order_sn = ?", in.OrderSn) + model = model.Where("order_sn = ?", in.OrderSn) } result := model.Take(&orderInfo) if result.Error != nil { @@ -820,23 +912,23 @@ func (d *defaultOrder) List(ctx context.Context, in *ListReq) (res *ListRes, err model := d.MysqlConn.Model(&gmodel.FsOrder{}).Where("is_del = ?", 0) // model.Where("pay_status > ?", 0) if in.UserId != 0 { - model.Where("user_id = ?", in.UserId) + model = model.Where("user_id = ?", in.UserId) } if in.DeliveryMethod != 0 { - model.Where("delivery_method = ?", in.DeliveryMethod) + model = model.Where("delivery_method = ?", in.DeliveryMethod) } if in.OrderCycle != "" { // 下单时间 switch in.OrderCycle { case "within_one_month": - model.Where("ctime >?", time.Now().UTC().AddDate(0, -1, 0).Unix()) + model = model.Where("ctime >?", time.Now().UTC().AddDate(0, -1, 0).Unix()) case "within_three_month": - model.Where("ctime >?", time.Now().UTC().AddDate(0, -3, 0).Unix()) + model = model.Where("ctime >?", time.Now().UTC().AddDate(0, -3, 0).Unix()) case "within_six_month": - model.Where("ctime >?", time.Now().UTC().AddDate(0, -6, 0).Unix()) + model = model.Where("ctime >?", time.Now().UTC().AddDate(0, -6, 0).Unix()) case "within_one_year": - model.Where("ctime >?", time.Now().UTC().AddDate(-1, 0, 0).Unix()) + model = model.Where("ctime >?", time.Now().UTC().AddDate(-1, 0, 0).Unix()) } } var count int64 @@ -880,10 +972,10 @@ func (d *defaultOrder) Detail(ctx context.Context, in *DetailReq) (res *DetailRe var orderInfo gmodel.FsOrder model := d.MysqlConn.Where("is_del = ?", 0) if in.UserId != 0 { - model.Where("user_id = ?", in.UserId) + model = model.Where("user_id = ?", in.UserId) } if in.OrderSn != "" { - model.Where("order_sn = ?", in.OrderSn) + model = model.Where("order_sn = ?", in.OrderSn) } result := model.Take(&orderInfo) if result.Error != nil { diff --git a/utils/basic/basic.go b/utils/basic/basic.go index 73222e45..2af8be79 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -111,6 +111,7 @@ var ( CodeErrOrderCreatePrePaymentNoUnPaid = &StatusResponse{5308, "create payment failed, order is not unpaid"} CodeErrOrderCreatePrePaymentPaid = &StatusResponse{5309, "create payment failed, order is paid"} CodeErrOrderCreatePrePaymentTimeout = &StatusResponse{5310, "create payment failed, timeout"} + CodeErrOrderDeleteStatusIllegality = &StatusResponse{5311, "delete order failed, status is illegality"} ) type Response struct {