|
|
@@ -0,0 +1,125 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.TrainApplication;
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.entity.TrainServiceOrder;
|
|
|
+import com.train.mapper.TrainServiceOrderMapper;
|
|
|
+import com.train.service.ServiceOrderService;
|
|
|
+import org.junit.jupiter.api.AfterEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.test.annotation.DirtiesContext;
|
|
|
+
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+
|
|
|
+/**
|
|
|
+ * finish 响应体快照陈旧修复的回归守护(冒烟 38/38 复现缺陷的固化)。
|
|
|
+ *
|
|
|
+ * 缺陷:finish 旧实现用 updateById 之前取的实体 toVO(o) 返回,响应体 status 仍是到达岗/服务中的
|
|
|
+ * 旧值、finishAt=null,与库内 + /api/order/detail 的真实 done 不一致 → 前端直接渲染会闪现错误状态。
|
|
|
+ *
|
|
|
+ * 修复口径:updateById 后 selectById 重取权威行再构造 VO(状态/finishAt/serviceItems/finishPhotos
|
|
|
+ * 一律取更新后的真实值)。本测试断言「响应体 status=done 且 finishAt 非空」,防止回归。
|
|
|
+ *
|
|
|
+ * 种子保护:合成工单(id 走自动序列,worker/order_no 唯一),@AfterEach 清 order + attach 留痕。
|
|
|
+ */
|
|
|
+@SpringBootTest(classes = TrainApplication.class)
|
|
|
+@DirtiesContext
|
|
|
+class FinishVoFreshnessTest {
|
|
|
+
|
|
|
+ private static final long SYN_WORKER = 990301L; // 合成服务人员(工单归属对象)
|
|
|
+ private static final long SYN_ORG = 990302L;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ServiceOrderService service;
|
|
|
+ @Autowired
|
|
|
+ private TrainServiceOrderMapper orderMapper;
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbc;
|
|
|
+
|
|
|
+ private Long trackedOrderId;
|
|
|
+
|
|
|
+ /** 造一张已到岗(status='arrived')且归属合成 worker 的工单(满足 mustOwn + 状态门禁) */
|
|
|
+ private long newSyntheticArrivedOrder() {
|
|
|
+ TrainServiceOrder o = new TrainServiceOrder();
|
|
|
+ o.setOrderNo("FIN" + System.nanoTime());
|
|
|
+ o.setOrgId(SYN_ORG);
|
|
|
+ o.setWorkerId(SYN_WORKER); // 归属本人,mustOwn 才通过
|
|
|
+ o.setFamilyName("合成家庭(测试)");
|
|
|
+ o.setServiceType("care");
|
|
|
+ o.setStatus("arrived"); // 已到岗 → finish 合法前置
|
|
|
+ o.setAmount(1000);
|
|
|
+ o.setPlatformFee(100);
|
|
|
+ o.setStaffIncome(900);
|
|
|
+ o.setServiceItems("[{\"name\":\"日常保洁\"}]");
|
|
|
+ Calendar arrive = Calendar.getInstance();
|
|
|
+ arrive.add(Calendar.HOUR_OF_DAY, -1); // 1 小时前到岗
|
|
|
+ o.setArriveAt(arrive.getTime());
|
|
|
+ o.setCreatedAt(new java.util.Date());
|
|
|
+ o.setUpdatedAt(new java.util.Date());
|
|
|
+ orderMapper.insert(o);
|
|
|
+ assertNotNull(o.getId());
|
|
|
+ trackedOrderId = o.getId();
|
|
|
+ return o.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterEach
|
|
|
+ void tearDown() {
|
|
|
+ if (trackedOrderId != null) {
|
|
|
+ jdbc.update("DELETE FROM train_order_dispatch WHERE order_id=?", trackedOrderId);
|
|
|
+ jdbc.update("DELETE FROM train_credit_ledger WHERE source_type='order' AND source_id=?", String.valueOf(trackedOrderId));
|
|
|
+ jdbc.update("DELETE FROM train_service_order WHERE id=?", trackedOrderId);
|
|
|
+ trackedOrderId = null;
|
|
|
+ }
|
|
|
+ jdbc.update("DELETE FROM train_service_order WHERE order_no LIKE 'FIN%'");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void finishResponseReturnsUpdatedStatusNotStaleSnapshot() {
|
|
|
+ long orderId = newSyntheticArrivedOrder();
|
|
|
+
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("items", "[{\"name\":\"日常保洁\",\"done\":1}]");
|
|
|
+ body.put("photos", "p1.jpg,p2.jpg");
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.finish(SYN_WORKER, orderId, body);
|
|
|
+ assertNotNull(r);
|
|
|
+ assertEquals(200, r.getCode(), "finish 必须成功, message=" + r.getMessage());
|
|
|
+ Map<String, Object> vo = r.getData();
|
|
|
+ assertNotNull(vo);
|
|
|
+
|
|
|
+ // 红线:响应体不得回陈旧快照 —— status 必须已是 done、finishAt 非空
|
|
|
+ assertEquals("done", vo.get("status"),
|
|
|
+ "finish 响应体 status 必须为 done(不得回 updateById 前的旧态 arrived/doing)");
|
|
|
+ assertNotNull(vo.get("finishAt"),
|
|
|
+ "finish 响应体 finishAt 不得为空");
|
|
|
+ // 附带字段:确认码/服务项/照片均需随响应带回(前端一步渲染所需)
|
|
|
+ assertNotNull(vo.get("clientConfirmCode"));
|
|
|
+ String confirmCode = String.valueOf(vo.get("clientConfirmCode"));
|
|
|
+ assertEquals(6, confirmCode.length(), "客户确认码必须 6 位");
|
|
|
+ List<?> items = (List<?>) vo.get("items");
|
|
|
+ assertEquals(1, items.size(), "服务项需随响应带回");
|
|
|
+ List<?> photos = (List<?>) vo.get("finishPhotos");
|
|
|
+ assertEquals(2, photos.size(), "完成照片需随响应带回");
|
|
|
+
|
|
|
+ // 库内一致性:响应体与库内真实状态一致(不再有「响应 done、库 arrived」的分裂)
|
|
|
+ TrainServiceOrder db = orderMapper.selectById(orderId);
|
|
|
+ assertEquals("done", db.getStatus());
|
|
|
+ assertNotNull(db.getFinishAt());
|
|
|
+ assertEquals(confirmCode, db.getClientConfirmCode());
|
|
|
+ // detail 读路径同样应读出 done(响应体 / 详情 / 库三方一致)
|
|
|
+ Result<Map<String, Object>> d = service.detail(SYN_WORKER, orderId);
|
|
|
+ assertNotNull(d);
|
|
|
+ assertEquals(200, d.getCode());
|
|
|
+ assertEquals("done", d.getData().get("status"), "detail 读出的状态也应为 done");
|
|
|
+ }
|
|
|
+}
|