TemplateServiceImpl.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. package jnpf.flowable.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.github.yulichang.toolkit.JoinWrappers;
  8. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  9. import com.google.common.collect.ImmutableList;
  10. import com.google.common.collect.Lists;
  11. import jnpf.base.UserInfo;
  12. import jnpf.base.entity.DictionaryDataEntity;
  13. import jnpf.base.entity.PrintDevEntity;
  14. import jnpf.base.entity.SystemEntity;
  15. import jnpf.base.entity.VisualdevEntity;
  16. import jnpf.base.model.base.SystemBaeModel;
  17. import jnpf.base.service.SuperServiceImpl;
  18. import jnpf.constant.AuthorizeConst;
  19. import jnpf.constant.MsgCode;
  20. import jnpf.exception.WorkFlowException;
  21. import jnpf.flowable.entity.*;
  22. import jnpf.flowable.enums.NodeEnum;
  23. import jnpf.flowable.enums.TemplateStatueEnum;
  24. import jnpf.flowable.mapper.TemplateMapper;
  25. import jnpf.flowable.model.template.*;
  26. import jnpf.flowable.model.templatejson.FlowFormModel;
  27. import jnpf.flowable.model.templatejson.TemplateJsonExportModel;
  28. import jnpf.flowable.model.templatenode.TemplateNodeUpFrom;
  29. import jnpf.flowable.model.util.FlowNature;
  30. import jnpf.flowable.service.*;
  31. import jnpf.flowable.util.ServiceUtil;
  32. import jnpf.flowable.util.TaskUtil;
  33. import jnpf.permission.entity.UserEntity;
  34. import jnpf.permission.model.authorize.AuthorizeVO;
  35. import jnpf.util.JsonUtil;
  36. import jnpf.util.RandomUtil;
  37. import jnpf.util.StringUtil;
  38. import jnpf.util.UserProvider;
  39. import jnpf.util.context.RequestContext;
  40. import org.apache.commons.lang3.StringUtils;
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.stereotype.Service;
  43. import java.util.*;
  44. import java.util.stream.Collectors;
  45. @Service
  46. public class TemplateServiceImpl extends SuperServiceImpl<TemplateMapper, TemplateEntity> implements TemplateService {
  47. @Autowired
  48. private TemplateJsonService templateJsonService;
  49. @Autowired
  50. private TemplateNodeService templateNodeService;
  51. @Autowired
  52. private ServiceUtil serviceUtil;
  53. @Autowired
  54. private DelegateService delegateService;
  55. @Autowired
  56. private CommonService commonService;
  57. @Autowired
  58. private TaskUtil taskUtil;
  59. @Autowired
  60. private TriggerTaskService triggerTaskService;
  61. @Autowired
  62. private TemplateUseNumService templateUseNumService;
  63. @Override
  64. public List<TemplateEntity> getList(TemplatePagination pagination) {
  65. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  66. String keyword = pagination.getKeyword();
  67. if (ObjectUtil.isNotEmpty(keyword)) {
  68. queryWrapper.lambda().and(t -> t.like(TemplateEntity::getEnCode, keyword).or().like(TemplateEntity::getFullName, keyword));
  69. }
  70. String category = pagination.getCategory();
  71. if (ObjectUtil.isNotEmpty(category)) {
  72. queryWrapper.lambda().eq(TemplateEntity::getCategory, category);
  73. }
  74. Integer type = pagination.getType();
  75. if (ObjectUtil.isNotEmpty(type)) {
  76. queryWrapper.lambda().eq(TemplateEntity::getType, type);
  77. }
  78. Integer enabledMark = pagination.getEnabledMark();
  79. if (ObjectUtil.isNotEmpty(enabledMark)) {
  80. queryWrapper.lambda().eq(TemplateEntity::getEnabledMark, enabledMark);
  81. }
  82. String systemId = pagination.getSystemId();
  83. if (ObjectUtil.isNotEmpty(systemId)) {
  84. queryWrapper.lambda().eq(TemplateEntity::getSystemId, systemId);
  85. }
  86. queryWrapper.lambda().orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime);
  87. Page<TemplateEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  88. IPage<TemplateEntity> userPage = this.page(page, queryWrapper);
  89. return pagination.setData(userPage.getRecords(), page.getTotal());
  90. }
  91. @Override
  92. public List<TemplatePageVo> getSelector(TemplatePagination pagination) {
  93. // 流程权限
  94. Set<String> flowId = new HashSet<>();
  95. String userId = UserProvider.getLoginUserId();
  96. if (StringUtil.isNotBlank(pagination.getDelegateUser())) {
  97. userId = pagination.getDelegateUser();
  98. }
  99. boolean isDelegate = ObjectUtil.equals(pagination.getIsDelegate(), 1);
  100. boolean isAuthority = ObjectUtil.equals(pagination.getIsAuthority(), 1);
  101. boolean isLaunch = ObjectUtil.equals(pagination.getIsLaunch(), 1);
  102. boolean isSystemFrom = ObjectUtil.equals(pagination.getIsSystemFrom(), 1);
  103. //委托流程的数据
  104. boolean isEntrust = StringUtils.equals("-1", pagination.getSystemId());
  105. //当前用户
  106. AuthorizeVO authorizeByUser = serviceUtil.getAuthorizeByUser();
  107. // 权限
  108. boolean commonUser = serviceUtil.isCommonUser(userId);
  109. if (commonUser) {
  110. if (isDelegate || isAuthority) {
  111. flowId.addAll(authorizeByUser.getFlowIdList());
  112. }
  113. }
  114. List<String> flowList = new ArrayList<>();
  115. List<DelegateEntity> delegateEntityList = delegateService.getByToUserId(userId, 0);
  116. //获取委托
  117. for (DelegateEntity delegate : delegateEntityList) {
  118. List<String> launchPermission = serviceUtil.getPermission(delegate.getUserId());
  119. if (StringUtil.isNotEmpty(delegate.getFlowId())) {
  120. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  121. queryWrapper.lambda().in(TemplateEntity::getId, Arrays.asList(delegate.getFlowId().split(",")));
  122. List<TemplateEntity> list = this.list(queryWrapper);
  123. //用户拥有权限
  124. List<String> authoirtyList = list.stream().filter(e -> ObjectUtil.equals(e.getVisibleType(), FlowNature.Authority)).map(TemplateEntity::getId).collect(Collectors.toList());
  125. launchPermission.retainAll(authoirtyList);
  126. flowList.addAll(launchPermission);
  127. //发起列表显示,委托列表不显示
  128. if (!isDelegate) {
  129. flowId.addAll(launchPermission);
  130. }
  131. //公开授权
  132. List<String> flowIdList = list.stream().map(TemplateEntity::getId).collect(Collectors.toList());
  133. flowIdList.removeAll(authoirtyList);
  134. flowList.addAll(flowIdList);
  135. } else {
  136. List<String> system = serviceUtil.getPermission(delegate.getUserId(), AuthorizeConst.SYSTEM);
  137. if (system.isEmpty()) {
  138. continue;
  139. }
  140. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  141. queryWrapper.lambda().eq(TemplateEntity::getSystemId, system)
  142. .eq(TemplateEntity::getVisibleType, FlowNature.All);
  143. List<TemplateEntity> list = this.list(queryWrapper);
  144. List<String> flowIdList = list.stream().map(TemplateEntity::getId).collect(Collectors.toList());
  145. flowList.addAll(flowIdList);
  146. }
  147. }
  148. MPJLambdaWrapper<TemplateEntity> wrapper = JoinWrappers.lambda(TemplateEntity.class)
  149. .selectAll(TemplateEntity.class)
  150. .eq(TemplateEntity::getEnabledMark, 1).ne(TemplateEntity::getType, FlowNature.Quest)
  151. .eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode());
  152. //关键字(流程名称、流程编码)
  153. String keyWord = pagination.getKeyword();
  154. if (ObjectUtil.isNotEmpty(keyWord)) {
  155. wrapper.and(t -> t.like(TemplateEntity::getEnCode, keyWord).or().like(TemplateEntity::getFullName, keyWord));
  156. }
  157. //流程显示类型
  158. if (isLaunch) {
  159. List<Integer> typeList = ImmutableList.of(FlowNature.AllShowType, FlowNature.FlowShowType);
  160. wrapper.in(TemplateEntity::getShowType, typeList);
  161. }
  162. List<String> systemIdList = authorizeByUser.getSystemList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).map(SystemBaeModel::getId).collect(Collectors.toList());
  163. if (isEntrust) {
  164. if (flowList.isEmpty()) {
  165. return new ArrayList<>();
  166. }
  167. List<List<String>> lists = Lists.partition(flowList, 1000);
  168. wrapper.and(t -> {
  169. for (List<String> list : lists) {
  170. t.in(TemplateEntity::getId, list).or();
  171. }
  172. });
  173. wrapper.notIn(!systemIdList.isEmpty(), TemplateEntity::getSystemId, systemIdList);
  174. } else {
  175. //应用主建
  176. String systemId = pagination.getSystemId();
  177. if (ObjectUtil.isNotEmpty(systemId)) {
  178. systemIdList.retainAll(Arrays.asList(systemId.split(",")));
  179. }
  180. if (systemIdList.isEmpty()) {
  181. return new ArrayList<>();
  182. }
  183. if (isAuthority) {
  184. if (commonUser) {
  185. wrapper.and(t -> t.eq(TemplateEntity::getVisibleType, FlowNature.All)
  186. .or().in(!flowId.isEmpty(), TemplateEntity::getId, flowId)
  187. );
  188. }
  189. }
  190. wrapper.in(TemplateEntity::getSystemId, systemIdList);
  191. //所属分类
  192. String category = pagination.getCategory();
  193. if (ObjectUtil.isNotEmpty(category)) {
  194. if (StringUtil.equals(category, "commonFlow")) {
  195. wrapper.leftJoin(CommonEntity.class, CommonEntity::getFlowId, TemplateJsonEntity::getId)
  196. .eq(CommonEntity::getCreatorUserId, userId).isNotNull(CommonEntity::getFlowId);
  197. } else {
  198. wrapper.in(TemplateEntity::getCategory, Arrays.asList(category.split(",")));
  199. }
  200. }
  201. }
  202. wrapper.orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime);
  203. Page<TemplatePageVo> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  204. IPage<TemplatePageVo> userPage = this.selectJoinListPage(page, TemplatePageVo.class, wrapper);
  205. List<TemplatePageVo> records = userPage.getRecords();
  206. // 设置常用流程标识
  207. List<CommonEntity> commonList = commonService.getCommonByUserId(userId);
  208. if (CollectionUtil.isNotEmpty(commonList)) {
  209. List<String> flowIds = commonList.stream().map(CommonEntity::getFlowId).distinct().collect(Collectors.toList());
  210. for (TemplatePageVo record : records) {
  211. if (flowIds.contains(record.getId())) {
  212. record.setIsCommonFlow(true);
  213. }
  214. }
  215. }
  216. //判断流程的表单类型
  217. if (isSystemFrom) {
  218. List<String> flowIdList = records.stream().map(TemplatePageVo::getFlowId).collect(Collectors.toList());
  219. List<TemplateNodeEntity> list = templateNodeService.getList(flowIdList, NodeEnum.start.getType());
  220. Map<String, String> formMap = new HashMap<>();
  221. for (TemplateNodeEntity entity : list) {
  222. formMap.put(entity.getFlowId(), entity.getFormId());
  223. }
  224. List<String> formList = serviceUtil.getFormList(new ArrayList<>(formMap.values())).stream().filter(e -> Objects.equals(e.getType(), 2)).map(VisualdevEntity::getId).collect(Collectors.toList());
  225. if (CollectionUtil.isNotEmpty(formList)) {
  226. for (TemplatePageVo record : records) {
  227. String formId = formMap.get(record.getFlowId());
  228. if (formList.contains(formId)) {
  229. record.setIsQuote(1);
  230. }
  231. }
  232. }
  233. }
  234. return pagination.setData(records, page.getTotal());
  235. }
  236. @Override
  237. public List<TemplateTreeListVo> getTreeCommon() {
  238. String userId = UserProvider.getLoginUserId();
  239. List<TemplateTreeListVo> vos = new ArrayList<>();
  240. List<CommonEntity> commonList = commonService.getCommonByUserId(userId);
  241. if (CollectionUtil.isEmpty(commonList)) {
  242. return vos;
  243. }
  244. String systemCodeById = serviceUtil.getSystemCodeById(RequestContext.getAppCode());
  245. List<String> flowIds = commonList.stream().map(CommonEntity::getFlowId).collect(Collectors.toList());
  246. List<TemplateEntity> templateList = this.getList(flowIds);
  247. if (StringUtil.isNotEmpty(systemCodeById)) {
  248. templateList = templateList.stream().filter(e -> Objects.equals(e.getSystemId(), systemCodeById)).collect(Collectors.toList());
  249. }
  250. if (CollectionUtil.isNotEmpty(templateList)) {
  251. TemplateTreeListVo allVo = new TemplateTreeListVo();
  252. allVo.setId(RandomUtil.uuId());
  253. allVo.setFullName("全部流程");
  254. List<TemplateTreeListVo> childrenList = this.getChildren(templateList);
  255. allVo.setChildren(childrenList);
  256. vos.add(allVo);
  257. Map<String, List<TemplateTreeListVo>> map = childrenList.stream().collect(Collectors.groupingBy(TemplateTreeListVo::getCategory));
  258. List<String> categoryIds = new ArrayList<>(map.keySet());
  259. List<DictionaryDataEntity> dictionNameList = serviceUtil.getDictionName(categoryIds);
  260. for (DictionaryDataEntity dict : dictionNameList) {
  261. TemplateTreeListVo vo = new TemplateTreeListVo();
  262. vo.setId(dict.getId());
  263. vo.setFullName(dict.getFullName());
  264. vo.setChildren(map.get(dict.getId()));
  265. vos.add(vo);
  266. }
  267. }
  268. return vos;
  269. }
  270. public List<TemplateTreeListVo> getChildren(List<TemplateEntity> templates) {
  271. List<TemplateTreeListVo> resList = new ArrayList<>();
  272. for (TemplateEntity template : templates) {
  273. TemplateTreeListVo vo = JsonUtil.getJsonToBean(template, TemplateTreeListVo.class);
  274. resList.add(vo);
  275. }
  276. return resList;
  277. }
  278. @Override
  279. public List<TemplateTreeListVo> treeList(Integer formType) {
  280. List<DictionaryDataEntity> dictionList = serviceUtil.getDiList();
  281. TemplatePagination pagination = new TemplatePagination();
  282. pagination.setSystemId(serviceUtil.getSystemCodeById(RequestContext.getAppCode()));
  283. List<TemplateEntity> list = getListAll(pagination, false);
  284. List<TemplateNodeEntity> startNodeList;
  285. // formType 1.系统表单 2.在线开发表单
  286. if (null != formType) {
  287. // 版本主键
  288. List<String> flowIds = list.stream().map(TemplateEntity::getFlowId).collect(Collectors.toList());
  289. startNodeList = templateNodeService.getList(flowIds, NodeEnum.start.getType());
  290. } else {
  291. startNodeList = new ArrayList<>();
  292. }
  293. List<String> formIds = startNodeList.stream().map(TemplateNodeEntity::getFormId).collect(Collectors.toList());
  294. List<VisualdevEntity> formList = serviceUtil.getFormList(formIds);
  295. List<TemplateTreeListVo> vos = new ArrayList<>();
  296. for (DictionaryDataEntity dict : dictionList) {
  297. TemplateTreeListVo vo = new TemplateTreeListVo();
  298. vo.setFullName(dict.getFullName());
  299. vo.setId(dict.getId());
  300. vo.setDisabled(true);
  301. if (CollectionUtil.isNotEmpty(list)) {
  302. List<TemplateEntity> childList = list.stream()
  303. .filter(e -> dict.getId().equals(e.getCategory())).collect(Collectors.toList());
  304. if (null != formType) {
  305. childList = childList.stream().filter(e -> {
  306. // 是流程显示类型
  307. if (ObjectUtil.equals(e.getShowType(), FlowNature.FlowShowType)) {
  308. return false;
  309. }
  310. TemplateNodeEntity node = startNodeList.stream().filter(a -> a.getFlowId().equals(e.getFlowId())).findFirst().orElse(null);
  311. if (null != node) {
  312. VisualdevEntity form = formList.stream().filter(b -> b.getId().equals(node.getFormId())).findFirst().orElse(null);
  313. if (null != form) {
  314. return form.getType().equals(1);
  315. }
  316. }
  317. return false;
  318. }).collect(Collectors.toList());
  319. }
  320. if (CollectionUtil.isNotEmpty(childList)) {
  321. childList = childList.stream()
  322. .sorted(Comparator.comparing(TemplateEntity::getSortCode, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(TemplateEntity::getCreatorTime).reversed())
  323. .collect(Collectors.toList());
  324. vo.setNum(childList.size());
  325. vo.setChildren(JsonUtil.getJsonToList(childList, TemplateTreeListVo.class));
  326. vos.add(vo);
  327. }
  328. }
  329. }
  330. return vos;
  331. }
  332. @Override
  333. public List<TemplateTreeListVo> treeListWithPower() {
  334. List<TemplateTreeListVo> vos = new ArrayList<>();
  335. MPJLambdaWrapper<TemplateEntity> wrapper = JoinWrappers.lambda(TemplateEntity.class)
  336. .selectAll(TemplateEntity.class)
  337. .eq(TemplateEntity::getVisibleType, FlowNature.Authority)
  338. .isNotNull(TemplateEntity::getFlowId);
  339. List<TemplateEntity> list = this.selectJoinList(TemplateEntity.class, wrapper);
  340. List<String> systemIdList = list.stream().map(TemplateEntity::getSystemId).collect(Collectors.toList());
  341. List<SystemEntity> systemList = serviceUtil.getSystemList(systemIdList);
  342. for (SystemEntity system : systemList) {
  343. TemplateTreeListVo vo = new TemplateTreeListVo();
  344. vo.setFullName(system.getFullName());
  345. vo.setId(system.getId());
  346. vo.setDisabled(true);
  347. if (CollectionUtil.isNotEmpty(list)) {
  348. List<TemplateEntity> childList = list.stream()
  349. .filter(e -> system.getId().equals(e.getSystemId()))
  350. .sorted(Comparator.comparing(TemplateEntity::getSortCode, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(TemplateEntity::getCreatorTime, Comparator.reverseOrder()))
  351. .collect(Collectors.toList());
  352. if (CollectionUtil.isNotEmpty(childList)) {
  353. vo.setNum(childList.size());
  354. vo.setChildren(JsonUtil.getJsonToList(childList, TemplateTreeListVo.class));
  355. vos.add(vo);
  356. }
  357. }
  358. }
  359. return vos;
  360. }
  361. @Override
  362. public List<TemplateEntity> getListAll(TemplatePagination pagination, boolean isPage) {
  363. // 定义变量判断是否需要使用修改时间倒序
  364. boolean flag = false;
  365. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  366. queryWrapper.lambda().eq(TemplateEntity::getEnabledMark, 1).eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode());
  367. if (ObjectUtil.isNotEmpty(pagination.getType())) {
  368. flag = true;
  369. queryWrapper.lambda().eq(TemplateEntity::getType, pagination.getType());
  370. }
  371. if (ObjectUtil.isNotEmpty(pagination.getKeyword())) {
  372. flag = true;
  373. queryWrapper.lambda().like(TemplateEntity::getFullName, pagination.getKeyword());
  374. }
  375. if (ObjectUtil.isNotEmpty(pagination.getCategory())) {
  376. flag = true;
  377. queryWrapper.lambda().eq(TemplateEntity::getCategory, pagination.getCategory());
  378. }
  379. if (ObjectUtil.isNotEmpty(pagination.getTemplateIdList())) {
  380. queryWrapper.lambda().in(TemplateEntity::getId, pagination.getTemplateIdList());
  381. }
  382. if (ObjectUtil.isNotEmpty(pagination.getSystemId())) {
  383. queryWrapper.lambda().eq(TemplateEntity::getSystemId, pagination.getSystemId());
  384. }
  385. queryWrapper.lambda().orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime);
  386. if (flag) {
  387. queryWrapper.lambda().orderByDesc(TemplateEntity::getLastModifyTime);
  388. }
  389. queryWrapper.lambda().select(
  390. TemplateEntity::getId, TemplateEntity::getEnCode,
  391. TemplateEntity::getFullName, TemplateEntity::getFlowId,
  392. TemplateEntity::getStatus, TemplateEntity::getShowType,
  393. TemplateEntity::getType, TemplateEntity::getIcon,
  394. TemplateEntity::getCategory, TemplateEntity::getIconBackground,
  395. TemplateEntity::getCreatorUserId, TemplateEntity::getSortCode,
  396. TemplateEntity::getEnabledMark, TemplateEntity::getCreatorTime
  397. );
  398. if (isPage) {
  399. Page<TemplateEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  400. IPage<TemplateEntity> userPage = this.page(page, queryWrapper);
  401. return pagination.setData(userPage.getRecords(), page.getTotal());
  402. } else {
  403. return this.list(queryWrapper);
  404. }
  405. }
  406. @Override
  407. public List<TemplateEntity> getListByFlowIds(List<String> flowIds) {
  408. List<TemplateEntity> list = new ArrayList<>();
  409. List<TemplateJsonEntity> jsonEntityList = templateJsonService.listByIds(flowIds);
  410. if (CollectionUtil.isNotEmpty(jsonEntityList)) {
  411. List<String> templateIds = jsonEntityList.stream().map(TemplateJsonEntity::getTemplateId).distinct().collect(Collectors.toList());
  412. if (CollectionUtil.isNotEmpty(templateIds)) {
  413. QueryWrapper<TemplateEntity> wrapper = new QueryWrapper<>();
  414. wrapper.lambda().in(TemplateEntity::getId, templateIds).eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode());
  415. list = this.list(wrapper);
  416. }
  417. }
  418. return list;
  419. }
  420. @Override
  421. public TemplateEntity getInfo(String id) throws WorkFlowException {
  422. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  423. queryWrapper.lambda().eq(TemplateEntity::getId, id);
  424. TemplateEntity templateEntity = this.getOne(queryWrapper);
  425. if (templateEntity == null) {
  426. throw new WorkFlowException(MsgCode.WF122.get());
  427. }
  428. return templateEntity;
  429. }
  430. @Override
  431. public boolean isExistByFullName(String fullName, String id, String systemId) {
  432. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  433. queryWrapper.lambda().eq(TemplateEntity::getFullName, fullName);
  434. if (!StringUtils.isEmpty(id)) {
  435. queryWrapper.lambda().ne(TemplateEntity::getId, id);
  436. }
  437. if (!StringUtils.isEmpty(systemId)) {
  438. queryWrapper.lambda().eq(TemplateEntity::getSystemId, systemId);
  439. }
  440. return this.count(queryWrapper) > 0;
  441. }
  442. @Override
  443. public boolean isExistByEnCode(String enCode, String id, String systemId) {
  444. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  445. queryWrapper.lambda().eq(TemplateEntity::getEnCode, enCode);
  446. if (!StringUtils.isEmpty(id)) {
  447. queryWrapper.lambda().ne(TemplateEntity::getId, id);
  448. }
  449. if (!StringUtils.isEmpty(systemId)) {
  450. queryWrapper.lambda().eq(TemplateEntity::getSystemId, systemId);
  451. }
  452. return this.count(queryWrapper) > 0;
  453. }
  454. @Override
  455. public void create(TemplateEntity entity, String flowXml, Map<String, Map<String, Object>> flowNodes) throws WorkFlowException {
  456. entity.setSystemId(serviceUtil.getSystemCodeById(RequestContext.getAppCode()));
  457. if (StringUtil.isEmpty(entity.getEnCode())) {
  458. entity.setEnCode(getEnCode(entity));
  459. }
  460. if (isExistByFullName(entity.getFullName(), entity.getId(), entity.getSystemId())) {
  461. throw new WorkFlowException(MsgCode.EXIST001.get());
  462. }
  463. if (isExistByEnCode(entity.getEnCode(), entity.getId(), null)) {
  464. throw new WorkFlowException(MsgCode.EXIST002.get());
  465. }
  466. UserInfo userInfo = UserProvider.getUser();
  467. entity.setId(StringUtil.isNotEmpty(entity.getId()) ? entity.getId() : RandomUtil.uuId());
  468. entity.setCreatorUserId(userInfo.getUserId());
  469. entity.setCreatorTime(new Date());
  470. entity.setFlowId(null);
  471. entity.setEnabledMark(0);
  472. entity.setStatus(TemplateStatueEnum.none.getCode());
  473. entity.setLastModifyUserId(null);
  474. entity.setLastModifyTime(null);
  475. this.setIgnoreLogicDelete().removeById(entity.getId());
  476. this.setIgnoreLogicDelete().saveOrUpdate(entity);
  477. this.clearIgnoreLogicDelete();
  478. TemplateNodeUpFrom from = new TemplateNodeUpFrom();
  479. from.setId(entity.getId());
  480. from.setFlowXml(flowXml);
  481. from.setFlowNodes(flowNodes);
  482. templateJsonService.create(from);
  483. }
  484. @Override
  485. public boolean update(String id, TemplateEntity entity) throws WorkFlowException {
  486. if (StringUtil.isEmpty(entity.getEnCode())) {
  487. entity.setEnCode(getEnCode(entity));
  488. }
  489. if (isExistByFullName(entity.getFullName(), id, entity.getSystemId())) {
  490. throw new WorkFlowException(MsgCode.EXIST001.get());
  491. }
  492. if (isExistByEnCode(entity.getEnCode(), id, entity.getSystemId())) {
  493. throw new WorkFlowException(MsgCode.EXIST002.get());
  494. }
  495. entity.setId(id);
  496. return this.updateById(entity);
  497. }
  498. @Override
  499. public void delete(TemplateEntity entity) throws WorkFlowException {
  500. if (entity != null) {
  501. if (ObjectUtil.equals(entity.getType(), FlowNature.Quest)) {
  502. List<TemplateJsonEntity> list = templateJsonService.getList(entity.getId());
  503. List<String> flowIds = list.stream().map(TemplateJsonEntity::getId).collect(Collectors.toList());
  504. if (triggerTaskService.checkByFlowIds(flowIds)) {
  505. throw new WorkFlowException(MsgCode.WF139.get());
  506. }
  507. } else {
  508. List<TaskEntity> taskList = taskUtil.getTaskByTemplate(entity.getId());
  509. if (CollectionUtil.isNotEmpty(taskList)) {
  510. throw new WorkFlowException(MsgCode.WF124.get());
  511. }
  512. }
  513. commonService.deleteFlow(entity.getId());
  514. templateUseNumService.deleteUseNum(entity.getId(), null);
  515. this.removeById(entity.getId());
  516. List<String> idList = templateJsonService.getList(entity.getId()).stream().map(TemplateJsonEntity::getId).collect(Collectors.toList());
  517. templateJsonService.delete(idList);
  518. }
  519. }
  520. @Override
  521. public void copy(TemplateEntity entity) throws WorkFlowException {
  522. try {
  523. TemplateJsonEntity jsonEntity = StringUtil.isNotEmpty(entity.getFlowId()) ? templateJsonService.getInfo(entity.getFlowId()) : null;
  524. String formXml = jsonEntity != null ? jsonEntity.getFlowXml() : null;
  525. List<TemplateNodeEntity> list = jsonEntity != null ? templateNodeService.getList(jsonEntity.getId()) : new ArrayList<>();
  526. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  527. entity.setFullName(entity.getFullName() + ".副本" + copyNum);
  528. entity.setEnCode(entity.getEnCode() + copyNum);
  529. entity.setId(null);
  530. Map<String, Map<String, Object>> flowNodes = new HashMap<>();
  531. for (TemplateNodeEntity nodeEntity : list) {
  532. flowNodes.put(nodeEntity.getNodeCode(), JsonUtil.stringToMap(nodeEntity.getNodeJson()));
  533. }
  534. this.create(entity, formXml, flowNodes);
  535. } catch (Exception e) {
  536. log.error(e.getMessage());
  537. throw new WorkFlowException(MsgCode.PRI006.get());
  538. }
  539. }
  540. @Override
  541. public TemplateExportModel export(String id) throws WorkFlowException {
  542. TemplateEntity entity = getInfo(id);
  543. TemplateExportModel exportModel = new TemplateExportModel();
  544. exportModel.setTemplate(entity);
  545. // 版本
  546. TemplateJsonEntity jsonEntity = templateJsonService.getInfo(entity.getFlowId());
  547. TemplateJsonExportModel versionModel = JsonUtil.getJsonToBean(jsonEntity, TemplateJsonExportModel.class);
  548. // 节点
  549. List<TemplateNodeEntity> list = templateNodeService.getList(entity.getFlowId());
  550. exportModel.setNodeList(list);
  551. exportModel.setFlowVersion(versionModel);
  552. return exportModel;
  553. }
  554. @Override
  555. public void importData(TemplateExportModel model, String type) throws WorkFlowException {
  556. TemplateEntity entity = model.getTemplate();
  557. TemplateJsonExportModel versionModel = model.getFlowVersion();
  558. String systemId = serviceUtil.getSystemCodeById(RequestContext.getAppCode());
  559. if (null != entity) {
  560. entity.setFlowId(null);
  561. entity.setVersion(null);
  562. entity.setCreatorUserId(UserProvider.getLoginUserId());
  563. entity.setCreatorTime(new Date());
  564. entity.setLastModifyTime(null);
  565. entity.setLastModifyUserId(null);
  566. entity.setEnabledMark(0);
  567. if (!Objects.equals(systemId, entity.getSystemId())) {
  568. entity.setId(RandomUtil.uuId());
  569. entity.setSystemId(systemId);
  570. entity.setEnCode(getEnCode(entity));
  571. }
  572. List<String> errList = new ArrayList<>();
  573. // type: 0.当导入数据不存在,作为新数据导入;数据已存在,不做处理 1.当导入数据已存在,增加相同记录新数据,名称和编码自动增加随机码
  574. TemplateEntity templateEntity = checkImportEntity(entity, type, errList);
  575. if (!errList.isEmpty()) {
  576. StringJoiner joiner = new StringJoiner(";");
  577. joiner.add(String.join("、", errList) + MsgCode.IMP007.get());
  578. if (StringUtil.isNotEmpty(joiner.toString())) {
  579. throw new WorkFlowException(joiner.toString());
  580. }
  581. }
  582. Map<String, Map<String, Object>> flowNodes = new HashMap<>();
  583. for (TemplateNodeEntity nodeEntity : model.getNodeList()) {
  584. flowNodes.put(nodeEntity.getNodeCode(), JsonUtil.stringToMap(nodeEntity.getNodeJson()));
  585. }
  586. this.create(templateEntity, versionModel.getFlowXml(), flowNodes);
  587. }
  588. }
  589. @Override
  590. public List<TemplateEntity> getList(List<String> ids) {
  591. if (ids.isEmpty()) {
  592. return new ArrayList<>();
  593. }
  594. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  595. queryWrapper.lambda().in(TemplateEntity::getId, ids).eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode());
  596. return this.list(queryWrapper);
  597. }
  598. @Override
  599. public List<TemplateEntity> getListOfHidden(List<String> ids) {
  600. if (ids.isEmpty()) {
  601. return new ArrayList<>();
  602. }
  603. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  604. List<Integer> statusList = ImmutableList.of(TemplateStatueEnum.up.getCode(), TemplateStatueEnum.downContinue.getCode());
  605. queryWrapper.lambda().in(TemplateEntity::getId, ids).in(TemplateEntity::getStatus, statusList);
  606. return this.list(queryWrapper);
  607. }
  608. // 校验导入的实体
  609. public TemplateEntity checkImportEntity(TemplateEntity templateEntity, String type, List<String> errList) {
  610. TemplateEntity entity = JsonUtil.getJsonToBean(templateEntity, TemplateEntity.class);
  611. boolean skip = Objects.equals("0", type);
  612. int num = 0;
  613. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  614. queryWrapper.lambda().eq(TemplateEntity::getId, entity.getId());
  615. if (this.count(queryWrapper) > 0) {
  616. num++;
  617. if (skip) {
  618. errList.add("ID");
  619. }
  620. }
  621. if (isExistByEnCode(entity.getEnCode(), null, entity.getSystemId())) {
  622. num++;
  623. if (skip) {
  624. errList.add(MsgCode.IMP009.get());
  625. }
  626. }
  627. if (isExistByFullName(entity.getFullName(), null, entity.getSystemId())) {
  628. num++;
  629. if (skip) {
  630. errList.add(MsgCode.IMP008.get());
  631. }
  632. }
  633. if (num > 0 && !skip) {
  634. String copyNum = UUID.randomUUID().toString().substring(0, 5);
  635. entity.setFullName(entity.getFullName() + ".副本" + copyNum);
  636. entity.setEnCode(entity.getEnCode() + copyNum);
  637. }
  638. entity.setId(RandomUtil.uuId());
  639. return entity;
  640. }
  641. @Override
  642. public FlowByFormModel getFlowByFormId(String formId, Boolean start) {
  643. FlowByFormModel res = new FlowByFormModel();
  644. List<TemplateByFormModel> resList = new ArrayList<>();
  645. QueryWrapper<TemplateNodeEntity> wrapper = new QueryWrapper<>();
  646. wrapper.lambda().eq(TemplateNodeEntity::getFormId, formId);
  647. // start 传true 仅发起节点 false 全部节点
  648. start = null == start || start;
  649. if (start) {
  650. wrapper.lambda().eq(TemplateNodeEntity::getNodeType, NodeEnum.start.getType());
  651. }
  652. List<TemplateNodeEntity> nodeList = templateNodeService.list(wrapper);
  653. if (CollectionUtil.isEmpty(nodeList)) {
  654. return res;
  655. }
  656. List<String> flowIds = nodeList.stream().map(TemplateNodeEntity::getFlowId).distinct().collect(Collectors.toList());
  657. if (CollectionUtil.isEmpty(flowIds)) {
  658. return res;
  659. }
  660. List<TemplateJsonEntity> versionList = templateJsonService.listByIds(flowIds);
  661. if (CollectionUtil.isEmpty(versionList)) {
  662. return res;
  663. }
  664. // 获取启用版本
  665. versionList = versionList.stream().filter(e -> e.getState().equals(1))
  666. .sorted(Comparator.comparing(TemplateJsonEntity::getCreatorTime).reversed()).collect(Collectors.toList());
  667. // 获取流程
  668. List<String> templateIds = versionList.stream().map(TemplateJsonEntity::getTemplateId).distinct().collect(Collectors.toList());
  669. List<TemplateEntity> templateList = CollectionUtil.isNotEmpty(templateIds) ? this.listByIds(templateIds) : new ArrayList<>();
  670. // 获取权限流程
  671. String loginUserId = UserProvider.getLoginUserId();
  672. //是否普通用户
  673. boolean commonUser = serviceUtil.isCommonUser(loginUserId);
  674. List<String> templatePermissionIds = new ArrayList<>();
  675. if (commonUser) {
  676. templatePermissionIds.addAll(serviceUtil.getLaunchPermission());
  677. }
  678. for (TemplateJsonEntity jsonEntity : versionList) {
  679. TemplateByFormModel model = new TemplateByFormModel();
  680. model.setId(jsonEntity.getId());
  681. TemplateEntity template = templateList.stream()
  682. .filter(e -> ObjectUtil.equals(e.getId(), jsonEntity.getTemplateId())).findFirst().orElse(null);
  683. if (null == template || !ObjectUtil.equals(template.getStatus(), TemplateStatueEnum.up.getCode())) {
  684. continue;
  685. }
  686. if (commonUser && ObjectUtil.equals(template.getVisibleType(), FlowNature.Authority)) {
  687. // 带权限
  688. if (templatePermissionIds.contains(jsonEntity.getId())) {
  689. model.setFullName(template.getFullName());
  690. resList.add(model);
  691. }
  692. } else {
  693. model.setFullName(template.getFullName());
  694. resList.add(model);
  695. }
  696. }
  697. res.setList(resList);
  698. res.setIsConfig(true);
  699. return res;
  700. }
  701. @Override
  702. public List<UserEntity> getSubFlowUserList(String flowId, TemplatePagination pagination) throws WorkFlowException {
  703. TemplateEntity template = this.getInfo(flowId);
  704. return serviceUtil.getLaunchUserByTemplateId(template, pagination);
  705. }
  706. @Override
  707. public VisualdevEntity getFormByTemplateId(String templateId) throws WorkFlowException {
  708. TemplateEntity template = this.getInfo(templateId);
  709. List<TemplateNodeEntity> nodeEntityList = templateNodeService.getList(template.getFlowId());
  710. TemplateNodeEntity nodeEntity = nodeEntityList.stream()
  711. .filter(e -> ObjectUtil.equals(e.getNodeType(), NodeEnum.start.getType())).findFirst().orElse(null);
  712. if (null == nodeEntity) {
  713. throw new WorkFlowException(MsgCode.FA001.get());
  714. }
  715. return serviceUtil.getFormInfo(nodeEntity.getFormId());
  716. }
  717. @Override
  718. public FlowFormModel getFormIdAndFlowId(String templateId) throws WorkFlowException {
  719. List<String> userId = ImmutableList.of(UserProvider.getLoginUserId());
  720. FlowFormModel model = getFormIdAndFlowId(userId, templateId);
  721. if (model.getUserId().isEmpty()) {
  722. throw new WorkFlowException(MsgCode.WF029.get());
  723. }
  724. return model;
  725. }
  726. @Override
  727. public FlowFormModel getFormIdAndFlowId(List<String> userIdAll, String templateId) throws WorkFlowException {
  728. List<String> userList = new ArrayList<>();
  729. TemplateEntity template = this.getInfo(templateId);
  730. List<TemplateNodeEntity> nodeEntityList = templateNodeService.getList(template.getFlowId());
  731. // 判断权限
  732. for (String userId : userIdAll) {
  733. userList.add(userId);
  734. if (ObjectUtil.equals(template.getVisibleType(), FlowNature.Authority)) {
  735. boolean commonUser = serviceUtil.isCommonUser(userId);
  736. if (commonUser) {
  737. List<String> flowIds = serviceUtil.getPermission(userId);
  738. if (!flowIds.contains(template.getId())) {
  739. userList.remove(userId);
  740. }
  741. }
  742. }
  743. }
  744. TemplateNodeEntity nodeEntity = nodeEntityList.stream()
  745. .filter(e -> ObjectUtil.equals(e.getNodeType(), NodeEnum.start.getType())).findFirst().orElse(null);
  746. if (null == nodeEntity) {
  747. throw new WorkFlowException(MsgCode.FA001.get());
  748. }
  749. FlowFormModel model = new FlowFormModel();
  750. model.setFormId(nodeEntity.getFormId());
  751. model.setFlowId(template.getFlowId());
  752. model.setUserId(userList);
  753. model.setUserIdAll(userIdAll);
  754. return model;
  755. }
  756. @Override
  757. public List<String> getFormList() {
  758. List<String> resList = new ArrayList<>();
  759. List<TemplateJsonEntity> list = templateJsonService.getListOfEnable();
  760. if (CollectionUtil.isNotEmpty(list)) {
  761. List<String> flowIds = list.stream().map(TemplateJsonEntity::getId).distinct().collect(Collectors.toList());
  762. List<TemplateNodeEntity> startNodeList = templateNodeService.getList(flowIds, NodeEnum.start.getType());
  763. resList = startNodeList.stream().map(TemplateNodeEntity::getFormId).distinct().collect(Collectors.toList());
  764. }
  765. return resList;
  766. }
  767. @Override
  768. public Map<String, String> getFlowFormMap() {
  769. Map<String, String> map = new HashMap<>();
  770. List<TemplateNodeEntity> listStart = templateNodeService.getListStart();
  771. List<String> collect = listStart.stream().map(TemplateNodeEntity::getFlowId).collect(Collectors.toList());
  772. if (ObjectUtil.isNotEmpty(collect)) {
  773. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  774. queryWrapper.lambda().select(TemplateEntity::getId, TemplateEntity::getFlowId);
  775. queryWrapper.lambda().in(TemplateEntity::getFlowId, collect);
  776. Map<String, String> flowTempMap = this.list(queryWrapper).stream().collect(Collectors.toMap(TemplateEntity::getFlowId, TemplateEntity::getId));
  777. for (TemplateNodeEntity templateNodeEntity : listStart) {
  778. if (ObjectUtil.isNotEmpty(flowTempMap.get(templateNodeEntity.getFlowId()))) {
  779. map.put(flowTempMap.get(templateNodeEntity.getFlowId()), templateNodeEntity.getFormId());
  780. }
  781. }
  782. }
  783. return map;
  784. }
  785. @Override
  786. public List<TemplatePageVo> getCommonList(TemplatePagination pagination) {
  787. AuthorizeVO authorize = pagination.getAuthorize();
  788. if (authorize == null) {
  789. authorize = serviceUtil.getAuthorizeByUser();
  790. }
  791. List<String> systemIdList = authorize.getSystemList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).map(SystemBaeModel::getId).collect(Collectors.toList());
  792. List<String> flowId = authorize.getFlowIdList();
  793. String userId = UserProvider.getLoginUserId();
  794. MPJLambdaWrapper<TemplateEntity> wrapper = JoinWrappers.lambda(TemplateEntity.class)
  795. .selectAll(TemplateEntity.class)
  796. .leftJoin(CommonEntity.class, CommonEntity::getFlowId, TemplateEntity::getId)
  797. .eq(TemplateEntity::getEnabledMark, 1).ne(TemplateEntity::getType, FlowNature.Quest)
  798. .eq(TemplateEntity::getStatus, TemplateStatueEnum.up.getCode());
  799. //应用主建
  800. String systemId = pagination.getSystemId();
  801. if (ObjectUtil.isNotEmpty(systemId)) {
  802. systemIdList.retainAll(Arrays.asList(systemId.split(",")));
  803. }
  804. if (systemIdList.isEmpty()) {
  805. return new ArrayList<>();
  806. }
  807. wrapper.and(t -> t.eq(TemplateEntity::getVisibleType, FlowNature.All)
  808. .or().in(!flowId.isEmpty(), TemplateEntity::getId, flowId)
  809. );
  810. wrapper.in(TemplateEntity::getSystemId, systemIdList);
  811. //关键字(流程名称、流程编码)
  812. String keyWord = pagination.getKeyword();
  813. if (ObjectUtil.isNotEmpty(keyWord)) {
  814. wrapper.and(t -> t.like(TemplateEntity::getEnCode, keyWord).or().like(TemplateEntity::getFullName, keyWord));
  815. }
  816. wrapper.eq(CommonEntity::getCreatorUserId, userId);
  817. wrapper.orderByAsc(TemplateEntity::getSortCode).orderByDesc(TemplateEntity::getCreatorTime);
  818. List<TemplatePageVo> list = this.selectJoinList(TemplatePageVo.class, wrapper);
  819. return list;
  820. }
  821. private String getEnCode(TemplateEntity entity) {
  822. String code = serviceUtil.getCode();
  823. boolean existByEnCode = isExistByEnCode(code, entity.getId(), null);
  824. if (existByEnCode) {
  825. return getEnCode(entity);
  826. }
  827. return code;
  828. }
  829. @Override
  830. public List<TemplateEntity> getListByCreUser(String creUser) {
  831. QueryWrapper<TemplateEntity> queryWrapper = new QueryWrapper<>();
  832. queryWrapper.lambda().eq(TemplateEntity::getVisibleType, FlowNature.Authority);
  833. queryWrapper.lambda().eq(TemplateEntity::getCreatorUserId, creUser);
  834. return this.list(queryWrapper);
  835. }
  836. }