吉安感知网项目-后端
linwei
3 days ago 43214587a294fbcef392aaf90f21e38e5d5e39ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 *      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.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<GdClueEventStatusFlowMapper, GdClueEventStatusFlowEntity> implements IGdClueEventStatusFlowService {
 
    private final IGdClueEventService gdClueEventService;
 
    /**
     * 根据事件ID查询状态流转记录列表
     * <p>
     * 步骤:
     * 1. 参数校验
     * 2. 查询状态流转记录列表
     * 3. 补充状态名称
     * </p>
     *
     * @param eventId 事件ID
     * @return 状态流转记录列表
     */
    @Override
    public List<GdClueEventStatusFlowVO> getStatusFlowsByEventId(Long eventId) {
        // 1. 参数校验
        if (eventId == null) {
            throw new RuntimeException("事件ID不能为空");
        }
 
        // 2. 查询状态流转记录列表
        List<GdClueEventStatusFlowVO> 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;
    }
 
    /**
     * 插入状态流转记录
     * <p>
     * 步骤:
     * 1. 参数校验
     * 2. 构建状态流转记录实体
     * 3. 保存状态流转记录
     * </p>
     *
     * @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查询状态时间线
     * <p>
     * 步骤:
     * 1. 参数校验
     * 2. 查询事件当前状态
     * 3. 查询状态流转记录
     * 4. 构建已到达状态集合
     * 5. 生成完整的状态时间线
     * </p>
     *
     * @param eventId 事件ID
     * @return 状态时间线列表(按状态编码升序)
     */
    @Override
    public List<GdClueEventStatusFlowVO> getStatusTimeline(Long eventId) {
        // 1. 参数校验
        if (eventId == null) {
            throw new RuntimeException("事件ID不能为空");
        }
 
        // 2. 查询事件当前状态
        GdClueEventEntity event = gdClueEventService.getById(eventId);
        if (event == null) {
            throw new RuntimeException("事件不存在");
        }
 
        // 3. 查询状态流转记录
        List<GdClueEventStatusFlowVO> flows = baseMapper.selectStatusFlowsByEventId(eventId);
 
        // 4. 构建每个状态的最后流转记录
        // key: 状态编码, value: 最后的流转记录
        Map<Integer, GdClueEventStatusFlowVO> 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<GdClueEventStatusFlowVO> 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;
    }
 
}