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 getParam(Map data){ Map 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 JsonObject = JSONTool.toObj(JSONTool.toJson(jsonObjects),Map.class); Map param = new TreeMap<>(); Map loginParam = new TreeMap<>(); Map 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 param){ StringBuilder sb = new StringBuilder(); for (Map.Entry 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 result) { Map 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 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; } }