郑永安
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.gk.hotwork.specialWork.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.hotwork.Service.Middle.AccountDepartmentService;
import com.gk.hotwork.Domain.co.ContextCacheUser;
import com.gk.hotwork.Domain.Exception.E;
import com.gk.hotwork.Domain.Enum.ResultCodes;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Vo.PageQuery;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Domain.Vo.SearchResultVO;
import com.gk.hotwork.specialWork.entity.ApprovalRuleItemStand;
import com.gk.hotwork.specialWork.entity.ApprovalRuleItemStandDO;
import com.gk.hotwork.specialWork.enums.RuleItemStandStatusEnum;
import com.gk.hotwork.specialWork.enums.WorkStandTypeEnum;
import com.gk.hotwork.specialWork.model.dto.req.ApprovalRuleStandAddReqDTO;
import com.gk.hotwork.specialWork.model.dto.req.ApprovalRuleStandModReqDTO;
import com.gk.hotwork.specialWork.model.dto.req.DeleteForm;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleItemStandPageRespDTO;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleStandListRespDTO;
import com.gk.hotwork.specialWork.model.query.ApprovalRuleStandListQuery;
import com.gk.hotwork.specialWork.model.query.ApprovalRuleStandPageQuery;
import com.gk.hotwork.specialWork.model.query.db.ApprovalRuleStandListDBQuery;
import com.gk.hotwork.specialWork.model.query.db.ApprovalRuleStandPageDBQuery;
import com.gk.hotwork.specialWork.model.update.EntityStatusBatchUO;
import com.gk.hotwork.specialWork.service.RuleStandService;
import com.gk.hotwork.specialWork.service.baseService.ApprovalRuleStandService;
import com.gk.hotwork.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.List;
 
 
@Service("ruleStandService")
public class RuleStandServiceImpl implements RuleStandService {
 
    @Autowired
    private AccountDepartmentService accountDepartmentService;
    @Autowired
    private ApprovalRuleStandService approvalRuleStandService;
    @Autowired
    private ApprovalRuleUnitItemService approvalRuleUnitItemService;
 
 
    @Override
    public void saveRuleStand(ContextCacheUser currentUser, ApprovalRuleStandAddReqDTO addReqDTO) {
        ;
        if (addReqDTO.getRuleStandType() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (StringUtils.isBlank(addReqDTO.getTitle())) {
            throw new BusinessException(E.DATA_PARAM_NULL, "标题为空");
        }
        WorkStandTypeEnum standTypeEnum = WorkStandTypeEnum.parse(addReqDTO.getRuleStandType());
        if (standTypeEnum == null) {
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "标准类型参数不合法");
        }
 
        // 1.保存审批项标准
        ApprovalRuleItemStand itemStandEntity = new ApprovalRuleItemStand();
//        itemStandEntity.setDepId(addReqDTO.getDepId());
        itemStandEntity.setType(addReqDTO.getRuleStandType());
//        itemStandEntity.setEid(addReqDTO.getEid());
        itemStandEntity.setMaxVal(addReqDTO.getMaxVal());
        itemStandEntity.setMaxValMatchPattern(addReqDTO.getMaxValMatchPattern());
        itemStandEntity.setMinVal(addReqDTO.getMinVal());
        itemStandEntity.setMinValMatchPattern(addReqDTO.getMinValMatchPattern());
        itemStandEntity.setTitle(addReqDTO.getTitle());
        itemStandEntity.setInfo(addReqDTO.getInfo());
        itemStandEntity.setCreateUid(currentUser.getUid());
        itemStandEntity.setCreateUname(currentUser.getRealName());
        itemStandEntity.setGmtCreate(LocalDateTime.now());
        itemStandEntity.setStatus(RuleItemStandStatusEnum.VALID.getCode());
        approvalRuleStandService.saveRuleStand(itemStandEntity);
    }
 
