package org.springblade.mdm.flow.excution;
|
|
import io.swagger.v3.oas.annotations.media.Schema;
|
import lombok.AllArgsConstructor;
|
import org.apache.commons.lang3.StringUtils;
|
import org.flowable.engine.IdentityService;
|
import org.flowable.engine.runtime.ProcessInstance;
|
import org.springblade.core.secure.utils.AuthUtil;
|
import org.springblade.core.tool.utils.Func;
|
import org.springblade.mdm.basesetting.producedivision.entity.ProduceDivision;
|
import org.springblade.mdm.basesetting.producedivision.service.ProduceDivisionService;
|
import org.springblade.mdm.flow.constants.FlowContants;
|
import org.springblade.mdm.flow.entity.FlowProgramFile;
|
import org.springblade.mdm.flow.service.FlowProgramFileService;
|
import org.springblade.mdm.flow.vo.TaskAssignVO;
|
import org.springblade.mdm.program.entity.NcNode;
|
import org.springblade.mdm.program.service.NcNodeService;
|
import org.springblade.mdm.program.service.NodeDeptQueryService;
|
import org.springblade.mdm.utils.EntityUtil;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.flowable.engine.RuntimeService;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@AllArgsConstructor
|
@Service("startDispatcher")
|
public class StartDispatcher {
|
|
private final RuntimeService runtimeService;
|
private final AutoAssignUsersService autoAssignUsersService;
|
|
private final IdentityService identityService;
|
private final NcNodeService ncNodeService;
|
|
private final FlowProgramFileService flowProgramFileService;
|
private final NodeDeptQueryService nodeDeptQueryService;
|
private final ProduceDivisionService produceDivisionService;
|
|
|
/**
|
* 启动派工流程
|
* @param startVO 表单数据
|
* @return 流程实例id
|
*/
|
@Transactional
|
public String start(TaskAssignVO startVO){
|
Map<String, Object> vars = new HashMap<>();
|
//Map<String,Object> preAssignee = autoAssignUsersService.autoAssignUsers(startVO);
|
this.putDefaultAssignees(vars,startVO.getDrawingNo());
|
|
vars.put(FlowContants.TITLE,startVO.getTitle());
|
//机床编号
|
vars.put(FlowContants.MACHINE_CODE,startVO.getMachineCode());
|
String workshop = nodeDeptQueryService.getWorkshopNameByMachineCode(startVO.getMachineCode());
|
vars.put(FlowContants.WORKSHOP,workshop);
|
//机床型号
|
vars.put(FlowContants.MACHINE_MODE,startVO.getMachineMode());
|
|
vars.put(FlowContants.PROCESS_NO,startVO.getProcessNo());
|
vars.put(FlowContants.PROCESS_NAME,startVO.getProcessName());
|
vars.put(FlowContants.PROCESS_EDITION,startVO.getProcessEdition());
|
vars.put(FlowContants.CRAFT_EDITION,startVO.getCraftEdition());
|
vars.put("planStartTime",startVO.getPlanStartTime());
|
vars.put(FlowContants.DRAWING_NO,startVO.getDrawingNo());
|
vars.put(FlowContants.DRAWING_NO_EDITION,startVO.getDrawingNoEdition());
|
vars.put("planLockDays",startVO.getPlanLockDays());
|
vars.put(FlowContants.PRODUCT_MODEL,startVO.getProductModel());
|
|
if(StringUtils.isNotBlank(startVO.getDeviation())) {
|
vars.put(FlowContants.DEVIATION, startVO.getDeviation());//偏离单
|
}
|
String programPkgName = NcNodeService.genProgramName(startVO.getDrawingNo(),startVO.getProcessNo());
|
vars.put(FlowContants.PROGRAM_PACKAGE_NAME,programPkgName);
|
|
//TODO 查询固化程序是,应该去掉有偏离单的?
|
NcNode curedProgramPackage = ncNodeService.getLastEditionCuredProgramPackage(programPkgName);
|
//设置是否有固化程序标记
|
vars.put(FlowContants.HAS_CURED_PROGRAM,curedProgramPackage != null ? FlowContants.Y:FlowContants.N);
|
//工序版次是否一致,是否在有效期内
|
if(curedProgramPackage != null && Func.isBlank(startVO.getDeviation())){//偏离单优先,如果有偏离单,那么找到固化程序的也不算固化了
|
vars.put(FlowContants.CURED_NODE_ID,curedProgramPackage.getId());
|
vars.put(FlowContants.IS_PROCESS_EDITION_SAME, StringUtils.equals(curedProgramPackage.getProcessEdition(),startVO.getProcessEdition()) ? "Y":"N");
|
vars.put(FlowContants.VALIDITY_PERIOD, curedProgramPackage.withinValidityPeriod() ? FlowContants.Y:FlowContants.N);
|
}else{
|
vars.put(FlowContants.IS_PROCESS_EDITION_SAME, FlowContants.Y);
|
vars.put(FlowContants.VALIDITY_PERIOD, FlowContants.Y);
|
}
|
String myProcessName = "试切下发流程";
|
if(vars.getOrDefault(FlowContants.HAS_CURED_PROGRAM,FlowContants.N).equals(FlowContants.Y)){
|
myProcessName = "固化下发流程";
|
}
|
if(StringUtils.isNotBlank(startVO.getDeviation()) ){
|
myProcessName = "偏离单";
|
}
|
vars.put(FlowContants.MY_PROCESS_NAME, myProcessName);
|
|
String businessKey = "0";//业务表key
|
identityService.setAuthenticatedUserId(String.valueOf(AuthUtil.getUserId()));//设置流程发起人
|
ProcessInstance inst = runtimeService.startProcessInstanceByKey(FlowContants.TRY_PROCESS_KEY,businessKey,vars);
|
|
if(curedProgramPackage != null) {//存在已固化程序,复制原来的程序文件。创建节点仍然在编程人员提交以后
|
copyFlowProgramFiles(curedProgramPackage.getProcessInstanceId(),inst.getProcessInstanceId());
|
}
|
|
return inst.getProcessInstanceId();
|
}
|
|
/**
|
* 将默认审批用户放入map中
|
* @param vars 启动流程的变量map
|
* @param drawingNo 零组件号
|
*/
|
void putDefaultAssignees(Map<String, Object> vars,String drawingNo){
|
ProduceDivision div = produceDivisionService.getByDrawingNoWithQinzhe(drawingNo);
|
vars.put(FlowContants.TEAM_LEADER,div.getTeamLeaderId());
|
vars.put(FlowContants.PROGRAMMER,div.getProgrammerId());
|
vars.put(FlowContants.CHECKER,div.getCheckerId());
|
vars.put(FlowContants.SENIOR,div.getSeniorId());
|
|
vars.put(FlowContants.ASSIGNEE,div.getTeamLeaderId());//第一个审批用户:组长
|
}
|
|
/**
|
* 复制流程程序文件
|
* @param processInstanceId 新启动的流程id
|
*/
|
private void copyFlowProgramFiles(String oldProcessInstanceId,String processInstanceId) {
|
List<FlowProgramFile> fileList = flowProgramFileService.lambdaQuery()
|
.eq(FlowProgramFile::getProcessInstanceId,oldProcessInstanceId).list();
|
|
for (FlowProgramFile file : fileList) {
|
FlowProgramFile newFile = new FlowProgramFile();
|
BeanUtils.copyProperties(file, newFile);
|
EntityUtil.clearBaseProperties(newFile);
|
|
newFile.setProcessInstanceId(processInstanceId);
|
|
this.flowProgramFileService.save(newFile);
|
}
|
}
|
|
|
}
|