16639036659
2023-08-28 8f76ba99a6d20fb5ca45b6029a47d59cb8efe7bb
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package com.gkhy.labRiskManage.application.experiment.service.impl;
 
import com.gkhy.labRiskManage.api.controller.experiment.dto.req.*;
import com.gkhy.labRiskManage.api.controller.experiment.dto.resp.ExperimentAndEmergencyRespDTO;
import com.gkhy.labRiskManage.application.experiment.converter.ExperimentAppConverter;
import com.gkhy.labRiskManage.application.experiment.dto.bo.*;
import com.gkhy.labRiskManage.application.experiment.dto.dto.*;
import com.gkhy.labRiskManage.application.experiment.service.ExperimentAppService;
import com.gkhy.labRiskManage.application.riskReport.dto.dto.ReportAppQueryDTO;
import com.gkhy.labRiskManage.commons.domain.SearchResult;
import com.gkhy.labRiskManage.commons.enums.ResultCode;
import com.gkhy.labRiskManage.commons.enums.StatusEnum;
import com.gkhy.labRiskManage.commons.exception.BusinessException;
import com.gkhy.labRiskManage.commons.model.PageQuery;
import com.gkhy.labRiskManage.commons.utils.BeanCopyUtils;
import com.gkhy.labRiskManage.domain.experiment.entity.ExperimentAssessLog;
import com.gkhy.labRiskManage.domain.experiment.entity.ExperimentInfo;
import com.gkhy.labRiskManage.domain.experiment.enums.ExperimentStatusEnum;
import com.gkhy.labRiskManage.domain.experiment.enums.ExperimentTagEnum;
import com.gkhy.labRiskManage.domain.experiment.model.bo.*;
import com.gkhy.labRiskManage.domain.experiment.enums.HazardousWasteEnum;
import com.gkhy.labRiskManage.domain.experiment.enums.TimeoutEnum;
import com.gkhy.labRiskManage.domain.experiment.model.dto.ExperimentInfoDTO;
import com.gkhy.labRiskManage.domain.experiment.service.*;
import com.gkhy.labRiskManage.domain.riskReport.service.ReportRiskAssessInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class ExperimentAppServiceImpl implements ExperimentAppService {
    @Autowired
    private ExperimentInfoService experimentInfoService;
    @Autowired
    private ExperimentHazardousWasteService experimentHazardousWasteService;
    @Autowired
    private ExperimentAndStuffService experimentAndStuffService;
    @Autowired
    private ExperimentAndDeviceService experimentAndDeviceService;
    @Autowired
    private ExperimentAndPersonService experimentAndPersonService;
    @Autowired
    private ReportRiskAssessInfoService reportRiskAssessInfoService;
    @Autowired
    private ExperimentAndSiteService experimentAndSiteService;
    @Autowired
    private ExperimentAndEmergencyService experimentAndEmergencyService;
    @Autowired
    private ExperimentAppConverter experimentAppConverter;
    @Autowired
    private ExperimentAssessLogService experimentAssessLogService;
    @Transactional
    @Override
    public int save(Long currentUserId, ExperimentInsertReqBO experimentInsertReqBO) {
 
        //实验基础信息
        ExperimentAppInsertBO experimentAppInsertBO = new ExperimentAppInsertBO();
        BeanUtils.copyProperties(experimentInsertReqBO,experimentAppInsertBO);
        ExperimentInfoDTO experimentInfoDTO = experimentInfoService.save(currentUserId,experimentAppInsertBO);
 
        //设备中间表
        List<ExperimentAndDeviceAppInsertBO> deviceAppInsertBOList = experimentAppConverter.getDeviceInsertBOList(experimentInsertReqBO.getDeviceList(),experimentInfoDTO.getId());
        //材料中间表
        List<ExperimentAndStuffAppInsertBO> stuffInsertBOList = experimentAppConverter.getStuffInsertBOList(experimentInsertReqBO.getStuffList(),experimentInfoDTO.getId());
        //实验人员
        List<Long> personIds = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentInsertReqBO.getPersons())){
            personIds = experimentInsertReqBO.getPersons().stream().map(ExperimentAndPersonInsertReqBO::getPersonId).collect(Collectors.toList());
        }
        //实验地点中间表
        List<Long> siteIds = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentInsertReqBO.getSiteList())){
            siteIds = experimentInsertReqBO.getSiteList().stream().map(ExperimentAndSiteInsertReqBO::getSiteId).collect(Collectors.toList());
        }
        //危废中间表
        List<ExperimentHazardousWasteAppInsertBO> hazardousWasteInsertBOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentAppInsertBO.getHazardousWasteList())){
            hazardousWasteInsertBOList = experimentAppConverter.getHazardousWasteInsertBOList(experimentInsertReqBO.getHazardousWasteList(),experimentInfoDTO.getId());
        }
        //应急演练
        List<ExperimentAndEmergencyAppInsertBO> experimentAndEmergencyAppInsertBOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentInsertReqBO.getEmergencyList())){
            experimentAndEmergencyAppInsertBOList = experimentAppConverter.getExperimentAndEmergencyAppInsertBOList(experimentInsertReqBO.getEmergencyList(), experimentInfoDTO.getId());
        }
        experimentAndDeviceService.saveBatch(currentUserId,deviceAppInsertBOList);
        experimentAndStuffService.saveBatch(currentUserId,stuffInsertBOList);
        experimentAndPersonService.saveBatch(currentUserId,personIds,experimentInfoDTO.getId());
        experimentAndSiteService.saveBatch(currentUserId, siteIds, experimentInfoDTO.getId());
        experimentHazardousWasteService.saveBatch(currentUserId,hazardousWasteInsertBOList);
        experimentAndEmergencyService.saveBatch(currentUserId,experimentAndEmergencyAppInsertBOList);
        Integer code = StatusEnum.SUCCESS.getCode();
        if(ObjectUtils.isEmpty(experimentInfoDTO)){
            code = StatusEnum.FAIL.getCode();
        }
        return code;
    }
    @Transactional
    @Override
    public int developSave(Long currentUserId, ExperimentInsertReqBO experimentInsertReqBO) {
        //实验基础信息
        ExperimentAppInsertBO experimentAppInsertBO = new ExperimentAppInsertBO();
        BeanUtils.copyProperties(experimentInsertReqBO,experimentAppInsertBO);
        ExperimentInfoDTO experimentInfoDTO = experimentInfoService.developSave(currentUserId,experimentAppInsertBO);
 
        //设备中间表
        List<ExperimentAndDeviceAppInsertBO> deviceAppInsertBOList = experimentAppConverter.getDeviceInsertBOList(experimentInsertReqBO.getDeviceList(),experimentInfoDTO.getId());
        //材料中间表
        List<ExperimentAndStuffAppInsertBO> stuffInsertBOList = experimentAppConverter.getStuffInsertBOList(experimentInsertReqBO.getStuffList(),experimentInfoDTO.getId());
        //实验人员
        List<Long> personIds = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentInsertReqBO.getPersons())){
            personIds = experimentInsertReqBO.getPersons().stream().map(ExperimentAndPersonInsertReqBO::getPersonId).collect(Collectors.toList());
        }
        //实验地点中间表
        List<Long> siteIds = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentInsertReqBO.getSiteList())){
            siteIds = experimentInsertReqBO.getSiteList().stream().map(ExperimentAndSiteInsertReqBO::getSiteId).collect(Collectors.toList());
        }
        //危废中间表
        List<ExperimentHazardousWasteAppInsertBO> hazardousWasteInsertBOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentAppInsertBO.getHazardousWasteList())){
            hazardousWasteInsertBOList = experimentAppConverter.getHazardousWasteInsertBOList(experimentInsertReqBO.getHazardousWasteList(),experimentInfoDTO.getId());
        }
        //应急演练
        List<ExperimentAndEmergencyAppInsertBO> experimentAndEmergencyAppInsertBOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(experimentInsertReqBO.getEmergencyList())){
            experimentAndEmergencyAppInsertBOList = experimentAppConverter.getExperimentAndEmergencyAppInsertBOList(experimentInsertReqBO.getEmergencyList(), experimentInfoDTO.getId());
        }
 
        experimentAndDeviceService.saveBatch(currentUserId,deviceAppInsertBOList);
        experimentAndStuffService.saveBatch(currentUserId,stuffInsertBOList);
        experimentAndPersonService.saveBatch(currentUserId,personIds,experimentInfoDTO.getId());
        experimentAndSiteService.saveBatch(currentUserId, siteIds, experimentInfoDTO.getId());
        experimentHazardousWasteService.saveBatch(currentUserId,hazardousWasteInsertBOList);
        experimentAndEmergencyService.saveBatch(currentUserId,experimentAndEmergencyAppInsertBOList);
        Integer code = StatusEnum.SUCCESS.getCode();
        if(ObjectUtils.isEmpty(experimentInfoDTO)){
            code = StatusEnum.FAIL.getCode();
        }
        return code;
    }
    @Transactional
    @Override
    public int rectifySave(Long currentUserId, ExperimentInsertReqBO experimentInsertReqBO) {
        //修该原有实验已整改
        boolean flag = experimentInfoService.updateRectify(experimentInsertReqBO.getId());
        //新建
        int save = this.save(currentUserId, experimentInsertReqBO);
        if(flag && save == StatusEnum.SUCCESS.getCode()){
            return StatusEnum.SUCCESS.getCode();
        }
        return StatusEnum.FAIL.getCode();
    }
 
    @Override
    public SearchResult<List<ExperimentInfoAppQueryDTO>> applyEvaluationListByPage(PageQuery<ExperimentInfoAppQueryBO> appPageQuery,Long currentUserId) {
        PageQuery<ExperimentInfoQueryBO> pageQuery = new PageQuery<>();
        pageQuery.setPageSize(appPageQuery.getPageSize());
        pageQuery.setPageIndex(appPageQuery.getPageIndex());
        ExperimentInfoQueryBO queryBO = new ExperimentInfoQueryBO();
        if(!ObjectUtils.isEmpty(appPageQuery.getSearchParams())){
            BeanUtils.copyProperties(appPageQuery.getSearchParams(),queryBO);
        }
        queryBO.setStatus(ExperimentStatusEnum.YES_APPLY.getValue());
        pageQuery.setSearchParams(queryBO);
        SearchResult<List<ExperimentInfoDTO>> searchResult = experimentInfoService.listByPage(pageQuery,currentUserId);
        SearchResult<List<ExperimentInfoAppQueryDTO>> result = new SearchResult<>();
        if(!ObjectUtils.isEmpty(searchResult)){
            BeanUtils.copyProperties(searchResult,result);
        }
        List<ExperimentInfoDTO> experimentInfoDTOS = (List<ExperimentInfoDTO>)searchResult.getData();
        for (ExperimentInfoDTO experimentInfo : experimentInfoDTOS) {
            List<ExperimentAssessLog> assessLogs = experimentAssessLogService.getAssessLogs(experimentInfo.getExperimentCode());
            if (assessLogs.size() > 0){
                experimentInfo.setExperimentAssessLogs(assessLogs);
            }
        }
 
        result.setData(getExperimentInfoAppQueryDTO(experimentInfoDTOS));
        //获取分页数据
        return result;
    }
 
    /**
     * 删除
     *
     * @param id
     * @param currentUserId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(Long id, Long currentUserId) {
        experimentInfoService.deleteById(id,currentUserId);
        experimentAndDeviceService.deleteByExperimentId(id,currentUserId);
        experimentHazardousWasteService.deleteByExperimentId(id,currentUserId);
        experimentAndPersonService.deleteByExperimentId(id,currentUserId);
        experimentAndStuffService.deleteByExperimentId(id,currentUserId);
        experimentAndSiteService.deleteByExperimentId(id);
        experimentAndEmergencyService.deleteByExperimentId(id);
        return StatusEnum.SUCCESS.getCode();
    }
 
    @Override
    public int updateDevelop(Long currentUserId, List<ExperimentAppDevelopUpdateBO> appDevelopUpdateBOList) {
        int code = StatusEnum.SUCCESS.getCode();
        List<ExperimentDevelopUpdateBO> developUpdateBOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(appDevelopUpdateBOList)){
            developUpdateBOList = BeanCopyUtils.copyBeanList(appDevelopUpdateBOList,ExperimentDevelopUpdateBO.class);
        }
        boolean flag = experimentInfoService.updateDevelop(currentUserId,developUpdateBOList);
        if(!flag){
            code = StatusEnum.FAIL.getCode();
        }
        return code;
    }
 
    @Override
    public int applyEvaluation(Long currentUserId, List<Long> ids) {
        int code = StatusEnum.SUCCESS.getCode();
        boolean flag = experimentInfoService.applyEvaluation(currentUserId,ids);
        if(!flag){
            code = StatusEnum.FAIL.getCode();
        }
        return code;
    }
    @Override
    public int revokeApplyEvaluation(Long currentUserId, List<Long> ids) {
        int code = StatusEnum.SUCCESS.getCode();
        boolean flag = experimentInfoService.revokeApplyEvaluation(currentUserId,ids);
        if(!flag){
            code = StatusEnum.FAIL.getCode();
        }
        return code;
    }
 
    @Override
    public List<ExperimentInfoAppVo> getExperimentInfoList(Long currentUserId) {
        List<ExperimentInfoDTO> experimentInfoList = experimentInfoService.getExperimentInfoList(currentUserId);
 
        return experimentAppConverter.getExperimentInfoAppVoList(experimentInfoList);
    }
 
 
 
    @Override
    public SearchResult<List<ExperimentInfoAppQueryDTO>> projectListByPage(PageQuery<ExperimentInfoAppQueryBO> appPageQuery,Long currentUserId) {
        PageQuery<ExperimentInfoQueryBO> pageQuery = new PageQuery<>();
        pageQuery.setPageSize(appPageQuery.getPageSize());
        pageQuery.setPageIndex(appPageQuery.getPageIndex());
        ExperimentInfoQueryBO queryBO = new ExperimentInfoQueryBO();
        if(!ObjectUtils.isEmpty(appPageQuery.getSearchParams())){
            BeanUtils.copyProperties(appPageQuery.getSearchParams(),queryBO);
        }
        queryBO.setExperimentTag(ExperimentTagEnum.NEW_CREATE.getValue());
        pageQuery.setSearchParams(queryBO);
        SearchResult<List<ExperimentInfoDTO>> searchResult = experimentInfoService.listByPage(pageQuery,currentUserId);
        SearchResult<List<ExperimentInfoAppQueryDTO>> result = new SearchResult<>();
        if(!ObjectUtils.isEmpty(searchResult)){
            BeanUtils.copyProperties(searchResult,result);
        }
        List<ExperimentInfoDTO> experimentInfoDTOS = (List<ExperimentInfoDTO>)searchResult.getData();
 
        for (ExperimentInfoDTO experimentInfo : experimentInfoDTOS) {
            List<ExperimentAssessLog> assessLogs = experimentAssessLogService.getAssessLogs(experimentInfo.getExperimentCode());
            if (assessLogs.size() > 0){
                experimentInfo.setExperimentAssessLogs(assessLogs);
            }
        }
 
        result.setData(getExperimentInfoAppQueryDTO(experimentInfoDTOS));
        //获取分页数据
        return result;
    }
 
    @Override
    public SearchResult<List<ExperimentInfoAppQueryDTO>> developListByPage(PageQuery<ExperimentInfoAppQueryBO> appPageQuery,Long currentUserId) {
        PageQuery<ExperimentInfoQueryBO> pageQuery = new PageQuery<>();
        pageQuery.setPageSize(appPageQuery.getPageSize());
        pageQuery.setPageIndex(appPageQuery.getPageIndex());
        ExperimentInfoQueryBO queryBO = new ExperimentInfoQueryBO();
        if(!ObjectUtils.isEmpty(appPageQuery.getSearchParams())){
            BeanUtils.copyProperties(appPageQuery.getSearchParams(),queryBO);
        }
        queryBO.setExperimentTag(ExperimentTagEnum.AREADLY_DEVELOP.getValue());
        pageQuery.setSearchParams(queryBO);
        SearchResult<List<ExperimentInfoDTO>> searchResult = experimentInfoService.listByPage(pageQuery,currentUserId);
        SearchResult<List<ExperimentInfoAppQueryDTO>> result = new SearchResult<>();
        if(!ObjectUtils.isEmpty(searchResult)){
            BeanUtils.copyProperties(searchResult,result);
        }
        List<ExperimentInfoDTO> experimentInfoDTOS = (List<ExperimentInfoDTO>)searchResult.getData();
 
        for (ExperimentInfoDTO experimentInfo : experimentInfoDTOS) {
            List<ExperimentAssessLog> assessLogs = experimentAssessLogService.getAssessLogs(experimentInfo.getExperimentCode());
            if (assessLogs.size() > 0){
                experimentInfo.setExperimentAssessLogs(assessLogs);
            }
        }
 
        result.setData(getExperimentInfoAppQueryDTO(experimentInfoDTOS));
        //获取分页数据
        return result;
    }
 
 
 
    private List<ExperimentInfoAppQueryDTO> getExperimentInfoAppQueryDTO(List<ExperimentInfoDTO> experimentInfoDTOS){
        List<ExperimentInfoAppQueryDTO> experimentInfoAppQueryDTOS = new ArrayList<>();
        if(!ObjectUtils.isEmpty(experimentInfoDTOS)){
            List<Long> expermentIds = experimentInfoDTOS.stream().map(ExperimentInfoDTO::getId).collect(Collectors.toList());
            //获取关联的评估数据
            List<ReportAppQueryDTO> reportAppQueryDTOS = reportRiskAssessInfoService.listRiskReport(expermentIds);
            //遍历实验
            for (ExperimentInfoDTO experimentInfoDTO:experimentInfoDTOS){
                ExperimentInfoAppQueryDTO experimentInfoAppQueryDTO = new ExperimentInfoAppQueryDTO();
                BeanUtils.copyProperties(experimentInfoDTO,experimentInfoAppQueryDTO);
                //实验场所
                List<ExperimentAndSiteAppQueryDTO> siteAppQueryDTOs = new ArrayList<>();
                if(!ObjectUtils.isEmpty(experimentInfoDTO.getSites())){
                    siteAppQueryDTOs = BeanCopyUtils.copyBeanList(experimentInfoDTO.getSites(),ExperimentAndSiteAppQueryDTO.class);
                }
                experimentInfoAppQueryDTO.setSites(siteAppQueryDTOs);
                //人员
                List<ExperimentAndPersonAppQueryDTO> personAppQueryDTOList = new ArrayList<>();
                if(!ObjectUtils.isEmpty(experimentInfoDTO.getPersons())){
                    personAppQueryDTOList = BeanCopyUtils.copyBeanList(experimentInfoDTO.getPersons(),ExperimentAndPersonAppQueryDTO.class);
                }
                experimentInfoAppQueryDTO.setPersons(personAppQueryDTOList);
                //设备
                List<ExperimentAndDeviceAppQueryDTO> deviceAppQueryDTOList = new ArrayList<>();
                if(!ObjectUtils.isEmpty(experimentInfoDTO.getDevices())){
                    deviceAppQueryDTOList = BeanCopyUtils.copyBeanList(experimentInfoDTO.getDevices(),ExperimentAndDeviceAppQueryDTO.class);
                }
                experimentInfoAppQueryDTO.setDevices(deviceAppQueryDTOList);
                //材料
                List<ExperimentAndStuffAppQueryDTO> stuffAppQueryDTOList = new ArrayList<>();
                if(!ObjectUtils.isEmpty(experimentInfoDTO.getStuffs())){
                    stuffAppQueryDTOList = BeanCopyUtils.copyBeanList(experimentInfoDTO.getStuffs(),ExperimentAndStuffAppQueryDTO.class);
                }
                experimentInfoAppQueryDTO.setStuffs(stuffAppQueryDTOList);
                //危废
                List<ExperimentHazardousWasteAppQueryDTO> wasteAppQueryDTOList = new ArrayList<>();
                if(!ObjectUtils.isEmpty(experimentInfoDTO.getWastes())){
                    wasteAppQueryDTOList = BeanCopyUtils.copyBeanList(experimentInfoDTO.getWastes(),ExperimentHazardousWasteAppQueryDTO.class);
                }
                experimentInfoAppQueryDTO.setWastes(wasteAppQueryDTOList);
 
                //应急演练
                List<ExperimentAndEmergencyRespDTO> emergencies = new ArrayList<>();
                if(!ObjectUtils.isEmpty(experimentInfoDTO.getEmergencies())){
                    emergencies = BeanCopyUtils.copyBeanList(experimentInfoDTO.getEmergencies(),ExperimentAndEmergencyRespDTO.class);
                }
                experimentInfoAppQueryDTO.setEmergencies(emergencies);
                //风险评估
                if(!ObjectUtils.isEmpty(reportAppQueryDTOS)){
                    List<ReportAppQueryDTO> selectList = reportAppQueryDTOS.stream().filter(item -> item.getExperimentId().equals(experimentInfoDTO.getId())).collect(Collectors.toList());
                    if(selectList.size()>0){
                        experimentInfoAppQueryDTO.setAssessLevel(selectList.get(0).getAssessLevel());
                        experimentInfoAppQueryDTO.setAssessTime(selectList.get(0).getAssessTime());
                    }
                }else {
                    experimentInfoAppQueryDTO.setAssessLevel((byte)0);
                }
                experimentInfoAppQueryDTOS.add(experimentInfoAppQueryDTO);
            }
        }
        return experimentInfoAppQueryDTOS;
    }
 
 
 
}