郑永安
2023-06-19 299fa5c882af4577ba867ed9de9e2dc4fade3f6d
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
package com.gk.hotwork.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.hotwork.Domain.*;
import com.gk.hotwork.Domain.Do.SafetySelfInspectionItemQualifiedCountDO;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Mapper.*;
import com.gk.hotwork.Service.SafetySelfInspectionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service("SafetySelfInspectionService")
@Transactional
public class SafetySelfInspectionImpl extends ServiceImpl<SafetySelfInspectionMapper,SafetySelfInspection> implements SafetySelfInspectionService {
 
    @Autowired
    private SafetySelfInspectionMapper safetySelfInspectionMapper;
    @Autowired
    private SafetySelfInspectionItemMapper safetySelfInspectionItemMapper;
    @Autowired
    private SafetySelfInspectionItemDeductionMapper safetySelfInspectionItemDeductionMapper;
    @Autowired
    private SafetyInspectionItemDeductionMapper safetyInspectionItemDeductionMapper;
    /**
     * @Description: 分页
     */
    @Override
    public IPage<SafetySelfInspection> selectPage(Page<SafetySelfInspection> page, Map<String, Object> filter, UserInfo user) {
        Integer type = user.getType();
        if (!type.equals(1) || !type.equals(2)) {
            Long companyid = user.getCompanyid();
            filter.put("companyid",companyid);
        }
        IPage<SafetySelfInspection> res = safetySelfInspectionMapper.selectPages(page, filter);
        List<SafetySelfInspection> records = res.getRecords();
        if (CollectionUtils.isNotEmpty(records)){
            DecimalFormat df = new DecimalFormat("0.00%");
            records = records.stream().map((safetySelfInspection) -> {
                if (safetySelfInspection.getStatus() == 2) {
                    Long id = safetySelfInspection.getId();
                    SafetySelfInspectionItemQualifiedCountDO countDO = safetySelfInspectionItemMapper.countQualifiedDataById(id);
                    safetySelfInspection.setUnqualifiedItem(countDO.getItemSum() - countDO.getQualifiedItem());
                    if (countDO != null && countDO.getItemSum() != 0 && countDO.getItemSum() != null) {
                        BigDecimal rate = new BigDecimal(countDO.getQualifiedItem())
                                .divide(new BigDecimal(countDO.getItemSum()), 4, BigDecimal.ROUND_HALF_UP);
                        String qualifiedRate = df.format(rate);
                        safetySelfInspection.setQualifiedRate(qualifiedRate);
                        safetySelfInspection.setItemSum(countDO.getItemSum());
                    }
                }
                return safetySelfInspection;
            }).collect(Collectors.toList());
            res.setRecords(records);
        }
        return res;
    }
 
 
    /**
     * @Description: 新增
     */
    @Override
    public void addOne(SafetySelfInspection param, UserInfo user) {
        requiredVerification(param);
 
        Date date = new Date();
        String username = user.getRealname();
        param.setValidFlag(Boolean.TRUE);
        param.setUpdateBy(username);
        param.setCreateBy(username);
        param.setUpdateTime(date);
        param.setCreateTime(date);
        this.save(param);
 
        if (CollectionUtils.isNotEmpty(param.getItemList())) {
            for (SafetySelfInspectionItem safetySelfInspectionItem : param.getItemList()) {
                safetySelfInspectionItem.setValidFlag(Boolean.TRUE);
                safetySelfInspectionItem.setUpdateBy(username);
                safetySelfInspectionItem.setCreateBy(username);
                safetySelfInspectionItem.setUpdateTime(date);
                safetySelfInspectionItem.setCreateTime(date);
                safetySelfInspectionItem.setSafetySelfInspectionId(param.getId());
                safetySelfInspectionItemMapper.insert(safetySelfInspectionItem);
            }
        }
    }
 
