p-honggang.li
7 天以前 522737d66480d2f42b8e83c2ddde0efa3b740f38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
 * 订单工作流程控制器
 * 管理订单状态流转和操作权限
 */
 
// 订单状态枚举(按照流程顺序)
export enum OrderStatus {
  WAIT_UPLOAD = '待上传文件',      // 1
  WAIT_AUTHORIZE = '待授权',       // 2
  WAIT_APPROVAL_AUTHORIZE = '待审批授权',
  WAIT_CONFIRM = '待交易确认',     // 3
  COMPLETED = '已完成',           // 4
  EVALUATED = '已评价'            // 5 (最终状态)
}
 
// 操作类型枚举
export enum ActionType {
  VIEW = '查看',
  TRACK = '追踪', 
  UPLOAD_FILE = '提交文件',
  AUTHORIZE = '授权',
  CONFIRM_TRADE = '交易确认',
  EVALUATE = '评价',
  CANCEL_ORDER = '取消订单',
  COMPLETED = '已完成',
  WAIT_APPROVAL_AUTHORIZE = '审批'
}
 
// 页面类型枚举
export enum PageType {
  TRADE_APPROVAL = 'tradeApproval',    // 交易审核
  BUYER_CENTER = 'buyerCenter',        // 买家中心
  SELLER_CENTER = 'sellerCenter'       // 卖家中心
}
 
// 操作配置接口
interface ActionConfig {
  type: ActionType
  routeName?: string
  params?: Record<string, any>
}
 
// 各页面在不同状态下的操作配置
const PAGE_ACTION_CONFIG: Record<PageType, Record<OrderStatus, ActionConfig[]>> = {
  [PageType.TRADE_APPROVAL]: {
    [OrderStatus.WAIT_UPLOAD]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ],
    [OrderStatus.WAIT_AUTHORIZE]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.AUTHORIZE, routeName: 'tradeApproval' }
    ],
    [OrderStatus.WAIT_APPROVAL_AUTHORIZE]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.WAIT_APPROVAL_AUTHORIZE, routeName: 'tradeApproval' }
    ],
    [OrderStatus.WAIT_CONFIRM]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ],
    [OrderStatus.COMPLETED]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ],
    [OrderStatus.EVALUATED]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ]
  },
  [PageType.BUYER_CENTER]: {
    [OrderStatus.WAIT_UPLOAD]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.UPLOAD_FILE, routeName: 'tradeOrderUpload' },
      { type: ActionType.CANCEL_ORDER }
    ],
    [OrderStatus.WAIT_AUTHORIZE]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.CANCEL_ORDER }
    ],
    [OrderStatus.WAIT_APPROVAL_AUTHORIZE]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.WAIT_APPROVAL_AUTHORIZE, routeName: 'tradeApproval' }
    ],
    [OrderStatus.WAIT_CONFIRM]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.CONFIRM_TRADE, routeName: 'tradeOrderConfirm' },
      { type: ActionType.CANCEL_ORDER }
    ],
    [OrderStatus.COMPLETED]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.EVALUATE, routeName: 'tradeOrderEvaluate' }
    ],
    [OrderStatus.EVALUATED]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ]
  },
  [PageType.SELLER_CENTER]: {
    [OrderStatus.WAIT_UPLOAD]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ],
    [OrderStatus.WAIT_AUTHORIZE]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ],
    [OrderStatus.WAIT_APPROVAL_AUTHORIZE]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK },
      { type: ActionType.WAIT_APPROVAL_AUTHORIZE, routeName: 'tradeApproval' }
    ],
    [OrderStatus.WAIT_CONFIRM]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ],
    [OrderStatus.COMPLETED]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ],
    [OrderStatus.EVALUATED]: [
      { type: ActionType.VIEW, routeName: 'tradeOrderDetail' },
      { type: ActionType.TRACK }
    ]
  }
}
 
// 状态流转映射(当前状态 -> 下一个状态)
const STATUS_FLOW_MAP: Record<OrderStatus, OrderStatus | null> = {
  [OrderStatus.WAIT_UPLOAD]: OrderStatus.WAIT_APPROVAL_AUTHORIZE,
  [OrderStatus.WAIT_AUTHORIZE]: OrderStatus.WAIT_CONFIRM,
  [OrderStatus.WAIT_APPROVAL_AUTHORIZE]: OrderStatus.WAIT_CONFIRM,
  [OrderStatus.WAIT_CONFIRM]: OrderStatus.COMPLETED,
  [OrderStatus.COMPLETED]: OrderStatus.EVALUATED,
  [OrderStatus.EVALUATED]: null // 最终状态,无下一状态
}
 
