郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.specialWork.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.specialWork.entity.ApprovalRuleItemStandDO;
import com.gkhy.safePlatform.specialWork.entity.ApprovalRuleUnitItem;
import com.gkhy.safePlatform.specialWork.entity.ApprovalRuleUnitItemDO;
import com.gkhy.safePlatform.specialWork.enums.RuleItemStandStatusEnum;
import com.gkhy.safePlatform.specialWork.enums.RuleItemStatusEnum;
import com.gkhy.safePlatform.specialWork.enums.RuleItemTypeEnum;
import com.gkhy.safePlatform.specialWork.enums.WorkStandTypeEnum;
import com.gkhy.safePlatform.specialWork.model.dto.resp.ApprovalRuleStandListRespDTO;
import com.gkhy.safePlatform.specialWork.model.update.EntityStatusBatchUO;
import com.gkhy.safePlatform.specialWork.model.dto.req.ApprovalRuleItemAddReqDTO;
import com.gkhy.safePlatform.specialWork.model.dto.req.ApprovalRuleItemModReqDTO;
import com.gkhy.safePlatform.specialWork.model.dto.req.DeleteForm;
import com.gkhy.safePlatform.specialWork.model.dto.resp.ApprovalRuleUnitItemListRespDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.ApprovalRuleUnitItemPageRespDTO;
import com.gkhy.safePlatform.specialWork.model.query.ApprovalRuleItemPageQuery;
import com.gkhy.safePlatform.specialWork.model.query.ApprovalRuleItemQuery;
import com.gkhy.safePlatform.specialWork.model.query.db.ApprovalRuleItemDBQuery;
import com.gkhy.safePlatform.specialWork.model.query.db.ApprovalRuleItemPageDBQuery;
import com.gkhy.safePlatform.specialWork.service.RuleItemService;
import com.gkhy.safePlatform.specialWork.service.baseService.ApprovalRuleStandService;
import com.gkhy.safePlatform.specialWork.service.baseService.ApprovalRuleUnitItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service("ruleItemService")
public class RuleItemServiceImpl implements RuleItemService {
 
    @Autowired
    private ApprovalRuleUnitItemService approvalRuleUnitItemService;
 
    @Autowired
    private ApprovalRuleStandService approvalRuleStandService;
 
