吉安感知网项目-后端
linwei
4 days ago d032522d3b4fcd78d5fe5cb32f50858d86fd81d1
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
/*
 *      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.springblade.core.tool.utils.Func;
import org.sxkj.gd.workorder.entity.GdClueDisposeRecordEntity;
import org.sxkj.gd.workorder.entity.GdClueEventEntity;
import org.sxkj.gd.workorder.enums.EventStatusEnum;
import org.sxkj.gd.workorder.mapper.GdClueDisposeRecordMapper;
import org.sxkj.gd.workorder.param.GdClueDisposeRecordAddParam;
import org.sxkj.gd.workorder.service.IGdClueDisposeRecordService;
import org.sxkj.gd.workorder.service.IGdClueEventService;
import org.sxkj.gd.workorder.service.IGdClueEventStatusFlowService;
import org.sxkj.gd.workorder.vo.GdClueDisposeRecordVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springblade.core.mp.base.BaseServiceImpl;
 
import java.util.Date;
import java.util.List;
 
/**
 * 事件处置流水记录表 服务实现类
 *
 * @author system
 * @since 2026-07-29
 */
@Slf4j
@Service
@AllArgsConstructor
public class GdClueDisposeRecordServiceImpl extends BaseServiceImpl<GdClueDisposeRecordMapper, GdClueDisposeRecordEntity> implements IGdClueDisposeRecordService {
 
    private final IGdClueEventService gdClueEventService;
    private final IGdClueEventStatusFlowService gdClueEventStatusFlowService;
 
    /**
     * 根据事件ID查询处置记录列表
     * <p>
     * 步骤:
     * 1. 参数校验
     * 2. 查询处置记录列表
     * 3. 补充状态名称
     * </p>
     *
     * @param eventId 事件ID
     * @return 处置记录列表
     */
    @Override
    public List<GdClueDisposeRecordVO> getDisposeRecordsByEventId(Long eventId) {
        // 1. 参数校验
        if (eventId == null) {
            throw new RuntimeException("事件ID不能为空");
        }
 
        // 2. 查询处置记录列表
        List<GdClueDisposeRecordVO> records = baseMapper.selectDisposeRecordsByEventId(eventId);
 
        // 3. 补充状态名称和类型名称
        if (Func.isNotEmpty(records)) {
            records.forEach(record -> {
                // 补充记录类型名称
                if (record.getRecordType() != null) {
                    record.setRecordTypeName(record.getRecordType() == 0 ? "核查记录" : "处置记录");
                }
                // 补充处置状态名称
                if (record.getDisposeStatus() != null) {
                    record.setDisposeStatusName(EventStatusEnum.getNameByCode(record.getDisposeStatus()));
                }
            });
        }
 
        return records;
    }
 
    /**
     * 新增处置记录
     * <p>
     * 步骤:
     * 1. 参数校验
     * 2. 查询事件信息
     * 3. 如果需要同步更新事件状态,则更新事件状态
     * 4. 构建处置记录实体
     * 5. 保存处置记录
     * </p>
     *
     * @param addParam 新增参数
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDisposeRecord(GdClueDisposeRecordAddParam addParam) {
        // 1. 参数校验
        if (addParam == null || addParam.getEventId() == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (addParam.getRecordType() == null) {
            throw new RuntimeException("记录类型不能为空");
        }
 
        // 2. 查询事件信息
        GdClueEventEntity eventEntity = gdClueEventService.getById(addParam.getEventId());
        if (eventEntity == null || (eventEntity.getIsDeleted() != null && eventEntity.getIsDeleted() != 0)) {
            throw new RuntimeException("事件不存在");
        }
 
        Integer oldStatus = eventEntity.getEventStatus();
 
        // 3. 如果需要同步更新事件状态,则更新事件状态
        if (addParam.getUpdateEventStatus() != null && addParam.getUpdateEventStatus()
                && addParam.getTargetEventStatus() != null) {
            // 校验目标状态合法性
            if (!EventStatusEnum.isValidCode(addParam.getTargetEventStatus())) {
                throw new RuntimeException("目标状态编码无效");
            }
 
            // 校验状态流转合法性
            if (!EventStatusEnum.isValidTransition(oldStatus, addParam.getTargetEventStatus())) {
                String oldStatusName = EventStatusEnum.getNameByCode(oldStatus);
                String newStatusName = EventStatusEnum.getNameByCode(addParam.getTargetEventStatus());
                throw new RuntimeException(String.format("状态流转非法:不允许从[%s]流转到[%s]", oldStatusName, newStatusName));
            }
 
            // 更新事件状态
            eventEntity.setEventStatus(addParam.getTargetEventStatus());
            eventEntity.setUpdateUser(AuthUtil.getUserId());
            eventEntity.setUpdateTime(new Date());
            if (!gdClueEventService.updateById(eventEntity)) {
                throw new RuntimeException("更新事件状态失败");
            }
 
            // 插入状态流转记录
            gdClueEventStatusFlowService.insertStatusFlow(
                    addParam.getEventId(),
                    oldStatus,
                    addParam.getTargetEventStatus(),
                    addParam.getDisposeContent(),
                    new Date()
            );
        }
 
        // 4. 构建处置记录实体
        GdClueDisposeRecordEntity recordEntity = new GdClueDisposeRecordEntity();
        recordEntity.setEventId(addParam.getEventId());
        recordEntity.setRecordType(addParam.getRecordType());
        recordEntity.setDisposeContent(addParam.getDisposeContent());
        recordEntity.setAttachUrl(addParam.getAttachUrl());
        recordEntity.setOperateTime(new Date());
        recordEntity.setDisposeStatus(eventEntity.getEventStatus());
        recordEntity.setDisposeUser(AuthUtil.getUserId());
        recordEntity.setDisposeDept(Long.valueOf(AuthUtil.getDeptId()));
        recordEntity.setCreateUser(AuthUtil.getUserId());
        recordEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
        recordEntity.setCreateTime(new Date());
 
        // 5. 保存处置记录
        return save(recordEntity);
    }
 
}