zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
package cn.autoform.web.service.process.impl;
 
import cn.autoform.bean.ProcessState;
import cn.autoform.bean.page.PageResult;
import cn.autoform.db.entity.ProcessLog;
import cn.autoform.db.entity.ProcessTask;
import cn.autoform.fw.exception.FormClientException;
import cn.autoform.util.thread.Keys;
import cn.autoform.util.thread.ThreadData;
import cn.autoform.util.tool.JSONTool;
import cn.autoform.util.tool.PageUtil;
import cn.autoform.web.client.FormClient;
import cn.autoform.web.client.WorkflowClient;
import cn.autoform.web.client.util.WorkflowTool;
import cn.autoform.web.mapper.formset.FormSetMapper;
import cn.autoform.web.mapper.process.ProcessLogMapper;
import cn.autoform.web.service.process.ProcessLogService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeoutException;
 
import static cn.autoform.bean.system.PrivateRestConst.USER_NAME;
 
@Slf4j
@Service("processLogServiceImpl")
public class ProcessLogServiceImpl implements ProcessLogService{
 
    @Resource
    private ProcessLogMapper processLogMapper;
 
    @Resource
    private FormClient client;
 
    @Resource
    private FormSetMapper formSetMapper;
 
    @Autowired
    private WorkflowClient workflowClient;
 
    @Autowired
    private WorkflowTool workflowTool;
 
    /**
     * 当前待办信息key
     */
    private static  final String PROCESSINFORMATION = "processInformation";
 
    /**
     *
     * @param map
     * @param dataType 10:审批,20:退回,30:办理中,40:办理完成
     * @return
     * @throws TimeoutException
     */
    @Override
    public Integer addProcessLog(Map<String,Object> map,String dataType, String activityDefName) throws TimeoutException {
        log.debug("开始添加流程日志---本次添加状态为:"+dataType);
        String formID = Objects.toString(map.get("formID"));
        Integer dataRowNum = Integer.parseInt(Objects.toString(map.get("dataRowNum")));
        String tenantID = Objects.toString(map.get("tenantID"));
        String processIntsId = Objects.toString(map.get("processInstId"));
        ProcessLog processLog = new ProcessLog(formID
                , dataRowNum
                , tenantID
                , processIntsId
                , ThreadData.get(Keys.COMPANY_ID)
                , ThreadData.get(Keys.OPEN_ID));
        processLog.setCreateUser(client.getUserName(formID, tenantID, dataRowNum));
        processLog.setDataType(dataType);
        processLog.init();
        processLog.setActivityDefName(activityDefName);
        if(Objects.equals("30",dataType)){
            processLog.setActivityDefName(getLastLogName(processIntsId));
            //办理中log
            processLog.setFormName(getFormName(formID,tenantID));
            processLog.setNumberForm(getNumber(formID,dataRowNum,tenantID));
            processLog.setExecuteTime(new Date());
            processLogMapper.updateState(processLog);
        }else{
            //修改原有退回审批得数据状态
            processLogMapper.updateState(processLog);
            //执行,退回log,取出当前活动
            ProcessTask processTask = getProcessTask(processIntsId,Objects.toString(map.get("activityInstId")));
            System.out.println("需要得待办:"+ JSONTool.toJson(processTask));
            if(processTask == null){
                System.out.println("添加日志失败:");
                return 0;
            }
            processLog.setFormName(processTask.getProcessChName());
            processLog.setNumberForm(processTask.getProcessInstName());
            processLog.setBacklogStartTime(processTask.getProcessInstStartTime());
            processLog.setBacklogArriveTime(processTask.getStartTime());
            processLog.setIsOvertime(processTask.getIsTimeOut());
            processLog.setActivityDefName(processTask.getActivityInstName());
        }
        processLog.setInsP(ThreadData.get(Keys.USER_NAME));
        processLogMapper.addProcessLog(processLog);
        log.debug("流程日志参数:"+ JSONTool.toJson(processLog));
        log.debug("本次添加流程日志id:"+processLog.getId());
        return processLog.getId();
    }
 
    @Override
    public Integer updateState(ProcessLog processLog) {
        log.debug("删除重复流程日志:"+ JSONTool.toJson(processLog));
        return processLogMapper.updateState(processLog);
    }
 
    @Override
    public PageResult<ProcessLog> selectPage(ProcessLog processLog) {
        processLog.setOpenId(ThreadData.get(Keys.OPEN_ID));
        processLog.setCompanyId(ThreadData.get(Keys.COMPANY_ID));
        processLog.setTenantID(ThreadData.get(Keys.TENANT_ID));
        PageUtil.initPage();
        PageResult<ProcessLog> page = new PageResult<>(
                processLogMapper.selectPage(processLog)
        );
 
        return page;
    }
 
    @Override
    public Integer delProcessLog(Integer id) {
        log.debug("错误流程日志id为:"+id );
        return processLogMapper.delProcessLog(id);
    }
 
    @Override
    public Integer updateDataType(ProcessLog processLog) {
        log.debug("修改办理完成流程日志:"+ JSONTool.toJson(processLog));
 
        return processLogMapper.updateDataType(processLog);
    }
 
 
    public int getPages(int a,int b){
        if(a%b>0){
            return a/b+1;
        }else{
            return a/b;
        }
    }
 
    //取出表单的自动生成编号
    public String getNumber(String formID,Integer dataRowNum,String tenantID) throws TimeoutException, FormClientException {
 
        ProcessState pro = new ProcessState(formID,dataRowNum,tenantID);
        //取出当前流程大状态
        ProcessState p = client.selectState(pro);
        return p.getNumber();
    }
 
    //取出表单名称
    public String getFormName(String formID,String tenantID){
        return formSetMapper.selectBuss(formID,tenantID);
    }
 
    public String getLastLogName(String processIntsId) throws TimeoutException {
        JSONObject jso = new JSONObject();
        jso.put("processInstId", processIntsId);
        Map<String, Object> result = workflowClient.instLog(workflowTool.getProcess(jso));
        List<Map> data = Optional.ofNullable(result)
                .map(d->d.get("data"))
                .map(d->(Map)d)
                .map(d->d.get("data"))
                .map(d->(List)d)
                .orElse(Collections.EMPTY_LIST);
        log.debug("查询log:{}", JSONTool.toJson(data));
        if(data.size() < 1){
            return "";
        }
        return Objects.toString(data.get(data.size()-1).get("activityDefName"));
    }
 
    /**
     * 取出当前待办
     */
    public ProcessTask getProcessTask(String processIntsId,String activityInstId) throws TimeoutException {
        JSONObject jso = new JSONObject();
        jso.put("processInstId", processIntsId);
        Map<String, Object> result = workflowClient.instSeacheHandle(workflowTool.getProcess(jso));
        System.out.println("待办:"+JSONTool.toJson(result));
        Map<String,Object> res = JSONTool.toObj(JSONTool.toJson(result.get("data")),Map.class);
        List<ProcessTask> list = null;
        System.out.println("data取出1:"+ JSONTool.toJson(res));
        if(res.get("data")!=null) {
            list = JSONTool.toList(JSONTool.toJson(res.get("data")), ProcessTask.class);
            System.out.println("转换实体后:"+ JSONTool.toJson(list));
        }else{
            return null;
        }
        System.out.println("当前:"+activityInstId);
        return list.stream()
                .filter(fi-> Objects.equals(fi.getActivityInstId(),activityInstId))
                    .findFirst().orElse(null);
    }
}