郑永安
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
package com.gk.hotwork.specialWork.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.hotwork.Domain.Exception.BusinessException;
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.Vo.PageQuery;
import com.gk.hotwork.Domain.Vo.ResultVO;
import com.gk.hotwork.Domain.Vo.SearchResultVO;
import com.gk.hotwork.specialWork.entity.ApprovalRuleItemMeasure;
import com.gk.hotwork.specialWork.enums.MeasureTypeEnum;
import com.gk.hotwork.specialWork.enums.RuleMeasureOptEnum;
import com.gk.hotwork.specialWork.enums.WorkTypeEnum;
import com.gk.hotwork.specialWork.model.dto.req.*;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleMeasurePageRespDTO;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleMeasureRespDTO;
import com.gk.hotwork.specialWork.model.query.db.ApprovalRuleItemMeasureQuery;
import com.gk.hotwork.specialWork.service.RuleMeasureService;
import com.gk.hotwork.specialWork.service.baseService.ApprovalRuleItemMeasureService;
import com.gk.hotwork.specialWork.service.baseService.ApprovalRuleUnitItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
@Service("ruleMeasureServiceImpl")
public class RuleMeasureServiceImpl implements RuleMeasureService {
 
    @Autowired
    private ApprovalRuleItemMeasureService approvalRuleItemMeasureService;
    @Autowired
    private ApprovalRuleUnitItemService approvalRuleUnitItemService;
    @Override
    public ResultVO saveMeasure(ContextCacheUser currentUser, ApprovalRuleItemMeasureAddReqDTO addReqDTO) {
        if (!WorkTypeEnum.checkWorkType(addReqDTO.getWorkType())) {
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"作业类型不合法!");
        }
        if(!MeasureTypeEnum.checkMeasureType(addReqDTO.getType())){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型不合法!");
        }
 
        ApprovalRuleItemMeasure measure = new ApprovalRuleItemMeasure();
        //如果是选择类型,正确值就不能为空
        if(addReqDTO.getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())){
            if(null == RuleMeasureOptEnum.parse(addReqDTO.getCorrectVal())){
                throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"正确值不合法!");
            }
            measure.setCorrectVal(addReqDTO.getCorrectVal());
        }
        measure.setContext(addReqDTO.getContext());
        measure.setWorkType(addReqDTO.getWorkType());
        measure.setType(addReqDTO.getType());
        measure.setCreateUid(currentUser.getUid());
        measure.setCreateUname(currentUser.getUsername());
        int i = approvalRuleItemMeasureService.saveOne(measure);
        if(i > 0){
            return new ResultVO(ResultCodes.OK);
        }
        return new ResultVO(ResultCodes.SERVER_ADD_ERROR);
    }
 
    @Override
    public ResultVO modMeasure(ContextCacheUser currentUser, ApprovalRuleItemMeasureModReqDTO modReqDTO) {
        if (!WorkTypeEnum.checkWorkType(modReqDTO.getWorkType())) {
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"作业类型不合法!");
        }
        if(!MeasureTypeEnum.checkMeasureType(modReqDTO.getType())){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型不合法!");
        }
        ApprovalRuleItemMeasure measure = new ApprovalRuleItemMeasure();
 
        //如果是选择类型,正确值就不能为空
        if(modReqDTO.getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())){
            if(null == RuleMeasureOptEnum.parse(modReqDTO.getCorrectVal())){
                throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"正确值不合法!");
 
            }
            measure.setCorrectVal(modReqDTO.getCorrectVal());
        }
        measure.setId(modReqDTO.getId());
        measure.setContext(modReqDTO.getContext());
        measure.setWorkType(modReqDTO.getWorkType());
        measure.setType(modReqDTO.getType());
        measure.setModifiedUid(currentUser.getUid());
        measure.setModifiedUname(currentUser.getUsername());
        int i = approvalRuleItemMeasureService.updateOne(measure);
        if(i > 0){
            return new ResultVO(ResultCodes.OK);
        }
        return new ResultVO(ResultCodes.SERVER_UPDATE_ERROR);
    }
 
    @Override
    public ResultVO deleteByIds(ContextCacheUser currentUser, DeleteForm deleteForm) {
        //判断是否被审批项绑定
        int count = approvalRuleUnitItemService.countByMeasureIds(deleteForm.getIds());
        if(count > 0){
            throw new BusinessException(E.DATA_BING_RELATION,"存在安全措施被审批项绑定,不可删除!");
        }
        int i = approvalRuleItemMeasureService.updateStatusByIds(deleteForm.getIds());
        if(i > 0){
            return new ResultVO(ResultCodes.OK);
        }
        return new ResultVO(ResultCodes.SERVER_DEL_ERROR);
    }
 
    @Override
    public ResultVO<List<ApprovalRuleMeasureRespDTO>> listMeasure(ContextCacheUser currentUser, ApprovalRuleItemMeasureQuery query) {
        List<ApprovalRuleItemMeasure> list = approvalRuleItemMeasureService.list(query);
        List<ApprovalRuleMeasureRespDTO> respList = new ArrayList<>();
        list.forEach(measure -> {
            ApprovalRuleMeasureRespDTO respDTO = new ApprovalRuleMeasureRespDTO();
            BeanUtils.copyProperties(measure,respDTO);
            respList.add(respDTO);
        });
        return new ResultVO<>(ResultCodes.OK,respList);
    }
 
    @Override
    public SearchResultVO<List<ApprovalRuleMeasurePageRespDTO>> listMeasureByPage(ContextCacheUser currentUser, PageQuery<ApprovalRuleItemMeasureQuery> query) {
 
        Page<ApprovalRuleItemMeasure> page = new Page<>(query.getPageIndex(), query.getPageSize());
        List<ApprovalRuleItemMeasure> list = approvalRuleItemMeasureService.listBypage(page,query.getSearchParams());
        List<ApprovalRuleMeasurePageRespDTO> respList = new ArrayList<>();
        list.forEach(measure -> {
            ApprovalRuleMeasurePageRespDTO respDTO = new ApprovalRuleMeasurePageRespDTO();
            BeanUtils.copyProperties(measure,respDTO);
            respList.add(respDTO);
        });
        return new SearchResultVO<>(
                true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK);
    }
}