Order 枚举
This commit is contained in:
@@ -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)
|
||||
// }
|
||||
|
||||
66
server/home-user-auth/test/useroderdeletelogic_test.go
Normal file
66
server/home-user-auth/test/useroderdeletelogic_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user