    @Override
    public void saveRuleItem(ContextCacheUser currentUser, ApprovalRuleItemAddReqDTO addReqDTO) {
        ;
        if (addReqDTO.getItemStandId() == null || addReqDTO.getItemType() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (StringUtils.isBlank(addReqDTO.getItemName())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "填报项名称不可为空");
        }
        // 填报类型判断
        RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(addReqDTO.getItemType());
        if (typeEnum == null) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "填报项类型校验非法");
        }
        // 判断填报项标准是否存在
        ApprovalRuleItemStandDO ruleItemStand = approvalRuleStandService.getRuleStandDOById(addReqDTO.getItemStandId());
        if (ruleItemStand == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "填报项标准不存在");
        }
        if (!ruleItemStand.getStatus().equals(RuleItemStandStatusEnum.VALID.getCode())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "填报项标准不存在");
        }
        // 1.新增审批项
        ApprovalRuleUnitItem itemEntity = new ApprovalRuleUnitItem();
        itemEntity.setItemName(addReqDTO.getItemName().trim());
        itemEntity.setType(addReqDTO.getItemType());
        itemEntity.setStandId(addReqDTO.getItemStandId());
        itemEntity.setGmtCreate(LocalDateTime.now());
        itemEntity.setCreateUid(currentUser.getUid());
        itemEntity.setCreateUname(currentUser.getRealName());
        itemEntity.setStatus(RuleItemStatusEnum.VALID.getCode());
        approvalRuleUnitItemService.saveRuleItem(itemEntity);
    }
 
    @Override
    public void updateRuleItem(ContextCacheUser currentUser, ApprovalRuleItemModReqDTO modReqDTO) {
        ;
        if (modReqDTO.getItemId() == null ||
                modReqDTO.getItemType() == null ||
                modReqDTO.getItemStandId() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (StringUtils.isBlank(modReqDTO.getItemName())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "填报项名称不可为空");
        }
        // 填报类型判断
        RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(modReqDTO.getItemType());
        if (typeEnum == null) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "填报项类型校验非法");
        }
        ApprovalRuleUnitItemDO item = approvalRuleUnitItemService.getApprovalRuleUnitItemDOById(modReqDTO.getItemId());
        if (item == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "填报项不存在");
        }
        if (!item.getStatus().equals(RuleItemStatusEnum.VALID.getCode())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "填报项不存在");
        }
        // 判断填报项标准是否存在
        ApprovalRuleItemStandDO ruleItemStand = approvalRuleStandService.getRuleStandDOById(modReqDTO.getItemStandId());
        if (ruleItemStand == null) {
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "填报项标准不存在");
        }
        if (!ruleItemStand.getStatus().equals(RuleItemStandStatusEnum.VALID.getCode())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "填报项标准不存在");
        }
 
        // 1.更新审批项
        ApprovalRuleUnitItem itemEntity = new ApprovalRuleUnitItem();
        itemEntity.setId(modReqDTO.getItemId());
        itemEntity.setItemName(modReqDTO.getItemName());
        itemEntity.setType(modReqDTO.getItemType());
        itemEntity.setGmtModified(LocalDateTime.now());
        itemEntity.setModifiedUname(currentUser.getRealName());
        itemEntity.setModifiedUid(currentUser.getUid());
        approvalRuleUnitItemService.updateRuleItem(itemEntity);
    }
 
    @Override
    public void deleteRuleItem(ContextCacheUser currentUser, DeleteForm deleteForm) {
        ;
        if (deleteForm.getIds() == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        if (deleteForm.getIds().size() > 10) {
            throw new BusinessException(ResultCodes.SERVER_DEL_OVER_SIZE);
        }
        // 1.id合法
        for (Long ruleItemId : deleteForm.getIds()) {
            ApprovalRuleUnitItemDO ruleUnitItemDO =  approvalRuleUnitItemService.getApprovalRuleUnitItemDOById(ruleItemId);
            if (ruleUnitItemDO == null) {
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "填报项不存在");
            }
            if (!ruleUnitItemDO.getStatus().equals(RuleItemStatusEnum.VALID.getCode())) {
                throw new AusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "填报项不存在");
            }
            // 2.todo 是否被占用
        }
        EntityStatusBatchUO entityBatchDeleteObject = new EntityStatusBatchUO();
        entityBatchDeleteObject.setIds(deleteForm.getIds());
        entityBatchDeleteObject.setModifiedUid(currentUser.getUid());
        entityBatchDeleteObject.setModifiedUname(currentUser.getRealName());
        entityBatchDeleteObject.setGmtModified(LocalDateTime.now());
        approvalRuleUnitItemService.deleteBatch(entityBatchDeleteObject);
 
    }
 
    @Override
    public SearchResultVO<List<ApprovalRuleUnitItemPageRespDTO>> listRuleItemByPage(ContextCacheUser currentUser, PageQuery<ApprovalRuleItemPageQuery> pageQuery) {
        ;
        // 查询对象
        ApprovalRuleItemPageDBQuery dbQuery = new ApprovalRuleItemPageDBQuery();
        if (pageQuery.getSearchParams() != null) {
            dbQuery.setItemName(pageQuery.getSearchParams().getItemName());
            dbQuery.setItemStandId(pageQuery.getSearchParams().getItemStandId());
            dbQuery.setItemType(pageQuery.getSearchParams().getItemType());
            dbQuery.setStatus(RuleItemStatusEnum.VALID.getCode());
        }
        // 分页对象
        Page<ApprovalRuleUnitItem> page = new Page<>(pageQuery.getPageIndex(), pageQuery.getPageSize());
        List<ApprovalRuleUnitItem> dbData = approvalRuleUnitItemService.listRuleItemByPage(page, dbQuery);
 
        List<ApprovalRuleUnitItemPageRespDTO> respDTOs = new ArrayList<>(dbData.size());
        ApprovalRuleUnitItemPageRespDTO respDTO;
        RuleItemTypeEnum itemTypeEnum;
        RuleItemStatusEnum statusEnum;
        RuleItemStandStatusEnum standStatusEnum;
        WorkStandTypeEnum standTypeEnum;
        ApprovalRuleItemStandDO ruleStandDO;
        Map<Long, ApprovalRuleStandListRespDTO> standPool = null;
        if (pageQuery.getSearchParams().isContainItemStandEnable()) {
            standPool = new HashMap<>();
        }
        if (dbData.size() > 0) {
            for (ApprovalRuleUnitItem item : dbData) {
                respDTO = new ApprovalRuleUnitItemPageRespDTO();
                respDTO.setItemId(item.getId());
                respDTO.setItemName(item.getItemName());
                respDTO.setItemStandId(item.getStandId());
                respDTO.setRuleId(item.getRuleId());
                respDTO.setStepId(item.getStepId());
                respDTO.setUnitId(item.getUnitId());
                respDTO.setCreateUid(item.getCreateUid());
                respDTO.setCreateUname(item.getCreateUname());
                respDTO.setGmtCreate(item.getGmtCreate());
                respDTO.setModifiedUid(item.getModifiedUid());
                respDTO.setModifiedUname(item.getModifiedUname());
                respDTO.setGmtModified(item.getGmtModified());
                respDTO.setItemType(item.getType());
                respDTO.setStatus(item.getStatus());
                itemTypeEnum = RuleItemTypeEnum.parse(item.getType());
                if (itemTypeEnum != null) {
                    respDTO.setItemTypeDesc(itemTypeEnum.getValue());
                }
                statusEnum = RuleItemStatusEnum.parse(item.getStatus());
                if (statusEnum != null) {
                    respDTO.setStatusDesc(statusEnum.getValue());
                }
 
                // 是否包含审批项标准
                if (pageQuery.getSearchParams().isContainItemStandEnable()) {
                    assert standPool != null;
                    if (item.getStandId() != null) {
                        if (!standPool.containsKey(item.getStandId())) {
                            ruleStandDO = approvalRuleStandService.getRuleStandDOById(item.getStandId());
                            if (ruleStandDO != null && ruleStandDO.getStatus().equals(RuleItemStatusEnum.VALID.getCode())) {
                                ApprovalRuleStandListRespDTO standListRespDTO = new ApprovalRuleStandListRespDTO();
                                standListRespDTO.setRuleStandId(ruleStandDO.getId());
                                standListRespDTO.setRuleStandType(ruleStandDO.getType());
                                standListRespDTO.setTitle(ruleStandDO.getTitle());
                                standListRespDTO.setMinVal(ruleStandDO.getMinVal());
                                standListRespDTO.setMinValMatchPattern(ruleStandDO.getMinValMatchPattern());
                                standListRespDTO.setMaxVal(ruleStandDO.getMaxVal());
                                standListRespDTO.setMaxValMatchPattern(ruleStandDO.getMaxValMatchPattern());
                                standListRespDTO.setStatus(ruleStandDO.getStatus());
                                standStatusEnum = RuleItemStandStatusEnum.parse(ruleStandDO.getStatus());
                                if (standStatusEnum != null) {
                                    standListRespDTO.setStatusDesc(standStatusEnum.getValue());
                                }
                                standTypeEnum = WorkStandTypeEnum.parse(ruleStandDO.getType());
                                if (standTypeEnum != null) {
                                    standListRespDTO.setRuleStandTypeDesc(standTypeEnum.getDesc());
                                }
                                standPool.put(item.getStandId(), standListRespDTO);
                        }
                            respDTO.setItemStand(standPool.get(item.getStandId()));
 
                        }
                    }
                }
 
 
                respDTOs.add(respDTO);
            }
        }
 
        return new SearchResultVO<>(
                true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                respDTOs,
                ResultCodes.OK);
    }
 
    @Override
    public List<ApprovalRuleUnitItemListRespDTO> listRuleItem(ContextCacheUser currentUser, ApprovalRuleItemQuery query) {
        ;
        ApprovalRuleItemDBQuery dbQuery = new ApprovalRuleItemDBQuery();
        dbQuery.setItemName(query.getItemName());
        dbQuery.setItemStandId(query.getItemStandId());
        dbQuery.setItemType(query.getItemType());
        dbQuery.setStatus(RuleItemStatusEnum.VALID.getCode());
        // db查询
        List<ApprovalRuleUnitItemDO> dbData = approvalRuleUnitItemService.listRuleItem(dbQuery);
        List<ApprovalRuleUnitItemListRespDTO> respDTOs = new ArrayList<>(dbData.size());
        ApprovalRuleUnitItemListRespDTO respDTO;
        RuleItemTypeEnum itemTypeEnum;
        RuleItemStatusEnum statusEnum;
        RuleItemStandStatusEnum standStatusEnum;
        WorkStandTypeEnum standTypeEnum;
        ApprovalRuleItemStandDO ruleStandDO;
        Map<Long, ApprovalRuleStandListRespDTO> standPool = null;
        if (query.isContainItemStandEnable()) {
            standPool = new HashMap<>();
        }
        if (dbData.size() > 0) {
            for (ApprovalRuleUnitItemDO itemDO : dbData) {
                respDTO = new ApprovalRuleUnitItemListRespDTO();
                respDTO.setItemId(itemDO.getId());
                respDTO.setItemName(itemDO.getItemName());
                respDTO.setItemStandId(itemDO.getStandId());
                respDTO.setRuleId(itemDO.getRuleId());
                respDTO.setStepId(itemDO.getStepId());
                respDTO.setUnitId(itemDO.getUnitId());
                respDTO.setItemType(itemDO.getType());
                respDTO.setStatus(itemDO.getStatus());
                itemTypeEnum = RuleItemTypeEnum.parse(itemDO.getType());
                if (itemTypeEnum != null) {
                    respDTO.setItemTypeDesc(itemTypeEnum.getValue());
                }
                statusEnum = RuleItemStatusEnum.parse(itemDO.getStatus());
                if (statusEnum != null) {
                    respDTO.setStatusDesc(statusEnum.getValue());
                }
 
                // 是否包含审批项标准
                if (query.isContainItemStandEnable()) {
                    assert standPool != null;
                    if (itemDO.getStandId() != null) {
                        if (!standPool.containsKey(itemDO.getStandId())) {
                            ruleStandDO = approvalRuleStandService.getRuleStandDOById(itemDO.getStandId());
                            if (ruleStandDO != null && ruleStandDO.getStatus().equals(RuleItemStatusEnum.VALID.getCode())) {
                                ApprovalRuleStandListRespDTO standListRespDTO = new ApprovalRuleStandListRespDTO();
                                standListRespDTO.setRuleStandId(ruleStandDO.getId());
                                standListRespDTO.setRuleStandType(ruleStandDO.getType());
                                standListRespDTO.setTitle(ruleStandDO.getTitle());
                                standListRespDTO.setMinVal(ruleStandDO.getMinVal());
                                standListRespDTO.setMinValMatchPattern(ruleStandDO.getMinValMatchPattern());
                                standListRespDTO.setMaxVal(ruleStandDO.getMaxVal());
                                standListRespDTO.setMaxValMatchPattern(ruleStandDO.getMaxValMatchPattern());
                                standListRespDTO.setStatus(ruleStandDO.getStatus());
                                standStatusEnum = RuleItemStandStatusEnum.parse(ruleStandDO.getStatus());
                                if (standStatusEnum != null) {
                                    standListRespDTO.setStatusDesc(standStatusEnum.getValue());
                                }
                                standTypeEnum = WorkStandTypeEnum.parse(ruleStandDO.getType());
                                if (standTypeEnum != null) {
                                    standListRespDTO.setRuleStandTypeDesc(standTypeEnum.getDesc());
                                }
                                standPool.put(itemDO.getStandId(), standListRespDTO);
                            }
                            respDTO.setItemStand(standPool.get(itemDO.getStandId()));
 
                        }
                    }
                }
 
 
 
                respDTOs.add(respDTO);
            }
        }
        return respDTOs;
    }
 
 
}