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
package cn.autoform.web.service.process.impl;
 
import cn.autoform.bean.flow.def.ProcessActivityFrom;
import cn.autoform.bean.flow.def.ProcessDefineExtend;
import cn.autoform.bean.flow.task.ProcessTaskExtend;
import cn.autoform.db.entity.ProcessApproval;
import cn.autoform.factory.FormFactory;
import cn.autoform.factory.product.ProductMethod;
import cn.autoform.util.thread.Keys;
import cn.autoform.util.thread.ThreadData;
import cn.autoform.util.tool.JSONTool;
import cn.autoform.util.tool.ObjectUtil;
import cn.autoform.web.client.WorkflowClient;
import cn.autoform.web.client.util.WorkflowTool;
import cn.autoform.web.mapper.process.ProcessApprovalMapper;
import cn.autoform.web.service.process.ProcessApprovalService;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
@Service
@Slf4j
public class ProcessApprovalServiceImpl implements ProcessApprovalService{
 
    @Resource
    private ProcessApprovalMapper processMapper;
 
    @Autowired
    private WorkflowClient workflowClient;
 
    @Autowired
    private WorkflowTool workflowTool;
    
    @Autowired
    private FormFactory formFactory;
    
    /**
     * 添加流程定义
     */
    
    @Override
    public int addProcessApproval(Map<String, Object> map,ProcessApproval pro) {
        Map<String,String> proMap = JSONTool.toObj(JSONTool.toJson(map.get("data")), new TypeReference<Map<String, String>>(){});
        if(proMap==null){
            return -1;
        }
        pro.setDefJson(JSONTool.toJson(proMap));
        pro.setProcessDefName(proMap.get("processDefName"));
        pro.setProcessDefId(proMap.get("processDefId"));
        pro.setTemplateProcessDefId(proMap.get("templateProcessDefId"));
        pro.setVersion(proMap.get("version"));
        pro.setTenantID(ThreadData.get(Keys.TENANT_ID));
        pro.setCompanyId(ThreadData.get(Keys.COMPANY_ID));
        return processMapper.addProcessApproval(pro);
    }
    
    /**
     * 删除流程定义
     */
    @Override
    public int deleteProcessApproval(String id) {
        
        return processMapper.deleteProcessApproval(id);
    }
    
    /**
     * 获取流程定义
     */
    @Override
    public ProcessApproval select(String  orgId,String formID) {
        return processMapper.select(orgId,formID, ThreadData.get(Keys.TENANT_ID),ThreadData.get(Keys.COMPANY_ID));
    }
 
    @Override
    public ProcessApproval getApproval(ProcessApproval pro) {
        pro.setTenantID(ThreadData.get(Keys.TENANT_ID));
        pro.setCompanyId(ThreadData.get(Keys.COMPANY_ID));
        return pro.getFormId()!=null && pro.getOrganizationId()!=null?processMapper.getApproval(pro):null;
    }
 
    @Override
    public ProcessApproval selectApp(String formID) {
        return processMapper.selectApp(formID, ThreadData.get(Keys.TENANT_ID),ThreadData.get(Keys.COMPANY_ID));
    }
 
    @Override
    public int definePeploy(Map<String, Object> result) {
        ProcessDefineExtend pde = JSONTool.toObj(JSONTool.toJson(result.get("data")), new TypeReference<ProcessDefineExtend>(){});
        if(pde==null){
            return -1;
        }
        ProcessApproval pa = new ProcessApproval();
        pa.setDefJson(JSONTool.toJson(pde));
        pa.setProcessDefId(Objects.toString(pde.getProcessDefId(), ""));
        pa.setVersion(pde.getVersion());
        return processMapper.updateDefInfo(pa);
    }
 
