songhuangfeng123
2022-07-21 ab78424bf520cbe8f2eab8dde235baa82c660e19
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.gkhy.safePlatform.incidentManage.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.incidentManage.entity.*;
import com.gkhy.safePlatform.incidentManage.enums.AccidentResultCodes;
import com.gkhy.safePlatform.incidentManage.exception.AccidentException;
import com.gkhy.safePlatform.incidentManage.model.dto.req.AccidentReportFileReqDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.req.AccidentReportReqDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.AccidentReportDetailRespDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.AccidentReportFileRespDTO;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.AccidentReportPageRespDTO;
import com.gkhy.safePlatform.incidentManage.query.AccidentReportQuery;
import com.gkhy.safePlatform.incidentManage.query.db.AccidentReportDBQuery;
import com.gkhy.safePlatform.incidentManage.service.AccidentReportService;
import com.gkhy.safePlatform.incidentManage.service.baseService.AccidentReportFileInfoService;
import com.gkhy.safePlatform.incidentManage.service.baseService.AccidentReportInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service("accidentReportService")
public class AccidentReportServiceImpl implements AccidentReportService {
 
    @Autowired
    private AccidentReportInfoService accidentReportInfoService;
 
    @Autowired
    private AccidentReportFileInfoService accidentReportFileInfoService;
 
 
    @Override
    public SearchResultVO<List<AccidentReportPageRespDTO>> selectAccidentReportList(PageQuery<AccidentReportQuery> query) {
        Long pageIndex = query.getPageIndex();
        Long pageSize = query.getPageSize();
        Page<AccidentReportInfoPageDO> page = new Page<>(pageIndex, pageSize);
 
        AccidentReportDBQuery accidentReportDBQuery = new AccidentReportDBQuery();
        if (query.getSearchParams() != null) {
            BeanUtils.copyProperties(query.getSearchParams(), accidentReportDBQuery);
        }
 
 
        List<AccidentReportInfoPageDO> accidentReportInfoPageDOList = accidentReportInfoService.selectAccidentReportList(page, accidentReportDBQuery);
        List<AccidentReportPageRespDTO> respList = BeanCopyUtils.copyBeanList(accidentReportInfoPageDOList, AccidentReportPageRespDTO.class);
 
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,
                page.getTotal(),
                respList,
                ResultCodes.OK
        );
    }
 
    @Override
    public ResultVO addAccidentReport(Long uid, AccidentReportReqDTO accidentReportReqDTO) {
        //必填项验证
        checkRequired(accidentReportReqDTO);
 
        Date nowDate = new Date();
        //1.新增事故报告
        AccidentReportInfo accidentReportInfo = new AccidentReportInfo();
        BeanUtils.copyProperties(accidentReportReqDTO, accidentReportInfo);
        accidentReportInfo.setDelFlag(false);
        accidentReportInfo.setCreateUid(uid);
        accidentReportInfo.setGmtCreate(nowDate);
        accidentReportInfo.setStatus(false);
        accidentReportInfoService.addAccidentReport(accidentReportInfo);
        //2.新增事故报告附件
        if (!CollectionUtils.isEmpty(accidentReportReqDTO.getFileList())){
            addAccidentReportFile(accidentReportInfo.getId(),uid,nowDate,accidentReportReqDTO.getFileList());
        }
        return new ResultVO(ResultCodes.OK);
    }
 
    private void  addAccidentReportFile(Long accidentReportId ,Long uid , Date nowDate , List<AccidentReportFileReqDTO> AccidentReportFileReqDTOList){
        List<AccidentReportFileInfo> fileInfoList = BeanCopyUtils.copyBeanList(AccidentReportFileReqDTOList, AccidentReportFileInfo.class);
        fileInfoList.forEach(AccidentReportFileInfo -> {
            AccidentReportFileInfo.setAccidentReportId(accidentReportId);
            AccidentReportFileInfo.setDelFlag(false);
            AccidentReportFileInfo.setCreateUid(uid);
            AccidentReportFileInfo.setGmtCreate(nowDate);
        });
        for (AccidentReportFileInfo AccidentReportFileInfo :fileInfoList){
            accidentReportFileInfoService.addAccidentReportFile(AccidentReportFileInfo);
        }
    }
 
    @Override
    public ResultVO<AccidentReportDetailRespDTO> getAccidentReportById(Long id) {
        AccidentReportDetailRespDTO AccidentReportDetailRespDTO = new AccidentReportDetailRespDTO();
        //查询是否存在
        AccidentReportInfoDetailDO AccidentReportInfoDetailDO = accidentReportInfoService.selectAccidentReportById(id);
        if (AccidentReportInfoDetailDO==null){
            throw new AccidentException(AccidentResultCodes.ACCIDENT_REPORT_NOT_EXIST);
        }else{
            BeanUtils.copyProperties(AccidentReportInfoDetailDO,AccidentReportDetailRespDTO);
            //查找对应的附件
            List<AccidentReportFileInfoDO> AccidentReportFileInfoDOList = accidentReportFileInfoService.selectByAccidentReportId(id);
            if (!CollectionUtils.isEmpty(AccidentReportFileInfoDOList)){
                List<AccidentReportFileRespDTO> accidentReportFileRespDTOList = BeanCopyUtils.copyBeanList(AccidentReportFileInfoDOList , AccidentReportFileRespDTO.class);
                AccidentReportDetailRespDTO.setFileList(accidentReportFileRespDTOList);
            }
            return new ResultVO<>(ResultCodes.OK ,AccidentReportDetailRespDTO);
        }
    }
 
    @Override
    public ResultVO updateAccidentReport(Long uid, AccidentReportReqDTO accidentReportReqDTO) {
        Date nowDate = new Date();
        //查询是否存在
        AccidentReportInfoDetailDO AccidentReportInfoDetailDO = accidentReportInfoService.selectAccidentReportById(accidentReportReqDTO.getId());
        if (AccidentReportInfoDetailDO==null){
            throw new AccidentException(AccidentResultCodes.ACCIDENT_REPORT_NOT_EXIST);
        }else{
            AccidentReportInfo accidentReportInfo = new AccidentReportInfo();
            BeanUtils.copyProperties(accidentReportReqDTO,accidentReportInfo);
            accidentReportInfo.setUpdateUid(uid);
            accidentReportInfo.setGmtModitify(nowDate);
            accidentReportInfoService.updateAccidentReport(accidentReportInfo);
            //修改事故报告附件
            updateAccidentReportFile(uid,accidentReportReqDTO.getId(),nowDate,accidentReportReqDTO.getFileList());
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    private void updateAccidentReportFile(Long uid ,Long accidentReportId ,Date nowDate,List<AccidentReportFileReqDTO> AccidentReportFileReqDTOList){
 
        List<AccidentReportFileInfoDO> accidentReportFileInfoDOList = accidentReportFileInfoService.selectByAccidentReportId(accidentReportId);
        List<Long> oldIdsList = accidentReportFileInfoDOList.stream().map(AccidentReportFileInfoDO::getId).collect(Collectors.toList());
        List<Long> newIdsList = new ArrayList<>();
 
        //新增的附件集合
        List<AccidentReportFileInfo> addList = new ArrayList<>();
        //删除的附件集合(id)
        List<Long> deleteList = new ArrayList<>();
        for (AccidentReportFileReqDTO AccidentReportFileReqDTO : AccidentReportFileReqDTOList){
            //如果不存在id则表示页面新增的附件
            if (AccidentReportFileReqDTO.getId() == null){
                AccidentReportFileInfo AccidentReportFileInfo = new AccidentReportFileInfo();
                BeanUtils.copyProperties(AccidentReportFileReqDTO,AccidentReportFileInfo);
                AccidentReportFileInfo.setDelFlag(false);
                AccidentReportFileInfo.setGmtCreate(nowDate);
                AccidentReportFileInfo.setCreateUid(uid);
                AccidentReportFileInfo.setAccidentReportId(accidentReportId);
                addList.add(AccidentReportFileInfo);
            }
            //如果存在id则判断页面是否删除
            else{
                newIdsList.add(AccidentReportFileReqDTO.getId());
            }
        }
        for (Long oldId : oldIdsList){
            if (!newIdsList.contains(oldId)){
                deleteList.add(oldId);
            }
        }
        if (!CollectionUtils.isEmpty(addList)){
            for (AccidentReportFileInfo AccidentReportFileInfo : addList){
                accidentReportFileInfoService.addAccidentReportFile(AccidentReportFileInfo);
            }
        }
        if (!CollectionUtils.isEmpty(deleteList)){
            accidentReportFileInfoService.deleteAccidentReportFileByIds(deleteList);
        }
    }
 
 
    @Override
    public ResultVO batchDeleteAccidentReport(String ids) {
        if (StringUtils.isBlank(ids)){
            throw new AccidentException(AccidentResultCodes.ACCIDENT_REPORT_NULL);
        }else{
            String[] idArr = ids.split(",");
            for (String id : idArr) {
                deleteAccidentReport(Long.valueOf(id));
            }
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    private void deleteAccidentReport(Long id) {
        //查询是否存在
        AccidentReportInfoDetailDO AccidentReportInfoDetailDO = accidentReportInfoService.selectAccidentReportById(id);
        if (AccidentReportInfoDetailDO==null){
            throw new AccidentException(AccidentResultCodes.ACCIDENT_REPORT_NOT_EXIST);
        }else{
            accidentReportInfoService.deleteAccidentReportById(id);
            //删除附件
            accidentReportFileInfoService.deleteAccidentReportFileByAccidentReportId(id);
        }
    }
 
 
 
 
    /**
     * 验证必填项
     * @return
     */
    private void checkRequired(AccidentReportReqDTO AccidentReportReqDTO) {
        //事故快报
        if (AccidentReportReqDTO.getAccidentExpressId() == null ) {
            throw new AccidentException(AccidentResultCodes.ACCIDENT_EXPRESS_NULL);
        }
        //事故类型
        if (StringUtils.isBlank(AccidentReportReqDTO.getAccidentType())) {
            throw new AccidentException(AccidentResultCodes.REPORT_TYPE_NULL);
        }
        //事故等级
        if (StringUtils.isBlank(AccidentReportReqDTO.getAccidentGrade())) {
            throw new AccidentException(AccidentResultCodes.REPORT_GRADE_NULL);
        }
        //经济损失
        if (AccidentReportReqDTO.getEconomicLoss() == null ) {
            throw new AccidentException(AccidentResultCodes.REPORT_ECONOMIC_LOSS_NULL);
        }
        //要求报告完成期限
        if (AccidentReportReqDTO.getReportDeadline() == null ) {
            throw new AccidentException(AccidentResultCodes.REPORT_REPORT_DEADLINE_NULL);
        }
        //事故级别
        if (StringUtils.isBlank(AccidentReportReqDTO.getAccidentLevel())) {
            throw new AccidentException(AccidentResultCodes.REPORT_LEVEL_NULL);
        }
        //原因综合分析(直接)
        if (StringUtils.isBlank(AccidentReportReqDTO.getComprehensiveAnalysisDirect())) {
            throw new AccidentException(AccidentResultCodes.REPORT_COMPREHENSIVE_ANALYSIS_DIRECT_NULL);
        }
        //原因综合分析(直接)
        if (StringUtils.isBlank(AccidentReportReqDTO.getComprehensiveAnalysisDirect())) {
            throw new AccidentException(AccidentResultCodes.REPORT_COMPREHENSIVE_ANALYSIS_DIRECT_NULL);
        }
        //原因综合分析(间接)
        if (StringUtils.isBlank(AccidentReportReqDTO.getComprehensiveAnalysisIndirect())) {
            throw new AccidentException(AccidentResultCodes.REPORT_COMPREHENSIVE_ANALYSIS_INDIRECT_NULL);
        }
        //整改措施
        if (StringUtils.isBlank(AccidentReportReqDTO.getRectificationMeasures())) {
            throw new AccidentException(AccidentResultCodes.REPORT_RECTIFICATION_MEASURES_NULL);
        }
        //事故处理
        if (StringUtils.isBlank(AccidentReportReqDTO.getAccidentHandling())) {
            throw new AccidentException(AccidentResultCodes.REPORT_ACCIDENT_HANDLING_NULL);
        }
        //填写人
        if (AccidentReportReqDTO.getFillInUserUid() == null ) {
            throw new AccidentException(AccidentResultCodes.REPORT_FILL_IN_USER_UID_NULL);
        }
        //填写日期
        if (AccidentReportReqDTO.getFillInTime() == null ) {
            throw new AccidentException(AccidentResultCodes.REPORT_FILL_IN_TIME_NULL);
        }
    }
 
}