/**
 * 订单工作流程控制器类
 */
export class OrderWorkflowController {
  
  /**
   * 获取指定页面和状态下的可用操作列表
   * @param pageType 页面类型
   * @param currentStatus 当前订单状态
   * @returns 操作配置列表
   */
  static getAvailableActions(pageType: PageType, currentStatus: OrderStatus): ActionConfig[] {
    const pageConfig = PAGE_ACTION_CONFIG[pageType]
    if (!pageConfig) {
      console.warn(`未找到页面类型 ${pageType} 的配置`)
      return []
    }
    
    const statusConfig = pageConfig[currentStatus]
    if (!statusConfig) {
      console.warn(`未找到状态 ${currentStatus} 在页面 ${pageType} 中的配置`)
      return []
    }
    
    return statusConfig
  }
  
  /**
   * 判断是否可以执行指定操作
   * @param pageType 页面类型
   * @param currentStatus 当前订单状态
   * @param actionType 操作类型
   * @returns 是否可以执行
   */
  static canExecuteAction(pageType: PageType, currentStatus: OrderStatus, actionType: ActionType): boolean {
    const actions = this.getAvailableActions(pageType, currentStatus)
    return actions.some(action => action.type === actionType)
  }
  
  /**
   * 获取下一个状态
   * @param currentStatus 当前状态
   * @returns 下一个状态,如果已是最终状态则返回null
   */
  static getNextStatus(currentStatus: OrderStatus): OrderStatus | null {
    return STATUS_FLOW_MAP[currentStatus] || null
  }
  
  /**
   * 判断当前状态是否为最终状态
   * @param currentStatus 当前状态
   * @returns 是否为最终状态
   */
  static isFinalStatus(currentStatus: OrderStatus): boolean {
    return currentStatus === OrderStatus.EVALUATED
  }
  
  /**
   * 验证状态流转是否合法
   * @param fromStatus 起始状态
   * @param toStatus 目标状态
   * @returns 是否为合法的状态流转
   */
  static validateStatusTransition(fromStatus: OrderStatus, toStatus: OrderStatus): boolean {
    const expectedNextStatus = this.getNextStatus(fromStatus)
    return expectedNextStatus === toStatus
  }
  
  /**
   * 获取所有状态的顺序列表
   * @returns 状态列表
   */
  static getAllStatusInOrder(): OrderStatus[] {
    return [
      OrderStatus.WAIT_UPLOAD,
      OrderStatus.WAIT_AUTHORIZE,
      OrderStatus.WAIT_CONFIRM,
      OrderStatus.COMPLETED,
      OrderStatus.EVALUATED
    ]
  }
  
  /**
   * 根据产品套件判断初始状态
   * @param orderDetails 订单详情列表
   * @returns 初始状态
   */
  static determineInitialStatus(orderDetails: any[]): OrderStatus {
    // 检查是否有价格类型为"协议"的套件
    const hasAgreementPrice = orderDetails.some(detail => {
      const priceType = String(detail.priceType || '').toLowerCase()
      return priceType.includes('协议') || priceType.includes('agreement')
    })
    
    // 如果有协议价格,从"待上传文件"开始,否则从"待授权"开始
    return hasAgreementPrice ? OrderStatus.WAIT_UPLOAD : OrderStatus.WAIT_AUTHORIZE
  }
}
 
// 状态映射工具函数(用于前后端状态转换)
export const StatusMapper = {
  /**
   * 前端枚举状态转后端中文状态
   */
  toServerStatus: (uiStatus: string): string => {
    // 直接返回中文状态,因为枚举值本身就是中文
    return uiStatus
  },
  
  /**
   * 后端中文状态转前端枚举状态
   */
  toUIStatus: (serverStatus: string): OrderStatus => {
    // 标准化状态名称
    const normalizedStatus = serverStatus?.trim()
    
    switch (normalizedStatus) {
      case '待上传文件':
        return OrderStatus.WAIT_UPLOAD
      case '待授权':
        return OrderStatus.WAIT_AUTHORIZE
      case '待审批授权':
        return OrderStatus.WAIT_APPROVAL_AUTHORIZE
      case '待交易确认':
        return OrderStatus.WAIT_CONFIRM
      case '已完成':
        return OrderStatus.COMPLETED
      case '已评价':
        return OrderStatus.EVALUATED
      default:
        console.warn(`未知的订单状态: ${serverStatus},默认返回待授权状态`)
        return OrderStatus.WAIT_AUTHORIZE
    }
  }
}