    @Override
    public void updateRuleStand(ContextCacheUser currentUser, ApprovalRuleStandModReqDTO modReqDTO) {
        ;
        if (modReqDTO.getRuleStandType() == null ||
                modReqDTO.getRuleStandId() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (StringUtils.isBlank(modReqDTO.getTitle())) {
            throw new BusinessException(E.DATA_PARAM_NULL, "标题为空");
        }
        WorkStandTypeEnum standTypeEnum = WorkStandTypeEnum.parse(modReqDTO.getRuleStandType());
        if (standTypeEnum == null) {
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "标准类型参数不合法");
        }
        ApprovalRuleItemStandDO ruleStandDO = approvalRuleStandService.getRuleStandDOById(modReqDTO.getRuleStandId());
        if (ruleStandDO == null) {
            throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "审批标准不存在");
        }
        if (!ruleStandDO.getStatus().equals(RuleItemStandStatusEnum.VALID.getCode())) {
            throw new BusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID, "审批标准不存在");
        }
 
        if (!WorkStandTypeEnum.checkWorkStandType(modReqDTO.getRuleStandType())) {
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "标准类型参数不合法");
        }
        // 更新
        ApprovalRuleItemStand itemStandEntity = new ApprovalRuleItemStand();
        itemStandEntity.setId(modReqDTO.getRuleStandId());
