郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
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.EmergencyPlanFile;
import com.gk.hotwork.Domain.SafetyInspectionItem;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.SafetyInspectionItemDeduction;
import com.gk.hotwork.Domain.UserInfo;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Mapper.SafetyInspectionItemDeductionMapper;
import com.gk.hotwork.Mapper.SafetyInspectionItemMapper;
import com.gk.hotwork.Service.SafetyInspectionItemService;
import org.apache.commons.collections4.CollectionUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service("SafetyInspectionItemService")
@Transactional
public class SafetyInspectionItemImpl extends ServiceImpl<SafetyInspectionItemMapper,SafetyInspectionItem> implements SafetyInspectionItemService {
 
    @Autowired
    private SafetyInspectionItemMapper safetyInspectionItemMapper;
    @Autowired
    private SafetyInspectionItemDeductionMapper safetyInspectionItemDeductionMapper;
 
    /**
    * @Description: 分页
    */
    @Override
    public IPage<SafetyInspectionItem> selectPage(Page<SafetyInspectionItem> page, Map<String, Object> filter, UserInfo user) {
        IPage<SafetyInspectionItem> res = safetyInspectionItemMapper.selectPages(page,filter);
        if (CollectionUtils.isNotEmpty(res.getRecords())){
            for (int i = 0 ; i < res.getRecords().size(); i++){
                List<SafetyInspectionItemDeduction> fileList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(res.getRecords().get(i).getId());
                res.getRecords().get(i).setDeductionList(fileList);
            }
        }
        return res;
    }
 
 
    /**
    * @Description: 新增
    */
    @Override
    public void addOne(SafetyInspectionItem 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.getDeductionList())){
            for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : param.getDeductionList()){
                safetyInspectionItemDeduction.setValidFlag(Boolean.TRUE);
                safetyInspectionItemDeduction.setUpdateBy(username);
                safetyInspectionItemDeduction.setCreateBy(username);
                safetyInspectionItemDeduction.setUpdateTime(date);
                safetyInspectionItemDeduction.setCreateTime(date);
                safetyInspectionItemDeduction.setSafetyInspectionItemId(param.getId());
                safetyInspectionItemDeductionMapper.insert(safetyInspectionItemDeduction);
            }
        }
    }
 
 
    /**
    * @Description: 修改
    */
    @Override
    public void modOne(SafetyInspectionItem 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);
        //更新扣分项表
 
        List<SafetyInspectionItemDeduction> oldList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(param.getId());
        List<SafetyInspectionItemDeduction> newList = param.getDeductionList();
 
        List<Long> oldIdList = new ArrayList<>();
        List<Long> newIdList = new ArrayList<>();
 
 
        for (SafetyInspectionItemDeduction oldSafetyInspectionItemDeduction :oldList ){
            oldIdList.add(oldSafetyInspectionItemDeduction.getId());
        }
        for (SafetyInspectionItemDeduction newSafetyInspectionItemDeduction :newList ){
            if (newSafetyInspectionItemDeduction.getId()==null){
                //1.添加新增的元素
                newSafetyInspectionItemDeduction.setValidFlag(Boolean.TRUE);
                newSafetyInspectionItemDeduction.setUpdateBy(username);
                newSafetyInspectionItemDeduction.setCreateBy(username);
                newSafetyInspectionItemDeduction.setUpdateTime(date);
                newSafetyInspectionItemDeduction.setCreateTime(date);
                newSafetyInspectionItemDeduction.setSafetyInspectionItemId(param.getId());
                safetyInspectionItemDeductionMapper.insert(newSafetyInspectionItemDeduction);
            }else{
                newIdList.add(newSafetyInspectionItemDeduction.getId());
                //2.修改id一致 的元素
                for (int i =0;i<oldList.size();i++){
                    if (oldList.get(i).getId().equals(newSafetyInspectionItemDeduction.getId())){
                        newSafetyInspectionItemDeduction.setUpdateBy(username);
                        newSafetyInspectionItemDeduction.setUpdateTime(date);
                        safetyInspectionItemDeductionMapper.updateById(newSafetyInspectionItemDeduction);
                    }
                }
            }
        }
        //2.删除不存在的元素
        List<Long> diffList = getDif(oldIdList,newIdList);
        if (CollectionUtils.isNotEmpty(diffList)){
            for (Long id : diffList){
                safetyInspectionItemDeductionMapper.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);
//        deleteVerification(SafetyInspectionItem);
 
        SafetyInspectionItem delOne = new SafetyInspectionItem();
        delOne.setId(id);
        delOne.setUpdateTime(new Date());
        delOne.setUpdateBy(user.getRealname());
        delOne.setValidFlag(Boolean.FALSE);
        this.updateById(delOne);
    }
 
    @Override
    public List<SafetyInspectionItem> infoElementA(Long id, UserInfo user) {
        List<SafetyInspectionItem> list = safetyInspectionItemMapper.infoElementA(id);
        if (CollectionUtils.isNotEmpty(list)){
            for (SafetyInspectionItem safetyInspectionItem : list){
                List<SafetyInspectionItemDeduction> deductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetyInspectionItem.getId());
                if (CollectionUtils.isNotEmpty(deductionList)){
                    safetyInspectionItem.setDeductionList(deductionList);
                }else{
                    safetyInspectionItem.setDeductionList(new ArrayList<>());
                }
            }
        }
        return list;
    }
 
    @Override
    public SafetyInspectionItem info(Long id, UserInfo user) {
        SafetyInspectionItem safetyInspectionItem = safetyInspectionItemMapper.getById(id);
        List<SafetyInspectionItemDeduction> list = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(id);
        if (CollectionUtils.isNotEmpty(list)){
            safetyInspectionItem.setDeductionList(list);
        }else{
            safetyInspectionItem.setDeductionList(new ArrayList<>());
        }
        return safetyInspectionItem;
    }
 
 
    /**
     * 查询验证
     * 验证对象存在
     */
    public void  selectVerification(Long id){
        if (id == null) throw new BusinessException("id传参不能为空");
        SafetyInspectionItem SafetyInspectionItem = this.getById(id);
        if (SafetyInspectionItem ==  null) throw new BusinessException("找不到对应实体");
    }
 
    /**
     * 操作验证
     * 验证必填项
     *
     */
    public void requiredVerification(SafetyInspectionItem param){
        if (param.getElementA() == null) throw new BusinessException("请选择A级要素");
        if (param.getElementB() == null) throw new BusinessException("请选择B级要素");
        if(StringUtils.isBlank(param.getStandardizationRequirements())) throw new BusinessException("请填写标准化要求");
        if(StringUtils.isBlank(param.getEnterpriseStandard())) throw new BusinessException("请填写企业达标标准");
        if(StringUtils.isBlank(param.getReviewMethod())) throw new BusinessException("请填写评审方法");
        if (CollectionUtils.isEmpty(param.getDeductionList())){
            throw new BusinessException("请填写扣分项列表");
        }
    }
 
    /**
     * 操作验证
     * 删除已被B级要素绑定的A级要素
     *
     */
//    public void deleteVerification(SafetyInspectionItem param){
//        if (param.getType()==0){
//            int count = this.count(new LambdaQueryWrapper<SafetyInspectionItem>()
//                    .eq(SafetyInspectionItem::getParentId, param.getId())
//                    .eq(SafetyInspectionItem::getType , 1)
//                    .eq(SafetyInspectionItem::getValidFlag, Boolean.TRUE));
//            if (count > 0) throw new BusinessException("当前A级要素仍然有已绑定的B级要素,无法删除");
//        }
//    }
}