whycxzp
2023-04-23 90206a8d6467a7ad80a6caf398a131d284e918c4
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
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.dto.Response;
import com.whyc.mapper.WorkflowMainMapper;
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 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;
 
    /**
     * 分派单号:
     * */
    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) {
        return mapper.getBaseInfo(mainId);
    }
 
    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);
        statistics.put(4,0);
        QueryWrapper<WorkflowMain> query = Wrappers.query();
        query.eq("create_user_id",userId);
        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);
    }
 
    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);
        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();
        query.eq("create_user_id",userId).eq("type",type).eq("status",status);
        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);
        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);
    }
}