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
package cn.autoform.web.client.util;
 
import cn.autoform.db.exten.Process;
import cn.autoform.util.thread.Keys;
import cn.autoform.util.thread.ThreadData;
import cn.autoform.util.tool.JSONTool;
import cn.autoform.web.client.WorkflowClient;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeoutException;
 
@Service
@Slf4j
public class WorkflowTool {
 
    @Getter
    @Setter
    private HttpServletRequest request;
 
    @Resource
    private WorkflowClient workflowClient;
    
    @Value("${process-service-id}")
    private Integer processServiceId;
    
    @Value("${process-access-id}")
    private String processAccessId;
    
    @Value("${process-app-key}")
    private String processAppKey;
     
    @Value("${process-security-key}")
    private String processSecurityKey;
    
    //请求参数
    public Map<String, Object> getParam(Map<String, Object> data){
        Map<String, Object> param = new TreeMap<>(data);
        param.put("id", processAccessId + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%09d", new Random().nextInt(999999999)));
        param.put("sign", countSign(param));
        return param;
    }
        
    public Process getProcess(JSONObject jsonObjects){
        @SuppressWarnings("unchecked")
        Map<String,String> JsonObject = JSONTool.toObj(JSONTool.toJson(jsonObjects),Map.class);
        Map<String, Object> param = new TreeMap<>();
        Map<String, Object> loginParam = new TreeMap<>();
        Map<String, Object> data = new TreeMap<>();
        param.put("data", JSONTool.toJson(jsonObjects));
        loginParam.put("loginSecurity",getLogInSecurity());
        data.putAll(param);
        data.putAll(loginParam);
        System.err.println();
        Process pro = new Process();
        pro.setFormID(JsonObject.get("formID"));
        System.out.println("进入粗");
        pro.setTenantID(Objects.toString(JsonObject.get("tenantID"),null));
        pro.setFlowmap(getParam(data));
        return pro;
    }
 
    public String getLogInSecurity() {
//        String token = ThreadData.get(Keys.TOKEN);
        String openId = ThreadData.get(Keys.OPEN_ID);
        String tenantId = ThreadData.get(Keys.TENANT_ID);
        String companyId = ThreadData.get(Keys.COMPANY_ID);
        Integer serviceId = this.processServiceId;
        return "{\"serviceId\":\"" + serviceId + "\",\"tenantId\":\""
                + tenantId + "\",\"openId\":\""
                + openId + "\",\"companyId\":\""
                + companyId + "\"}";
    }
    
    //取出Sign
    public String countSign(Map<String, Object> param){
        StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> map : param.entrySet()) {
                /* 拼接条件 */
                sb.append(map.getKey())
                    .append('=')
                    .append(map.getValue())
                    .append('&');    
            }
            sb.append("key=").append(this.processSecurityKey);
        log.debug("调用工作流接口,参数为:{}", sb);
            return MD5.GetMD5Code(sb.toString().replaceAll(" ",""));
    }
    
    /**
     * 流程结束
     * @param result
     * @return
     */
    public boolean isOver(Map<String, Object> result) {
        Map<String, Object> inst =
                Optional.ofNullable(result.get("data"))
                        .filter(data->data instanceof Map)
                        .map(data->(Map) data)
                        .orElse(new HashMap<>());
        return Optional.ofNullable(inst)
                .map(data->data.get("workItems"))
                .filter(data->data instanceof List)
                .map(data->(List) data)
                .map(List::size)
                .orElse(0) == 0
                &&
                Objects.equals(
                        Optional.ofNullable(inst)
                                .map(data->data.get("processState"))
                                .map(Object::toString)
                                .orElse("")
                        , "20");
    }
 
    public boolean isOne(String processInstId) throws TimeoutException {
        JSONObject j = new JSONObject();
        j.put("processInstId", processInstId);
        Map workflowResult = workflowClient.instLog(getProcess(j));
        List<Map> data = Optional.ofNullable(workflowResult)
                .map(d->d.get("data"))
                .map(d->(Map)d)
                .map(d->d.get("data"))
                .map(d->(List)d)
                .orElse(Collections.EMPTY_LIST);
        if(data.size() <= 1){
            return false;
        }
        if(Objects.equals(data.get(0).get("activityDefId"), data.get(data.size()-1).get("activityDefId"))){
            return true;
        }
        return false;
    }
 
}