whycxzp
2023-08-30 c547c4be4e3b21109315694531c4294cd661761f
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.WorkflowDischargePlanEnum;
import com.whyc.constant.WorkflowEnum;
import com.whyc.dto.Response;
import com.whyc.mapper.WorkflowMainMapper;
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 com.whyc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class WorkflowMainService {
 
    @Resource
    private WorkflowMainMapper mapper;
 
    @Autowired
    private WorkflowLinkService linkService;
 
    @Autowired
    private BattDischargePlanTempService tempService;
 
    /**
     * 分派单号:
     * */
    public String getNextOrderId(String typeName) {
        ServletContext application = ActionUtil.getApplication();
        List<String> orderIdList = (List) application.getAttribute("orderIdList");
        if(orderIdList == null){
            orderIdList = new LinkedList<>();
        }
        String nextSequence = "";
        QueryWrapper<WorkflowMain> wrapper = Wrappers.query();
 
        String orderId = "FG-"+typeName+"-";
        String ymd = new SimpleDateFormat("yyyyMMdd").format(new Date());
        orderId = orderId+ymd+"-";
        //先查询是否缓存中是否存在前缀相同的单号,有的话存起来
        List<Integer> sequenceList = new LinkedList<>();
        sequenceList.add(0);
        for (String item : orderIdList) {
            if(item.startsWith(orderId)){
                String sequence = item.split("-")[3];
                sequenceList.add(Integer.parseInt(sequence));
            }
        }
        wrapper.likeRight("order_id",orderId).orderByDesc("order_id").last(" limit 1");
        WorkflowMain workflowMain = mapper.selectOne(wrapper);
        if(workflowMain != null){
            String sequence = workflowMain.getOrderId().split("-")[3];
            sequenceList.add(Integer.parseInt(sequence));
        }
        Integer maxSequence = sequenceList.stream().max(Comparator.comparing(Integer::intValue)).get();
        nextSequence = String.format("%05d", maxSequence+1);
        String nextOrderId = orderId + nextSequence;
        //加入缓存中
        orderIdList.add(nextOrderId);
        application.setAttribute("orderIdList",orderIdList);
        return nextOrderId;
    }
 
    public void add(WorkflowMain main) {
        mapper.insert(main);
    }
 
    public void addBatch(List<WorkflowMain> workflowMainList) {
        mapper.insertBatchSomeColumn(workflowMainList);
    }
 
 
    public WorkflowMain getBaseInfo(Integer mainId) {
        //根据mainId查询是哪种类型
        QueryWrapper<WorkflowMain> query = Wrappers.query();
        query.eq("id",mainId).last(" limit 1");
        WorkflowMain workflowMain = mapper.selectOne(query);
        Integer type = workflowMain.getType();
        if(type ==1){
            List<BattDischargePlanTemp> tempList = tempService.getListByMainId(mainId);
            workflowMain.setTempList(tempList);
        }
        List<WorkflowLink> linkList = linkService.getWorkflowInfo(mainId);
        workflowMain.setLinkList(linkList);
 
        return workflowMain;
    }
 
    public List<WorkflowLink> getAssignReply(Integer mainId) {
        QueryWrapper<WorkflowLink> wrapper = Wrappers.query();
        //wrapper.eq("main_id",mainId)
        //return linkMapper.
        return null;
    }
 
    public void updateStatus(Integer id, Integer status,String endReason,Date endTime) {
        WorkflowMain main = new WorkflowMain(id,status,endReason,endTime);
        mapper.updateById(main);
    }
 
    /**
     *
     * @param userId
     * @param type
     * @see com.whyc.constant.WorkflowEnum
     *
     * @return
     */
    public Response<Map<Integer,Integer>> getOwnStatistics(int userId, int type) {
        Map<Integer,Integer> statistics = new HashMap<>();
        statistics.put(1,0);
        statistics.put(2,0);
        statistics.put(3,0);
        QueryWrapper<WorkflowMain> query = Wrappers.query();
        query.eq("create_user_id",userId).eq("type",type);
        List<WorkflowMain> mains = mapper.selectList(query);
        Map<Integer, List<WorkflowMain>> statusListMap = mains.stream().collect(Collectors.groupingBy(WorkflowMain::getStatus));
        Set<Integer> statusSet = statusListMap.keySet();
        for (Integer status : statusSet) {
            statistics.put(status,statusListMap.get(status).size());
        }
        return new Response<Map<Integer,Integer>>().set(1,statistics);
    }
 
    /**
     * 增加了全部 key=0
     * @param userId
     * @param type
     * @see com.whyc.constant.WorkflowEnum
     *
     * @return
     */
    public Response<Map<Integer,Integer>> getOwnStatistics2(int userId, int type) {
        Map<Integer,Integer> statistics = new HashMap<>();
        statistics.put(0,0);
        statistics.put(1,0);
        statistics.put(2,0);
        statistics.put(3,0);
        QueryWrapper<WorkflowMain> query = Wrappers.query();
        query.eq("create_user_id",userId).eq("type",type);
        List<WorkflowMain> mains = mapper.selectList(query);
        Map<Integer, List<WorkflowMain>> statusListMap = mains.stream().collect(Collectors.groupingBy(WorkflowMain::getStatus));
        Set<Integer> statusSet = statusListMap.keySet();
        int sum = 0;
        for (Integer status : statusSet) {
            int size = statusListMap.get(status).size();
            statistics.put(status, size);
            sum+=size;
        }
        statistics.put(0,sum);
        return new Response<Map<Integer,Integer>>().set(1,statistics);
    }
 
    public Response<PageInfo<WorkflowMain>> ownListPage(int userId, int type, int status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<WorkflowMain> mains = getOwnListByUserAndType(userId,type,status);
        if(type == 1) {
            for (WorkflowMain main : mains) {
                Integer id = main.getId();
                List<BattDischargePlanTemp> tempList = tempService.getListByMainId(id);
                main.setTempList(tempList);
            }
        }
        PageInfo<WorkflowMain> pageInfo = new PageInfo<>(mains);
        return new Response<PageInfo<WorkflowMain>>().set(1,pageInfo);
    }
 
    private List<WorkflowMain> getOwnListByUserAndType(int userId, int type, int status) {
        QueryWrapper<WorkflowMain> query = Wrappers.query();
        if(status == 0){
            query.eq("create_user_id",userId).eq("type",type).orderByDesc("id");
        }else {
            query.eq("create_user_id", userId).eq("type", type).eq("status", status).orderByDesc("id");
        }
        return mapper.selectList(query);
    }
 
    public Response<Map<Integer,Integer>> getReceivedStatistics(int type, UserInf user) {
        Map<Integer,Integer> statisticsMap = linkService.getReceivedStatistics(type,user);
        return new Response<Map<Integer,Integer>>().set(1,statisticsMap);
    }
 
    public Response<PageInfo<WorkflowMain>> getReceivedListPage(int type, int status, UserInf user, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<WorkflowMain> mains = getReceivedListByUserAndType(user,type,status);
        if (type == 1) {
            for (WorkflowMain main : mains) {
                Integer id = main.getId();
                List<BattDischargePlanTemp> tempList = tempService.getListByMainId(id);
                main.setTempList(tempList);
            }
        }
        PageInfo<WorkflowMain> pageInfo = new PageInfo<>(mains);
        return new Response<PageInfo<WorkflowMain>>().set(1, pageInfo);
    }
 
    private List<WorkflowMain> getReceivedListByUserAndType(UserInf user, int type, int status) {
        return mapper.getReceivedListByUserAndType(user, type, status);
    }
 
    public WorkflowMain getOne(Integer mainId) {
        return mapper.selectById(mainId);
    }
 
    //查询表单编号
    public int getMaxId() {
        Integer id = mapper.getMaxId();
        if (id == null) {
            id = 1;
        } else {
            id = id + 1;
        }
        return id;
    }
 
    //插入主表带指定主键id不要自增
    public void addWorkMain(WorkflowMain main) {
        mapper.addWorkMain(main);
    }
 
    /**
     * 通用提交方法,提交给角色-角色层
     * @param taskDesc 审批描述
     * @param mainType 流程类型
     * @param mainTypeCN 流程中文名
     * @param mainTypeEn 流程英文首字母大写
     * @param userId 用户id
     * @param dealRoleId 需要提交给的角色 id
     * @param now 当前时间
     * @return
     */
    public int submit(String taskDesc,Integer mainType,String mainTypeCN,String mainTypeEn,Integer userId,Integer dealRoleId,Date now,String processLevel){
        //1.提交到单据审批流程
        WorkflowMain main = new WorkflowMain();
        String orderId = getNextOrderId(mainTypeEn);
        String title = mainTypeCN+"审批单-"+ DateUtil.YYYY_MM_DD_HH_MM_SS.format(now);
        Integer mainStatus = WorkflowEnum.MAIN_STATUS_DEALING.getValue();
        main.setOrderId(orderId);
        main.setTitle(title);
        main.setTaskDesc(taskDesc);
        main.setCreateUserId(userId);
        main.setCreateTime(now);
        main.setBeginTime(now);
        main.setStatus(mainStatus);
        main.setType(mainType);
        main.setProcessLevel(processLevel);
        add(main);
        //内存中去除已插入数据库的单号
        ServletContext application = ActionUtil.getApplication();
        List<String> orderIdList = (List<String>) application.getAttribute("orderIdList");
        //校验是否去除
        boolean remove = orderIdList.remove(orderId);
        if(!remove){
            System.err.println("没有去除掉!!!!!!!!!!!!!");
        }
        application.setAttribute("orderIdList",orderIdList);
 
        WorkflowLink link = new WorkflowLink();
        link.setMainId(main.getId());
        link.setParentId(0);
        if(mainType.intValue() == WorkflowEnum.MAIN_TYPE_FAULT_UPLOAD.getValue()){ //如果提交类型是故障隐患,对应提交给管理层,对应过程是管理审批
            link.setProcessLevel(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevel());
            link.setProcessLevelName(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevelName());
        }
        link.setCreateTime(now);
        link.setDealRoleId(dealRoleId);
        link.setDealType(WorkflowEnum.TYPE_DELIVER.getValue());
        link.setDealDesc(taskDesc);
        link.setStatus(WorkflowEnum.STATUS_TAKING.getValue());
 
        linkService.add(link);
        return main.getId();
    }
 
    public void updateProcessLevel(String processLevel, Integer mainId) {
        UpdateWrapper<WorkflowMain> update = Wrappers.update();
        update.set("process_level",processLevel).eq("id",mainId);
        mapper.update(null,update);
    }
}