fix
songhuangfeng123
2022-09-20 f1f506172a3edff8d31e4db77b8940f115660919
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
package com.gkhy.safePlatform.targetDuty.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.utils.PageUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.targetDuty.entity.TargetDivideDetail;
import com.gkhy.safePlatform.targetDuty.entity.TargetDutyWorkApprove;
import com.gkhy.safePlatform.targetDuty.enums.TargetDutyResultCodes;
import com.gkhy.safePlatform.targetDuty.excepiton.TargetDutyException;
import com.gkhy.safePlatform.targetDuty.model.dto.req.TargetDutyWorkApproveQueryCriteria;
import com.gkhy.safePlatform.targetDuty.model.dto.resp.ListCheckDataDto;
import com.gkhy.safePlatform.targetDuty.model.dto.resp.TargetApproveDivideDetailDto;
import com.gkhy.safePlatform.targetDuty.model.dto.resp.TargetDutyWorkApproveDto;
import com.gkhy.safePlatform.targetDuty.model.dto.resp.WorkApproveRelateDesc;
import com.gkhy.safePlatform.targetDuty.service.CommonService;
import com.gkhy.safePlatform.targetDuty.service.TargetDutyWorkApproveService;
import com.gkhy.safePlatform.targetDuty.service.baseService.TargetDivideDetailBaseService;
import com.gkhy.safePlatform.targetDuty.service.baseService.TargetDutyWorkApproveBaseService;
import com.gkhy.safePlatform.targetDuty.utils.QueryHelpPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * 目标责任管理工作流审批表(TargetDutyWorkApprove)表服务实现类
 *
 * @author xurui
 * @since 2022-08-11 10:13:37
 */
@Service("targetDutyWorkApproveService")
public class TargetDutyWorkApproveServiceImpl implements TargetDutyWorkApproveService {
 
    @Autowired
    private TargetDutyWorkApproveBaseService targetDutyWorkApproveBaseService;
 
    @Resource
    private CommonService commonService;
 
