whycxzp
2023-08-16 618fd4fe93cb0901011c59f05a4a68376b378fe2
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.RoleEnum;
import com.whyc.constant.WorkflowDischargePlanEnum;
import com.whyc.constant.WorkflowEnum;
import com.whyc.dto.Response;
import com.whyc.dto.WorkflowLinkDTO;
import com.whyc.mapper.*;
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class WorkflowLinkService {
 
    @Resource
    private WorkflowLinkMapper mapper;
 
    @Autowired
    private WorkflowMainService mainService;
 
    @Autowired
    private BattDischargePlanTempService battDischargePlanTempService;
 
    @Autowired
    private BattdischargePlanService planService;
 
    @Autowired(required = false)
    private Fbs9100SetParamTempMapper tmpMapper;
 
    @Autowired(required = false)
    private BatttestdataInfMapper testInfMapper;
 
    @Autowired(required = false)
    private BattInfMapper binfMapper;
 
    @Autowired(required = false)
    private WorkflowMainMapper mainMapper;
 
    @Autowired(required = false)
    private WorkflowLinkMapper linkMapper;
 
    public void addBatch(List<WorkflowLink> links) {
        mapper.insertBatchSomeColumn(links);
    }
 
    public List<WorkflowLink> getWorkflowInfo(Integer mainId) {
        QueryWrapper<WorkflowLink> wrapper = Wrappers.query();
        wrapper.eq("main_id", mainId);
        return mapper.selectList(wrapper);
    }
 
 
    /**传入id,mainId,processLevel,status,dealReason或者dealRejectReason
     * @param linkDTO 节点数据传输类
     * */
    public Response updateLinkOfDischargePlanTemp(WorkflowLinkDTO linkDTO) {
        WorkflowLink link = linkDTO.getLink();
        List<BattDischargePlanTemp> tempList = linkDTO.getTempList();
        String processLevel = link.getProcessLevel();
        Date now = new Date();
        int userId = ActionUtil.getUser().getUId().intValue();
        switch (processLevel){
            case "P1"://当前节点为过程1-会签,本场景没有拒绝选项
            {
                battDischargePlanTempService.updateList(tempList);
                updateLinkField(link);
                //判断会签是否完成,完成则新建下一步的节点
                int leftAssignNum = getLeftAssignNum(link.getMainId(),link.getProcessLevel());
                if(leftAssignNum == 0){ //剩余会签数量为0,说明会签已经完成
                    WorkflowLink linkNew = new WorkflowLink();
                    linkNew.setMainId(link.getMainId());
                    linkNew.setParentId(link.getId());
                    linkNew.setProcessLevel(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevel());
                    linkNew.setProcessLevelName(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevelName());
                    linkNew.setCreateTime(now);
                    linkNew.setDealRoleId(RoleEnum.ADMIN.getId());
                    linkNew.setDealType(WorkflowEnum.TYPE_TRANSFER_DISPATCH.getValue());
                    linkNew.setDealDesc("班组会签完成");
                    linkNew.setStatus(WorkflowEnum.STATUS_TAKING.getValue());
                    mapper.insert(linkNew);
                }
                break;
            }
            case "P2"://当前节点为过程2-管理审核
            {
                link.setDealUserId(userId);
                if(link.getStatus() == WorkflowEnum.STATUS_REJECT.getValue().intValue()){ //驳回
                    updateLinkFieldAndFinish(link);
                }else{ //通过
                    updateLinkField(link);
                    //新建下一节点-领导层
                    WorkflowLink linkNew = new WorkflowLink();
                    linkNew.setMainId(link.getMainId());
                    linkNew.setParentId(link.getId());
                    linkNew.setProcessLevel(WorkflowDischargePlanEnum.PROCESS_MANAGER2SIGN.getProcessLevel());
                    linkNew.setProcessLevelName(WorkflowDischargePlanEnum.PROCESS_MANAGER2SIGN.getProcessLevelName());
                    linkNew.setCreateTime(now);
                    linkNew.setDealRoleId(RoleEnum.LEADER.getId());
                    linkNew.setDealType(WorkflowEnum.TYPE_TRANSFER_DISPATCH.getValue());
                    String dealUserName = ActionUtil.getUser().getUName();
                    linkNew.setDealDesc(dealUserName+"审核完成");
                    linkNew.setStatus(WorkflowEnum.STATUS_TAKING.getValue());
                    mapper.insert(linkNew);
                }
                break;
            }
            case "P3"://当前节点为过程3-领导审批
            {
                link.setDealUserId(userId);
                updateLinkFieldAndFinish(link);
 
                Integer mainId = link.getMainId();
                WorkflowMain main = mainService.getOne(mainId);
                if(main.getType()==1){ //将计划转移到正式表
                    List<BattDischargePlanTemp> tempListByMain = battDischargePlanTempService.getListByMainId(mainId);
                    planService.insertConfirmDischargePlan(tempListByMain);
                }
                break;
            }
        }
        return new Response().setII(1,"更新完成");
    }
 
    /**
     *
     * @param mainId 特定的单据
     * @param processLevel 特定的过程
     * @return 特定单据,特定过程,未完成的会签的数量
     */
    private int getLeftAssignNum(Integer mainId, String processLevel) {
        QueryWrapper<WorkflowLink> query = Wrappers.query();
        query.select("count(*) as id").eq("main_id",mainId).eq("process_level",processLevel).ne("status",WorkflowEnum.STATUS_FINISH.getValue());
        return mapper.selectOne(query).getId();
    }
 
    /**
     * 更新节点,主节点不完结
     * @param link 参数id,status,dealReason或者dealRejectReason
     */
    private void updateLinkField(WorkflowLink link) {
        Date now = new Date();
        link.setDealTime(now);
        mapper.updateById(link);
    }
 
    /**
     * 更新节点,主节点完结,单据完结
     * @param link 参数id,mainId,status,dealReason或者dealRejectReason
     */
    private void updateLinkFieldAndFinish(WorkflowLink link) {
        updateLinkField(link);
        Date now = link.getDealTime();
        if(link.getStatus() == WorkflowEnum.STATUS_FINISH.getValue().intValue()){ //通过
            mainService.updateStatus(link.getMainId(),WorkflowEnum.MAIN_STATUS_END_PASS.getValue(),link.getDealReason(),now);
        }else{ //驳回
            mainService.updateStatus(link.getMainId(),WorkflowEnum.MAIN_STATUS_END_REJECT.getValue(),link.getDealRejectReason(),now);
        }
    }
 
    public Map<Integer, Integer> getReceivedStatistics(int type, UserInf user) {
        Map<Integer,Integer> statistics = new HashMap<>();
        statistics.put(1,0);
        statistics.put(6,0);
        statistics.put(58,0);
        List<WorkflowLink> links = mapper.getReceivedList(type,user);
        Map<Integer, List<WorkflowLink>> receivedListMap = links.stream().collect(Collectors.groupingBy(WorkflowLink::getStatus));
        Set<Integer> statusSet = receivedListMap.keySet();
        for (Integer status : statusSet) {
            if (status == 5 || status == 8) {
                statistics.put(58, statistics.get(58) + receivedListMap.get(status).size());
            } else {
                statistics.put(status, receivedListMap.get(status).size());
            }
        }
        return statistics;
    }
 
    public void add(WorkflowLink link) {
        mapper.insert(link);
    }
 
    //更新节点(除放电计划之外的)
    public Response linkOfTemp(WorkflowLink link) {
        Date now = new Date();
        int userId = ActionUtil.getUser().getUId().intValue();
        int roleId = ActionUtil.getUser().getURole();
        switch (roleId) {
            case 0://当前节点为过程0-管理员审批
            {
                link.setDealUserId(userId);
                link.setDealDesc("管理员处理通过");
                if (link.getStatus() == WorkflowEnum.STATUS_REJECT.getValue().intValue()) { //驳回
                    link.setDealDesc("管理员驳回");
                    updateLinkFieldAndFinish(link);
                } else { //通过
                    updateLinkField(link);
                    //新建下一节点-领导层
                    WorkflowLink linkNew = new WorkflowLink();
                    linkNew.setMainId(link.getMainId());
                    linkNew.setParentId(link.getId());
                    linkNew.setCreateTime(now);
                    linkNew.setDealRoleId(RoleEnum.LEADER.getId());
                    linkNew.setDealType(WorkflowEnum.TYPE_TRANSFER_DISPATCH.getValue());
                    linkNew.setDealDesc("领导层处理中");
                    linkNew.setStatus(WorkflowEnum.STATUS_WAIT_FOR_APPROVE.getValue());
                    mapper.insert(linkNew);
                }
                break;
            }
            case 10://当前节点为过程10-领导层审批
            {
                link.setDealUserId(userId);
                if (link.getStatus() == WorkflowEnum.STATUS_REJECT.getValue().intValue()) { //驳回
                    link.setDealDesc("领导层驳回");
                } else {
                    link.setDealDesc("领导层审批通过");
                }
                updateLinkFieldAndFinish(link);
            }
        }
        return new Response().set(1, true);
    }
 
    //查询节点(除放电计划之外的)
    public Response searchTmp(int uId, int uRole, int type, int status, int pageNum, int pageSize) {
        //查询自己需要处理或者审批的
        PageHelper.startPage(pageNum, pageSize);
        List<WorkflowLink> list = mapper.searchTmp(uId, uRole, type, status);
        PageInfo pageInfo = new PageInfo(list);
        return new Response().setII(1, list == null ? false : true, pageInfo, "查询节点(除放电计划之外的)");
    }
 
    //节点为二次放电时查询二次放电参数和本年上一次有效核容数据
    public Response searchLastHrById(int mid) {
        Map<String, Object> map = searchByMId(mid);
        return new Response().setII(1, true, map, "节点为二次放电时查询二次放电参数和本年上一次有效核容数据");
    }
 
    //查询自己申请的工单(去除放电计划)
    public Response getFlowMainByUid(int uId, int type, int status, int pageNum, int pageSize) {
        //查询自己需要处理或者审批的
        PageHelper.startPage(pageNum, pageSize);
        List<WorkflowMain> list = mapper.getFlowMainByUid(uId, type, status);
        if (list != null && list.size() > 0) {
            for (WorkflowMain main : list) {
                if (main.getType() == 2) {
                    Map<String, Object> map = searchByMId(main.getId());
                    main.setHrMap(map);
                }
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return new Response().setII(1, list == null ? false : true, pageInfo, "查询自己申请的工单(去除放电计划)");
    }
 
    //节点为二次放电时查询二次放电参数和本年上一次有效核容数据
    public Map searchByMId(int mid) {
        Map<String, Object> map = new HashMap<>();
        //查询二次放电参数
        QueryWrapper tmpWrapper = Wrappers.query();
        tmpWrapper.eq("main_id", mid);
        tmpWrapper.last("limit 1");
        Fbs9100SetParamTemp tmp = tmpMapper.selectOne(tmpWrapper);
        map.put("paramTmp", tmp);
        //main表单信息
        QueryWrapper mainWrapper = Wrappers.query();
        mainWrapper.eq("id", mid);
        mainWrapper.last("limit 1");
        WorkflowMain main = mainMapper.selectOne(mainWrapper);
        map.put("main", main);
        //link表单信息
        QueryWrapper linkWrapper = Wrappers.query();
        linkWrapper.eq("main_id", mid);
        linkWrapper.orderByAsc("parent_id");
        List<WorkflowLink> linkList = linkMapper.selectList(linkWrapper);
        map.put("linkList", linkList);
        //本年上一次有效核容数据
        if (tmp != null) {
            BatttestdataInf tinf = testInfMapper.searchHrYear(tmp.getBattGroupId());
            map.put("testInf", tinf);
            QueryWrapper infWrapper = new QueryWrapper();
            infWrapper.eq("battGroupId", tmp.getBattGroupId());
            infWrapper.last("limit 1");
            Battinf binf = binfMapper.selectOne(infWrapper);
            map.put("binf", binf);
        }
        return map;
    }
 
}