lxw
2023-05-25 f3c27fb78447449a950ba73c5e72ceda64ad8a12
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.WorkflowLinkMapper;
import com.whyc.pojo.BattDischargePlanTemp;
import com.whyc.pojo.UserInf;
import com.whyc.pojo.WorkflowLink;
import com.whyc.pojo.WorkflowMain;
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;
 
    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;
    }
}