/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.sxkj.gd.workorder.service.impl; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.secure.utils.AuthUtil; import org.springframework.beans.factory.annotation.Autowired; import org.sxkj.common.utils.SpringContextUtil; import org.sxkj.gd.workorder.entity.GdClueEventEntity; import org.sxkj.gd.workorder.entity.GdClueEventStatusFlowEntity; import org.sxkj.gd.workorder.enums.EventStatusEnum; import org.sxkj.gd.workorder.mapper.GdClueEventStatusFlowMapper; import org.sxkj.gd.workorder.service.IGdClueEventService; import org.sxkj.gd.workorder.service.IGdClueEventStatusFlowService; import org.sxkj.gd.workorder.vo.GdClueEventStatusFlowVO; import org.springframework.stereotype.Service; import org.springblade.core.mp.base.BaseServiceImpl; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 事件状态流转履历表 服务实现类 * * @author system * @since 2026-07-29 */ @Slf4j @Service @AllArgsConstructor public class GdClueEventStatusFlowServiceImpl extends BaseServiceImpl implements IGdClueEventStatusFlowService { /** * 根据事件ID查询状态流转记录列表 *

* 步骤: * 1. 参数校验 * 2. 查询状态流转记录列表 * 3. 补充状态名称 *

* * @param eventId 事件ID * @return 状态流转记录列表 */ @Override public List getStatusFlowsByEventId(Long eventId) { // 1. 参数校验 if (eventId == null) { throw new RuntimeException("事件ID不能为空"); } // 2. 查询状态流转记录列表 List flows = baseMapper.selectStatusFlowsByEventId(eventId); // 3. 补充状态名称 if (flows != null && !flows.isEmpty()) { flows.forEach(flow -> { // 补充变更前状态名称 if (flow.getOldStatus() != null) { flow.setOldStatusName(EventStatusEnum.getNameByCode(flow.getOldStatus())); } // 补充变更后状态名称 if (flow.getNewStatus() != null) { flow.setNewStatusName(EventStatusEnum.getNameByCode(flow.getNewStatus())); } }); } return flows; } /** * 插入状态流转记录 *

* 步骤: * 1. 参数校验 * 2. 构建状态流转记录实体 * 3. 保存状态流转记录 *

* * @param eventId 事件ID * @param oldStatus 变更前状态 * @param newStatus 变更后状态 * @param operateContent 操作备注 * @param operateTime 操作时间 * @return 是否成功 */ @Override public boolean insertStatusFlow(Long eventId, Integer oldStatus, Integer newStatus, String operateContent, Date operateTime) { // 1. 参数校验 if (eventId == null) { throw new RuntimeException("事件ID不能为空"); } if (newStatus == null) { throw new RuntimeException("目标状态不能为空"); } // 2. 构建状态流转记录实体 GdClueEventStatusFlowEntity flowEntity = new GdClueEventStatusFlowEntity(); flowEntity.setEventId(eventId); flowEntity.setOldStatus(oldStatus); flowEntity.setNewStatus(newStatus); flowEntity.setOperateContent(operateContent); flowEntity.setOperateTime(operateTime != null ? operateTime : new Date()); flowEntity.setOperateUser(AuthUtil.getUserId()); flowEntity.setOperateDept(Long.valueOf(AuthUtil.getDeptId())); flowEntity.setCreateUser(AuthUtil.getUserId()); flowEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId())); flowEntity.setCreateTime(new Date()); // 3. 保存状态流转记录 return save(flowEntity); } /** * 根据事件ID查询状态时间线 *

* 步骤: * 1. 参数校验 * 2. 查询事件当前状态 * 3. 查询状态流转记录 * 4. 构建已到达状态集合 * 5. 生成完整的状态时间线 *

* * @param eventId 事件ID * @return 状态时间线列表(按状态编码升序) */ @Override public List getStatusTimeline(Long eventId) { // 1. 参数校验 if (eventId == null) { throw new RuntimeException("事件ID不能为空"); } // 2. 查询事件当前状态 IGdClueEventService gdClueEventService = SpringContextUtil.getBean(IGdClueEventService.class); GdClueEventEntity event = gdClueEventService.getById(eventId); if (event == null) { throw new RuntimeException("事件不存在"); } // 3. 查询状态流转记录 List flows = baseMapper.selectStatusFlowsByEventId(eventId); // 4. 构建每个状态的最后流转记录 // key: 状态编码, value: 最后的流转记录 Map lastFlowMap = new HashMap<>(); if (flows != null && !flows.isEmpty()) { for (GdClueEventStatusFlowVO flow : flows) { // 记录目标状态的最后流转 if (flow.getNewStatus() != null) { lastFlowMap.put(flow.getNewStatus(), flow); } // 记录源状态的最后流转 if (flow.getOldStatus() != null) { lastFlowMap.put(flow.getOldStatus(), flow); } } } // 5. 生成完整的状态时间线 List timeline = new java.util.ArrayList<>(); for (Integer statusCode : EventStatusEnum.getAllStatusCodes()) { GdClueEventStatusFlowVO item = new GdClueEventStatusFlowVO(); // 使用 newStatus 字段存储当前状态编码 item.setNewStatus(statusCode); item.setNewStatusName(EventStatusEnum.getNameByCode(statusCode)); GdClueEventStatusFlowVO lastFlow = lastFlowMap.get(statusCode); if (lastFlow != null) { // 根据最后流转记录判断是否已到达 // 如果目标是该状态,则已到达 // 如果源是该状态(目标不是),则未到达(已离开) boolean isReached = lastFlow.getNewStatus().equals(statusCode); item.setIsReached(isReached); if (isReached) { // 已到达状态:设置操作信息 item.setOperateUser(lastFlow.getOperateUser()); item.setOperateUserName(lastFlow.getOperateUserName()); item.setOperateDept(lastFlow.getOperateDept()); item.setOperateDeptName(lastFlow.getOperateDeptName()); item.setOperateContent(lastFlow.getOperateContent()); item.setOperateTime(lastFlow.getOperateTime()); } } else { // 没有流转记录,未到达 item.setIsReached(false); } timeline.add(item); } return timeline; } }