diff --git a/internal/domain/errors.go b/internal/domain/errors.go index 15bfbf5..050f912 100644 --- a/internal/domain/errors.go +++ b/internal/domain/errors.go @@ -23,6 +23,11 @@ const ( TimeoutOrderErrorCode ) +const ( + _ ErrorCode = 10 + iota + DataNotFoundErrorCode +) + func CommentError(ec ErrorCode, s ...string) *ers.LibError { return ers.NewError(code.CloudEPOrder, code.DBError, ec.ToUint32(), strings.Join(s, " ")) } @@ -34,3 +39,7 @@ func CommentErrorL(ec ErrorCode, return e } + +func NotFoundError(ec ErrorCode, s ...string) *ers.LibError { + return ers.NewError(code.CloudEPOrder, code.ResourceNotFound, ec.ToUint32(), strings.Join(s, " ")) +} diff --git a/internal/logic/orderservice/decimal_tools.go b/internal/logic/orderservice/decimal_tools.go index cdfc726..80bb3a6 100644 --- a/internal/logic/orderservice/decimal_tools.go +++ b/internal/logic/orderservice/decimal_tools.go @@ -47,7 +47,7 @@ func convertDecimalToDecimal128(d decimal.Decimal) primitive.Decimal128 { // 將 primitive.Decimal128 轉換為字符串 func convertDecimal128ToString(d128 *primitive.Decimal128) *string { - if d128 != nil { + if d128 == nil { return nil } result := d128.String() diff --git a/internal/logic/orderservice/get_order_logic.go b/internal/logic/orderservice/get_order_logic.go index c1753fe..1d3030c 100644 --- a/internal/logic/orderservice/get_order_logic.go +++ b/internal/logic/orderservice/get_order_logic.go @@ -2,9 +2,12 @@ package orderservicelogic import ( "app-cloudep-order-server/gen_result/pb/order" + "app-cloudep-order-server/internal/domain" "app-cloudep-order-server/internal/svc" ers "code.30cm.net/digimon/library-go/errs" "context" + "errors" + "github.com/zeromicro/go-zero/core/stores/mon" "github.com/zeromicro/go-zero/core/logx" ) @@ -40,6 +43,9 @@ func (l *GetOrderLogic) GetOrder(in *order.GetOrderReq) (*order.GetOrderResp, er o, err := l.svcCtx.OrderModel.FindOneBusinessID(l.ctx, in.GetBusinessId()) if err != nil { + if errors.As(err, &mon.ErrNotFound) { + return nil, domain.NotFoundError(domain.DataNotFoundErrorCode, "failed to get this order id:", in.GetBusinessId()) + } return nil, err } diff --git a/internal/logic/orderservice/list_order_logic.go b/internal/logic/orderservice/list_order_logic.go index 2f589f6..c020dc7 100644 --- a/internal/logic/orderservice/list_order_logic.go +++ b/internal/logic/orderservice/list_order_logic.go @@ -52,7 +52,7 @@ type GetOrderListReq struct { // ListOrder 取得訂單列表 func (l *ListOrderLogic) ListOrder(in *order.ListOrderReq) (*order.ListOrderResp, error) { - // 驗證資料,目前只有 Page 必帶,其他要驗證在驗證 + // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&GetOrderListReq{ PageIndex: in.GetPageIndex(), PageSize: in.GetPageSize(), @@ -61,14 +61,39 @@ func (l *ListOrderLogic) ListOrder(in *order.ListOrderReq) (*order.ListOrderResp return nil, ers.InvalidFormat(err.Error()) } - orders, total, err := l.svcCtx.OrderModel.ListOrder(l.ctx, model.GetOrderListReq{}) + // 構建查詢條件 + req := model.GetOrderListReq{ + PageIndex: in.GetPageIndex(), + PageSize: in.GetPageSize(), + ReferenceID: in.ReferenceId, + ReferenceUID: in.ReferenceUid, + BusinessID: in.BusinessId, + UID: in.Uid, + OrderType: int(in.OrderType), + DirectionType: in.DirectionType, + OrderStatus: in.OrderStatus, + StartCreateTime: in.StartCreateTime, + EndCreateTime: in.EndCreateTime, + StartUpdateTime: in.StartUpdateTime, + EndUpdateTime: in.EndUpdateTime, + StartOrderArrivalTime: in.StartOrderArrivalTime, + EndOrderArrivalTime: in.EndOrderArrivalTime, + StartOrderPaymentTime: in.StartOrderPaymentTime, + EndOrderPaymentTime: in.EndOrderPaymentTime, + CryptoType: in.CryptoType, + TxHash: in.TxHash, + } + + // 查詢訂單 + orders, total, err := l.svcCtx.OrderModel.ListOrder(l.ctx, req) if err != nil { return nil, err } + // 構建返回結果 res := make([]*order.GetOrderResp, 0, len(orders)) for _, item := range orders { - res = append(res, ConvertOrderToGetOrderResp(item)) + res = append(res, convertOrderToGetOrderResp(item)) } return &order.ListOrderResp{ @@ -82,7 +107,7 @@ func (l *ListOrderLogic) ListOrder(in *order.ListOrderReq) (*order.ListOrderResp } // ConvertOrderToGetOrderResp 將 Order 結構轉換為 GetOrderResp -func ConvertOrderToGetOrderResp(o model.Order) *order.GetOrderResp { +func convertOrderToGetOrderResp(o model.Order) *order.GetOrderResp { return &order.GetOrderResp{ BusinessId: o.BusinessID, OrderType: int32(o.OrderType), diff --git a/internal/logic/orderservice/modify_order_status_logic.go b/internal/logic/orderservice/modify_order_status_logic.go index 9167370..5254a21 100644 --- a/internal/logic/orderservice/modify_order_status_logic.go +++ b/internal/logic/orderservice/modify_order_status_logic.go @@ -4,9 +4,8 @@ import ( "app-cloudep-order-server/gen_result/pb/order" "app-cloudep-order-server/internal/domain" model "app-cloudep-order-server/internal/model/mongo" - "context" - ers "code.30cm.net/digimon/library-go/errs" + "context" "app-cloudep-order-server/internal/svc" diff --git a/internal/model/mongo/order_model.go b/internal/model/mongo/order_model.go index f60bfd5..9d760ac 100644 --- a/internal/model/mongo/order_model.go +++ b/internal/model/mongo/order_model.go @@ -49,8 +49,8 @@ type ( BusinessID string `json:"business_id"` UID string `json:"uid"` OrderType int `json:"order_type"` - DirectionType []int64 `json:"direction_type"` - OrderStatus []int64 `json:"order_status"` + DirectionType []int32 `json:"direction_type"` + OrderStatus []int32 `json:"order_status"` StartCreateTime int64 `json:"start_create_time"` EndCreateTime int64 `json:"end_create_time"` @@ -80,7 +80,7 @@ func (m *customOrderModel) UpdateStatus(ctx context.Context, data UpdateStatusRe // 更新 update_time 和 status updates["update_time"] = time.Now().UTC().UnixNano() - updates["status"] = data.Status + updates["order_status"] = data.Status // 使用 updates map 作為 $set 的內容 res, err := m.conn.UpdateOne(ctx, @@ -93,6 +93,7 @@ func (m *customOrderModel) UpdateStatus(ctx context.Context, data UpdateStatusRe }, bson.M{"$set": updates}, // 使用 updates 而不是整個 data ) + return res, err }