package jnpf.flowable.service.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.toolkit.JoinWrappers; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import jnpf.base.UserInfo; import jnpf.base.entity.DictionaryDataEntity; import jnpf.base.entity.PrintDevEntity; import jnpf.base.entity.SystemEntity; import jnpf.base.entity.VisualdevEntity; import jnpf.base.model.base.SystemBaeModel; import jnpf.base.service.SuperServiceImpl; import jnpf.constant.AuthorizeConst; import jnpf.constant.MsgCode; import jnpf.exception.WorkFlowException; import jnpf.flowable.entity.*; import jnpf.flowable.enums.NodeEnum; import jnpf.flowable.enums.TemplateStatueEnum; import jnpf.flowable.mapper.TemplateMapper; import jnpf.flowable.model.template.*; import jnpf.flowable.model.templatejson.FlowFormModel; import jnpf.flowable.model.templatejson.TemplateJsonExportModel; import jnpf.flowable.model.templatenode.TemplateNodeUpFrom; import jnpf.flowable.model.util.FlowNature; import jnpf.flowable.service.*; import jnpf.flowable.util.ServiceUtil; import jnpf.flowable.util.TaskUtil; import jnpf.permission.entity.UserEntity; import jnpf.permission.model.authorize.AuthorizeVO; import jnpf.util.JsonUtil; import jnpf.util.RandomUtil; import jnpf.util.StringUtil; import jnpf.util.UserProvider; import jnpf.util.context.RequestContext; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors; @Service public class TemplateServiceImpl extends SuperServiceImpl implements TemplateService { @Autowired private TemplateJsonService templateJsonService; @Autowired private TemplateNodeService templateNodeService; @Autowired private ServiceUtil serviceUtil; @Autowired private DelegateService delegateService; @Autowired private CommonService commonService; @Autowired private TaskUtil taskUtil; @Autowired private TriggerTaskService triggerTaskService; @Autowired private TemplateUseNumService templateUseNumService; @Override public List getList(TemplatePagination pagination) { QueryWrapper queryWrapper = new QueryWrapper<>(); String keyword = pagination.getKeyword(); if (ObjectUtil.isNotEmpty(keyword)) { queryWrapper.lambda().and(t -> t.like(TemplateEntity::getEnCode, keyword).or().like(TemplateEntity::getFullName, keyword)); } String category = pagination.getCategory(); if (ObjectUtil.isNotEmpty(category)) { queryWrapper.lambda().eq(TemplateEntity::getCategory, category); } Integer type = pagination.getType(); if (ObjectUtil.isNotEmpty(type)) { queryWrapper.lambda().eq(TemplateEntity::getType, type); } Integer enabledMark = pagination.getEnabledMark(); if (ObjectUtil.isNotEmpty(enabledMark)) { queryWrapper.lambda().eq(TemplateEntity::getEnabledMark, enabledMark); } String systemId = pagination.getSystemId(); if (ObjectUtil.isNotEmpty(systemId)) { queryWrapper.lambda().eq(TemplateEntity::getSystemId, systemId); } queryWrapper.lambda().orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime); Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage userPage = this.page(page, queryWrapper); return pagination.setData(userPage.getRecords(), page.getTotal()); } @Override public List getSelector(TemplatePagination pagination) { // 流程权限 Set flowId = new HashSet<>(); String userId = UserProvider.getLoginUserId(); if (StringUtil.isNotBlank(pagination.getDelegateUser())) { userId = pagination.getDelegateUser(); } boolean isDelegate = ObjectUtil.equals(pagination.getIsDelegate(), 1); boolean isAuthority = ObjectUtil.equals(pagination.getIsAuthority(), 1); boolean isLaunch = ObjectUtil.equals(pagination.getIsLaunch(), 1); boolean isSystemFrom = ObjectUtil.equals(pagination.getIsSystemFrom(), 1); //委托流程的数据 boolean isEntrust = StringUtils.equals("-1", pagination.getSystemId()); //当前用户 AuthorizeVO authorizeByUser = serviceUtil.getAuthorizeByUser(); // 权限 boolean commonUser = serviceUtil.isCommonUser(userId); if (commonUser) { if (isDelegate || isAuthority) { flowId.addAll(authorizeByUser.getFlowIdList()); } } List flowList = new ArrayList<>(); List delegateEntityList = delegateService.getByToUserId(userId, 0); //获取委托 for (DelegateEntity delegate : delegateEntityList) { List launchPermission = serviceUtil.getPermission(delegate.getUserId()); if (StringUtil.isNotEmpty(delegate.getFlowId())) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().in(TemplateEntity::getId, Arrays.asList(delegate.getFlowId().split(","))); List list = this.list(queryWrapper); //用户拥有权限 List authoirtyList = list.stream().filter(e -> ObjectUtil.equals(e.getVisibleType(), FlowNature.Authority)).map(TemplateEntity::getId).collect(Collectors.toList()); launchPermission.retainAll(authoirtyList); flowList.addAll(launchPermission); //发起列表显示,委托列表不显示 if (!isDelegate) { flowId.addAll(launchPermission); } //公开授权 List flowIdList = list.stream().map(TemplateEntity::getId).collect(Collectors.toList()); flowIdList.removeAll(authoirtyList); flowList.addAll(flowIdList); } else { List system = serviceUtil.getPermission(delegate.getUserId(), AuthorizeConst.SYSTEM); if (system.isEmpty()) { continue; } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(TemplateEntity::getSystemId, system) .eq(TemplateEntity::getVisibleType, FlowNature.All); List list = this.list(queryWrapper); List flowIdList = list.stream().map(TemplateEntity::getId).collect(Collectors.toList()); flowList.addAll(flowIdList); } } MPJLambdaWrapper wrapper = JoinWrappers.lambda(TemplateEntity.class) .selectAll(TemplateEntity.class) .eq(TemplateEntity::getEnabledMark, 1).ne(TemplateEntity::getType, FlowNature.Quest) .eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode()); //关键字(流程名称、流程编码) String keyWord = pagination.getKeyword(); if (ObjectUtil.isNotEmpty(keyWord)) { wrapper.and(t -> t.like(TemplateEntity::getEnCode, keyWord).or().like(TemplateEntity::getFullName, keyWord)); } //流程显示类型 if (isLaunch) { List typeList = ImmutableList.of(FlowNature.AllShowType, FlowNature.FlowShowType); wrapper.in(TemplateEntity::getShowType, typeList); } List systemIdList = authorizeByUser.getSystemList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).map(SystemBaeModel::getId).collect(Collectors.toList()); if (isEntrust) { if (flowList.isEmpty()) { return new ArrayList<>(); } List> lists = Lists.partition(flowList, 1000); wrapper.and(t -> { for (List list : lists) { t.in(TemplateEntity::getId, list).or(); } }); wrapper.notIn(!systemIdList.isEmpty(), TemplateEntity::getSystemId, systemIdList); } else { //应用主建 String systemId = pagination.getSystemId(); if (ObjectUtil.isNotEmpty(systemId)) { systemIdList.retainAll(Arrays.asList(systemId.split(","))); } if (systemIdList.isEmpty()) { return new ArrayList<>(); } if (isAuthority) { if (commonUser) { wrapper.and(t -> t.eq(TemplateEntity::getVisibleType, FlowNature.All) .or().in(!flowId.isEmpty(), TemplateEntity::getId, flowId) ); } } wrapper.in(TemplateEntity::getSystemId, systemIdList); //所属分类 String category = pagination.getCategory(); if (ObjectUtil.isNotEmpty(category)) { if (StringUtil.equals(category, "commonFlow")) { wrapper.leftJoin(CommonEntity.class, CommonEntity::getFlowId, TemplateJsonEntity::getId) .eq(CommonEntity::getCreatorUserId, userId).isNotNull(CommonEntity::getFlowId); } else { wrapper.in(TemplateEntity::getCategory, Arrays.asList(category.split(","))); } } } wrapper.orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime); Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage userPage = this.selectJoinListPage(page, TemplatePageVo.class, wrapper); List records = userPage.getRecords(); // 设置常用流程标识 List commonList = commonService.getCommonByUserId(userId); if (CollectionUtil.isNotEmpty(commonList)) { List flowIds = commonList.stream().map(CommonEntity::getFlowId).distinct().collect(Collectors.toList()); for (TemplatePageVo record : records) { if (flowIds.contains(record.getId())) { record.setIsCommonFlow(true); } } } //判断流程的表单类型 if (isSystemFrom) { List flowIdList = records.stream().map(TemplatePageVo::getFlowId).collect(Collectors.toList()); List list = templateNodeService.getList(flowIdList, NodeEnum.start.getType()); Map formMap = new HashMap<>(); for (TemplateNodeEntity entity : list) { formMap.put(entity.getFlowId(), entity.getFormId()); } List formList = serviceUtil.getFormList(new ArrayList<>(formMap.values())).stream().filter(e -> Objects.equals(e.getType(), 2)).map(VisualdevEntity::getId).collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(formList)) { for (TemplatePageVo record : records) { String formId = formMap.get(record.getFlowId()); if (formList.contains(formId)) { record.setIsQuote(1); } } } } return pagination.setData(records, page.getTotal()); } @Override public List getTreeCommon() { String userId = UserProvider.getLoginUserId(); List vos = new ArrayList<>(); List commonList = commonService.getCommonByUserId(userId); if (CollectionUtil.isEmpty(commonList)) { return vos; } String systemCodeById = serviceUtil.getSystemCodeById(RequestContext.getAppCode()); List flowIds = commonList.stream().map(CommonEntity::getFlowId).collect(Collectors.toList()); List templateList = this.getList(flowIds); if (StringUtil.isNotEmpty(systemCodeById)) { templateList = templateList.stream().filter(e -> Objects.equals(e.getSystemId(), systemCodeById)).collect(Collectors.toList()); } if (CollectionUtil.isNotEmpty(templateList)) { TemplateTreeListVo allVo = new TemplateTreeListVo(); allVo.setId(RandomUtil.uuId()); allVo.setFullName("全部流程"); List childrenList = this.getChildren(templateList); allVo.setChildren(childrenList); vos.add(allVo); Map> map = childrenList.stream().collect(Collectors.groupingBy(TemplateTreeListVo::getCategory)); List categoryIds = new ArrayList<>(map.keySet()); List dictionNameList = serviceUtil.getDictionName(categoryIds); for (DictionaryDataEntity dict : dictionNameList) { TemplateTreeListVo vo = new TemplateTreeListVo(); vo.setId(dict.getId()); vo.setFullName(dict.getFullName()); vo.setChildren(map.get(dict.getId())); vos.add(vo); } } return vos; } public List getChildren(List templates) { List resList = new ArrayList<>(); for (TemplateEntity template : templates) { TemplateTreeListVo vo = JsonUtil.getJsonToBean(template, TemplateTreeListVo.class); resList.add(vo); } return resList; } @Override public List treeList(Integer formType) { List dictionList = serviceUtil.getDiList(); TemplatePagination pagination = new TemplatePagination(); pagination.setSystemId(serviceUtil.getSystemCodeById(RequestContext.getAppCode())); List list = getListAll(pagination, false); List startNodeList; // formType 1.系统表单 2.在线开发表单 if (null != formType) { // 版本主键 List flowIds = list.stream().map(TemplateEntity::getFlowId).collect(Collectors.toList()); startNodeList = templateNodeService.getList(flowIds, NodeEnum.start.getType()); } else { startNodeList = new ArrayList<>(); } List formIds = startNodeList.stream().map(TemplateNodeEntity::getFormId).collect(Collectors.toList()); List formList = serviceUtil.getFormList(formIds); List vos = new ArrayList<>(); for (DictionaryDataEntity dict : dictionList) { TemplateTreeListVo vo = new TemplateTreeListVo(); vo.setFullName(dict.getFullName()); vo.setId(dict.getId()); vo.setDisabled(true); if (CollectionUtil.isNotEmpty(list)) { List childList = list.stream() .filter(e -> dict.getId().equals(e.getCategory())).collect(Collectors.toList()); if (null != formType) { childList = childList.stream().filter(e -> { // 是流程显示类型 if (ObjectUtil.equals(e.getShowType(), FlowNature.FlowShowType)) { return false; } TemplateNodeEntity node = startNodeList.stream().filter(a -> a.getFlowId().equals(e.getFlowId())).findFirst().orElse(null); if (null != node) { VisualdevEntity form = formList.stream().filter(b -> b.getId().equals(node.getFormId())).findFirst().orElse(null); if (null != form) { return form.getType().equals(1); } } return false; }).collect(Collectors.toList()); } if (CollectionUtil.isNotEmpty(childList)) { childList = childList.stream() .sorted(Comparator.comparing(TemplateEntity::getSortCode, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(TemplateEntity::getCreatorTime).reversed()) .collect(Collectors.toList()); vo.setNum(childList.size()); vo.setChildren(JsonUtil.getJsonToList(childList, TemplateTreeListVo.class)); vos.add(vo); } } } return vos; } @Override public List treeListWithPower() { List vos = new ArrayList<>(); MPJLambdaWrapper wrapper = JoinWrappers.lambda(TemplateEntity.class) .selectAll(TemplateEntity.class) .eq(TemplateEntity::getVisibleType, FlowNature.Authority) .isNotNull(TemplateEntity::getFlowId); List list = this.selectJoinList(TemplateEntity.class, wrapper); List systemIdList = list.stream().map(TemplateEntity::getSystemId).collect(Collectors.toList()); List systemList = serviceUtil.getSystemList(systemIdList); for (SystemEntity system : systemList) { TemplateTreeListVo vo = new TemplateTreeListVo(); vo.setFullName(system.getFullName()); vo.setId(system.getId()); vo.setDisabled(true); if (CollectionUtil.isNotEmpty(list)) { List childList = list.stream() .filter(e -> system.getId().equals(e.getSystemId())) .sorted(Comparator.comparing(TemplateEntity::getSortCode, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(TemplateEntity::getCreatorTime, Comparator.reverseOrder())) .collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(childList)) { vo.setNum(childList.size()); vo.setChildren(JsonUtil.getJsonToList(childList, TemplateTreeListVo.class)); vos.add(vo); } } } return vos; } @Override public List getListAll(TemplatePagination pagination, boolean isPage) { // 定义变量判断是否需要使用修改时间倒序 boolean flag = false; QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(TemplateEntity::getEnabledMark, 1).eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode()); if (ObjectUtil.isNotEmpty(pagination.getType())) { flag = true; queryWrapper.lambda().eq(TemplateEntity::getType, pagination.getType()); } if (ObjectUtil.isNotEmpty(pagination.getKeyword())) { flag = true; queryWrapper.lambda().like(TemplateEntity::getFullName, pagination.getKeyword()); } if (ObjectUtil.isNotEmpty(pagination.getCategory())) { flag = true; queryWrapper.lambda().eq(TemplateEntity::getCategory, pagination.getCategory()); } if (ObjectUtil.isNotEmpty(pagination.getTemplateIdList())) { queryWrapper.lambda().in(TemplateEntity::getId, pagination.getTemplateIdList()); } if (ObjectUtil.isNotEmpty(pagination.getSystemId())) { queryWrapper.lambda().eq(TemplateEntity::getSystemId, pagination.getSystemId()); } queryWrapper.lambda().orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime); if (flag) { queryWrapper.lambda().orderByDesc(TemplateEntity::getLastModifyTime); } queryWrapper.lambda().select( TemplateEntity::getId, TemplateEntity::getEnCode, TemplateEntity::getFullName, TemplateEntity::getFlowId, TemplateEntity::getStatus, TemplateEntity::getShowType, TemplateEntity::getType, TemplateEntity::getIcon, TemplateEntity::getCategory, TemplateEntity::getIconBackground, TemplateEntity::getCreatorUserId, TemplateEntity::getSortCode, TemplateEntity::getEnabledMark, TemplateEntity::getCreatorTime ); if (isPage) { Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage userPage = this.page(page, queryWrapper); return pagination.setData(userPage.getRecords(), page.getTotal()); } else { return this.list(queryWrapper); } } @Override public List getListByFlowIds(List flowIds) { List list = new ArrayList<>(); List jsonEntityList = templateJsonService.listByIds(flowIds); if (CollectionUtil.isNotEmpty(jsonEntityList)) { List templateIds = jsonEntityList.stream().map(TemplateJsonEntity::getTemplateId).distinct().collect(Collectors.toList()); if (CollectionUtil.isNotEmpty(templateIds)) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().in(TemplateEntity::getId, templateIds).eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode()); list = this.list(wrapper); } } return list; } @Override public TemplateEntity getInfo(String id) throws WorkFlowException { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(TemplateEntity::getId, id); TemplateEntity templateEntity = this.getOne(queryWrapper); if (templateEntity == null) { throw new WorkFlowException(MsgCode.WF122.get()); } return templateEntity; } @Override public boolean isExistByFullName(String fullName, String id, String systemId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(TemplateEntity::getFullName, fullName); if (!StringUtils.isEmpty(id)) { queryWrapper.lambda().ne(TemplateEntity::getId, id); } if (!StringUtils.isEmpty(systemId)) { queryWrapper.lambda().eq(TemplateEntity::getSystemId, systemId); } return this.count(queryWrapper) > 0; } @Override public boolean isExistByEnCode(String enCode, String id, String systemId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(TemplateEntity::getEnCode, enCode); if (!StringUtils.isEmpty(id)) { queryWrapper.lambda().ne(TemplateEntity::getId, id); } if (!StringUtils.isEmpty(systemId)) { queryWrapper.lambda().eq(TemplateEntity::getSystemId, systemId); } return this.count(queryWrapper) > 0; } @Override public void create(TemplateEntity entity, String flowXml, Map> flowNodes) throws WorkFlowException { entity.setSystemId(serviceUtil.getSystemCodeById(RequestContext.getAppCode())); if (StringUtil.isEmpty(entity.getEnCode())) { entity.setEnCode(getEnCode(entity)); } if (isExistByFullName(entity.getFullName(), entity.getId(), entity.getSystemId())) { throw new WorkFlowException(MsgCode.EXIST001.get()); } if (isExistByEnCode(entity.getEnCode(), entity.getId(), null)) { throw new WorkFlowException(MsgCode.EXIST002.get()); } UserInfo userInfo = UserProvider.getUser(); entity.setId(StringUtil.isNotEmpty(entity.getId()) ? entity.getId() : RandomUtil.uuId()); entity.setCreatorUserId(userInfo.getUserId()); entity.setCreatorTime(new Date()); entity.setFlowId(null); entity.setEnabledMark(0); entity.setStatus(TemplateStatueEnum.none.getCode()); entity.setLastModifyUserId(null); entity.setLastModifyTime(null); this.setIgnoreLogicDelete().removeById(entity.getId()); this.setIgnoreLogicDelete().saveOrUpdate(entity); this.clearIgnoreLogicDelete(); TemplateNodeUpFrom from = new TemplateNodeUpFrom(); from.setId(entity.getId()); from.setFlowXml(flowXml); from.setFlowNodes(flowNodes); templateJsonService.create(from); } @Override public boolean update(String id, TemplateEntity entity) throws WorkFlowException { if (StringUtil.isEmpty(entity.getEnCode())) { entity.setEnCode(getEnCode(entity)); } if (isExistByFullName(entity.getFullName(), id, entity.getSystemId())) { throw new WorkFlowException(MsgCode.EXIST001.get()); } if (isExistByEnCode(entity.getEnCode(), id, entity.getSystemId())) { throw new WorkFlowException(MsgCode.EXIST002.get()); } entity.setId(id); return this.updateById(entity); } @Override public void delete(TemplateEntity entity) throws WorkFlowException { if (entity != null) { if (ObjectUtil.equals(entity.getType(), FlowNature.Quest)) { List list = templateJsonService.getList(entity.getId()); List flowIds = list.stream().map(TemplateJsonEntity::getId).collect(Collectors.toList()); if (triggerTaskService.checkByFlowIds(flowIds)) { throw new WorkFlowException(MsgCode.WF139.get()); } } else { List taskList = taskUtil.getTaskByTemplate(entity.getId()); if (CollectionUtil.isNotEmpty(taskList)) { throw new WorkFlowException(MsgCode.WF124.get()); } } commonService.deleteFlow(entity.getId()); templateUseNumService.deleteUseNum(entity.getId(), null); this.removeById(entity.getId()); List idList = templateJsonService.getList(entity.getId()).stream().map(TemplateJsonEntity::getId).collect(Collectors.toList()); templateJsonService.delete(idList); } } @Override public void copy(TemplateEntity entity) throws WorkFlowException { try { TemplateJsonEntity jsonEntity = StringUtil.isNotEmpty(entity.getFlowId()) ? templateJsonService.getInfo(entity.getFlowId()) : null; String formXml = jsonEntity != null ? jsonEntity.getFlowXml() : null; List list = jsonEntity != null ? templateNodeService.getList(jsonEntity.getId()) : new ArrayList<>(); String copyNum = UUID.randomUUID().toString().substring(0, 5); entity.setFullName(entity.getFullName() + ".副本" + copyNum); entity.setEnCode(entity.getEnCode() + copyNum); entity.setId(null); Map> flowNodes = new HashMap<>(); for (TemplateNodeEntity nodeEntity : list) { flowNodes.put(nodeEntity.getNodeCode(), JsonUtil.stringToMap(nodeEntity.getNodeJson())); } this.create(entity, formXml, flowNodes); } catch (Exception e) { log.error(e.getMessage()); throw new WorkFlowException(MsgCode.PRI006.get()); } } @Override public TemplateExportModel export(String id) throws WorkFlowException { TemplateEntity entity = getInfo(id); TemplateExportModel exportModel = new TemplateExportModel(); exportModel.setTemplate(entity); // 版本 TemplateJsonEntity jsonEntity = templateJsonService.getInfo(entity.getFlowId()); TemplateJsonExportModel versionModel = JsonUtil.getJsonToBean(jsonEntity, TemplateJsonExportModel.class); // 节点 List list = templateNodeService.getList(entity.getFlowId()); exportModel.setNodeList(list); exportModel.setFlowVersion(versionModel); return exportModel; } @Override public void importData(TemplateExportModel model, String type) throws WorkFlowException { TemplateEntity entity = model.getTemplate(); TemplateJsonExportModel versionModel = model.getFlowVersion(); String systemId = serviceUtil.getSystemCodeById(RequestContext.getAppCode()); if (null != entity) { entity.setFlowId(null); entity.setVersion(null); entity.setCreatorUserId(UserProvider.getLoginUserId()); entity.setCreatorTime(new Date()); entity.setLastModifyTime(null); entity.setLastModifyUserId(null); entity.setEnabledMark(0); if (!Objects.equals(systemId, entity.getSystemId())) { entity.setId(RandomUtil.uuId()); entity.setSystemId(systemId); entity.setEnCode(getEnCode(entity)); } List errList = new ArrayList<>(); // type: 0.当导入数据不存在,作为新数据导入;数据已存在,不做处理 1.当导入数据已存在,增加相同记录新数据,名称和编码自动增加随机码 TemplateEntity templateEntity = checkImportEntity(entity, type, errList); if (!errList.isEmpty()) { StringJoiner joiner = new StringJoiner(";"); joiner.add(String.join("、", errList) + MsgCode.IMP007.get()); if (StringUtil.isNotEmpty(joiner.toString())) { throw new WorkFlowException(joiner.toString()); } } Map> flowNodes = new HashMap<>(); for (TemplateNodeEntity nodeEntity : model.getNodeList()) { flowNodes.put(nodeEntity.getNodeCode(), JsonUtil.stringToMap(nodeEntity.getNodeJson())); } this.create(templateEntity, versionModel.getFlowXml(), flowNodes); } } @Override public List getList(List ids) { if (ids.isEmpty()) { return new ArrayList<>(); } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().in(TemplateEntity::getId, ids).eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode()); return this.list(queryWrapper); } @Override public List getListOfHidden(List ids) { if (ids.isEmpty()) { return new ArrayList<>(); } QueryWrapper queryWrapper = new QueryWrapper<>(); List statusList = ImmutableList.of(TemplateStatueEnum.up.getCode(), TemplateStatueEnum.downContinue.getCode()); queryWrapper.lambda().in(TemplateEntity::getId, ids).in(TemplateEntity::getStatus, statusList); return this.list(queryWrapper); } // 校验导入的实体 public TemplateEntity checkImportEntity(TemplateEntity templateEntity, String type, List errList) { TemplateEntity entity = JsonUtil.getJsonToBean(templateEntity, TemplateEntity.class); boolean skip = Objects.equals("0", type); int num = 0; QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(TemplateEntity::getId, entity.getId()); if (this.count(queryWrapper) > 0) { num++; if (skip) { errList.add("ID"); } } if (isExistByEnCode(entity.getEnCode(), null, entity.getSystemId())) { num++; if (skip) { errList.add(MsgCode.IMP009.get()); } } if (isExistByFullName(entity.getFullName(), null, entity.getSystemId())) { num++; if (skip) { errList.add(MsgCode.IMP008.get()); } } if (num > 0 && !skip) { String copyNum = UUID.randomUUID().toString().substring(0, 5); entity.setFullName(entity.getFullName() + ".副本" + copyNum); entity.setEnCode(entity.getEnCode() + copyNum); } entity.setId(RandomUtil.uuId()); return entity; } @Override public FlowByFormModel getFlowByFormId(String formId, Boolean start) { FlowByFormModel res = new FlowByFormModel(); List resList = new ArrayList<>(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(TemplateNodeEntity::getFormId, formId); // start 传true 仅发起节点 false 全部节点 start = null == start || start; if (start) { wrapper.lambda().eq(TemplateNodeEntity::getNodeType, NodeEnum.start.getType()); } List nodeList = templateNodeService.list(wrapper); if (CollectionUtil.isEmpty(nodeList)) { return res; } List flowIds = nodeList.stream().map(TemplateNodeEntity::getFlowId).distinct().collect(Collectors.toList()); if (CollectionUtil.isEmpty(flowIds)) { return res; } List versionList = templateJsonService.listByIds(flowIds); if (CollectionUtil.isEmpty(versionList)) { return res; } // 获取启用版本 versionList = versionList.stream().filter(e -> e.getState().equals(1)) .sorted(Comparator.comparing(TemplateJsonEntity::getCreatorTime).reversed()).collect(Collectors.toList()); // 获取流程 List templateIds = versionList.stream().map(TemplateJsonEntity::getTemplateId).distinct().collect(Collectors.toList()); List templateList = CollectionUtil.isNotEmpty(templateIds) ? this.listByIds(templateIds) : new ArrayList<>(); // 获取权限流程 String loginUserId = UserProvider.getLoginUserId(); //是否普通用户 boolean commonUser = serviceUtil.isCommonUser(loginUserId); List templatePermissionIds = new ArrayList<>(); if (commonUser) { templatePermissionIds.addAll(serviceUtil.getLaunchPermission()); } for (TemplateJsonEntity jsonEntity : versionList) { TemplateByFormModel model = new TemplateByFormModel(); model.setId(jsonEntity.getId()); TemplateEntity template = templateList.stream() .filter(e -> ObjectUtil.equals(e.getId(), jsonEntity.getTemplateId())).findFirst().orElse(null); if (null == template || !ObjectUtil.equals(template.getStatus(), TemplateStatueEnum.up.getCode())) { continue; } if (commonUser && ObjectUtil.equals(template.getVisibleType(), FlowNature.Authority)) { // 带权限 if (templatePermissionIds.contains(jsonEntity.getId())) { model.setFullName(template.getFullName()); resList.add(model); } } else { model.setFullName(template.getFullName()); resList.add(model); } } res.setList(resList); res.setIsConfig(true); return res; } @Override public List getSubFlowUserList(String flowId, TemplatePagination pagination) throws WorkFlowException { TemplateEntity template = this.getInfo(flowId); return serviceUtil.getLaunchUserByTemplateId(template, pagination); } @Override public VisualdevEntity getFormByTemplateId(String templateId) throws WorkFlowException { TemplateEntity template = this.getInfo(templateId); List nodeEntityList = templateNodeService.getList(template.getFlowId()); TemplateNodeEntity nodeEntity = nodeEntityList.stream() .filter(e -> ObjectUtil.equals(e.getNodeType(), NodeEnum.start.getType())).findFirst().orElse(null); if (null == nodeEntity) { throw new WorkFlowException(MsgCode.FA001.get()); } return serviceUtil.getFormInfo(nodeEntity.getFormId()); } @Override public FlowFormModel getFormIdAndFlowId(String templateId) throws WorkFlowException { List userId = ImmutableList.of(UserProvider.getLoginUserId()); FlowFormModel model = getFormIdAndFlowId(userId, templateId); if (model.getUserId().isEmpty()) { throw new WorkFlowException(MsgCode.WF029.get()); } return model; } @Override public FlowFormModel getFormIdAndFlowId(List userIdAll, String templateId) throws WorkFlowException { List userList = new ArrayList<>(); TemplateEntity template = this.getInfo(templateId); List nodeEntityList = templateNodeService.getList(template.getFlowId()); // 判断权限 for (String userId : userIdAll) { userList.add(userId); if (ObjectUtil.equals(template.getVisibleType(), FlowNature.Authority)) { boolean commonUser = serviceUtil.isCommonUser(userId); if (commonUser) { List flowIds = serviceUtil.getPermission(userId); if (!flowIds.contains(template.getId())) { userList.remove(userId); } } } } TemplateNodeEntity nodeEntity = nodeEntityList.stream() .filter(e -> ObjectUtil.equals(e.getNodeType(), NodeEnum.start.getType())).findFirst().orElse(null); if (null == nodeEntity) { throw new WorkFlowException(MsgCode.FA001.get()); } FlowFormModel model = new FlowFormModel(); model.setFormId(nodeEntity.getFormId()); model.setFlowId(template.getFlowId()); model.setUserId(userList); model.setUserIdAll(userIdAll); return model; } @Override public List getFormList() { List resList = new ArrayList<>(); List list = templateJsonService.getListOfEnable(); if (CollectionUtil.isNotEmpty(list)) { List flowIds = list.stream().map(TemplateJsonEntity::getId).distinct().collect(Collectors.toList()); List startNodeList = templateNodeService.getList(flowIds, NodeEnum.start.getType()); resList = startNodeList.stream().map(TemplateNodeEntity::getFormId).distinct().collect(Collectors.toList()); } return resList; } @Override public Map getFlowFormMap() { Map map = new HashMap<>(); List listStart = templateNodeService.getListStart(); List collect = listStart.stream().map(TemplateNodeEntity::getFlowId).collect(Collectors.toList()); if (ObjectUtil.isNotEmpty(collect)) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().select(TemplateEntity::getId, TemplateEntity::getFlowId); queryWrapper.lambda().in(TemplateEntity::getFlowId, collect); Map flowTempMap = this.list(queryWrapper).stream().collect(Collectors.toMap(TemplateEntity::getFlowId, TemplateEntity::getId)); for (TemplateNodeEntity templateNodeEntity : listStart) { if (ObjectUtil.isNotEmpty(flowTempMap.get(templateNodeEntity.getFlowId()))) { map.put(flowTempMap.get(templateNodeEntity.getFlowId()), templateNodeEntity.getFormId()); } } } return map; } @Override public List getCommonList(TemplatePagination pagination) { AuthorizeVO authorize = pagination.getAuthorize(); if (authorize == null) { authorize = serviceUtil.getAuthorizeByUser(); } List systemIdList = authorize.getSystemList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).map(SystemBaeModel::getId).collect(Collectors.toList()); List flowId = authorize.getFlowIdList(); String userId = UserProvider.getLoginUserId(); MPJLambdaWrapper wrapper = JoinWrappers.lambda(TemplateEntity.class) .selectAll(TemplateEntity.class) .leftJoin(CommonEntity.class, CommonEntity::getFlowId, TemplateEntity::getId) .eq(TemplateEntity::getEnabledMark, 1).ne(TemplateEntity::getType, FlowNature.Quest) .eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode()); //应用主建 String systemId = pagination.getSystemId(); if (ObjectUtil.isNotEmpty(systemId)) { systemIdList.retainAll(Arrays.asList(systemId.split(","))); } if (systemIdList.isEmpty()) { return new ArrayList<>(); } wrapper.and(t -> t.eq(TemplateEntity::getVisibleType, FlowNature.All) .or().in(!flowId.isEmpty(), TemplateEntity::getId, flowId) ); wrapper.in(TemplateEntity::getSystemId, systemIdList); //关键字(流程名称、流程编码) String keyWord = pagination.getKeyword(); if (ObjectUtil.isNotEmpty(keyWord)) { wrapper.and(t -> t.like(TemplateEntity::getEnCode, keyWord).or().like(TemplateEntity::getFullName, keyWord)); } wrapper.eq(CommonEntity::getCreatorUserId, userId); wrapper.orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime); List list = this.selectJoinList(TemplatePageVo.class, wrapper); return list; } private String getEnCode(TemplateEntity entity) { String code = serviceUtil.getCode(); boolean existByEnCode = isExistByEnCode(code, entity.getId(), null); if (existByEnCode) { return getEnCode(entity); } return code; } @Override public List getListByCreUser(String creUser) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(TemplateEntity::getVisibleType, FlowNature.Authority); queryWrapper.lambda().eq(TemplateEntity::getCreatorUserId, creUser); return this.list(queryWrapper); } }