|
@@ -0,0 +1,266 @@
|
|
|
+package com.platform.service.activiti.impl;
|
|
|
+
|
|
|
+import com.platform.common.bean.AbstractPageResultBean;
|
|
|
+import com.platform.common.exception.BusinessException;
|
|
|
+import com.platform.common.util.StringUtils;
|
|
|
+import com.platform.dao.bean.MyPage;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.platform.dao.enums.ActivitiUserModelTypeEnum;
|
|
|
+import com.platform.dao.vo.query.activiti.ActivitiUserModelVO;
|
|
|
+import com.platform.dao.dto.activiti.ActivitiUserModelDTO;
|
|
|
+import com.platform.dao.entity.activiti.ActivitiUserModel;
|
|
|
+import com.platform.dao.mapper.activiti.ActivitiUserModelMapper;
|
|
|
+import com.platform.service.activiti.ActivitiAssignStrategy;
|
|
|
+import com.platform.service.activiti.ActivitiUserModelService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.activiti.engine.ProcessEngineConfiguration;
|
|
|
+import org.activiti.engine.RepositoryService;
|
|
|
+import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
|
|
|
+import org.activiti.engine.repository.Model;
|
|
|
+import org.dom4j.Document;
|
|
|
+import org.dom4j.DocumentException;
|
|
|
+import org.dom4j.Element;
|
|
|
+import org.dom4j.io.SAXReader;
|
|
|
+import org.dom4j.io.XMLWriter;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import com.platform.service.base.impl.BaseServiceImpl;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import tk.mybatis.mapper.weekend.Weekend;
|
|
|
+import tk.mybatis.mapper.weekend.WeekendCriteria;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description 用户节点模型 service 实现类
|
|
|
+ * @Author xc
|
|
|
+ * @Date 2022-05-20 14:38:45
|
|
|
+ * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
|
|
|
+ */
|
|
|
+@AllArgsConstructor
|
|
|
+@Service("activitiUserModelService")
|
|
|
+@Slf4j
|
|
|
+public class ActivitiUserModelServiceImpl extends BaseServiceImpl<ActivitiUserModelMapper, ActivitiUserModel, ActivitiUserModelDTO> implements ActivitiUserModelService {
|
|
|
+
|
|
|
+ private List<ActivitiAssignStrategy> activitiAssignStrategyList;
|
|
|
+ private RepositoryService repositoryService;
|
|
|
+ private ProcessEngineConfiguration processEngineConfiguration;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int batchDelete(List<String> ids) {
|
|
|
+ Weekend<ActivitiUserModel> weekend = new Weekend<>(ActivitiUserModel.class);
|
|
|
+ WeekendCriteria<ActivitiUserModel, Object> weekendCriteria = weekend.weekendCriteria();
|
|
|
+ weekendCriteria.andIn(ActivitiUserModel::getId, ids);
|
|
|
+ mapper.deleteByExample(weekend);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ActivitiUserModelVO getVOById(String id) {
|
|
|
+ return mapper.getVOById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AbstractPageResultBean<ActivitiUserModelVO> selectPageList(ActivitiUserModelDTO record, int pageNum, int pageSize) {
|
|
|
+ PageHelper.startPage(pageNum, pageSize);
|
|
|
+ return new MyPage(mapper.selectList(record));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AbstractPageResultBean<ActivitiUserModel> selectPageInfo(ActivitiUserModelDTO record, int pageNum, int pageSize) {
|
|
|
+ PageHelper.startPage(pageNum, pageSize);
|
|
|
+ return new MyPage(mapper.selectList(record));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateTypeById(String id, Integer type) {
|
|
|
+ ActivitiUserModel updInfo = new ActivitiUserModel();
|
|
|
+ updInfo.setId(id);
|
|
|
+ updInfo.setType(type);
|
|
|
+ mapper.updateByPrimaryKeySelective(updInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询usermodel。如果为空说明第一次加载,需要新建
|
|
|
+ *
|
|
|
+ * @param modelId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<ActivitiUserModel> getModelByModelId(String modelId) {
|
|
|
+ Weekend<ActivitiUserModel> weekend = new Weekend<>(ActivitiUserModel.class);
|
|
|
+ WeekendCriteria<ActivitiUserModel, Object> weekendCriteria = weekend.weekendCriteria();
|
|
|
+ weekendCriteria.andEqualTo(ActivitiUserModel::getReModelId, modelId);
|
|
|
+ List<ActivitiUserModel> activitiUserModelList = mapper.selectByExample(weekend);
|
|
|
+ if (CollectionUtils.isEmpty(activitiUserModelList)) {
|
|
|
+ Model model = repositoryService.getModel(modelId);
|
|
|
+ try {
|
|
|
+ activitiUserModelList = getUserTaskList(modelId, model.getDeploymentId());
|
|
|
+ mapper.insertListforComplex(activitiUserModelList);
|
|
|
+ } catch (IOException | DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("查询流程图失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return activitiUserModelList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param modelId
|
|
|
+ * @param deploymentId
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public List<ActivitiUserModel> getUserTaskList(String modelId, String deploymentId) throws IOException, DocumentException {
|
|
|
+ byte[] byteArray = this.getByteArray(deploymentId);
|
|
|
+ if (byteArray == null) {
|
|
|
+ throw new BusinessException("流程尚未发布,请先发布");
|
|
|
+ }
|
|
|
+ SAXReader saxReader = new SAXReader();
|
|
|
+ // 获取流程图文件中的userTask节点的所有属性
|
|
|
+ ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
|
|
|
+ Document document = saxReader.read(bis);
|
|
|
+ Element rootElement = document.getRootElement();
|
|
|
+ Element process = rootElement.element("process");
|
|
|
+ List<Element> userTaskList = process.elements("userTask");
|
|
|
+ List<ActivitiUserModel> list = new ArrayList<>();
|
|
|
+ // 包装成适合前端展示的集合并返回
|
|
|
+ for (Element element : userTaskList) {
|
|
|
+ ActivitiUserModel userTaskModel = new ActivitiUserModel();
|
|
|
+ userTaskModel.setId(element.attributeValue("id"));
|
|
|
+ userTaskModel.setReModelId(modelId);
|
|
|
+ userTaskModel.setDeploymentId(deploymentId);
|
|
|
+ userTaskModel.setName(element.attributeValue("name"));
|
|
|
+ Integer type = ActivitiUserModelTypeEnum.USER.getValue();
|
|
|
+ String assignee = element.attributeValue("assignee");
|
|
|
+ String candidateUsers = element.attributeValue("candidateUsers");
|
|
|
+ String candidateGroups = element.attributeValue("candidateGroups");
|
|
|
+ if (StringUtils.isNotEmpty(candidateGroups)) {
|
|
|
+ type = 3;
|
|
|
+ userTaskModel.setCandidateGroups(candidateGroups);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(candidateUsers)) {
|
|
|
+ type = 2;
|
|
|
+ userTaskModel.setCandidateUsers(candidateUsers);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(assignee)) {
|
|
|
+ type = 1;
|
|
|
+ userTaskModel.setAssignee(assignee);
|
|
|
+ }
|
|
|
+ userTaskModel.setType(type);
|
|
|
+ list.add(userTaskModel);
|
|
|
+ }
|
|
|
+ bis.close();
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从ACT_GE_BYTEARRAY表中获取流程图的二进制文件
|
|
|
+ * 此操作对象必须是已部署的模型,此时流程定义的二进制文件才是以bpmn20.xml结尾的。
|
|
|
+ *
|
|
|
+ * @param deploymentId
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private byte[] getByteArray(String deploymentId) {
|
|
|
+ Map<String, Object> result = mapper.queryActByteArray(deploymentId);
|
|
|
+ byte[] object = (byte[]) result.get("bytes");
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改审批节点
|
|
|
+ *
|
|
|
+ * @param modeId
|
|
|
+ * @param list
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void modify(String modeId, List<ActivitiUserModel> list) {
|
|
|
+ try {
|
|
|
+ Model model = repositoryService.getModel(modeId);
|
|
|
+ String deploymentId = model.getDeploymentId();
|
|
|
+ byte[] byteArray = this.getByteArray(deploymentId);
|
|
|
+ SAXReader saxReader = new SAXReader();
|
|
|
+ saxReader.setEncoding("UTF-8");
|
|
|
+ ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
|
|
|
+ // 获取所有用户节点
|
|
|
+ Document document = saxReader.read(bis);
|
|
|
+ Element rootElement = document.getRootElement();
|
|
|
+ Element process = rootElement.element("process");
|
|
|
+ List<Element> userTaskNodeList = process.elements("userTask");
|
|
|
+ // 遍历对应节点id,修改节点属性
|
|
|
+ for (Element element : userTaskNodeList) {
|
|
|
+ String id = element.attributeValue("id");
|
|
|
+ ActivitiUserModel userTaskModel =
|
|
|
+ list.stream().filter(u -> u.getId().equals(id)).collect(Collectors.toList()).get(0);
|
|
|
+ Integer type = userTaskModel.getType();
|
|
|
+ /*Attribute assignee = element.attribute("assignee");
|
|
|
+ if (assignee != null) {
|
|
|
+ element.remove(assignee);
|
|
|
+ }
|
|
|
+ Attribute candidateUsers = element.attribute("candidateUsers");
|
|
|
+ if (candidateUsers != null) {
|
|
|
+ element.remove(candidateUsers);
|
|
|
+ }
|
|
|
+ Attribute candidateGroups = element.attribute("candidateGroups");
|
|
|
+ if (candidateGroups != null) {
|
|
|
+ element.remove(candidateGroups);
|
|
|
+ }*/
|
|
|
+ ActivitiAssignStrategy activitiAssignStrategy =
|
|
|
+ activitiAssignStrategyList.stream().filter(strategy -> strategy.getType().equals(type)).collect(Collectors.toList()).get(0);
|
|
|
+ activitiAssignStrategy.handleAssign(element, userTaskModel);
|
|
|
+ userTaskModel.setDeploymentId(model.getDeploymentId());
|
|
|
+ /*switch (type) {
|
|
|
+ case 1:
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ element.addAttribute("activiti:assignee", userTaskModel.getAssignee());
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ element.addAttribute("activiti:candidateUsers", userTaskModel.getCandidateUsers());
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ element.addAttribute("activiti:candidateGroups", userTaskModel.getCandidateGroups());
|
|
|
+ break;
|
|
|
+ }*/
|
|
|
+ }
|
|
|
+ // 将修改后的流程图文件转为字节数组
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
+ XMLWriter xmlWriter = new XMLWriter(out);
|
|
|
+ xmlWriter.write(document);
|
|
|
+ xmlWriter.flush();
|
|
|
+ final byte[] bytes = out.toByteArray();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("deploymentId", deploymentId);
|
|
|
+ map.put("bytes", bytes);
|
|
|
+ mapper.updateActByteArray(map);
|
|
|
+ /**流程定义发布后,若你使用了该流程,其就会在缓存中缓存了流程定义的解析后的对象,供整个引擎使用,
|
|
|
+ * 这时你更改了流程定义的XML后,那份缓存并没有实现了更改,因此,需要告诉引擎,让他清空缓存中的该流程定义
|
|
|
+ */
|
|
|
+ ((ProcessEngineConfigurationImpl) processEngineConfiguration).getProcessDefinitionCache().remove(deploymentId);
|
|
|
+ for (ActivitiUserModel activitiUserModel : list) {
|
|
|
+ activitiUserModel.setDeploymentId(model.getDeploymentId());
|
|
|
+ }
|
|
|
+ mapper.updateBatch(list);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|