| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- 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<TemplateMapper, TemplateEntity> 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<TemplateEntity> getList(TemplatePagination pagination) {
- QueryWrapper<TemplateEntity> 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<TemplateEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<TemplateEntity> userPage = this.page(page, queryWrapper);
- return pagination.setData(userPage.getRecords(), page.getTotal());
- }
- @Override
- public List<TemplatePageVo> getSelector(TemplatePagination pagination) {
- // 流程权限
- Set<String> 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<String> flowList = new ArrayList<>();
- List<DelegateEntity> delegateEntityList = delegateService.getByToUserId(userId, 0);
- //获取委托
- for (DelegateEntity delegate : delegateEntityList) {
- List<String> launchPermission = serviceUtil.getPermission(delegate.getUserId());
- if (StringUtil.isNotEmpty(delegate.getFlowId())) {
- QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().in(TemplateEntity::getId, Arrays.asList(delegate.getFlowId().split(",")));
- List<TemplateEntity> list = this.list(queryWrapper);
- //用户拥有权限
- List<String> 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<String> flowIdList = list.stream().map(TemplateEntity::getId).collect(Collectors.toList());
- flowIdList.removeAll(authoirtyList);
- flowList.addAll(flowIdList);
- } else {
- List<String> system = serviceUtil.getPermission(delegate.getUserId(), AuthorizeConst.SYSTEM);
- if (system.isEmpty()) {
- continue;
- }
- QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(TemplateEntity::getSystemId, system)
- .eq(TemplateEntity::getVisibleType, FlowNature.All);
- List<TemplateEntity> list = this.list(queryWrapper);
- List<String> flowIdList = list.stream().map(TemplateEntity::getId).collect(Collectors.toList());
- flowList.addAll(flowIdList);
- }
- }
- MPJLambdaWrapper<TemplateEntity> 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<Integer> typeList = ImmutableList.of(FlowNature.AllShowType, FlowNature.FlowShowType);
- wrapper.in(TemplateEntity::getShowType, typeList);
- }
- List<String> 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<List<String>> lists = Lists.partition(flowList, 1000);
- wrapper.and(t -> {
- for (List<String> 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<TemplatePageVo> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<TemplatePageVo> userPage = this.selectJoinListPage(page, TemplatePageVo.class, wrapper);
- List<TemplatePageVo> records = userPage.getRecords();
- // 设置常用流程标识
- List<CommonEntity> commonList = commonService.getCommonByUserId(userId);
- if (CollectionUtil.isNotEmpty(commonList)) {
- List<String> 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<String> flowIdList = records.stream().map(TemplatePageVo::getFlowId).collect(Collectors.toList());
- List<TemplateNodeEntity> list = templateNodeService.getList(flowIdList, NodeEnum.start.getType());
- Map<String, String> formMap = new HashMap<>();
- for (TemplateNodeEntity entity : list) {
- formMap.put(entity.getFlowId(), entity.getFormId());
- }
- List<String> 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<TemplateTreeListVo> getTreeCommon() {
- String userId = UserProvider.getLoginUserId();
- List<TemplateTreeListVo> vos = new ArrayList<>();
- List<CommonEntity> commonList = commonService.getCommonByUserId(userId);
- if (CollectionUtil.isEmpty(commonList)) {
- return vos;
- }
- String systemCodeById = serviceUtil.getSystemCodeById(RequestContext.getAppCode());
- List<String> flowIds = commonList.stream().map(CommonEntity::getFlowId).collect(Collectors.toList());
- List<TemplateEntity> 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<TemplateTreeListVo> childrenList = this.getChildren(templateList);
- allVo.setChildren(childrenList);
- vos.add(allVo);
- Map<String, List<TemplateTreeListVo>> map = childrenList.stream().collect(Collectors.groupingBy(TemplateTreeListVo::getCategory));
- List<String> categoryIds = new ArrayList<>(map.keySet());
- List<DictionaryDataEntity> 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<TemplateTreeListVo> getChildren(List<TemplateEntity> templates) {
- List<TemplateTreeListVo> resList = new ArrayList<>();
- for (TemplateEntity template : templates) {
- TemplateTreeListVo vo = JsonUtil.getJsonToBean(template, TemplateTreeListVo.class);
- resList.add(vo);
- }
- return resList;
- }
- @Override
- public List<TemplateTreeListVo> treeList(Integer formType) {
- List<DictionaryDataEntity> dictionList = serviceUtil.getDiList();
- TemplatePagination pagination = new TemplatePagination();
- pagination.setSystemId(serviceUtil.getSystemCodeById(RequestContext.getAppCode()));
- List<TemplateEntity> list = getListAll(pagination, false);
- List<TemplateNodeEntity> startNodeList;
- // formType 1.系统表单 2.在线开发表单
- if (null != formType) {
- // 版本主键
- List<String> flowIds = list.stream().map(TemplateEntity::getFlowId).collect(Collectors.toList());
- startNodeList = templateNodeService.getList(flowIds, NodeEnum.start.getType());
- } else {
- startNodeList = new ArrayList<>();
- }
- List<String> formIds = startNodeList.stream().map(TemplateNodeEntity::getFormId).collect(Collectors.toList());
- List<VisualdevEntity> formList = serviceUtil.getFormList(formIds);
- List<TemplateTreeListVo> 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<TemplateEntity> 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<TemplateTreeListVo> treeListWithPower() {
- List<TemplateTreeListVo> vos = new ArrayList<>();
- MPJLambdaWrapper<TemplateEntity> wrapper = JoinWrappers.lambda(TemplateEntity.class)
- .selectAll(TemplateEntity.class)
- .eq(TemplateEntity::getVisibleType, FlowNature.Authority)
- .isNotNull(TemplateEntity::getFlowId);
- List<TemplateEntity> list = this.selectJoinList(TemplateEntity.class, wrapper);
- List<String> systemIdList = list.stream().map(TemplateEntity::getSystemId).collect(Collectors.toList());
- List<SystemEntity> 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<TemplateEntity> 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<TemplateEntity> getListAll(TemplatePagination pagination, boolean isPage) {
- // 定义变量判断是否需要使用修改时间倒序
- boolean flag = false;
- QueryWrapper<TemplateEntity> 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<TemplateEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<TemplateEntity> userPage = this.page(page, queryWrapper);
- return pagination.setData(userPage.getRecords(), page.getTotal());
- } else {
- return this.list(queryWrapper);
- }
- }
- @Override
- public List<TemplateEntity> getListByFlowIds(List<String> flowIds) {
- List<TemplateEntity> list = new ArrayList<>();
- List<TemplateJsonEntity> jsonEntityList = templateJsonService.listByIds(flowIds);
- if (CollectionUtil.isNotEmpty(jsonEntityList)) {
- List<String> templateIds = jsonEntityList.stream().map(TemplateJsonEntity::getTemplateId).distinct().collect(Collectors.toList());
- if (CollectionUtil.isNotEmpty(templateIds)) {
- QueryWrapper<TemplateEntity> 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<TemplateEntity> 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<TemplateEntity> 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<TemplateEntity> 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<String, Map<String, Object>> 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<TemplateJsonEntity> list = templateJsonService.getList(entity.getId());
- List<String> flowIds = list.stream().map(TemplateJsonEntity::getId).collect(Collectors.toList());
- if (triggerTaskService.checkByFlowIds(flowIds)) {
- throw new WorkFlowException(MsgCode.WF139.get());
- }
- } else {
- List<TaskEntity> 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<String> 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<TemplateNodeEntity> 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<String, Map<String, Object>> 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<TemplateNodeEntity> 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<String> 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<String, Map<String, Object>> 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<TemplateEntity> getList(List<String> ids) {
- if (ids.isEmpty()) {
- return new ArrayList<>();
- }
- QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().in(TemplateEntity::getId, ids).eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode());
- return this.list(queryWrapper);
- }
- @Override
- public List<TemplateEntity> getListOfHidden(List<String> ids) {
- if (ids.isEmpty()) {
- return new ArrayList<>();
- }
- QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
- List<Integer> 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<String> errList) {
- TemplateEntity entity = JsonUtil.getJsonToBean(templateEntity, TemplateEntity.class);
- boolean skip = Objects.equals("0", type);
- int num = 0;
- QueryWrapper<TemplateEntity> 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<TemplateByFormModel> resList = new ArrayList<>();
- QueryWrapper<TemplateNodeEntity> 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<TemplateNodeEntity> nodeList = templateNodeService.list(wrapper);
- if (CollectionUtil.isEmpty(nodeList)) {
- return res;
- }
- List<String> flowIds = nodeList.stream().map(TemplateNodeEntity::getFlowId).distinct().collect(Collectors.toList());
- if (CollectionUtil.isEmpty(flowIds)) {
- return res;
- }
- List<TemplateJsonEntity> 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<String> templateIds = versionList.stream().map(TemplateJsonEntity::getTemplateId).distinct().collect(Collectors.toList());
- List<TemplateEntity> templateList = CollectionUtil.isNotEmpty(templateIds) ? this.listByIds(templateIds) : new ArrayList<>();
- // 获取权限流程
- String loginUserId = UserProvider.getLoginUserId();
- //是否普通用户
- boolean commonUser = serviceUtil.isCommonUser(loginUserId);
- List<String> 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<UserEntity> 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<TemplateNodeEntity> 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<String> 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<String> userIdAll, String templateId) throws WorkFlowException {
- List<String> userList = new ArrayList<>();
- TemplateEntity template = this.getInfo(templateId);
- List<TemplateNodeEntity> 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<String> 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<String> getFormList() {
- List<String> resList = new ArrayList<>();
- List<TemplateJsonEntity> list = templateJsonService.getListOfEnable();
- if (CollectionUtil.isNotEmpty(list)) {
- List<String> flowIds = list.stream().map(TemplateJsonEntity::getId).distinct().collect(Collectors.toList());
- List<TemplateNodeEntity> startNodeList = templateNodeService.getList(flowIds, NodeEnum.start.getType());
- resList = startNodeList.stream().map(TemplateNodeEntity::getFormId).distinct().collect(Collectors.toList());
- }
- return resList;
- }
- @Override
- public Map<String, String> getFlowFormMap() {
- Map<String, String> map = new HashMap<>();
- List<TemplateNodeEntity> listStart = templateNodeService.getListStart();
- List<String> collect = listStart.stream().map(TemplateNodeEntity::getFlowId).collect(Collectors.toList());
- if (ObjectUtil.isNotEmpty(collect)) {
- QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().select(TemplateEntity::getId, TemplateEntity::getFlowId);
- queryWrapper.lambda().in(TemplateEntity::getFlowId, collect);
- Map<String, String> 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<TemplatePageVo> getCommonList(TemplatePagination pagination) {
- AuthorizeVO authorize = pagination.getAuthorize();
- if (authorize == null) {
- authorize = serviceUtil.getAuthorizeByUser();
- }
- List<String> systemIdList = authorize.getSystemList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).map(SystemBaeModel::getId).collect(Collectors.toList());
- List<String> flowId = authorize.getFlowIdList();
- String userId = UserProvider.getLoginUserId();
- MPJLambdaWrapper<TemplateEntity> 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<TemplatePageVo> 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<TemplateEntity> getListByCreUser(String creUser) {
- QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(TemplateEntity::getVisibleType, FlowNature.Authority);
- queryWrapper.lambda().eq(TemplateEntity::getCreatorUserId, creUser);
- return this.list(queryWrapper);
- }
- }
|