whyclxw
3 天以前 a4e25fc0cd113518980305af3c061892b1b24b14
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.constant.WorkflowEnum;
import com.whyc.dto.Response;
import com.whyc.mapper.*;
import com.whyc.pojo.db_user.User;
import com.whyc.pojo.web_site.*;
import com.whyc.util.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class WorkflowLinkService {
 
    @Resource
    private WorkflowLinkMapper mapper;
 
    @Autowired
    @Lazy
    private WorkflowMainService mainService;
 
    @Autowired
    private WorkflowDeviceService deviceService;
 
    @Autowired
    private DeviceSpareService deviceSpareService;
    @Autowired
    private DeviceScrapService deviceScrapService;
 
 
    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);
    }
 
    @Transactional
    public Response updateLink(WorkflowLink link) {
        //获取当前节点的记录
        WorkflowLink linkInDB = mapper.selectById(link.getId());
        User user = CommonUtil.getUser();
        Date now = new Date();
        //根据id.查询关联的主表
        WorkflowMain mainInDB = mainService.getById(linkInDB.getMainId());
        //查看申请流程类型
        switch (mainInDB.getType()) {
            case 1: { //设备维修申请
                //查看主表的状态
                //因为是单个链路节点,所以不需要判断是不是待处理,肯定只有1个节点. 审批通过或者驳回
                //if(main.getStatus() == WorkflowEnum.MAIN_STATUS_DEALING.getValue().intValue()){ //待处理,下一步是审批通过,待处理 或者驳回
                if(link.getStatus() == WorkflowEnum.LINK_STATUS_PASS.getValue().intValue()){
                    mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_WAIT_FOR_DEALING.getValue());
                    mainService.updateById(mainInDB);
                }else if(link.getStatus() == WorkflowEnum.LINK_STATUS_REJECT.getValue().intValue()){
                    mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_REJECT.getValue());
                    mainInDB.setEndReason(link.getDealRejectReason());
                    mainInDB.setEndTime(now);
                    mainService.updateById(mainInDB);
                    link.setDealAndClose(1);
                }
            }break;
            //设备入库申请
            case 2:
            //设备报废申请
            case 3:{
                if(link.getStatus() == WorkflowEnum.LINK_STATUS_PASS.getValue().intValue()){
                    mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_PASS.getValue());
                    mainInDB.setEndTime(now);
                    mainInDB.setEndReason(link.getDealReason());
                    mainService.updateById(mainInDB);
 
                    List<WorkflowDevice> deviceListInDB = deviceService.getByMainId(mainInDB.getId());
                    //入库
                    if(mainInDB.getType() == 2){
                        List<DeviceSpare> spareList = new ArrayList<>();
                        deviceListInDB.forEach(device -> {
                            DeviceSpare spare = new DeviceSpare();
                            /*spare.setName(device.getName());
                            spare.setModel(device.getModel());
                            spare.setVersion(device.getVersion());
                            spare.setQuantity(device.getQuantity());
                            spare.setBrand(device.getBrand());
                            spare.setType(device.getType());
                            spare.setSupplier(device.getSupplier());*/
                            BeanUtils.copyProperties(device,spare);
 
                            spareList.add(spare);
                        });
                        deviceSpareService.addOrUpdate(spareList);
                    }else{ //进入报废库
                        List<DeviceScrap> scrapList = new ArrayList<>();
                        deviceListInDB.forEach(device -> {
                            DeviceScrap deviceScrap = new DeviceScrap();
                            BeanUtils.copyProperties(device,deviceScrap);
                            deviceScrap.setApplyUserId(mainInDB.getCreateUserId());
                            deviceScrap.setApplyUserName(mainInDB.getCreateUserName());
                            deviceScrap.setCreateTime(now);
                            deviceScrap.setMainId(mainInDB.getId());
                            scrapList.add(deviceScrap);
                        });
                        deviceScrapService.add(scrapList);
 
                    }
                }else if(link.getStatus() == WorkflowEnum.LINK_STATUS_REJECT.getValue().intValue()){
                    mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_REJECT.getValue());
                    mainInDB.setEndReason(link.getDealRejectReason());
                    mainInDB.setEndTime(now);
                    //检查是否有关联工单.
                    // 如果有关联工单,关联工单状态重置为完结待处理,完成时间重置为空
                    if(mainInDB.getRelatedId() != null){
                        Integer relatedId = mainInDB.getRelatedId();
                        mainService.resetRepairStatus(relatedId);
                        //关联工单的设备附属表未处理数量也要回退,等于主表设备数量
                        List<WorkflowDevice> deviceRelatedListInDB = deviceService.getByMainId(mainInDB.getRelatedId());
                        List<WorkflowDevice> deviceListInDB = deviceService.getByMainId(mainInDB.getId());
                        for (int i = 0; i < deviceListInDB.size(); i++) {
                            WorkflowDevice deviceInDB = deviceListInDB.get(i);
                            for (int j = 0; j < deviceRelatedListInDB.size(); j++) {
                                WorkflowDevice deviceRelatedInDB = deviceRelatedListInDB.get(j);
                                if (deviceInDB.getName().equals(deviceRelatedInDB.getName())
                                        && deviceInDB.getModel().equals(deviceRelatedInDB.getModel())
                                        && deviceInDB.getVersion().equals(deviceRelatedInDB.getVersion())
                                        && deviceInDB.getBrand().equals(deviceRelatedInDB.getBrand())
                                        && deviceInDB.getType().equals(deviceRelatedInDB.getType())
                                        && deviceInDB.getSupplier().equals(deviceRelatedInDB.getSupplier())
                                ) {
                                    deviceRelatedInDB.setQuantityUnprocessed(deviceInDB.getQuantity());
                                    //关联工单当前设备附属的未处理数量回退完成,下一个
                                    break;
                                }
                            }
                        }
                        deviceService.updateQuantityUnprocessedBatch(deviceRelatedListInDB);
                    }
                    mainService.updateById(mainInDB);
                    link.setDealAndClose(1);
                }
            }break;
            case 4:{ // 出库申请
                if(link.getStatus() == WorkflowEnum.LINK_STATUS_PASS.getValue().intValue()){
                    mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_PASS.getValue());
                    mainInDB.setEndTime(now);
                    mainInDB.setEndReason(link.getDealReason());
                    mainService.updateById(mainInDB);
                    //获取出库申请单设备
                    List<WorkflowDevice> deviceListInDB = deviceService.getByMainId(mainInDB.getId());
                    List<DeviceSpare> spareList = new ArrayList<>();
                    deviceListInDB.forEach(device -> {
                        DeviceSpare spare = new DeviceSpare();
                        BeanUtils.copyProperties(device,spare);
                        spareList.add(spare);
                    });
                    //更新库存
                    deviceSpareService.outBound(spareList);
                }else if(link.getStatus() == WorkflowEnum.LINK_STATUS_REJECT.getValue().intValue()){
                    mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_REJECT.getValue());
                    mainInDB.setEndReason(link.getDealRejectReason());
                    mainInDB.setEndTime(now);
                    mainService.updateById(mainInDB);
                    link.setDealAndClose(1);
                }
            }break;
            default:
                break;
 
        }
 
        link.setId(linkInDB.getId());
        link.setDealUserId(user.getId());
        link.setDealTime(now);
 
        mapper.updateById(link);
 
        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.LINK_STATUS_PASS.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);
    }
 
 
    public Map<Integer, Integer> getReceivedStatistics(int type, User 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;
    }
 
    /**
     *
     * 二次核容和隐患故障的统计
     *
     * 增加了全部 0
     * 0:全部
     * 1:审批中
     * 2:通过
     * 3:驳回
     *
     * status含义:1-待接单,6-待审核,58-已审核
     * @param type
     * @param user
     * @return
     */
    public Response getReceivedStatistics2(int type, User user) {
        Map<Integer,Integer> statistics = new HashMap<>();
        statistics.put(0,0);
        statistics.put(1,0);
        statistics.put(2,0);
        statistics.put(3,0);
        List<WorkflowLink> links = mapper.getReceivedList2(type,user);
        Map<Integer, List<WorkflowLink>> receivedListMap = links.stream().collect(Collectors.groupingBy(WorkflowLink::getStatus));
        Set<Integer> statusSet = receivedListMap.keySet();
        int sum = 0;
        for (Integer status : statusSet) {
            int size = receivedListMap.get(status).size();
            if (status == 1 || status == 6) { //审批中
                statistics.put(1, statistics.get(1) + size);
            } else if(status == 5){ //通过
                statistics.put(2, size);
            }else if(status == 8){ //驳回
                statistics.put(3,size);
            }
            sum+=size;
        }
        statistics.put(0,sum);
        return new Response().set(1,statistics);
    }
 
 
    public void add(WorkflowLink link) {
        mapper.insert(link);
    }
 
 
}