kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
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
package com.highdatas.mdm.controller;
 
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.highdatas.mdm.entity.Flows;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.pojo.ActivitiBusinessType;
import com.highdatas.mdm.pojo.ActivitiStatus;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.IFlowsService;
import com.highdatas.mdm.util.DbUtils;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 *  前端控制器
 * </p>
 * @description 流程实例业务接口
 * @author kimi
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/flows")
public class FlowsController {
    @Autowired
    IFlowsService flowsService;
 
    @Autowired
    RuntimeService runtimeService;
 
    /**
     *
     * @description:  分页获取流程实例
     * @param pageno 页数
     * @return: 流程实例
     *
     */
    @RequestMapping(value = "/{pageno}", method = RequestMethod.GET)
    public Result<Page<Flows>> getAll(@PathVariable int pageno, HttpServletRequest request) {
        //获取每页数据量
        String pageSize = request.getParameter("pageSize");
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "15";
        }
        EntityWrapper<Flows> flowsEntityWrapper = new EntityWrapper<>();
        //筛选-- 开启用户
        String userId = request.getParameter("userId");
        if (!StringUtils.isEmpty(userId)) {
            flowsEntityWrapper.eq("user_id", userId);
        }
        //筛选--状态
        String status = request.getParameter("status");
        if (!StringUtils.isEmpty(status)) {
            flowsEntityWrapper.eq("status", status);
        }
        Integer size = Integer.valueOf(pageSize);
        Page page = new Page(pageno, size);
        Page<Flows> menuPage = flowsService.selectPage(page, flowsEntityWrapper);
        //获取分页后的流程实例
        List<Flows> records = menuPage.getRecords();
        List<Flows> recordsStr = new ArrayList<>();
        for (Flows flows : records) {
            //更新状态中文名给前端
            flows.setBusinessTypeStr(flows.getBusinessType().toString());
            TUser userById = DbUtils.getUserById(flows.getUserId());
            //查询用户的名称给前端
            flows.setUserName(userById.getUserName());
            recordsStr.add(flows);
        }
        menuPage.setRecords(recordsStr);
        return Result.success(menuPage);
    }
    /**
     *
     * @description:  By 用户删除流程
     * @param userId 用户id
     * @return: 是否成功
     *
     */
 
    @RequestMapping(value = "closeByUser/{userId}", method = RequestMethod.GET)
    public Result closeByUser(@PathVariable String userId, HttpServletRequest request) {
        Wrapper<Flows> user_id = new EntityWrapper<Flows>().eq("user_id", userId);
        //By user查询流程
        List<Flows> flows = flowsService.selectList(user_id);
        for (Flows flow : flows) {
            //循环删除流程以及对应的流程信息
            closeById(flow.getId());
        }
        return Result.success(null);
    }
    /**
     *
     * @description:  通过id删除流程
     * @param id flows的id
     * @return: 是否删除
     *
     */
    @RequestMapping(value = "close/{id}", method = RequestMethod.GET)
    public Result close(@PathVariable String id, HttpServletRequest request) {
        return closeById(id);
    }
 
    /**
     *
     * @description:  通过id删除流程
     * @param id flows的id
     * @return: 是否删除
     *
     */
    private Result closeById(@PathVariable String id) {
        //通过id获取flow信息
        Flows flows = flowsService.selectById(id);
        if (flows == null) {
            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
        }
        //获取流程id
        String workflowId = flows.getWorkflowId();
        //获取activiti流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(workflowId).singleResult();
        if (processInstance != null) {
            //删除流程
            runtimeService.deleteProcessInstance(workflowId,"终止流程");
        }
        //设置状态
        flows.setStatus(ActivitiStatus.close);
        if (!flows.getBusinessType().equals(ActivitiBusinessType.exists)) {
            /*调起流程结束后的动作*/
            flowsService.aduitFinish(flows);
        }
        //更新数据库信息
        boolean update = flows.updateById();
        if (update) {
            return Result.success(flows);
        }else {
            return Result.error(CodeMsg.UPDATE_ERROR);
        }
    }
}