OrderLifecycleServiceTest.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package com.ruoyi.app.order;
  2. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  3. import com.ruoyi.app.order.dto.AdminOrderActionRequest;
  4. import com.ruoyi.app.order.dto.AdminOrderStatusContext;
  5. import com.ruoyi.app.order.dto.AdminOrderStatusUpdateRequest;
  6. import com.ruoyi.common.exception.ServiceException;
  7. import com.ruoyi.system.domain.PosOrder;
  8. import com.ruoyi.system.domain.PosOrderOmgPayment;
  9. import com.ruoyi.system.domain.PosOrderOmgRefund;
  10. import com.ruoyi.system.service.IPosOrderOmgPaymentService;
  11. import com.ruoyi.system.service.IPosOrderOmgRefundService;
  12. import com.ruoyi.system.service.IPosOrderService;
  13. import com.ruoyi.system.service.IPointsTransactionService;
  14. import com.ruoyi.system.service.IUserWalletService;
  15. import com.ruoyi.system.utils.OrderLogHelper;
  16. import org.junit.jupiter.api.BeforeEach;
  17. import org.junit.jupiter.api.Test;
  18. import java.util.List;
  19. import static org.junit.jupiter.api.Assertions.assertEquals;
  20. import static org.junit.jupiter.api.Assertions.assertFalse;
  21. import static org.junit.jupiter.api.Assertions.assertNotNull;
  22. import static org.junit.jupiter.api.Assertions.assertThrows;
  23. import static org.junit.jupiter.api.Assertions.assertTrue;
  24. import static org.mockito.ArgumentMatchers.any;
  25. import static org.mockito.ArgumentMatchers.anyInt;
  26. import static org.mockito.ArgumentMatchers.anyLong;
  27. import static org.mockito.ArgumentMatchers.anyString;
  28. import static org.mockito.Mockito.doAnswer;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.never;
  31. import static org.mockito.Mockito.verify;
  32. import static org.mockito.Mockito.when;
  33. class OrderLifecycleServiceTest {
  34. private IPosOrderService posOrderService;
  35. private OrderService billingService;
  36. private OrderLogHelper orderLogHelper;
  37. private IUserWalletService userWalletService;
  38. private IPointsTransactionService pointsTransactionService;
  39. private IPosOrderOmgPaymentService omgPaymentService;
  40. private IPosOrderOmgRefundService omgRefundService;
  41. private OrderLifecycleService service;
  42. @BeforeEach
  43. void setUp() {
  44. posOrderService = mock(IPosOrderService.class);
  45. billingService = mock(OrderService.class);
  46. orderLogHelper = mock(OrderLogHelper.class);
  47. userWalletService = mock(IUserWalletService.class);
  48. pointsTransactionService = mock(IPointsTransactionService.class);
  49. omgPaymentService = mock(IPosOrderOmgPaymentService.class);
  50. omgRefundService = mock(IPosOrderOmgRefundService.class);
  51. service = new OrderLifecycleService(posOrderService, billingService, orderLogHelper,
  52. userWalletService, pointsTransactionService, omgPaymentService, omgRefundService);
  53. }
  54. @Test
  55. void exposesOnlyLegalNextOrderAndDeliveryStates() {
  56. PosOrder order = deliveryOrder();
  57. when(posOrderService.getById(10L)).thenReturn(order);
  58. PosOrderOmgPayment payment = new PosOrderOmgPayment();
  59. payment.setId(1L);
  60. payment.setPayStatus(1);
  61. when(omgPaymentService.getLatestByDdId(order.getDdId())).thenReturn(payment);
  62. when(omgRefundService.listByPayment(1L)).thenReturn(List.of());
  63. AdminOrderStatusContext context = service.getStatusContext(10L);
  64. assertEquals(List.of(2L), context.getAllowedOrderStates());
  65. assertEquals(List.of(2L, 3L), context.getAllowedDeliveryStatuses());
  66. assertTrue(context.getCanRefundOmg());
  67. assertFalse(context.getCanConfirmOfflinePayment());
  68. }
  69. @Test
  70. void marksDeliveryAsDeliveredAndCompletesExactlyOnce() {
  71. PosOrder order = deliveryOrder();
  72. when(posOrderService.getById(10L)).thenReturn(order);
  73. doAnswer(invocation -> {
  74. PosOrder update = invocation.getArgument(0);
  75. order.setState(update.getState());
  76. order.setDeliveryStatus(update.getDeliveryStatus());
  77. order.setPayStatus(update.getPayStatus());
  78. order.setAfterSaleStatus(update.getAfterSaleStatus());
  79. order.setSdTime(update.getSdTime());
  80. return true;
  81. }).when(posOrderService).update(any(PosOrder.class), any(Wrapper.class));
  82. AdminOrderStatusUpdateRequest request = statusRequest(2L, 2L, 1L, 0L);
  83. request.setTargetDeliveryStatus(3L);
  84. request.setReason("骑手端漏操作");
  85. AdminOrderStatusContext context = service.updateStatus(10L, request, 99L, "平台管理员");
  86. assertEquals(3L, context.getState());
  87. assertEquals(3L, context.getDeliveryStatus());
  88. assertNotNull(context.getSdTime());
  89. verify(billingService).setSanghuBilling(order);
  90. verify(billingService).setQishouBilling(order);
  91. verify(orderLogHelper).logSync(anyString(), anyInt(), anyLong(), anyString(), anyString());
  92. }
  93. @Test
  94. void staleSnapshotHasNoSideEffects() {
  95. PosOrder order = deliveryOrder();
  96. order.setPayStatus(0L);
  97. when(posOrderService.getById(10L)).thenReturn(order);
  98. AdminOrderStatusUpdateRequest request = statusRequest(2L, 2L, 1L, 0L);
  99. request.setTargetDeliveryStatus(3L);
  100. request.setReason("陈旧页面提交");
  101. ServiceException error = assertThrows(ServiceException.class,
  102. () -> service.updateStatus(10L, request, 99L, "平台管理员"));
  103. assertTrue(error.getMessage().contains("状态已变化"));
  104. verify(posOrderService, never()).update(any(PosOrder.class), any(Wrapper.class));
  105. verify(billingService, never()).setSanghuBilling(any());
  106. verify(orderLogHelper, never()).logSync(anyString(), anyInt(), any(), anyString(), anyString());
  107. }
  108. @Test
  109. void casFailureAfterReadHasNoSideEffects() {
  110. PosOrder order = deliveryOrder();
  111. when(posOrderService.getById(10L)).thenReturn(order);
  112. when(posOrderService.update(any(PosOrder.class), any(Wrapper.class))).thenReturn(false);
  113. AdminOrderStatusUpdateRequest request = statusRequest(2L, 2L, 1L, 0L);
  114. request.setTargetDeliveryStatus(3L);
  115. request.setReason("并发提交");
  116. ServiceException error = assertThrows(ServiceException.class,
  117. () -> service.updateStatus(10L, request, 99L, "平台管理员"));
  118. assertTrue(error.getMessage().contains("状态已变化"));
  119. verify(billingService, never()).setSanghuBilling(any());
  120. verify(orderLogHelper, never()).logSync(anyString(), anyInt(), any(), anyString(), anyString());
  121. }
  122. @Test
  123. void rejectsDeliveryRollback() {
  124. PosOrder order = deliveryOrder();
  125. when(posOrderService.getById(10L)).thenReturn(order);
  126. AdminOrderStatusUpdateRequest request = statusRequest(2L, 2L, 1L, 0L);
  127. request.setTargetDeliveryStatus(1L);
  128. request.setReason("错误回退");
  129. assertThrows(ServiceException.class,
  130. () -> service.updateStatus(10L, request, 99L, "平台管理员"));
  131. verify(posOrderService, never()).update(any(PosOrder.class), any(Wrapper.class));
  132. }
  133. @Test
  134. void confirmsOnlyOfflinePayment() {
  135. PosOrder order = pickupOrder("1", 0L);
  136. when(posOrderService.getById(20L)).thenReturn(order);
  137. doAnswer(invocation -> {
  138. PosOrder update = invocation.getArgument(0);
  139. order.setPayStatus(update.getPayStatus());
  140. return true;
  141. }).when(posOrderService).update(any(PosOrder.class), any(Wrapper.class));
  142. AdminOrderStatusContext context = service.confirmOfflinePayment(20L,
  143. actionRequest(2L, null, 0L, 0L, "门店已收现金"), 99L, "平台管理员");
  144. assertEquals(1L, context.getPayStatus());
  145. verify(orderLogHelper).logSync(anyString(), anyInt(), anyLong(), anyString(), anyString());
  146. }
  147. @Test
  148. void rejectsOmgOrderFromOfflinePaymentPath() {
  149. PosOrder order = pickupOrder("7", 0L);
  150. when(posOrderService.getById(20L)).thenReturn(order);
  151. assertThrows(ServiceException.class, () -> service.confirmOfflinePayment(20L,
  152. actionRequest(2L, null, 0L, 0L, "不能伪造在线支付"), 99L, "平台管理员"));
  153. verify(posOrderService, never()).update(any(PosOrder.class), any(Wrapper.class));
  154. }
  155. @Test
  156. void offlineRefundSynchronizesAllBusinessStates() {
  157. PosOrder order = pickupOrder("1", 1L);
  158. order.setState(1L);
  159. when(posOrderService.getById(20L)).thenReturn(order);
  160. doAnswer(invocation -> {
  161. PosOrder update = invocation.getArgument(0);
  162. order.setState(update.getState());
  163. order.setPayStatus(update.getPayStatus());
  164. order.setAfterSaleStatus(update.getAfterSaleStatus());
  165. return true;
  166. }).when(posOrderService).update(any(PosOrder.class), any(Wrapper.class));
  167. AdminOrderStatusContext context = service.confirmOfflineRefund(20L,
  168. actionRequest(1L, null, 1L, 0L, "已现场退还现金"), 99L, "平台管理员");
  169. assertEquals(4L, context.getState());
  170. assertEquals(2L, context.getPayStatus());
  171. assertEquals(3L, context.getAfterSaleStatus());
  172. }
  173. @Test
  174. void completedOrderCannotBeRefunded() {
  175. PosOrder order = pickupOrder("1", 1L);
  176. order.setState(3L);
  177. when(posOrderService.getById(20L)).thenReturn(order);
  178. assertThrows(ServiceException.class, () -> service.confirmOfflineRefund(20L,
  179. actionRequest(3L, null, 1L, 0L, "申请退款"), 99L, "平台管理员"));
  180. verify(posOrderService, never()).update(any(PosOrder.class), any(Wrapper.class));
  181. }
  182. @Test
  183. void blankReasonIsRejectedBeforeAnyWrite() {
  184. PosOrder order = pickupOrder("1", 0L);
  185. when(posOrderService.getById(20L)).thenReturn(order);
  186. assertThrows(ServiceException.class, () -> service.confirmOfflinePayment(20L,
  187. actionRequest(2L, null, 0L, 0L, " "), 99L, "平台管理员"));
  188. verify(posOrderService, never()).update(any(PosOrder.class), any(Wrapper.class));
  189. }
  190. @Test
  191. void riderDeliveryCompletesCashOrderAndPreservesInvariant() {
  192. PosOrder order = deliveryOrder();
  193. order.setPayType("1");
  194. order.setCollectPayment("1");
  195. order.setPayStatus(0L);
  196. when(posOrderService.getById(10L)).thenReturn(order);
  197. doAnswer(invocation -> {
  198. PosOrder update = invocation.getArgument(0);
  199. order.setState(update.getState());
  200. order.setDeliveryStatus(update.getDeliveryStatus());
  201. order.setPayStatus(update.getPayStatus());
  202. order.setSdTime(update.getSdTime());
  203. return true;
  204. }).when(posOrderService).update(any(PosOrder.class), any(Wrapper.class));
  205. AdminOrderStatusContext context = service.completeDeliveryByRider(10L, 88L, "骑手甲");
  206. assertEquals(3L, context.getState());
  207. assertEquals(3L, context.getDeliveryStatus());
  208. assertEquals(1L, context.getPayStatus());
  209. verify(billingService).setSanghuBilling(order);
  210. verify(billingService).setQishouBilling(order);
  211. }
  212. @Test
  213. void synchronizesSystemOmgRefundToAllThreeBusinessStates() {
  214. PosOrder order = pickupOrder("7", 1L);
  215. order.setState(1L);
  216. when(posOrderService.getById(20L)).thenReturn(order);
  217. doAnswer(invocation -> {
  218. PosOrder update = invocation.getArgument(0);
  219. order.setState(update.getState());
  220. order.setPayStatus(update.getPayStatus());
  221. order.setAfterSaleStatus(update.getAfterSaleStatus());
  222. return true;
  223. }).when(posOrderService).update(any(PosOrder.class), any(Wrapper.class));
  224. AdminOrderStatusContext context = service.finalizeSystemOmgRefund(20L);
  225. assertEquals(4L, context.getState());
  226. assertEquals(2L, context.getPayStatus());
  227. assertEquals(3L, context.getAfterSaleStatus());
  228. verify(orderLogHelper).logSync(anyString(), anyInt(), any(), anyString(), anyString());
  229. }
  230. @Test
  231. void exposesManualRefundLocalCompensationAfterLedgerAlreadyCompleted() {
  232. PosOrder order = pickupOrder("7", 1L);
  233. order.setState(1L);
  234. PosOrderOmgPayment payment = new PosOrderOmgPayment();
  235. payment.setId(3L);
  236. payment.setPayStatus(3);
  237. payment.setPayType("ATM_TAISHIN");
  238. PosOrderOmgRefund completed = new PosOrderOmgRefund();
  239. completed.setRtnCode(1);
  240. when(posOrderService.getById(20L)).thenReturn(order);
  241. when(omgPaymentService.getLatestByDdId(order.getDdId())).thenReturn(payment);
  242. when(omgRefundService.listByPayment(3L)).thenReturn(List.of(completed));
  243. AdminOrderStatusContext context = service.getStatusContext(20L);
  244. assertTrue(context.getCanConfirmManualOmgRefund());
  245. assertFalse(context.getCanRefundOmg());
  246. assertFalse(context.getManualRefundPending());
  247. }
  248. private PosOrder deliveryOrder() {
  249. PosOrder order = new PosOrder();
  250. order.setId(10L);
  251. order.setDdId("202608100001");
  252. order.setType(0L);
  253. order.setPayType("7");
  254. order.setState(2L);
  255. order.setDeliveryStatus(2L);
  256. order.setPayStatus(1L);
  257. order.setAfterSaleStatus(0L);
  258. order.setQsId(88L);
  259. order.setShId(66L);
  260. order.setMdId(77L);
  261. order.setAmount(100);
  262. order.setFood("[]");
  263. order.setFreight(10D);
  264. return order;
  265. }
  266. private PosOrder pickupOrder(String payType, Long payStatus) {
  267. PosOrder order = new PosOrder();
  268. order.setId(20L);
  269. order.setDdId("202608100002");
  270. order.setType(1L);
  271. order.setPayType(payType);
  272. order.setState(2L);
  273. order.setPayStatus(payStatus);
  274. order.setAfterSaleStatus(0L);
  275. return order;
  276. }
  277. private AdminOrderStatusUpdateRequest statusRequest(Long state, Long delivery, Long pay, Long afterSale) {
  278. AdminOrderStatusUpdateRequest request = new AdminOrderStatusUpdateRequest();
  279. request.setExpectedState(state);
  280. request.setExpectedDeliveryStatus(delivery);
  281. request.setExpectedPayStatus(pay);
  282. request.setExpectedAfterSaleStatus(afterSale);
  283. return request;
  284. }
  285. private AdminOrderActionRequest actionRequest(Long state, Long delivery, Long pay, Long afterSale,
  286. String reason) {
  287. AdminOrderActionRequest request = new AdminOrderActionRequest();
  288. request.setExpectedState(state);
  289. request.setExpectedDeliveryStatus(delivery);
  290. request.setExpectedPayStatus(pay);
  291. request.setExpectedAfterSaleStatus(afterSale);
  292. request.setReason(reason);
  293. return request;
  294. }
  295. }