| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- // statusMachine.ts - Class版本 (修正版)
- export class OrderStatusMachine {
- constructor(row, currentTime) {
- this.row = row
- }
- // 是否未过期
- isNotExpired() {
- return !this.row.expireTime || this.getDate() < this.row.expireTime
- }
- // 判断是否已完成 (type 1)
- isCompletedType1() {
- return this.row.status === 2 &&
- this.row.executionStatus === 2 &&
- this.row.callbackStatus === 1
- }
- // 判断是否已完成 (type 2)
- isCompletedType2() {
- return this.row.status === 2 &&
- this.row.executionStatus === 2 &&
- this.row.submitStatus === 2
- }
- // 判断是否已拒绝
- isRefused() {
- return this.row.callbackStatus === 2 ||
- this.row.status === 3 ||
- this.row.executionStatus === 3
- }
- // 判断是否已取消
- isCancelled() {
- return this.row.status === 5
- }
- getDate() {
- let timezone = 2; //目标时区时间,东3区 东时区正数 西市区负数
- let offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差,单位为分钟
- let nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数
- let now = new Date(
- nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000
- );
- //当前时间
- let hh = now.getHours(); //时
- let mm = now.getMinutes(); //分
- let ss = now.getSeconds(); //分
- let clock;
- let year = now.getFullYear(); //年
- let month = now.getMonth() + 1; //月
- let day = now.getDate(); //日
- if (month < 10) {
- month = "0" + month;
- }
- if (day < 10) {
- day = "0" + day;
- }
- if (hh < 10) {
- hh = "0" + hh;
- }
- if (mm < 10) {
- mm = "0" + mm;
- }
- if (ss < 10) {
- ss = "0" + ss;
- }
- clock = year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
- this.time = clock;
- }
- // 判断是否过期(考虑状态优先级)
- isExpired() {
- const r = this.row
- // 必须有过期时间
- if (!r.expireTime) return false
- // 当前时间必须大于过期时间
- if (this.getDate() <= r.expireTime) return false
- // 已取消的不过期
- if (this.isCancelled()) return false
- // 已拒绝的不过期
- if (this.isRefused()) return false
- // 已完成的不过期
- if (r.type === 1 && this.isCompletedType1()) return false
- if (r.type === 2 && this.isCompletedType2()) return false
- console.log(23423423);
- return true
- }
- // 获取状态码
- getStatus() {
- const r = this.row
- console.log(r, '订单数据');
- // 1. 优先检查已取消
- if (this.isCancelled()) return 6
- // 2. 检查已拒绝
- if (this.isRefused()) return 4
- // 3. 检查是否已完成 (不同type分别判断)
- if (r.type === 1 && this.isCompletedType1()) return 2
- if (r.type === 2 && this.isCompletedType2()) return 2
- // 4. 检查是否过期 - 按照原始逻辑,只有不是最终状态才判断过期
- if (this.isExpired()) return 5
- // 5. 根据类型判断待处理或进行中
- if (r.type === 1) {
- return this._getType1Status()
- } else if (r.type === 2) {
- return this._getType2Status()
- }
- console.log('未匹配到任何状态');
- return null
- }
- // type 1 状态 (非最终状态)
- _getType1Status() {
- const r = this.row
- // 待处理
- if (r.status === 1 && r.callbackStatus === 0 && this.isNotExpired()) {
- console.log('type1 待处理');
- return 1
- }
- // 进行中
- if ((r.status === 2 && [0, 1].includes(r.executionStatus) && r.callbackStatus !== 2 && this.isNotExpired()) ||
- (r.status === 1 && r.callbackStatus === 1 && this.isNotExpired())) {
- console.log('type1 进行中');
- return 3
- }
- console.log('type1 未匹配到状态');
- return null
- }
- // type 2 状态 (非最终状态)
- _getType2Status() {
- const r = this.row
- // 待处理
- if (r.status === 1 && r.callbackStatus === 0 && this.isNotExpired()) {
- console.log('type2 待处理');
- return 1
- }
- // 进行中
- if ((r.status === 2 && [0, 1].includes(r.executionStatus) && r.callbackStatus !== 2) ||
- (r.status === 2 && r.executionStatus === 2 && r.callbackStatus === 0 && r.submitStatus !== 2)) {
- console.log('type2 进行中');
- return 3
- }
- console.log('type2 未匹配到状态');
- return null
- }
- // 获取状态信息
- getStatusInfo() {
- const status = this.getStatus()
- const statusMap = {
- 1: { text: '待处理', class: 'warning', value: 1 },
- 2: { text: '已完成', class: 'success', value: 2 },
- 3: { text: '进行中', class: 'processing', value: 3 },
- 4: { text: '已拒绝', class: 'danger', value: 4 },
- 5: { text: '已过期', class: 'expired', value: 5 },
- 6: { text: '已取消', class: 'cancelled', value: 6 }
- }
- console.log(status, '最终状态码');
- return statusMap[status] || { text: '-', class: '', value: null }
- }
- // 检查是否可以取消
- canCancel() {
- const r = this.row
- // type=2 且待处理
- if (r.type === 2 &&
- r.status === 1 &&
- r.callbackStatus === 0 &&
- this.isNotExpired()) {
- return true
- }
- // type=2 且满足取消条件
- if (r.type === 2 &&
- r.status === 2 &&
- r.executionStatus === 2 &&
- r.backstageStatus === 1) {
- return true
- }
- return false
- }
- // 获取可执行的操作列表
- getAvailableActions() {
- const actions = []
- if (this.canCancel()) {
- actions.push({
- type: 'cancel',
- text: '取消',
- handler: 'handleCancel'
- })
- }
- return actions
- }
- // 调试方法:打印所有状态判断结果
- debug() {
- console.log({
- row: this.row,
- isCancelled: this.isCancelled(),
- isRefused: this.isRefused(),
- isCompletedType1: this.isCompletedType1(),
- isCompletedType2: this.isCompletedType2(),
- isExpired: this.isExpired(),
- isNotExpired: this.isNotExpired(),
- finalStatus: this.getStatus()
- }, 312321312)
- }
- }
- // 工厂函数
- export const createOrderStatusMachine = (row, currentTime) => {
- return new OrderStatusMachine(row, currentTime)
- }
|