    @Autowired
    private TargetDivideDetailBaseService targetDivideDetailBaseService;
    
    
    @Override
    public ResultVO queryAll(PageQuery<TargetDutyWorkApproveQueryCriteria> pageQuery) {
        PageUtils.checkCheck(pageQuery);
        Long pageIndex = pageQuery.getPageIndex();
        Long pageSize = pageQuery.getPageSize();
        IPage<TargetDutyWorkApprove> page = new Page<>(pageIndex, pageSize);
 
        QueryWrapper queryWrapper = QueryHelpPlus.getPredicate(TargetDutyWorkApprove.class, pageQuery.getSearchParams());
        queryWrapper.eq("del_flag","0");
        page = targetDutyWorkApproveBaseService.selectPage(page,queryWrapper);
        List<TargetDutyWorkApproveDto> respList = BeanCopyUtils.copyBeanList(page.getRecords(), TargetDutyWorkApproveDto.class);
 
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
        );
    }
 
 
    @Override
    public List<TargetDutyWorkApprove> queryAll(TargetDutyWorkApproveQueryCriteria criteria) {
        return targetDutyWorkApproveBaseService.queryAll( criteria);
    }
 
    @Override
    public void updateInfo(ContextCacheUser currentUser, TargetDutyWorkApprove targetDutyWorkApprove) {
 
        Date nowDate = new Date();
        Long uid = currentUser.getUid();
        String uName = currentUser.getRealName();
 
        //新增下级审批记录
        TargetDutyWorkApprove addInfo = new TargetDutyWorkApprove();
        if (targetDutyWorkApprove.getApproveStatus() == 2){
            BeanUtils.copyProperties(targetDutyWorkApprove, addInfo);
 
            addInfo.setId(null);
            addInfo.setDelFlag(0);
            addInfo.setCreateUid(uid);
            addInfo.setGmtCreate(new Timestamp(nowDate.getTime()));
            addInfo.setSubmitPersonId(uid);
            addInfo.setSubmitPersonName(uName);
            addInfo.setApproveResult(null);
            addInfo.setApproveMemo(null);
            addInfo.setSort(addInfo.getSort()+1);
            targetDutyWorkApproveBaseService.save(addInfo);
        }
 
        // 更新原审批记录
        TargetDutyWorkApprove updateInfo = new TargetDutyWorkApprove();
        updateInfo.setId(targetDutyWorkApprove.getId());
        updateInfo.setUpdateUid(uid);
        updateInfo.setGmtModitify(new Timestamp(nowDate.getTime()));
        updateInfo.setApproveStatus(targetDutyWorkApprove.getApproveStatus());
        updateInfo.setApproveResult(targetDutyWorkApprove.getApproveResult());
        updateInfo.setApproveMemo(targetDutyWorkApprove.getApproveMemo());
        updateInfo.setChildId(addInfo.getId());
        targetDutyWorkApproveBaseService.updateById(updateInfo);
    }
 
    @Override
    public void addInfo(ContextCacheUser currentUser, TargetDutyWorkApprove targetDutyWorkApprove) {
        // 业务类型
        if (targetDutyWorkApprove.getRelateType()==null){
            throw new TargetDutyException(TargetDutyResultCodes.APPROVE_RELATE_ID_NULL);
        }
        // 业务主表id
        if (targetDutyWorkApprove.getRelateId()==null){
            throw new TargetDutyException(TargetDutyResultCodes.APPROVE_RELATE_TYPE_NULL);
        }
        // 关联业务json
        if (!StringUtils.hasText(targetDutyWorkApprove.getRelateDesc())){
            throw new TargetDutyException("参数错误");
        }
        // 审批人
        if (targetDutyWorkApprove.getApprovePersonId()==null){
            throw new TargetDutyException(TargetDutyResultCodes.APPROVE_PERSON_NULL);
        }
        // 审批状态
        if (targetDutyWorkApprove.getApproveStatus()==null){
            throw new TargetDutyException(TargetDutyResultCodes.APPROVE_STATUS_NULL);
        }
 
 
        Date nowDate = new Date();
        Long uid = currentUser.getUid();
        String uName = currentUser.getRealName();
        //1.新增应急审批
        TargetDutyWorkApprove info = new TargetDutyWorkApprove();
        BeanUtils.copyProperties(targetDutyWorkApprove, info);
        info.setDelFlag(0);
        info.setCreateUid(uid);
        info.setGmtCreate(new Timestamp(nowDate.getTime()));
        info.setSubmitPersonId(uid);
        info.setSubmitPersonName(uName);
        info.setSort(1);
        targetDutyWorkApproveBaseService.save(info);
    }
 
    @Override
    public ResultVO listCheckData(PageQuery<TargetDutyWorkApproveQueryCriteria> pageQuery) {
        Long pageIndex = pageQuery.getPageIndex();
        Long pageSize = pageQuery.getPageSize();
        IPage<ListCheckDataDto> page = new Page<>(pageIndex, pageSize);
 
        page = targetDutyWorkApproveBaseService.listCheckData(page,pageQuery.getSearchParams().getRelateId());
 
        List<ListCheckDataDto> respList = page.getRecords();
        // --------------------------- 获取部门信息-----------------------
        //收集所用到的部门ID
        Set<Long> collectDepIdSet = new HashSet();
        respList.forEach(f->{
            collectDepIdSet.add(f.getDutyDepartmentId());
            collectDepIdSet.add(f.getMakerDepartmentId());
        });
        //获取部门名集合
        Map<Long,String> depNameMap = commonService.getDepName(collectDepIdSet);
 
        respList.forEach(f->{
            f.setDutyDepartmentName(depNameMap.get(f.getDutyDepartmentId()));
            f.setMakerDepartmentName(depNameMap.get(f.getMakerDepartmentId()));
        });
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
        );
    }
 
    @Override
    public TargetDutyWorkApproveDto selectOne(Serializable id) {
        TargetDutyWorkApprove targetDutyWorkApprove = targetDutyWorkApproveBaseService.getById(id);
        if(targetDutyWorkApprove == null){
            return new TargetDutyWorkApproveDto();
        }
 
        TargetDutyWorkApproveDto dto = BeanCopyUtils.copyBean(targetDutyWorkApprove, TargetDutyWorkApproveDto.class);
 
        List<WorkApproveRelateDesc> relateDescList = JSONObject.parseArray(targetDutyWorkApprove.getRelateDesc(),WorkApproveRelateDesc.class);
        Map<Long,WorkApproveRelateDesc> relateDescMap = relateDescList.stream()
        .collect(Collectors.toMap(WorkApproveRelateDesc::getId, Function.identity(),(k1, k2)->k1));
 
        //
        List<TargetDivideDetail> dList = targetDivideDetailBaseService.selectBatchIds(relateDescMap.keySet());
        List<TargetApproveDivideDetailDto> divideDetailList = BeanCopyUtils.copyBeanList(dList, TargetApproveDivideDetailDto.class);
 
        divideDetailList.forEach(f->{
            WorkApproveRelateDesc relateDesc = relateDescMap.get(f.getId());
            if(relateDesc != null){
                f.setPersonId(relateDesc.getPersonId());
                f.setPersonName(relateDesc.getPersonName());
                f.setCommitValue(relateDesc.getValue());
            }
        });
 
        // --------------------------- 获取部门信息-----------------------
        //收集所用到的部门ID
        Set<Long> collectDepIdSet = new HashSet();
        divideDetailList.forEach(f->{
            collectDepIdSet.add(f.getDutyDepartmentId());
            collectDepIdSet.add(f.getMakerDepartmentId());
        });
        //获取部门名集合
        Map<Long,String> depNameMap = commonService.getDepName(collectDepIdSet);
 
        divideDetailList.forEach(f->{
            f.setDutyDepartmentName(depNameMap.get(f.getDutyDepartmentId()));
            f.setMakerDepartmentName(depNameMap.get(f.getMakerDepartmentId()));
        });
 
        dto.setTargetDivideDetailList(divideDetailList);
        return dto;
    }
 
    @Override
    public void delete(Long[] ids) {
        if(ids == null){
            throw new TargetDutyException(ResultCodes.CLIENT_PARAM_ILLEGAL);
        }
        List<Long> idList = Arrays.asList(ids);
 
        List<TargetDutyWorkApprove> delList = new ArrayList<>();
        idList.forEach(f->{
            TargetDutyWorkApprove info = new TargetDutyWorkApprove();
            info.setDelFlag(1);
            info.setId(f);
            delList.add(info);
        });
        targetDutyWorkApproveBaseService.updateBatchById(delList);
    }
}