feat: 后台订单按平台、代理商、在线支付拆分列表与导出
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m34s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m34s
- 新增 GET /orders/platform、/orders/agent、/orders/online,类别由路径固定,参数、响应、权限同原订单列表 - 新增导出场景 order_platform、order_agent、order_online,筛选与表头同 order - 类别按钱从哪来划分:客户端下单为在线支付;后台代理钱包扣款(含平台代扣)为代理商;其余后台订单为平台 - 列表与导出共用 internal/query/ordercategory 的类别条件 - 企业账号访问订单列表、详情、购买预检改为无权限,修复其可看到全部订单的问题 - 原订单列表与 order 导出场景保留不变 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/query/ordercategory"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
@@ -20,19 +21,30 @@ const (
|
||||
orderExportOperatorHeader = "操作人"
|
||||
)
|
||||
|
||||
// OrderDataSource 订单导出数据源。
|
||||
// OrderDataSource 订单导出数据源。category 为空时导出全部订单(场景 order),
|
||||
// 非空时只导出该类别的订单(场景 order_platform / order_agent / order_online),其余逻辑完全相同。
|
||||
type OrderDataSource struct {
|
||||
db *gorm.DB
|
||||
db *gorm.DB
|
||||
category string
|
||||
}
|
||||
|
||||
// NewOrderDataSource 创建订单导出数据源。
|
||||
func NewOrderDataSource(db *gorm.DB) *OrderDataSource {
|
||||
return &OrderDataSource{db: db}
|
||||
// NewOrderDataSource 创建订单导出数据源;category 为空表示不限类别。
|
||||
func NewOrderDataSource(db *gorm.DB, category string) *OrderDataSource {
|
||||
return &OrderDataSource{db: db, category: category}
|
||||
}
|
||||
|
||||
// Scene 返回导出场景编码。
|
||||
func (s *OrderDataSource) Scene() string {
|
||||
return constants.ExportTaskSceneOrder
|
||||
switch s.category {
|
||||
case constants.OrderCategoryPlatform:
|
||||
return constants.ExportTaskSceneOrderPlatform
|
||||
case constants.OrderCategoryAgent:
|
||||
return constants.ExportTaskSceneOrderAgent
|
||||
case constants.OrderCategoryOnline:
|
||||
return constants.ExportTaskSceneOrderOnline
|
||||
default:
|
||||
return constants.ExportTaskSceneOrder
|
||||
}
|
||||
}
|
||||
|
||||
// Count 统计订单导出行数。
|
||||
@@ -129,6 +141,14 @@ func (s *OrderDataSource) baseQuery(ctx context.Context) *gorm.DB {
|
||||
// is_expired 在接口 Service 中未进入 Store 筛选,这里按当前接口行为忽略。
|
||||
func (s *OrderDataSource) applyFilters(ctx context.Context, query *gorm.DB, params ExportParams) (*gorm.DB, error) {
|
||||
query = s.applyOrderScope(query, params)
|
||||
// 订单类别与列表共用同一份条件,导出条数与对应列表一致。
|
||||
if s.category != "" {
|
||||
condition, args, ok := ordercategory.Condition("o", s.category)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("不支持的订单类别: %s", s.category)
|
||||
}
|
||||
query = query.Where(condition, args...)
|
||||
}
|
||||
|
||||
if params.UserType == constants.UserTypeAgent {
|
||||
if params.CreatorShopID != nil {
|
||||
|
||||
@@ -33,7 +33,10 @@ func NewDefaultRegistry(db *gorm.DB) *Registry {
|
||||
return NewRegistry(
|
||||
NewDeviceDataSource(db),
|
||||
NewIotCardDataSource(db),
|
||||
NewOrderDataSource(db),
|
||||
NewOrderDataSource(db, ""),
|
||||
NewOrderDataSource(db, constants.OrderCategoryPlatform),
|
||||
NewOrderDataSource(db, constants.OrderCategoryAgent),
|
||||
NewOrderDataSource(db, constants.OrderCategoryOnline),
|
||||
NewPackageDataSource(db),
|
||||
NewAgentWalletTransactionDataSource(db),
|
||||
NewAgentRechargeDataSource(db),
|
||||
@@ -78,6 +81,9 @@ func IsSupportedScene(scene string) bool {
|
||||
case constants.ExportTaskSceneDevice,
|
||||
constants.ExportTaskSceneIotCard,
|
||||
constants.ExportTaskSceneOrder,
|
||||
constants.ExportTaskSceneOrderPlatform,
|
||||
constants.ExportTaskSceneOrderAgent,
|
||||
constants.ExportTaskSceneOrderOnline,
|
||||
constants.ExportTaskScenePackage,
|
||||
constants.ExportTaskSceneAgentWalletTransaction,
|
||||
constants.ExportTaskSceneAgentRecharge,
|
||||
|
||||
@@ -22,6 +22,9 @@ var timeFilterScenes = map[string]struct{}{
|
||||
constants.ExportTaskSceneExchange: {},
|
||||
constants.ExportTaskSceneAgentRecharge: {},
|
||||
constants.ExportTaskSceneOrder: {},
|
||||
constants.ExportTaskSceneOrderPlatform: {},
|
||||
constants.ExportTaskSceneOrderAgent: {},
|
||||
constants.ExportTaskSceneOrderOnline: {},
|
||||
constants.ExportTaskSceneCommissionRecord: {},
|
||||
constants.ExportTaskSceneExpiringAsset: {},
|
||||
constants.ExportTaskScenePackageTrafficAlert: {},
|
||||
|
||||
@@ -74,6 +74,9 @@ func (h *OrderHandler) Create(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func (h *OrderHandler) Get(c *fiber.Ctx) error {
|
||||
if err := rejectEnterpriseOrderAccess(c); err != nil {
|
||||
return err
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "无效的订单ID")
|
||||
@@ -87,7 +90,32 @@ func (h *OrderHandler) Get(c *fiber.Ctx) error {
|
||||
return response.Success(c, order)
|
||||
}
|
||||
|
||||
// List 获取订单列表(不限类别)。
|
||||
func (h *OrderHandler) List(c *fiber.Ctx) error {
|
||||
return h.list(c, "")
|
||||
}
|
||||
|
||||
// ListPlatform 获取平台订单列表:后台下单且不走代理钱包。
|
||||
func (h *OrderHandler) ListPlatform(c *fiber.Ctx) error {
|
||||
return h.list(c, constants.OrderCategoryPlatform)
|
||||
}
|
||||
|
||||
// ListAgent 获取代理商订单列表:后台下单且从代理钱包扣款。
|
||||
func (h *OrderHandler) ListAgent(c *fiber.Ctx) error {
|
||||
return h.list(c, constants.OrderCategoryAgent)
|
||||
}
|
||||
|
||||
// ListOnline 获取在线支付订单列表:客户端下单。
|
||||
func (h *OrderHandler) ListOnline(c *fiber.Ctx) error {
|
||||
return h.list(c, constants.OrderCategoryOnline)
|
||||
}
|
||||
|
||||
// list 是订单列表的唯一实现:参数解析、时间范围与代理买家条件只有一份,
|
||||
// 按类别拆分的接口只额外固定订单类别,权限与数据范围与原列表一致。
|
||||
func (h *OrderHandler) list(c *fiber.Ctx, category string) error {
|
||||
if err := rejectEnterpriseOrderAccess(c); err != nil {
|
||||
return err
|
||||
}
|
||||
var req dto.OrderListRequest
|
||||
if err := c.QueryParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
@@ -112,7 +140,7 @@ func (h *OrderHandler) List(c *fiber.Ctx) error {
|
||||
buyerID = 0
|
||||
}
|
||||
|
||||
orders, err := h.service.List(ctx, &req, startTime, endTime, buyerType, buyerID)
|
||||
orders, err := h.service.List(ctx, &req, startTime, endTime, buyerType, buyerID, category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -146,6 +174,9 @@ func (h *OrderHandler) Cancel(c *fiber.Ctx) error {
|
||||
// 参数: order_type(订单类型), resource_id(资源ID), package_ids(套餐ID列表)
|
||||
// 响应: 套餐总价、强充要求、实际支付金额等预检结果
|
||||
func (h *OrderHandler) PurchaseCheck(c *fiber.Ctx) error {
|
||||
if err := rejectEnterpriseOrderAccess(c); err != nil {
|
||||
return err
|
||||
}
|
||||
var req dto.PurchaseCheckRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
||||
@@ -158,3 +189,12 @@ func (h *OrderHandler) PurchaseCheck(c *fiber.Ctx) error {
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
// rejectEnterpriseOrderAccess 拒绝企业账号访问后台订单接口:后台对企业账号不加订单数据范围,
|
||||
// 放行会看到全部订单。企业账号的下单与取消已由各自接口拒绝。
|
||||
func rejectEnterpriseOrderAccess(c *fiber.Ctx) error {
|
||||
if middleware.GetUserTypeFromContext(c.UserContext()) == constants.UserTypeEnterprise {
|
||||
return errors.New(errors.CodeForbidden, "企业账号无权访问订单")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
35
internal/query/ordercategory/condition.go
Normal file
35
internal/query/ordercategory/condition.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Package ordercategory 定义后台订单类别的查询条件,供订单列表与订单导出共用,保证两边口径一致
|
||||
// (split-admin-order-list-by-category)。
|
||||
package ordercategory
|
||||
|
||||
import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// Condition 按表别名生成订单类别条件;alias 为空时不加前缀。类别不受支持时 ok 为 false。
|
||||
//
|
||||
// 按钱从哪来划分,不按操作人:
|
||||
// - 在线支付:客户端下单;
|
||||
// - 代理商:后台下单且以代理钱包支付(含平台代扣代理钱包);
|
||||
// - 平台:后台下单且不走代理钱包(线下支付,支付方式为空也归此类,含平台代购)。
|
||||
func Condition(alias, category string) (string, []any, bool) {
|
||||
prefix := ""
|
||||
if alias != "" {
|
||||
prefix = alias + "."
|
||||
}
|
||||
source := prefix + "source"
|
||||
paymentMethod := prefix + "payment_method"
|
||||
switch category {
|
||||
case constants.OrderCategoryOnline:
|
||||
return source + " = ?", []any{constants.OrderSourceClient}, true
|
||||
case constants.OrderCategoryAgent:
|
||||
return source + " = ? AND " + paymentMethod + " = ?",
|
||||
[]any{constants.OrderSourceAdmin, model.PaymentMethodWallet}, true
|
||||
case constants.OrderCategoryPlatform:
|
||||
return source + " = ? AND (" + paymentMethod + " IS NULL OR " + paymentMethod + " <> ?)",
|
||||
[]any{constants.OrderSourceAdmin, model.PaymentMethodWallet}, true
|
||||
default:
|
||||
return "", nil, false
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,9 @@ func registerExportTaskRoutes(router fiber.Router, handler *admin.ExportTaskHand
|
||||
|
||||
Register(exportTasks, doc, groupPath, "POST", "", handler.Create, RouteSpec{
|
||||
Summary: "创建导出任务",
|
||||
Description: "创建统一导出任务,支持场景 scene=device/iot_card/order/package/agent_wallet_transaction/agent_recharge/refund/exchange/commission_record/package_traffic_alert/expiring_asset/operations_activation/operations_renewal/stop_resume_rule_dry_run/polling_pause/polling_stop_refused_alert 和格式 format=xlsx/csv。" +
|
||||
Description: "创建统一导出任务,支持场景 scene=device/iot_card/order/order_platform/order_agent/order_online/package/agent_wallet_transaction/agent_recharge/refund/exchange/commission_record/package_traffic_alert/expiring_asset/operations_activation/operations_renewal/stop_resume_rule_dry_run/polling_pause/polling_stop_refused_alert 和格式 format=xlsx/csv。" +
|
||||
"polling_pause 的 query.filters 同 GET /card-polling-pauses(asset_type、carrier_id、source、status、identifier、operator_id、start_time、end_time);polling_stop_refused_alert 的 query.filters 为 carrier_id、status、iccid、start_time、end_time;两者仅超级管理员与平台账号可导出。device、iot_card 场景支持 enable_polling 筛选,导出最后一列为「是否轮询」。" +
|
||||
"order_platform、order_agent、order_online 分别导出平台订单、代理商订单、在线支付订单(与 GET /orders/platform、/orders/agent、/orders/online 一一对应),query.filters、表头与数据范围同 order,不接收类别参数。" +
|
||||
"受影响场景的时间筛选固定为 query.filters.start_time 与 query.filters.end_time,取值必须为带显式时区的 RFC3339 秒级时间,创建时规范化为 UTC 秒级字符串后冻结。",
|
||||
Tags: []string{"导出任务"},
|
||||
Input: new(dto.CreateExportTaskRequest),
|
||||
|
||||
@@ -20,19 +20,49 @@ func registerAdminOrderRoutes(router fiber.Router, handler *admin.OrderHandler,
|
||||
})
|
||||
|
||||
Register(router, doc, basePath, "GET", "/orders", handler.List, RouteSpec{
|
||||
Summary: "获取订单列表",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.OrderListRequest),
|
||||
Output: new(dto.OrderListResponse),
|
||||
Auth: true,
|
||||
Summary: "获取订单列表",
|
||||
Description: "平台订单、代理商订单、在线支付订单全部返回;按类别查看请用 `/orders/platform`、`/orders/agent`、`/orders/online`。代理账号只看到买家是本店铺的订单。企业账号无权访问。",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.OrderListRequest),
|
||||
Output: new(dto.OrderListResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
// 按类别拆分的列表是静态路径,必须注册在 /orders/:id 之前,否则会被当作订单 ID 解析。
|
||||
Register(router, doc, basePath, "GET", "/orders/platform", handler.ListPlatform, RouteSpec{
|
||||
Summary: "获取平台订单列表",
|
||||
Description: "平台订单:后台下单且不走代理钱包(线下支付),含平台自营、赠送、平台代购代理名下的卡。查询参数、分页、响应结构、权限与数据范围与 `GET /orders` 相同,只额外固定订单类别,不接收类别参数。代理账号只看到买家是本店铺的订单。企业账号无权访问。",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.OrderListRequest),
|
||||
Output: new(dto.OrderListResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(router, doc, basePath, "GET", "/orders/agent", handler.ListAgent, RouteSpec{
|
||||
Summary: "获取代理商订单列表",
|
||||
Description: "代理商订单:后台下单且从代理钱包扣款,含代理自购、代理给下级购买、平台代扣代理钱包、代理 OpenAPI 下单。查询参数、分页、响应结构、权限与数据范围与 `GET /orders` 相同,只额外固定订单类别,不接收类别参数。代理账号只看到买家是本店铺的订单。企业账号无权访问。",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.OrderListRequest),
|
||||
Output: new(dto.OrderListResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(router, doc, basePath, "GET", "/orders/online", handler.ListOnline, RouteSpec{
|
||||
Summary: "获取在线支付订单列表",
|
||||
Description: "在线支付订单:个人客户在客户端下的全部订单,含微信、支付宝、卡钱包支付、自动续费、自动购买与待支付订单。查询参数、分页、响应结构、权限与数据范围与 `GET /orders` 相同,只额外固定订单类别,不接收类别参数。代理账号只看到买家是本店铺的订单。企业账号无权访问。在线支付订单的买家是个人客户,代理账号调用结果为空。",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.OrderListRequest),
|
||||
Output: new(dto.OrderListResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(router, doc, basePath, "GET", "/orders/:id", handler.Get, RouteSpec{
|
||||
Summary: "获取订单详情",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.GetOrderRequest),
|
||||
Output: new(dto.OrderDetailResponse),
|
||||
Auth: true,
|
||||
Summary: "获取订单详情",
|
||||
Description: "企业账号无权访问。",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.GetOrderRequest),
|
||||
Output: new(dto.OrderDetailResponse),
|
||||
Auth: true,
|
||||
})
|
||||
|
||||
Register(router, doc, basePath, "POST", "/orders/:id/cancel", handler.Cancel, RouteSpec{
|
||||
@@ -44,11 +74,12 @@ func registerAdminOrderRoutes(router fiber.Router, handler *admin.OrderHandler,
|
||||
})
|
||||
|
||||
Register(router, doc, basePath, "POST", "/orders/purchase-check", handler.PurchaseCheck, RouteSpec{
|
||||
Summary: "套餐购买预检",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.PurchaseCheckRequest),
|
||||
Output: new(dto.PurchaseCheckResponse),
|
||||
Auth: true,
|
||||
Summary: "套餐购买预检",
|
||||
Description: "企业账号无权访问。",
|
||||
Tags: []string{"订单管理"},
|
||||
Input: new(dto.PurchaseCheckRequest),
|
||||
Output: new(dto.PurchaseCheckResponse),
|
||||
Auth: true,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1370,7 +1370,8 @@ func (s *Service) GetDetail(ctx context.Context, id uint) (*dto.OrderDetailRespo
|
||||
return &dto.OrderDetailResponse{OrderResponse: resp, Payments: payments}, nil
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, req *dto.OrderListRequest, startTime, endTime *time.Time, buyerType string, buyerID uint) (*dto.OrderListResponse, error) {
|
||||
// List 查询后台订单列表。category 为空表示不限类别(原订单列表),非空时只返回该类别的订单。
|
||||
func (s *Service) List(ctx context.Context, req *dto.OrderListRequest, startTime, endTime *time.Time, buyerType string, buyerID uint, category string) (*dto.OrderListResponse, error) {
|
||||
page := req.Page
|
||||
pageSize := req.PageSize
|
||||
if page == 0 {
|
||||
@@ -1386,6 +1387,9 @@ func (s *Service) List(ctx context.Context, req *dto.OrderListRequest, startTime
|
||||
}
|
||||
|
||||
filters := map[string]any{}
|
||||
if category != "" {
|
||||
filters["order_category"] = category
|
||||
}
|
||||
// 仅代理用户需要限定买家范围;平台/超管不加限制,可查看所有订单
|
||||
if buyerType != "" {
|
||||
filters["buyer_type"] = buyerType
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/query/ordercategory"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
@@ -90,6 +92,15 @@ func (s *OrderStore) List(ctx context.Context, opts *store.QueryOptions, filters
|
||||
|
||||
query = s.applyOrderScope(ctx, query)
|
||||
|
||||
// 订单类别是内部过滤键,由后台按类别拆分的列表接口固定写入,不来自请求参数。
|
||||
if v, ok := filters["order_category"].(string); ok && v != "" {
|
||||
condition, args, valid := ordercategory.Condition("", v)
|
||||
if !valid {
|
||||
return nil, 0, errors.New(errors.CodeInvalidParam, "不支持的订单类别")
|
||||
}
|
||||
query = query.Where(condition, args...)
|
||||
}
|
||||
|
||||
if v, ok := filters["payment_status"]; ok {
|
||||
query = query.Where("payment_status = ?", v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user