huangzhen
2023-09-15 ea09629d8857e0afa329858f72cf1c93e25b813c
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
package com.gkhy.exam.coalmine.service.impl;
 
import com.gkhy.exam.coalmine.entity.EmonExamRecord;
import com.gkhy.exam.coalmine.entity.EmonTrainRecord;
import com.gkhy.exam.coalmine.model.dto.req.ExamReceiveReqDTO;
import com.gkhy.exam.coalmine.model.dto.req.TrainReceiveReqDTO;
import com.gkhy.exam.coalmine.model.dto.resp.AttachmentInfoRespDTO;
import com.gkhy.exam.coalmine.model.dto.resp.GetExamDataRespDTO;
import com.gkhy.exam.coalmine.model.dto.resp.GetTrainDataRespDTO;
import com.gkhy.exam.coalmine.service.EmonRecordManagerService;
import com.gkhy.exam.coalmine.service.baseService.EmonExamRecordService;
import com.gkhy.exam.coalmine.service.baseService.EmonTrainRecordService;
import com.gkhy.exam.coalmine.utils.AttachmentUtil;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.coalmineEnums.DeleteStatusEnum;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.file.entity.AttachmentInfo;
import com.ruoyi.file.service.AttachmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author Mr.huang
 * @decription
 * @date 2023/9/13 10:19
 */
@Service
public class EmonRecordManagerServiceImpl implements EmonRecordManagerService {
 
    @Resource
    private AttachmentService attachmentService;
 
    @Resource
    private EmonExamRecordService  emonExamRecordService;
 
    @Resource
    private EmonTrainRecordService emonTrainRecordService;
 
 
    private Boolean examDataIsValid(MultipartFile[] file,ExamReceiveReqDTO reqDTO){
        boolean valid = reqDTO != null && reqDTO.getReportTime() != null && reqDTO.getDistrictId() != null
                && StringUtils.isNotBlank(reqDTO.getExamCenter()) && StringUtils.isNotBlank(reqDTO.getExcType())
                && reqDTO.getIsCm() != null && StringUtils.isNotBlank(reqDTO.getModule()) && !CollectionUtils.isEmpty(Arrays.asList(file));
        return valid;
    }
 
    @Override
    @Transactional
    public AjaxResult examReceive(MultipartFile[] file,ExamReceiveReqDTO reqDTO) {
        if (examDataIsValid(file,reqDTO)) {
            List<AttachmentInfo> attachmentInfos = (List<AttachmentInfo>) attachmentService.saveBatchFileToPath(file, reqDTO.getModule());
            if (CollectionUtils.isEmpty(attachmentInfos))
                throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL, "考场异常监控记录图片保存失败");
            String attachmentIds = AttachmentUtil.idConvertString(attachmentInfos);
            EmonExamRecord emonExamRecord = new EmonExamRecord();
            BeanUtils.copyProperties(reqDTO, emonExamRecord);
            emonExamRecord.setDelFlag(DeleteStatusEnum.NO.getStatus());
            emonExamRecord.setExcImage(attachmentIds);
            boolean save = emonExamRecordService.save(emonExamRecord);
            if (!save)
                throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL, "考场异常监控记录保存失败");
 