    @Override
    public Map<String, String> getProcessParam(String formId, String tenantID,
            Integer dataRowNum) throws Exception{
        log.debug("查询执行流程参数,表单id:{},租户id:{},数据id{}", formId, tenantID, dataRowNum);
        Map<String, String> param = new HashMap<>();
        // 查询表单数据
        ProductMethod productMethod = this.formFactory.createFormMethod();
        Map<String, Object> sf = productMethod.queryFormDataList(formId, tenantID, dataRowNum, null);
        Map<String, Object> mainInfo = (Map<String, Object>)sf.get("main");
        if(mainInfo == null || mainInfo.isEmpty()) {
            return param;
        }
        mainInfo.entrySet()
                .stream()
                .filter(e->!(e.getValue() instanceof List))
                .filter(e->!Objects.equals("solidification", e.getKey()))
                .forEach(e->param.put(e.getKey(), ObjectUtil.empty(e.getValue()) ? "NULL" : e.getValue().toString()));
        return param;
    }
    
    public Map<String, String> getProcessParam(String processActivityDefId, Long processDefId, String formId, String tenantID,
            Integer dataRowNum) throws Exception{
        Map<String, String> param = new HashMap<>();
        // 查询流程定义信息
        ProcessApproval processApproval = processMapper.selectProcessApproval(Objects.toString(processDefId, ""));
        if(processApproval == null || processApproval.getDefJson() == null) {
            return param;
        }
        // 查询流程定义
        ProcessDefineExtend processDefine = JSONTool.toObj(processApproval.getDefJson(), ProcessDefineExtend.class);
        if(processDefine == null) {
            return param;
        }
        List<ProcessActivityFrom> froms = processDefine.getActivityForm(processActivityDefId);
        if(froms == null || froms.isEmpty()) {
            return param;
        }
        // 查询表单数据
        ProductMethod productMethod = this.formFactory.createFormMethod();
        Map<String, Object> sf = productMethod.queryFormDataList(formId, tenantID, dataRowNum, null);
        Map<String, Object> mainInfo = (Map<String, Object>)sf.get("main");
        if(mainInfo == null || mainInfo.isEmpty()) {
            return param;
        }
        froms.stream()
            .forEach(from->{
                Object val = mainInfo.get(from.getFromPath());
                if(ObjectUtil.notEmpty(val)) {
                    param.put(from.getFromPath(), Objects.toString(val));
                }else{
                    param.put(from.getFromPath(), "NULL");
                }
            });
        return param;
    }
 
    protected ProcessTaskExtend getProcessTask(Map<String, Object> param) throws TimeoutException{
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(param);
//        jsonObject.put("activityInstId", processActivityInstId);
        Map<String, Object> res = workflowClient.instSeacheHandle(workflowTool.getProcess(jsonObject));
        Map<String, Object> list = (Map<String, Object>) res.get("data");
        if(list == null || list.isEmpty()) {
            return null;
        }
        List<ProcessTaskExtend> taskList = JSONTool.toObj(JSONTool.toJson(list.get("data")), new TypeReference<List<ProcessTaskExtend>>(){});
        if(taskList == null || taskList.size() == 0) {
            return null;
        }
        return taskList.get(0);
    }
 
    @Override
    public Map<String, String> getProcessOneActivityParam(String processIntsId, String processDefId, String formId, String tenantID, Integer dataRowNum)
            throws Exception {
        log.debug("查询执行流程参数:流程实例id:{},流程定义id:{},表单id:{},租户id:{},数据id{}", processIntsId, processDefId, formId, tenantID, dataRowNum);
 
        ProcessTaskExtend pTask = getProcessTask(new ModelMap("processInstId", processIntsId));
        if(ObjectUtil.empty(pTask)) {
            return new HashMap<>();
        }
        
        return getProcessParam(pTask.getActivityDefId(), NumberUtils.createLong(processDefId), formId, tenantID, dataRowNum);
    }
    public static void main(String... args){
        System.out.println(
                JSONTool.toObj("", Map.class).get("budget_itemMoney")
        );
    }
}