    /**
     * @Description: 修改
     */
    @Override
    public void modOne(SafetySelfInspection param, UserInfo user) {
        selectVerification(param.getId());
        requiredVerification(param);
        //更新主表
        Date date = new Date();
        String username = user.getRealname();
        param.setUpdateTime(date);
        param.setUpdateBy(username);
        this.updateById(param);
        //更新检查项表
        //1.新增新的元素
        //2.删除旧的不存在的元素
        List<SafetySelfInspectionItem> oldFileList = safetySelfInspectionItemMapper.getBySafetySelfInspectionId(param.getId());
        List<SafetySelfInspectionItem> newFileList = param.getItemList();
 
        List<Long> oldIdList = new ArrayList<>();
        List<Long> newIdList = new ArrayList<>();
 
        for (SafetySelfInspectionItem oldSafetySelfInspectionItem :oldFileList ){
            oldIdList.add(oldSafetySelfInspectionItem.getId());
        }
        for (SafetySelfInspectionItem newSafetySelfInspectionItem :newFileList ){
            if (newSafetySelfInspectionItem.getId()==null){
                //1.添加新增的元素
                newSafetySelfInspectionItem.setValidFlag(Boolean.TRUE);
                newSafetySelfInspectionItem.setUpdateBy(username);
                newSafetySelfInspectionItem.setCreateBy(username);
                newSafetySelfInspectionItem.setUpdateTime(date);
                newSafetySelfInspectionItem.setCreateTime(date);
                newSafetySelfInspectionItem.setSafetySelfInspectionId(param.getId());
                safetySelfInspectionItemMapper.insert(newSafetySelfInspectionItem);
            }else{
                newIdList.add(newSafetySelfInspectionItem.getId());
            }
        }
        //2.删除不存在的元素
        List<Long> diffList = getDif(oldIdList,newIdList);
        if (CollectionUtils.isNotEmpty(diffList)){
            for (Long id : diffList){
                safetySelfInspectionItemMapper.deleteById(id,username,date);
            }
        }
    }
 
    public  List<Long> getDif(List<Long> oldIdList , List<Long> newIdList ){
        ArrayList<Long> dif = new ArrayList<>();
        //查找出oldIdList表中不包含newIdList的元素
        for (Long id : oldIdList) {
            if (!(newIdList.contains(id))) {
                dif.add(id);
            }
        }
        return dif;
    }
 
    /**
     * @Description: 删除
     */
    @Override
    public void delOne(Long id, UserInfo user) {
        selectVerification(id);
 
        SafetySelfInspection delOne = new SafetySelfInspection();
        delOne.setId(id);
        delOne.setUpdateTime(new Date());
        delOne.setUpdateBy(user.getRealname());
        delOne.setValidFlag(Boolean.FALSE);
        this.updateById(delOne);
    }
 