            return AjaxResult.success();
        }
        return null;
    }
 
    private Boolean trainDataIsValid(MultipartFile[] file,TrainReceiveReqDTO reqDTO){
        boolean valid = reqDTO != null && reqDTO.getReportTime() != null && reqDTO.getDistrictId() != null
                && StringUtils.isNotBlank(reqDTO.getExamCenter()) && StringUtils.isNotBlank(reqDTO.getExcType())
                && reqDTO.getIsCm() != null && !CollectionUtils.isEmpty(Arrays.asList(file))
                && StringUtils.isNotBlank(reqDTO.getModule());
        return valid;
    }
 
    @Override
    public AjaxResult trainReceive(MultipartFile[] file,TrainReceiveReqDTO reqDTO) {
        List<AttachmentInfo> attachmentInfos = (List<AttachmentInfo>) attachmentService.saveBatchFileToPath(file, reqDTO.getModule());
        if (CollectionUtils.isEmpty(attachmentInfos))
            throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL, "培训监控异常记录图片保存失败");
        String attachmentIds = AttachmentUtil.idConvertString(attachmentInfos);
        if (trainDataIsValid(file,reqDTO)) {
            EmonTrainRecord emonTrainRecord = new EmonTrainRecord ();
            BeanUtils.copyProperties(reqDTO,emonTrainRecord);
            emonTrainRecord.setDelFlag(DeleteStatusEnum.NO.getStatus());
            emonTrainRecord.setExcImage(attachmentIds);
            boolean save = emonTrainRecordService.save(emonTrainRecord);
            if (!save)
                throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL, "培训监控异常记录保存失败");
            return AjaxResult.success();
        }
        return null;
    }
 
    @Override
    public List<GetExamDataRespDTO> getExamData() {
        List<EmonExamRecord> emonExamRecords =  emonExamRecordService.listValid();
        if (!CollectionUtils.isEmpty(emonExamRecords)){
            List<GetExamDataRespDTO> list = emonExamRecords.stream().map(emonExamRecord -> {
                GetExamDataRespDTO dto = new GetExamDataRespDTO();
                BeanUtils.copyProperties(emonExamRecord, dto);
                String excImage = emonExamRecord.getExcImage();
                if (StringUtils.isNotBlank(excImage)) {
                    List<Long> ids = AttachmentUtil.stringConvertIds(excImage);
                    List<AttachmentInfo> attachmentInfos = attachmentService.findByIds(ids);
                    if (!CollectionUtils.isEmpty(attachmentInfos)) {
                        List<AttachmentInfoRespDTO> attachmentInfoRespDTOS = attachmentInfos.stream().map(attachmentInfo -> {
                            AttachmentInfoRespDTO respDTO = new AttachmentInfoRespDTO();
                            BeanUtils.copyProperties(attachmentInfo, respDTO);
                            return respDTO;
                        }).collect(Collectors.toList());
                        dto.setAttachmentInfos(attachmentInfoRespDTOS);
                    }
                }
                return dto;
            }).collect(Collectors.toList());
            return list;
        }
        return null;
    }
 
    @Override
    public List<GetTrainDataRespDTO> getTrainData() {
        List<EmonTrainRecord> emonTrainRecords =  emonTrainRecordService.listValid();
        if (!CollectionUtils.isEmpty(emonTrainRecords)){
            List<GetTrainDataRespDTO> list = emonTrainRecords.stream().map(emonTrainRecord -> {
                GetTrainDataRespDTO dto = new GetTrainDataRespDTO();
                BeanUtils.copyProperties(emonTrainRecord, dto);
                String excImage = emonTrainRecord.getExcImage();
                if (StringUtils.isNotBlank(excImage)) {
                    List<Long> ids = AttachmentUtil.stringConvertIds(excImage);
                    List<AttachmentInfo> attachmentInfos = attachmentService.findByIds(ids);
                    if (!CollectionUtils.isEmpty(attachmentInfos)) {
                        List<AttachmentInfoRespDTO> attachmentInfoRespDTOS = attachmentInfos.stream().map(attachmentInfo -> {
                            AttachmentInfoRespDTO respDTO = new AttachmentInfoRespDTO();
                            BeanUtils.copyProperties(attachmentInfo, respDTO);
                            return respDTO;
                        }).collect(Collectors.toList());
                        dto.setAttachmentInfos(attachmentInfoRespDTOS);
                    }
                }
                return dto;
            }).collect(Collectors.toList());
            return list;
        }
        return null;
    }
 
    @Override
    public AjaxResult deleteExamRecord(Long id) {
        EmonExamRecord emonExamRecord = emonExamRecordService.findValidById(id);
        if (emonExamRecord == null)
            return AjaxResult.success();
        emonExamRecord.setDelFlag(DeleteStatusEnum.YES.getStatus());
        boolean update = emonExamRecordService.updateById(emonExamRecord);
        if (!update)
            throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL, "考场异常监控记录删除失败");
        String excImage = emonExamRecord.getExcImage();
        if (StringUtils.isNotBlank(excImage)) {
            List<Long> ids = AttachmentUtil.stringConvertIds(excImage);
            List<AttachmentInfo> attachmentInfos = attachmentService.findByIds(ids);
            if (!CollectionUtils.isEmpty(attachmentInfos)) {
                attachmentInfos = attachmentInfos.stream().map(attachmentInfo -> {
                    attachmentInfo.setDelFlag(1);
                    attachmentInfo.setUpdateBy(SecurityUtils.getUsername());
                    attachmentInfo.setUpdateTime(new Date());
                    return attachmentInfo;
                }).collect(Collectors.toList());
                attachmentService.updateBatchById(attachmentInfos);
            }
        }
        return AjaxResult.success("考场异常监控记录删除成功");
    }
 
    @Override
    public AjaxResult deleteTrainRecord(Long id) {
        EmonTrainRecord emonTrainRecord = emonTrainRecordService.findValidById(id);
        if (emonTrainRecord == null)
            return AjaxResult.success();
        emonTrainRecord.setDelFlag(DeleteStatusEnum.YES.getStatus());
        boolean update = emonTrainRecordService.updateById(emonTrainRecord);
        if (!update)
            throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL, "培训异常监控记录删除失败");
        String excImage = emonTrainRecord.getExcImage();
        if (StringUtils.isNotBlank(excImage)) {
            List<AttachmentInfo> attachmentInfos = attachmentService.findByBusinessId(id);
            if (!CollectionUtils.isEmpty(attachmentInfos)) {
                attachmentInfos = attachmentInfos.stream().map(attachmentInfo -> {
                    attachmentInfo.setDelFlag(1);
                    attachmentInfo.setUpdateBy(SecurityUtils.getUsername());
                    attachmentInfo.setUpdateTime(new Date());
                    return attachmentInfo;
                }).collect(Collectors.toList());
                attachmentService.updateBatchById(attachmentInfos);
            }
        }
        return AjaxResult.success("培训异常监控记录删除成功");
    }
}