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 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 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 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 result = workflowClient.instLog(workflowTool.getProcess(jso)); List 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 result = workflowClient.instSeacheHandle(workflowTool.getProcess(jso)); System.out.println("待办:"+JSONTool.toJson(result)); Map res = JSONTool.toObj(JSONTool.toJson(result.get("data")),Map.class); List 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); } }