    @Override
    public SafetySelfInspection infoOne(Long id,String unqualified, UserInfo user) {
        SafetySelfInspection safetySelfInspection=selectVerification(id);
        //组装检查项
        List<SafetySelfInspectionItem> itemList= safetySelfInspectionItemMapper.getDetailBySafetySelfInspectionId(id,unqualified);
        if(CollectionUtils.isNotEmpty(itemList)){
            //组装扣分项
            for (SafetySelfInspectionItem safetySelfInspectionItem : itemList){
                if (safetySelfInspectionItem.getSafetyInspectionItemResult()==null){
                    //根据检查项id获取原始扣分项
                    List<SafetyInspectionItemDeduction> safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId());
                    if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){
                        List<SafetySelfInspectionItemDeduction> oldSafetySelfInspectionItemDeductionList=new ArrayList<>();
                        for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : safetyInspectionItemDeductionList){
                            SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction = new SafetySelfInspectionItemDeduction();
                            safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetyInspectionItemDeduction.getId());
                            safetySelfInspectionItemDeduction.setName(safetyInspectionItemDeduction.getName());
                            safetySelfInspectionItemDeduction.setPoint(0);
                            oldSafetySelfInspectionItemDeductionList.add(safetySelfInspectionItemDeduction);
                        }
                        safetySelfInspectionItem.setSelfDeductionList(oldSafetySelfInspectionItemDeductionList);
                    }else{
                        safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>());
                    }
                }else if (safetySelfInspectionItem.getSafetyInspectionItemResult().equals(1)){
                    //根据自查清单记录的检查项id获取其扣分项
                    List<SafetySelfInspectionItemDeduction> selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId());
                    if (CollectionUtils.isNotEmpty(selfDeductionList)){
                        safetySelfInspectionItem.setSelfDeductionList(selfDeductionList);
                    }
                }
            }
            safetySelfInspection.setItemList(itemList);
        }else{
            safetySelfInspection.setItemList(new ArrayList<>());
        }
        return safetySelfInspection;
    }
 
    @Override
    public SafetySelfInspectionItem itemInfoOne(Long id, UserInfo user){
        SafetySelfInspectionItem safetySelfInspectionItem = safetySelfInspectionItemMapper.getDetailById(id);
        List<SafetySelfInspectionItemDeduction> selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId());
        if (CollectionUtils.isNotEmpty(selfDeductionList)){
            safetySelfInspectionItem.setSelfDeductionList(selfDeductionList);
        }else{
            //根据自查清单检查项里的检查项表去找原有的扣分项
            List<SafetyInspectionItemDeduction> safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId());
            if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){
                List<SafetySelfInspectionItemDeduction> oldSafetySelfInspectionItemDeductionList=new ArrayList<>();
                for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : safetyInspectionItemDeductionList){
                    SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction = new SafetySelfInspectionItemDeduction();
                    safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetyInspectionItemDeduction.getId());
                    safetySelfInspectionItemDeduction.setName(safetyInspectionItemDeduction.getName());
                    safetySelfInspectionItemDeduction.setPoint(0);
                    oldSafetySelfInspectionItemDeductionList.add(safetySelfInspectionItemDeduction);
                }
                safetySelfInspectionItem.setSelfDeductionList(oldSafetySelfInspectionItemDeductionList);
            }else{
                safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>());
            }
        }
        return safetySelfInspectionItem;
    }
 
    @Override
    public void modItemInfo(SafetySelfInspectionItem param, UserInfo user) {
        if (param.getSafetyInspectionItemResult()==0 && StringUtils.isBlank(param.getSafetyInspectionItemResultDesc())){
            throw new BusinessException("否决说明必填!");
        }
        Date date = new Date();
        String username = user.getRealname();
        param.setUpdateTime(date);
        param.setUpdateBy(username);
        safetySelfInspectionItemMapper.updateById(param);
        if (param.getSafetyInspectionItemResult()==0 || param.getSafetyInspectionItemResult() == 2){
            //否决 合格--删除扣分记录
            safetySelfInspectionItemDeductionMapper.delBySafetySelfInspectionItemId(param.getId(),username,date);
        }else{
            //扣分
            List<SafetySelfInspectionItemDeduction> list = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(param.getId());
            if (CollectionUtils.isEmpty(list)){
                //新增扣分记录
                for (SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction : param.getSelfDeductionList()){
                    safetySelfInspectionItemDeduction.setValidFlag(Boolean.TRUE);
                    safetySelfInspectionItemDeduction.setUpdateBy(username);
                    safetySelfInspectionItemDeduction.setCreateBy(username);
                    safetySelfInspectionItemDeduction.setUpdateTime(date);
                    safetySelfInspectionItemDeduction.setCreateTime(date);
                    safetySelfInspectionItemDeduction.setSafetySelfInspectionItemId(param.getId());
                    safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetySelfInspectionItemDeduction.getSafetyInspectionItemDeductionId());
                    safetySelfInspectionItemDeductionMapper.insert(safetySelfInspectionItemDeduction);
                }
            }else{
                //更新扣分记录
                for (SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction : param.getSelfDeductionList()){
                    safetySelfInspectionItemDeduction.setUpdateBy(username);
                    safetySelfInspectionItemDeduction.setUpdateTime(date);
                    safetySelfInspectionItemDeductionMapper.updateById(safetySelfInspectionItemDeduction);
                }
            }
        }
    }
 
    @Override
    public void finish(Long id, UserInfo user) {
        SafetySelfInspection safetySelfInspection=selectVerification(id);
        Date date = new Date();
        String username = user.getRealname();
        safetySelfInspection.setStatus(2);
        safetySelfInspection.setUpdateTime(date);
        safetySelfInspection.setUpdateBy(username);
        safetySelfInspectionMapper.updateById(safetySelfInspection);
    }
 
 
    /**
     * 查询验证
     * 验证对象存在
     */
    public SafetySelfInspection selectVerification(Long id) {
        if (id == null) throw new BusinessException("id传参不能为空");
        SafetySelfInspection SafetySelfInspection = this.getById(id);
        if (SafetySelfInspection == null) throw new BusinessException("找不到对应实体");
        return SafetySelfInspection;
    }
 
    /**
     * 操作验证
     * 验证必填项
     */
    public void requiredVerification(SafetySelfInspection param) {
        if (StringUtils.isBlank(param.getInspectionName())) throw new BusinessException("请填写自查清单名称");
        if (param.getInspectionTime() == null) throw new BusinessException("请选择自查时间");
        if (param.getInspector() == null) throw new BusinessException("请选择自查人员");
        if (CollectionUtils.isEmpty(param.getItemList())) {
            throw new BusinessException("请选择检查项");
        }
        if (param.getCheckedCompanyId() == null){
            throw new BusinessException("请选择被检查公司");
        }
    }
}