|
|
@@ -0,0 +1,217 @@
|
|
|
+package com.zksy.cockpit.workorder;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.zksy.base.alarm.domain.AlarmData;
|
|
|
+import com.zksy.base.alarm.service.AlarmDataService;
|
|
|
+import com.zksy.base.domain.EquipmentBase;
|
|
|
+import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
|
+import com.zksy.base.service.EquipmentTypeService;
|
|
|
+import com.zksy.base.warning.mapper.AlarmWarningRelMapper;
|
|
|
+import com.zksy.base.warning.service.EarlyWarningService;
|
|
|
+import com.zksy.ops.domain.WorkOrder;
|
|
|
+import com.zksy.ops.domain.vo.WorkOrderLogVO;
|
|
|
+import com.zksy.ops.service.WorkOrderService;
|
|
|
+import com.zksy.system.mapper.SysDictDataMapper;
|
|
|
+import com.zksy.system.service.ISysUserService;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.apache.ibatis.session.Configuration;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 工单「处置流程」附件图片:{@code work_order_log.attach_url}(逗号分隔串)→
|
|
|
+ * {@link CockpitWorkOrderTimelineEntry#getAttachImages()} 数组。
|
|
|
+ *
|
|
|
+ * <p>断言:多个/单个/空串/null/含空白段 五种输入;既有时间线字段不变;
|
|
|
+ * 附件随同一批 log 行读出,不新增查询;JSON 形状稳定(无附件为空数组)。</p>
|
|
|
+ */
|
|
|
+class CockpitWorkOrderTimelineAttachTest {
|
|
|
+ private static final String ORDER_CODE = "WO202610281058412626";
|
|
|
+
|
|
|
+ private WorkOrderService workOrderService;
|
|
|
+ private AlarmDataService alarmDataService;
|
|
|
+ private EquipmentBaseMapper equipmentBaseMapper;
|
|
|
+ private SysDictDataMapper sysDictDataMapper;
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+ private BusinessCockpitWorkOrderReadAdapter adapter;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void setUp() {
|
|
|
+ for (Class<?> entity : Arrays.asList(EquipmentBase.class, WorkOrder.class, AlarmData.class)) {
|
|
|
+ TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new Configuration(), ""), entity);
|
|
|
+ }
|
|
|
+ workOrderService = mock(WorkOrderService.class);
|
|
|
+ alarmDataService = mock(AlarmDataService.class);
|
|
|
+ equipmentBaseMapper = mock(EquipmentBaseMapper.class);
|
|
|
+ sysDictDataMapper = mock(SysDictDataMapper.class);
|
|
|
+ sysUserService = mock(ISysUserService.class);
|
|
|
+ adapter = new BusinessCockpitWorkOrderReadAdapter(
|
|
|
+ workOrderService,
|
|
|
+ mock(EarlyWarningService.class),
|
|
|
+ mock(AlarmWarningRelMapper.class),
|
|
|
+ alarmDataService,
|
|
|
+ equipmentBaseMapper,
|
|
|
+ mock(EquipmentTypeService.class),
|
|
|
+ sysDictDataMapper,
|
|
|
+ sysUserService);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void multipleAttachmentsAreSplitTrimmedAndKeepOrder() {
|
|
|
+ List<String> images = timelineAttachImages("http://img/a.jpg,http://img/b.jpg,http://img/c.jpg");
|
|
|
+
|
|
|
+ assertEquals(3, images.size());
|
|
|
+ assertArrayEquals(new String[]{"http://img/a.jpg", "http://img/b.jpg", "http://img/c.jpg"},
|
|
|
+ images.toArray(new String[0]));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void singleAttachmentBecomesOneElementArray() {
|
|
|
+ List<String> images = timelineAttachImages("http://img/only.jpg");
|
|
|
+
|
|
|
+ assertEquals(1, images.size());
|
|
|
+ assertEquals("http://img/only.jpg", images.get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void emptyStringYieldsEmptyArrayNotFakeElement() {
|
|
|
+ assertTrue(timelineAttachImages("").isEmpty());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void nullYieldsEmptyArray() {
|
|
|
+ assertTrue(timelineAttachImages(null).isEmpty());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void blankSegmentsAreDroppedWithoutReorderingTheRest() {
|
|
|
+ List<String> images = timelineAttachImages(" http://img/a.jpg , , http://img/b.jpg ,, http://img/c.jpg ");
|
|
|
+
|
|
|
+ assertEquals(3, images.size());
|
|
|
+ assertArrayEquals(new String[]{"http://img/a.jpg", "http://img/b.jpg", "http://img/c.jpg"},
|
|
|
+ images.toArray(new String[0]));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void attachmentsComeFromTheAlreadyLoadedLogRowsWithoutExtraQueries() {
|
|
|
+ WorkOrder order = order();
|
|
|
+ stubTimeline(order, Arrays.asList(
|
|
|
+ log(11L, "处置", "http://img/before.jpg,http://img/after.jpg"),
|
|
|
+ log(12L, "提交验收", null)));
|
|
|
+
|
|
|
+ CockpitWorkOrderTimelineSnapshot snapshot = adapter.loadTimeline(ORDER_CODE);
|
|
|
+
|
|
|
+ assertNotNull(snapshot);
|
|
|
+ List<CockpitWorkOrderTimelineEntry> entries = snapshot.getTimelineEntries();
|
|
|
+ assertEquals(2, entries.size());
|
|
|
+ // operationName 取状态字典(与既有行为一致),operDesc 才是本行描述
|
|
|
+ assertEquals("处理中", entries.get(0).getOperationName());
|
|
|
+ assertEquals("处置", entries.get(0).getOperationDesc());
|
|
|
+ assertArrayEquals(new String[]{"http://img/before.jpg", "http://img/after.jpg"},
|
|
|
+ entries.get(0).getAttachImages().toArray(new String[0]));
|
|
|
+ // 无附件的操作如实空数组,不伪造占位图
|
|
|
+ assertTrue(entries.get(1).getAttachImages().isEmpty());
|
|
|
+ // 既有字段不受影响
|
|
|
+ assertEquals(ORDER_CODE, entries.get(0).getWorkOrderCode());
|
|
|
+ assertEquals("张三", entries.get(0).getOperatorName());
|
|
|
+ // 日志只取一次,附件没有引出逐条查询
|
|
|
+ verify(workOrderService, times(1)).listLogs(279L);
|
|
|
+ verify(alarmDataService, times(0)).listByIds(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void entrySerializesAttachImagesAsArrayAndKeepsExistingFieldNames() throws Exception {
|
|
|
+ CockpitWorkOrderTimelineEntry entry = new CockpitWorkOrderTimelineEntry(
|
|
|
+ 11L, ORDER_CODE, 3, "处理中", "处置", "已更换配件", "张三",
|
|
|
+ "2026-10-28T15:41:03+08:00",
|
|
|
+ Arrays.asList("http://img/before.jpg", "http://img/after.jpg"));
|
|
|
+
|
|
|
+ JsonNode json = new ObjectMapper().readTree(new ObjectMapper().writeValueAsString(entry));
|
|
|
+
|
|
|
+ assertEquals(2, json.get("attachImages").size());
|
|
|
+ assertEquals("http://img/before.jpg", json.get("attachImages").get(0).asText());
|
|
|
+ assertEquals("http://img/after.jpg", json.get("attachImages").get(1).asText());
|
|
|
+ // 既有字段名与顺序不变
|
|
|
+ assertEquals(ORDER_CODE, json.get("workOrderCode").asText());
|
|
|
+ assertEquals(3, json.get("stateCode").asInt());
|
|
|
+ assertEquals("处理中", json.get("stateName").asText());
|
|
|
+ assertEquals("处置", json.get("operationName").asText());
|
|
|
+ assertEquals("已更换配件", json.get("operationDesc").asText());
|
|
|
+ assertEquals("张三", json.get("operatorName").asText());
|
|
|
+ assertEquals("2026-10-28T15:41:03+08:00", json.get("operatedAt").asText());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void legacyConstructorWithoutAttachmentsStillWorksAndYieldsEmptyArray() {
|
|
|
+ CockpitWorkOrderTimelineEntry entry = new CockpitWorkOrderTimelineEntry(
|
|
|
+ 11L, ORDER_CODE, 0, "待派单", "创建", "创建工单", "系统",
|
|
|
+ "2026-10-28T10:58:42+08:00");
|
|
|
+
|
|
|
+ assertNotNull(entry.getAttachImages());
|
|
|
+ assertTrue(entry.getAttachImages().isEmpty());
|
|
|
+ }
|
|
|
+
|
|
|
+ // --- fixtures ---
|
|
|
+
|
|
|
+ private List<String> timelineAttachImages(String attachUrl) {
|
|
|
+ WorkOrder order = order();
|
|
|
+ stubTimeline(order, Collections.singletonList(log(11L, "处置", attachUrl)));
|
|
|
+ return adapter.loadTimeline(ORDER_CODE).getTimelineEntries().get(0).getAttachImages();
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private void stubTimeline(WorkOrder order, List<WorkOrderLogVO> logs) {
|
|
|
+ LambdaQueryChainWrapper<WorkOrder> chain = mock(LambdaQueryChainWrapper.class);
|
|
|
+ when(workOrderService.lambdaQuery()).thenReturn(chain);
|
|
|
+ when(chain.eq(any(), any())).thenReturn(chain);
|
|
|
+ when(chain.one()).thenReturn(order);
|
|
|
+ when(workOrderService.listLogs(279L)).thenReturn(logs);
|
|
|
+ when(alarmDataService.list(any(LambdaQueryWrapper.class))).thenReturn(Collections.emptyList());
|
|
|
+ when(equipmentBaseMapper.selectBatchIds(any())).thenReturn(Collections.emptyList());
|
|
|
+ when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
|
|
|
+ when(sysUserService.selectEnabledUsersByIds(any())).thenReturn(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static WorkOrder order() {
|
|
|
+ WorkOrder order = new WorkOrder();
|
|
|
+ order.setOrderId(279L);
|
|
|
+ order.setOrderNo(ORDER_CODE);
|
|
|
+ order.setDeviceId("equipment-id");
|
|
|
+ order.setOrderLevel(2);
|
|
|
+ order.setOrderStatus(3);
|
|
|
+ order.setCreateTime(Date.from(java.time.Instant.parse("2026-10-28T02:58:42Z")));
|
|
|
+ return order;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static WorkOrderLogVO log(Long logId, String operation, String attachUrl) {
|
|
|
+ WorkOrderLogVO log = new WorkOrderLogVO();
|
|
|
+ log.setLogId(logId);
|
|
|
+ log.setWorkOrderId(279L);
|
|
|
+ log.setOperType(3);
|
|
|
+ log.setOperDesc(operation);
|
|
|
+ log.setOperUserName("张三");
|
|
|
+ log.setOperTime(Date.from(java.time.Instant.parse("2026-10-28T07:41:03Z")));
|
|
|
+ log.setAttachUrl(attachUrl);
|
|
|
+ return log;
|
|
|
+ }
|
|
|
+}
|