//        itemStandEntity.setDepId(modReqDTO.getDepId());
        itemStandEntity.setTitle(modReqDTO.getTitle());
        itemStandEntity.setMinVal(modReqDTO.getMinVal());
        itemStandEntity.setMinValMatchPattern(modReqDTO.getMinValMatchPattern());
        itemStandEntity.setMaxVal(modReqDTO.getMaxVal());
        itemStandEntity.setMaxValMatchPattern(modReqDTO.getMaxValMatchPattern());
        itemStandEntity.setType(modReqDTO.getRuleStandType());
        itemStandEntity.setGmtModified(LocalDateTime.now());
        itemStandEntity.setModifiedUid(currentUser.getUid());
        itemStandEntity.setModifiedUname(currentUser.getRealName());
        itemStandEntity.setInfo(modReqDTO.getInfo());
        approvalRuleStandService.updateRuleStand(itemStandEntity);
    }
 
    @Override
    public List<ApprovalRuleStandListRespDTO> listRuleStand(ContextCacheUser currentUser,ApprovalRuleStandListQuery query) {
        ;
        // 查询对象
        ApprovalRuleStandListDBQuery dbQuery = new ApprovalRuleStandListDBQuery();
        dbQuery.setRuleStandType(query.getRuleStandType());
        dbQuery.setStatus(RuleItemStandStatusEnum.VALID.getCode());
        dbQuery.setTitle(query.getTitle());
        // 执行
        List<ApprovalRuleItemStandDO> dbData = approvalRuleStandService.listRuleStandDO(dbQuery);
        List<ApprovalRuleStandListRespDTO> respDTOs = new ArrayList<>(dbData.size());
        ApprovalRuleStandListRespDTO respDTO;
        RuleItemStandStatusEnum itemStandStatusEnum;
        WorkStandTypeEnum standTypeEnum;
        if (dbData.size() > 0) {
            for (ApprovalRuleItemStandDO ruleItemStandDO : dbData) {
                respDTO = new ApprovalRuleStandListRespDTO();
                respDTO.setTitle(ruleItemStandDO.getTitle());
                respDTO.setRuleStandId(ruleItemStandDO.getId());
                respDTO.setTitle(ruleItemStandDO.getTitle());
                respDTO.setMaxVal(ruleItemStandDO.getMaxVal());
                respDTO.setMaxValMatchPattern(ruleItemStandDO.getMaxValMatchPattern());
                respDTO.setMinVal(ruleItemStandDO.getMinVal());
                respDTO.setMinValMatchPattern(ruleItemStandDO.getMinValMatchPattern());
                respDTO.setRuleStandType(ruleItemStandDO.getType());
                respDTO.setStatus(ruleItemStandDO.getStatus());
                respDTO.setInfo(ruleItemStandDO.getInfo());
                standTypeEnum = WorkStandTypeEnum.parse(ruleItemStandDO.getType());
                if (standTypeEnum != null) {
                    respDTO.setRuleStandTypeDesc(standTypeEnum.getDesc());
                }
                itemStandStatusEnum = RuleItemStandStatusEnum.parse(ruleItemStandDO.getStatus());
                if (itemStandStatusEnum != null) {
                    respDTO.setStatusDesc(itemStandStatusEnum.getValue());
                }
                respDTOs.add(respDTO);
            }
        }
        return respDTOs;
    }
 
    @Override
    public SearchResultVO<List<ApprovalRuleItemStandPageRespDTO>> listRuleStandByPage(ContextCacheUser currentUser, PageQuery<ApprovalRuleStandPageQuery> query) {
        ;
        // 查询对象
        ApprovalRuleStandPageDBQuery dbQuery = new ApprovalRuleStandPageDBQuery();
        dbQuery.setStatus(RuleItemStandStatusEnum.VALID.getCode());
        if (query.getSearchParams() != null) {
            dbQuery.setTitle(query.getSearchParams().getTitle());
            dbQuery.setRuleStandType(query.getSearchParams().getRuleStandType());
        }
        // 分页对象
        Page<ApprovalRuleItemStand> page = new Page<>(query.getPageIndex(), query.getPageSize());
        List<ApprovalRuleItemStand> dbData = approvalRuleStandService.listRuleStandByPage(page, dbQuery);
        List<ApprovalRuleItemStandPageRespDTO> respDTOs = new ArrayList<>(dbData.size());
        ApprovalRuleItemStandPageRespDTO respDTO;
        RuleItemStandStatusEnum itemStandStatusEnum;
        WorkStandTypeEnum standTypeEnum;
        if (dbData.size() > 0) {
            for (ApprovalRuleItemStand ruleItemStandDO : dbData) {
                respDTO = new ApprovalRuleItemStandPageRespDTO();
                respDTO.setRuleStandId(ruleItemStandDO.getId());
                respDTO.setMaxVal(ruleItemStandDO.getMaxVal());
                respDTO.setMaxValMatchPattern(ruleItemStandDO.getMaxValMatchPattern());
                respDTO.setMinVal(ruleItemStandDO.getMinVal());
                respDTO.setMinValMatchPattern(ruleItemStandDO.getMinValMatchPattern());
                respDTO.setRuleStandType(ruleItemStandDO.getType());
                respDTO.setTitle(ruleItemStandDO.getTitle());
                respDTO.setInfo(ruleItemStandDO.getInfo());
                respDTO.setStatus(ruleItemStandDO.getStatus());
                respDTO.setCreateUid(ruleItemStandDO.getCreateUid());
                respDTO.setCreateUname(ruleItemStandDO.getCreateUname());
                respDTO.setGmtCreate(ruleItemStandDO.getGmtCreate());
                respDTO.setModifiedUid(ruleItemStandDO.getModifiedUid());
                respDTO.setModifiedUname(ruleItemStandDO.getModifiedUname());
                respDTO.setGmtModified(ruleItemStandDO.getGmtModified());
                standTypeEnum = WorkStandTypeEnum.parse(ruleItemStandDO.getType());
                if (standTypeEnum != null) {
                    respDTO.setRuleStandTypeDesc(standTypeEnum.getDesc());
                }
                itemStandStatusEnum = RuleItemStandStatusEnum.parse(ruleItemStandDO.getStatus());
                if (itemStandStatusEnum != null) {
                    respDTO.setStatusDesc(itemStandStatusEnum.getValue());
                }
                respDTOs.add(respDTO);
            }
        }
 
        return new SearchResultVO<>(
                true,page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                respDTOs,
                ResultCodes.OK);
    }
 
    @Override
    public void deleteRuleStand(ContextCacheUser currentUser, DeleteForm deleteForm) {
        ;
        if (deleteForm.getIds() == null || deleteForm.getIds().size() == 0) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (deleteForm.getIds().size() > 10) {
            throw new BusinessException(ResultCodes.SERVER_DEL_OVER_SIZE);
        }
        ApprovalRuleItemStandDO ruleItemStandDO;
        long count;
        for (Long ruleStandId : deleteForm.getIds()) {
            ruleItemStandDO = approvalRuleStandService.getRuleStandDOById(ruleStandId);
            if (ruleItemStandDO == null) {
                throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"审批项标准不存在");
            }
            if (!ruleItemStandDO.getStatus().equals(RuleItemStandStatusEnum.VALID.getCode())) {
                throw new BusinessException(E.DATA_DATABASE_EXIST_BUT_NOT_VALID,"审批项标准不存在");
            }
            // 是否被审批项占用
            count = approvalRuleUnitItemService.countByRuleStandId(ruleStandId);
            if (count > 0) {
                throw new BusinessException(E.DATA_BING_RELATION, "审批项关联,无法删除");
            }
        }
        EntityStatusBatchUO batchDeleteObj = new EntityStatusBatchUO();
        batchDeleteObj.setIds(deleteForm.getIds());
        batchDeleteObj.setModifiedUname(currentUser.getRealName());
        batchDeleteObj.setGmtModified(LocalDateTime.now());
        batchDeleteObj.setModifiedUid(currentUser.getUid());
        batchDeleteObj.setStatus(RuleItemStandStatusEnum.ABANDONED.getCode());
        approvalRuleStandService.deleteBatch(batchDeleteObj);
    }
}