From 2d3c394c73bd98bf2cd95e537d1f49a46d453494 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 20 Jun 2023 16:46:56 +0800 Subject: [PATCH] =?UTF-8?q?Order=20=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constants/delivery.go | 9 ++ constants/order.go | 10 +- model/gmodel/fs_order_logic.go | 8 +- .../internal/logic/useroderdeletelogic.go | 101 ++++++++++++------ .../test/useroderdeletelogic_test.go | 66 ++++++++++++ utils/basic/basic.go | 36 ++++--- utils/order/order_status.go | 2 +- 7 files changed, 172 insertions(+), 60 deletions(-) create mode 100644 constants/delivery.go create mode 100644 server/home-user-auth/test/useroderdeletelogic_test.go diff --git a/constants/delivery.go b/constants/delivery.go new file mode 100644 index 00000000..fc033565 --- /dev/null +++ b/constants/delivery.go @@ -0,0 +1,9 @@ +package constants + +type DeliveryMethod int + +const ( + // 配送方式 + DELIVERY_METHOD_ADDRESS DeliveryMethod = 1 // 直接发货到收获地址 + DELIVERY_METHOD_CLOUD DeliveryMethod = 2 // 云仓 +) diff --git a/constants/order.go b/constants/order.go index eeabb9f6..95533431 100644 --- a/constants/order.go +++ b/constants/order.go @@ -68,13 +68,9 @@ package constants // // 云仓完成 // const STATUS_FONT_COMPLETED_CLOUD = 8 -type Order int +type DELIVERY_METHOD int -const ( - // 配送方式 - DELIVERY_METHOD_ADDRESS Order = 1 // 直接发货到收获地址 - DELIVERY_METHOD_CLOUD Order = 2 // 云仓 -) +type Order int // 订单状态 const ( @@ -87,7 +83,7 @@ const ( // 订单已确认 STATUS_NEW_SURE Order = 3 // 订单生产中 - STATUS_NEW_PRODUCING Order = 4 + STATUS_NEW_PRODUTING Order = 4 // 订单生产完成 STATUS_NEW_PRODUT_COMPLETED Order = 5 // 订单已到库 diff --git a/model/gmodel/fs_order_logic.go b/model/gmodel/fs_order_logic.go index ed30cc0c..6301bac4 100755 --- a/model/gmodel/fs_order_logic.go +++ b/model/gmodel/fs_order_logic.go @@ -8,12 +8,18 @@ import ( ) func (o *FsOrderModel) FindOneBySn(ctx context.Context, userId int64, sn string) (resp FsOrder, err error) { - err = o.db.WithContext(ctx).Model(&FsOrder{}).Where(" `user_id` = ? and `sn` = ? ", userId, sn).First(&resp).Error + err = o.db.WithContext(ctx).Model(&FsOrder{}).Where(" `user_id` = ? and `sn` = ? ", userId, sn).Take(&resp).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return FsOrder{}, err } return resp, nil } + +func (o *FsOrderModel) FindOne(ctx context.Context, userId int64, OrderId int64) (order FsOrder, err error) { + err = o.db.WithContext(ctx).Model(&order).Where("`user_id` = ? and `id` = ?", userId, OrderId).Take(&order).Error + return order, err +} + func (o *FsOrderModel) Update(ctx context.Context, id int64, data FsOrder) error { return o.db.WithContext(ctx).Model(&FsOrder{}).Where("`id` = ?", id).Updates(data).Error } diff --git a/server/home-user-auth/internal/logic/useroderdeletelogic.go b/server/home-user-auth/internal/logic/useroderdeletelogic.go index 36f7abff..0afe4b94 100644 --- a/server/home-user-auth/internal/logic/useroderdeletelogic.go +++ b/server/home-user-auth/internal/logic/useroderdeletelogic.go @@ -1,9 +1,12 @@ package logic import ( + "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "log" + "time" "context" @@ -11,6 +14,7 @@ import ( "fusenapi/server/home-user-auth/internal/types" "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" ) type UserOderDeleteLogic struct { @@ -30,50 +34,77 @@ func NewUserOderDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Us func (l *UserOderDeleteLogic) UserOderDelete(req *types.RequestOrderId, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null + + log.Println(req) + if !userinfo.IsUser() { return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误 } //订单id orderId := req.OrderId - if orderId < 0 { + if orderId < 1 { return resp.SetStatus(basic.CodeRequestParamsErr) } + m := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn) + order, err := m.FindOne(l.ctx, userinfo.UserId, orderId) + if err != nil { + if err == gorm.ErrRecordNotFound { + return resp.SetStatus(basic.CodeOrderNotFoundErr) + } + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr) + } + + if auth.CheckValueRange[constants.Order]( + constants.Order(*order.Status), + constants.STATUS_NEW_NOT_PAY, + constants.STATUS_NEW_PAY_COMPLETED, + constants.STATUS_NEW_PART_PAY, + ) { + return resp.SetStatus(basic.CodeOrderNotCancelledErr) + } + + if *order.IsPayCompleted == 1 && + time.Now().After(time.Unix(*order.Ctime, 0).Add(48*time.Hour)) { + return resp.SetStatus(basic.CodeOrderNotCancelledErr) + } + return resp.SetStatus(basic.CodeOK) } -func (l *OrderLogic) CancelOrder(req *types.RequestCancelOrder, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null - if !userinfo.IsUser() { - return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误 - } - //订单id - id := req.ReqParam.Get("id") - if id == "" { - return resp.SetStatus(basic.CodeParamErr) - } - //验证当前订单是否可以取消 - order, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).GetOne(l.ctx, map[string]interface{}{"user_id": userinfo.UserId, "id": id}) - if err != nil { - return resp.SetStatus(basic.CodeDbQueryErr) - } - if order == nil { - return resp.SetStatus(basic.CodeNotFound) - } - //那些状态下是不能关闭的? (订单完成,退款完成,关闭订单) - if order.Status == gconst.OrderStatusComplete || - order.Status == gconst.OrderStatusRefund || - order.Status == gconst.OrderStatusClose { - return resp.SetStatus(basic.CodeCannotOperate) - } - //订单状态修改 - order.Status = gconst.OrderStatusDelete - order.IsDeleted = 1 - result, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).UpdateOne(l.ctx, order) - if err != nil { - return resp.SetStatus(basic.CodeDbUpdateErr) - } - return resp.SetStatus(basic.CodeOK, result) -} +// func (l *OrderLogic) CancelOrder(req *types.RequestCancelOrder, userinfo *auth.UserInfo) (resp *basic.Response) { +// // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) +// // userinfo 传入值时, 一定不为null +// if !userinfo.IsUser() { +// return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误 +// } +// //订单id +// id := req.ReqParam.Get("id") +// if id == "" { +// return resp.SetStatus(basic.CodeParamErr) +// } +// //验证当前订单是否可以取消 +// order, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).GetOne(l.ctx, map[string]interface{}{"user_id": userinfo.UserId, "id": id}) +// if err != nil { +// return resp.SetStatus(basic.CodeDbQueryErr) +// } +// if order == nil { +// return resp.SetStatus(basic.CodeNotFound) +// } +// //那些状态下是不能关闭的? (订单完成,退款完成,关闭订单) +// if order.Status == gconst.OrderStatusComplete || +// order.Status == gconst.OrderStatusRefund || +// order.Status == gconst.OrderStatusClose { +// return resp.SetStatus(basic.CodeCannotOperate) +// } +// //订单状态修改 +// order.Status = gconst.OrderStatusDelete +// order.IsDeleted = 1 +// result, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).UpdateOne(l.ctx, order) +// if err != nil { +// return resp.SetStatus(basic.CodeDbUpdateErr) +// } +// return resp.SetStatus(basic.CodeOK, result) +// } diff --git a/server/home-user-auth/test/useroderdeletelogic_test.go b/server/home-user-auth/test/useroderdeletelogic_test.go new file mode 100644 index 00000000..fb46bfed --- /dev/null +++ b/server/home-user-auth/test/useroderdeletelogic_test.go @@ -0,0 +1,66 @@ +package logic + +import ( + "fmt" + "fusenapi/server/home-user-auth/internal/types" + "testing" + + "github.com/474420502/requests" + "github.com/tidwall/gjson" +) + +func TestCaseLogic(t *testing.T) { + var err error + var resp *requests.Response + var result gjson.Result + + // 获取 session,并携带 JWT token + ses := GetSesssionWithUserToken(t, gserver) + + // 向服务器发送 GET 请求,获取用户类型信息 + tp := ses.Post(fmt.Sprintf("http://%s:%d/user/order-delete", cnf.Host, cnf.Port)) + reqO := types.RequestOrderId{ + OrderId: 12, + } + tp.SetBodyJson(reqO) + resp, err = tp.TestExecute(gserver) + + if err != nil { + t.Error(err) + } + + // 使用 gjson 解析返回的 json 数据 + result = gjson.Parse(resp.ContentString()) + + // 检查返回值中的 code 字段是否存在,并且值是否为 200 + code := result.Get("code").Int() + if code != 200 { + t.Errorf("Invalid code value: %d", code) + } + + // 检查返回值中的 msg 字段是否存在,并且值是否为 "success" + msg := result.Get("msg").String() + if msg != "success" { + t.Errorf(`Invalid msg value: "%s"`, msg) + } + + // 检查返回值中的 data 字段是否存在,并且值是否为数组类型 + dataArray := result.Get("data").Array() + if len(dataArray) == 0 { + t.Error("Empty data field") + } + + // 遍历每个元素,检查其 key 和 name 字段是否存在,并且值是否符合预期 + for i, item := range dataArray { + key := item.Get("key").Int() + name := item.Get("name").String() + + if key != int64(i+1) { + t.Errorf("Unexpected key value at index %d: %d", i, key) + } + + if len(name) == 0 { + t.Errorf("Missing name value at index %d", i) + } + } +} diff --git a/utils/basic/basic.go b/utils/basic/basic.go index cf201884..5f338c0a 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -2,31 +2,35 @@ package basic // StatusResponse 公司自定义状态码 type StatusResponse struct { - Code int - Message string + Code int // 状态码 + Message string // 状态码相关解析 } var ( CodeOK = &StatusResponse{200, "success"} // 成功 - CodeApiErr = &StatusResponse{500, "api error"} // api 错误 - CodeSaveErr = &StatusResponse{501, "fail to save"} // 储存错误 - CodeServiceErr = &StatusResponse{510, "server logic error"} // server logic 错误 + CodeApiErr = &StatusResponse{500, "api error"} // API错误 + CodeSaveErr = &StatusResponse{501, "fail to save"} // 保存失败 + CodeServiceErr = &StatusResponse{510, "server logic error"} // 服务逻辑错误 CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权 - CodeEmailNotFoundErr = &StatusResponse{5050, "the email was not found"} // email 不存在 - CodeUserIdNotFoundErr = &StatusResponse{5051, "the user was not found"} // userid 不存在 - CodePasswordErr = &StatusResponse{5052, "invalid password"} // 密码错误 + CodeEmailNotFoundErr = &StatusResponse{5050, "the email was not found"} // 未找到email + CodeUserIdNotFoundErr = &StatusResponse{5051, "the user was not found"} // 未找到用户 + CodePasswordErr = &StatusResponse{5052, "invalid password"} // 无效密码 - CodeSafeValueRangeErr = &StatusResponse{5040, "the value was not in values"} // 值不存在范围内 + CodeSafeValueRangeErr = &StatusResponse{5040, "the value was not in values"} // 值不在范围内 - CodeGuestDupErr = &StatusResponse{5010, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误 - CodeGuestGenErr = &StatusResponse{5011, "serialization failed for guest_id of the visitor"} // 游客guest_id序列化失败 + CodeOrderNotFoundErr = &StatusResponse{5030, "the order not found"} //未找到订单 + CodeOrderNotCancelledErr = &StatusResponse{5031, "The current order cannot be cancelled"} // 当前订单无法取消 - CodeDbUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误 - CodeDupGuestErr = &StatusResponse{5001, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误 - CodeRequestParamsErr = &StatusResponse{5002, "invalid request param"} // api参数校验 错误 - CodeDbRecordNotFoundErr = &StatusResponse{5003, "db record not found"} - CodeDbCreateErr = &StatusResponse{5004, "create one in database error"} + CodeGuestDupErr = &StatusResponse{5010, "the user is already a guest user and does not need to apply again"} // 用户已经是访客用户,不需要重复申请 + CodeGuestGenErr = &StatusResponse{5011, "serialization failed for guest_id of the visitor"} // 访客ID序列化失败 + + CodeDbUpdateErr = &StatusResponse{5000, "update database error"} // 数据库更新错误 + CodeDupGuestErr = &StatusResponse{5001, "the user is already a guest user and does not need to apply again"} // 用户已经是访客用户,不需要重复申请 + CodeRequestParamsErr = &StatusResponse{5002, "invalid request param"} // 请求参数无效 + CodeDbRecordNotFoundErr = &StatusResponse{5003, "db record not found"} //未在数据库中找到记录 + CodeDbCreateErr = &StatusResponse{5004, "create one in database error"} // 在数据库中创建错误 + CodeDbSqlErr = &StatusResponse{5005, "database is error"} // 数据库错误 ) type Response struct { diff --git a/utils/order/order_status.go b/utils/order/order_status.go index f45322e3..c3f7dba8 100644 --- a/utils/order/order_status.go +++ b/utils/order/order_status.go @@ -3,7 +3,7 @@ package order import "fusenapi/constants" // 获取订单生产状态 -func GetOrderStatus(orderStatus int64, deliveryMethod int64) int64 { +func GetOrderStatus(orderStatus constants.Order, deliveryMethod constants.DeliveryMethod) constants.Order { switch orderStatus { //已支付 case constants.STATUS_NEW_PART_PAY, constants.STATUS_NEW_PAY_COMPLETED, constants.STATUS_NEW_SURE: