statusMachine.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // statusMachine.ts - Class版本 (修正版)
  2. export class OrderStatusMachine {
  3. constructor(row, currentTime) {
  4. this.row = row
  5. }
  6. // 是否未过期
  7. isNotExpired() {
  8. return !this.row.expireTime || this.getDate() < this.row.expireTime
  9. }
  10. // 判断是否已完成 (type 1)
  11. isCompletedType1() {
  12. return this.row.status === 2 &&
  13. this.row.executionStatus === 2 &&
  14. this.row.callbackStatus === 1
  15. }
  16. // 判断是否已完成 (type 2)
  17. isCompletedType2() {
  18. return this.row.status === 2 &&
  19. this.row.executionStatus === 2 &&
  20. this.row.submitStatus === 2
  21. }
  22. // 判断是否已拒绝
  23. isRefused() {
  24. return this.row.callbackStatus === 2 ||
  25. this.row.status === 3 ||
  26. this.row.executionStatus === 3
  27. }
  28. // 判断是否已取消
  29. isCancelled() {
  30. return this.row.status === 5
  31. }
  32. getDate() {
  33. let timezone = 2; //目标时区时间,东3区 东时区正数 西市区负数
  34. let offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差,单位为分钟
  35. let nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数
  36. let now = new Date(
  37. nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000
  38. );
  39. //当前时间
  40. let hh = now.getHours(); //时
  41. let mm = now.getMinutes(); //分
  42. let ss = now.getSeconds(); //分
  43. let clock;
  44. let year = now.getFullYear(); //年
  45. let month = now.getMonth() + 1; //月
  46. let day = now.getDate(); //日
  47. if (month < 10) {
  48. month = "0" + month;
  49. }
  50. if (day < 10) {
  51. day = "0" + day;
  52. }
  53. if (hh < 10) {
  54. hh = "0" + hh;
  55. }
  56. if (mm < 10) {
  57. mm = "0" + mm;
  58. }
  59. if (ss < 10) {
  60. ss = "0" + ss;
  61. }
  62. clock = year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
  63. this.time = clock;
  64. }
  65. // 判断是否过期(考虑状态优先级)
  66. isExpired() {
  67. const r = this.row
  68. // 必须有过期时间
  69. if (!r.expireTime) return false
  70. // 当前时间必须大于过期时间
  71. if (this.getDate() <= r.expireTime) return false
  72. // 已取消的不过期
  73. if (this.isCancelled()) return false
  74. // 已拒绝的不过期
  75. if (this.isRefused()) return false
  76. // 已完成的不过期
  77. if (r.type === 1 && this.isCompletedType1()) return false
  78. if (r.type === 2 && this.isCompletedType2()) return false
  79. console.log(23423423);
  80. return true
  81. }
  82. // 获取状态码
  83. getStatus() {
  84. const r = this.row
  85. console.log(r, '订单数据');
  86. // 1. 优先检查已取消
  87. if (this.isCancelled()) return 6
  88. // 2. 检查已拒绝
  89. if (this.isRefused()) return 4
  90. // 3. 检查是否已完成 (不同type分别判断)
  91. if (r.type === 1 && this.isCompletedType1()) return 2
  92. if (r.type === 2 && this.isCompletedType2()) return 2
  93. // 4. 检查是否过期 - 按照原始逻辑,只有不是最终状态才判断过期
  94. if (this.isExpired()) return 5
  95. // 5. 根据类型判断待处理或进行中
  96. if (r.type === 1) {
  97. return this._getType1Status()
  98. } else if (r.type === 2) {
  99. return this._getType2Status()
  100. }
  101. console.log('未匹配到任何状态');
  102. return null
  103. }
  104. // type 1 状态 (非最终状态)
  105. _getType1Status() {
  106. const r = this.row
  107. // 待处理
  108. if (r.status === 1 && r.callbackStatus === 0 && this.isNotExpired()) {
  109. console.log('type1 待处理');
  110. return 1
  111. }
  112. // 进行中
  113. if ((r.status === 2 && [0, 1].includes(r.executionStatus) && r.callbackStatus !== 2 && this.isNotExpired()) ||
  114. (r.status === 1 && r.callbackStatus === 1 && this.isNotExpired())) {
  115. console.log('type1 进行中');
  116. return 3
  117. }
  118. console.log('type1 未匹配到状态');
  119. return null
  120. }
  121. // type 2 状态 (非最终状态)
  122. _getType2Status() {
  123. const r = this.row
  124. // 待处理
  125. if (r.status === 1 && r.callbackStatus === 0 && this.isNotExpired()) {
  126. console.log('type2 待处理');
  127. return 1
  128. }
  129. // 进行中
  130. if ((r.status === 2 && [0, 1].includes(r.executionStatus) && r.callbackStatus !== 2) ||
  131. (r.status === 2 && r.executionStatus === 2 && r.callbackStatus === 0 && r.submitStatus !== 2)) {
  132. console.log('type2 进行中');
  133. return 3
  134. }
  135. console.log('type2 未匹配到状态');
  136. return null
  137. }
  138. // 获取状态信息
  139. getStatusInfo() {
  140. const status = this.getStatus()
  141. const statusMap = {
  142. 1: { text: '待处理', class: 'warning', value: 1 },
  143. 2: { text: '已完成', class: 'success', value: 2 },
  144. 3: { text: '进行中', class: 'processing', value: 3 },
  145. 4: { text: '已拒绝', class: 'danger', value: 4 },
  146. 5: { text: '已过期', class: 'expired', value: 5 },
  147. 6: { text: '已取消', class: 'cancelled', value: 6 }
  148. }
  149. console.log(status, '最终状态码');
  150. return statusMap[status] || { text: '-', class: '', value: null }
  151. }
  152. // 检查是否可以取消
  153. canCancel() {
  154. const r = this.row
  155. // type=2 且待处理
  156. if (r.type === 2 &&
  157. r.status === 1 &&
  158. r.callbackStatus === 0 &&
  159. this.isNotExpired()) {
  160. return true
  161. }
  162. // type=2 且满足取消条件
  163. if (r.type === 2 &&
  164. r.status === 2 &&
  165. r.executionStatus === 2 &&
  166. r.backstageStatus === 1) {
  167. return true
  168. }
  169. return false
  170. }
  171. // 获取可执行的操作列表
  172. getAvailableActions() {
  173. const actions = []
  174. if (this.canCancel()) {
  175. actions.push({
  176. type: 'cancel',
  177. text: '取消',
  178. handler: 'handleCancel'
  179. })
  180. }
  181. return actions
  182. }
  183. // 调试方法:打印所有状态判断结果
  184. debug() {
  185. console.log({
  186. row: this.row,
  187. isCancelled: this.isCancelled(),
  188. isRefused: this.isRefused(),
  189. isCompletedType1: this.isCompletedType1(),
  190. isCompletedType2: this.isCompletedType2(),
  191. isExpired: this.isExpired(),
  192. isNotExpired: this.isNotExpired(),
  193. finalStatus: this.getStatus()
  194. }, 312321312)
  195. }
  196. }
  197. // 工厂函数
  198. export const createOrderStatusMachine = (row, currentTime) => {
  199. return new OrderStatusMachine(row, currentTime)
  200. }