330 lines
12 KiB
Java
330 lines
12 KiB
Java
package cn.palmte.work.service;
|
|
|
|
import cn.palmte.work.config.activiti.ActConstant;
|
|
|
|
import cn.palmte.work.model.*;
|
|
import cn.palmte.work.pojo.ActHisTask;
|
|
import cn.palmte.work.utils.ActUtil;
|
|
import cn.palmte.work.utils.InterfaceUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import org.activiti.engine.*;
|
|
import org.activiti.engine.impl.persistence.entity.TaskEntity;
|
|
import org.activiti.engine.task.Task;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import top.jfunc.common.db.QueryHelper;
|
|
import top.jfunc.common.db.utils.Pagination;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
public class ActTaskDefService {
|
|
private static final Logger logger = LoggerFactory.getLogger(ActTaskDefService.class);
|
|
@Autowired
|
|
private RepositoryService repositoryService; //管理流程定义 与流程定义和部署对象相关的Service
|
|
@Autowired
|
|
private ProcessEngine processEngine; //流程引擎对象
|
|
@Autowired
|
|
private TaskService taskService; //任务管理 与正在执行的任务管理相关的Service
|
|
@Autowired
|
|
private ActTaskDefRepository actTaskDefRepository;
|
|
@Autowired
|
|
private AccountService accountService;
|
|
@Autowired
|
|
Pagination pagination;
|
|
@Autowired
|
|
private ActUtil actUtil;
|
|
|
|
|
|
/**
|
|
* 审批任务
|
|
*
|
|
* @param json
|
|
*/
|
|
public void completeTask(JSONObject json) {
|
|
String taskId = json.getString("taskId");
|
|
String procInsId = json.getString("procInsId");
|
|
String message = json.getString("message");
|
|
int type = json.getInteger("type");
|
|
|
|
String userId = InterfaceUtil.getAdminId() + "";
|
|
taskService.addComment(taskId, procInsId, message);
|
|
|
|
actTaskDefRepository.updateHiTaskAssign(userId, procInsId, taskId);
|
|
actTaskDefRepository.updateHiActAssign(userId, procInsId, taskId);
|
|
|
|
Task currentTask = taskService.createTaskQuery().taskId(taskId).singleResult();
|
|
ActTaskDef actTaskDef = findFirstByProcDefIdAndTaskKey(currentTask.getProcessDefinitionId(), currentTask.getTaskDefinitionKey());
|
|
|
|
if (ActConstant.TASK_TYPE_SINGE == actTaskDef.getTaskType()) {
|
|
//或签处理
|
|
handleSinge(taskId, procInsId, type, userId, actTaskDef);
|
|
} else if (ActConstant.TASK_TYPE_MULTI == actTaskDef.getTaskType()) {
|
|
//会签处理
|
|
handleMulti(taskId, procInsId, type, userId, actTaskDef);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* 会签处理
|
|
*
|
|
* @param taskId
|
|
* @param procInsId
|
|
* @param type
|
|
* @param userId
|
|
* @param actTaskDef
|
|
*/
|
|
private void handleMulti(String taskId, String procInsId, int type, String userId, ActTaskDef actTaskDef) {
|
|
/* Double instanceCount = Double.parseDouble(ActUtil.filterNullToZero(taskService.getVariable(taskId, ActConstant.NUMBER_OF_INSTANCES))); //会签任务总数
|
|
Double instanceActiveCount = Double.parseDouble(ActUtil.filterNullToZero(taskService.getVariable(taskId, ActConstant.NUMBER_OF_ACTIVE_INSTANCES))); //活动的会签任务数
|
|
Double instanceCompleteCount = Double.parseDouble(ActUtil.filterNullToZero(taskService.getVariable(taskId, ActConstant.NUMBER_OF_COMPLETED_INSTANCES))); //完成会签任务数*/
|
|
|
|
if (ActConstant.TYPE_ROLLBACK == type) {
|
|
//一个人驳回 整个任务节点驳回
|
|
List<Task> taskList = taskService.createTaskQuery().processInstanceId(procInsId).list();
|
|
for (int i = 0; i < taskList.size(); i++) {
|
|
TaskEntity taskEntity = (TaskEntity) taskList.get(i);
|
|
if (!taskId.equals(taskEntity.getId())) {
|
|
taskEntity.setProcessInstanceId(null);
|
|
taskEntity.setExecutionId(null);
|
|
taskService.saveTask(taskEntity);
|
|
taskService.addComment(taskEntity.getId(), procInsId, "会签驳回,任务失效");
|
|
taskService.deleteTask(taskEntity.getId(), false); // 不删除历史记录
|
|
}
|
|
}
|
|
|
|
//驳回到配置的节点
|
|
actUtil.jumpToTargetTask(taskId, actTaskDef.getRollbackTaskKey());
|
|
return;
|
|
}
|
|
|
|
taskService.complete(taskId);
|
|
}
|
|
|
|
/**
|
|
* 或签处理
|
|
*
|
|
* @param taskId
|
|
* @param procInsId
|
|
* @param type
|
|
* @param userId
|
|
* @param actTaskDef
|
|
*/
|
|
private void handleSinge(String taskId, String procInsId, int type, String userId, ActTaskDef actTaskDef) {
|
|
if (ActConstant.TYPE_APPROVE == type) {
|
|
//审批通过
|
|
taskService.setAssignee(taskId, userId);
|
|
taskService.complete(taskId);
|
|
|
|
//执行配置的审批通过脚本
|
|
int endScript = actTaskDef.getEndScript();
|
|
if (endScript != 0) {
|
|
actUtil.invokeEventScript(endScript, procInsId);
|
|
} else {
|
|
logger.info("未配置审批通过脚本 task:{}", actTaskDef.getTaskName());
|
|
}
|
|
|
|
} else if (ActConstant.TYPE_ROLLBACK == type) {
|
|
//驳回
|
|
String rollbackTaskKey = actTaskDef.getRollbackTaskKey();
|
|
actUtil.jumpToTargetTask(taskId, rollbackTaskKey);
|
|
|
|
//执行配置的驳回脚本
|
|
int rollbackScript = actTaskDef.getRollbackScript();
|
|
if (rollbackScript != 0) {
|
|
actUtil.invokeEventScript(rollbackScript, procInsId);
|
|
} else {
|
|
logger.info("未配置驳回脚本 task:{}", actTaskDef.getTaskName());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 审批过程任务查询
|
|
*
|
|
* @param procIncId
|
|
* @return
|
|
*/
|
|
public List<ActHisTask> hisTaskList(String procIncId) {
|
|
String select = "select ht.PROC_DEF_ID_ as procDefId, ht.PROC_INST_ID_ as procInsId, ht.EXECUTION_ID_ as executionId," +
|
|
" ht.ID_ as taskId, ht.NAME_ as taskName, " +
|
|
" ht.ASSIGNEE_ as assign, ht.START_TIME_ as startTime, ht.END_TIME_ as endTime, " +
|
|
"ht.DURATION_ as duration, ht.DELETE_REASON_ as deleteReason, hc.MESSAGE_ AS comments";
|
|
QueryHelper queryHelper = new QueryHelper(select, " ACT_HI_TASKINST ht " +
|
|
" LEFT JOIN ACT_HI_COMMENT hc on ht.ID_ = hc.TASK_ID_ and hc.type_='comment' ");
|
|
queryHelper.addCondition("ht.PROC_INST_ID_ =?", procIncId);
|
|
queryHelper.addOrderProperty("ht.start_time_", true);
|
|
List<ActHisTask> taskList = pagination.find(queryHelper.getSql(), ActHisTask.class);
|
|
for (ActHisTask actHisTask : taskList) {
|
|
setAssign(actHisTask);
|
|
|
|
//办理用时
|
|
String duration = actHisTask.getDuration();
|
|
if (StringUtils.isNotBlank(duration)) {
|
|
Long ztime = Long.parseLong(duration);
|
|
Long day = ztime / (1000 * 60 * 60 * 24);
|
|
Long hour = (ztime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
|
|
Long minute = (ztime % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60) / (1000 * 60);
|
|
Long second = (ztime % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60) % (1000 * 60) / 1000;
|
|
actHisTask.setDuration(day + "天" + hour + "时" + minute + "分" + second + "秒");
|
|
} else {
|
|
actHisTask.setDuration("正在处理。。。");
|
|
}
|
|
|
|
if (StringUtils.isNotBlank(actHisTask.getDeleteReason())) {
|
|
actHisTask.setComments("【驳回】" + actHisTask.getComments());
|
|
}
|
|
}
|
|
|
|
return taskList;
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据流程定义查询任务
|
|
*
|
|
* @param procDefId
|
|
* @return
|
|
*/
|
|
public List<ActTaskDef> findByProcDefId(String procDefId) {
|
|
List<ActTaskDef> list = actTaskDefRepository.findByProcDefId(procDefId);
|
|
for (ActTaskDef actTaskDef : list) {
|
|
candidatesHandle(actTaskDef);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据流程定义和任务key查询任务
|
|
*
|
|
* @param procDefId
|
|
* @param taskKey
|
|
* @return
|
|
*/
|
|
public ActTaskDef findFirstByProcDefIdAndTaskKey(String procDefId, String taskKey) {
|
|
ActTaskDef def = actTaskDefRepository.findFirstByProcDefIdAndTaskKey(procDefId, taskKey);
|
|
candidatesHandle(def);
|
|
return def;
|
|
}
|
|
|
|
|
|
/**
|
|
* 保存任务配置
|
|
*
|
|
* @param taskDef
|
|
*/
|
|
public void saveConfig(ActTaskDef taskDef) {
|
|
ActTaskDef one = actTaskDefRepository.findOne(taskDef.getId());
|
|
one.setCandidateUsers(taskDef.getCandidateUsers());
|
|
one.setCandidateRoles(taskDef.getCandidateRoles());
|
|
one.setRollbackTaskKey(taskDef.getRollbackTaskKey());
|
|
one.setEndScript(taskDef.getEndScript());
|
|
one.setRollbackScript(taskDef.getRollbackScript());
|
|
one.setLastUpdatedTime(new Date());
|
|
|
|
actTaskDefRepository.save(one);
|
|
logger.info("saveTaskConfig uerId:{}, config:{}", InterfaceUtil.getAdminId(), JSONObject.toJSONString(one));
|
|
}
|
|
|
|
|
|
/**
|
|
* 查找任务配置的审批人
|
|
*
|
|
* @param procDefId
|
|
* @param procInsId
|
|
* @param taskDefKey
|
|
* @return
|
|
*/
|
|
public Set<String> findCandidateUsers(String procDefId, String procInsId, String taskDefKey) {
|
|
ActTaskDef taskDef = findFirstByProcDefIdAndTaskKey(procDefId, taskDefKey);
|
|
if (taskDef.getTaskIndex() == ActConstant.TASK_INDEX_FIRST_USER_TASK) {
|
|
//任务驳回到发起节点 审批人设置为发起人
|
|
String startUserId = actUtil.getStartUserId(procInsId);
|
|
Set<String> res = new HashSet<>(1);
|
|
logger.info("findCandidateUsers-0-task:{}, startUserId:{}", taskDef.getTaskName(), startUserId);
|
|
res.add(startUserId);
|
|
return res;
|
|
}
|
|
|
|
List<String> resList = new ArrayList<>();
|
|
|
|
//通过人员id查询
|
|
List<String> candidateUserList = taskDef.getCandidateUserList();
|
|
logger.info("findCandidateUsers-1-task:{}, userList:{}", taskDef.getTaskName(), candidateUserList);
|
|
if (!candidateUserList.isEmpty()) {
|
|
resList.addAll(candidateUserList);
|
|
}
|
|
|
|
//通过角色id查询
|
|
List<String> candidateRoleList = taskDef.getCandidateRoleList();
|
|
logger.info("findCandidateUsers-2-task:{}, roleList:{}", taskDef.getTaskName(), candidateRoleList);
|
|
List<String> list = accountService.getUserIsByRole(candidateRoleList);
|
|
logger.info("findCandidateUsers-3-task:{}, userIdListByRole:{}", taskDef.getTaskName(), list);
|
|
if (!list.isEmpty()) {
|
|
resList.addAll(list);
|
|
}
|
|
|
|
//去重
|
|
Set<String> res = new HashSet<>(resList);
|
|
logger.info("findCandidateUsers-4-task:{}, resIds:{}", taskDef.getTaskName(), res);
|
|
return res;
|
|
}
|
|
|
|
|
|
/**
|
|
* 任务审批人列表从sring转成list
|
|
*
|
|
* @param actTaskDef
|
|
*/
|
|
private void candidatesHandle(ActTaskDef actTaskDef) {
|
|
String candidateUsers = actTaskDef.getCandidateUsers();
|
|
List<String> userIdList = new ArrayList<>();
|
|
if (StringUtils.isNotBlank(candidateUsers)) {
|
|
userIdList = Arrays.asList(candidateUsers.split("#"));
|
|
}
|
|
actTaskDef.setCandidateUserList(userIdList);
|
|
|
|
String candidateRoles = actTaskDef.getCandidateRoles();
|
|
List<String> roleIdList = new ArrayList<>();
|
|
if (StringUtils.isNotBlank(candidateRoles)) {
|
|
roleIdList = Arrays.asList(candidateRoles.split("#"));
|
|
}
|
|
actTaskDef.setCandidateRoleList(roleIdList);
|
|
}
|
|
|
|
|
|
/**
|
|
* 设置历史任务审批人
|
|
*
|
|
* @param actHisTask
|
|
*/
|
|
private void setAssign(ActHisTask actHisTask) {
|
|
if (StringUtils.isNotBlank(actHisTask.getAssign())) {
|
|
String userName = accountService.getNameById(Integer.parseInt(actHisTask.getAssign()));
|
|
actHisTask.setAssign(userName);
|
|
return;
|
|
}
|
|
|
|
Task task = taskService.createTaskQuery().taskId(actHisTask.getTaskId()).singleResult();
|
|
if (task == null) {
|
|
return;
|
|
}
|
|
|
|
if (StringUtils.isNotBlank(task.getAssignee())) {
|
|
actHisTask.setAssign(task.getAssignee());
|
|
return;
|
|
}
|
|
|
|
actHisTask.setAssign(actUtil.getAssigneeByIdentityLink(task.getId()));
|
|
}
|
|
|
|
|
|
}
|