DelegateServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package jnpf.flowable.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.bean.copier.CopyOptions;
  4. import cn.hutool.core.collection.CollectionUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.google.common.collect.ImmutableList;
  10. import jnpf.base.Pagination;
  11. import jnpf.base.UserInfo;
  12. import jnpf.base.service.SuperServiceImpl;
  13. import jnpf.base.vo.ListVO;
  14. import jnpf.constant.MsgCode;
  15. import jnpf.exception.WorkFlowException;
  16. import jnpf.flowable.entity.DelegateEntity;
  17. import jnpf.flowable.entity.DelegateInfoEntity;
  18. import jnpf.flowable.entity.TemplateEntity;
  19. import jnpf.flowable.entity.TemplateJsonEntity;
  20. import jnpf.flowable.enums.TemplateStatueEnum;
  21. import jnpf.flowable.mapper.DelegateMapper;
  22. import jnpf.flowable.model.candidates.CandidateUserVo;
  23. import jnpf.flowable.model.delegate.DelegateCrForm;
  24. import jnpf.flowable.model.delegate.DelegateListVO;
  25. import jnpf.flowable.model.delegate.DelegatePagination;
  26. import jnpf.flowable.model.delegate.DelegateUpForm;
  27. import jnpf.flowable.model.message.DelegateModel;
  28. import jnpf.flowable.model.util.FlowConstant;
  29. import jnpf.flowable.model.util.FlowNature;
  30. import jnpf.flowable.service.DelegateInfoService;
  31. import jnpf.flowable.service.DelegateService;
  32. import jnpf.flowable.service.TemplateJsonService;
  33. import jnpf.flowable.service.TemplateService;
  34. import jnpf.flowable.util.MsgUtil;
  35. import jnpf.flowable.util.ServiceUtil;
  36. import jnpf.flowable.util.TaskUtil;
  37. import jnpf.message.model.SentMessageForm;
  38. import jnpf.util.*;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.stereotype.Service;
  41. import java.util.*;
  42. import java.util.stream.Collectors;
  43. /**
  44. * 类的描述
  45. *
  46. * @author JNPF@YinMai Info. Co., Ltd
  47. * @version 5.0.x
  48. * @since 2024/5/13 16:56
  49. */
  50. @Service
  51. public class DelegateServiceImpl extends SuperServiceImpl<DelegateMapper, DelegateEntity> implements DelegateService {
  52. @Autowired
  53. private TemplateService templateService;
  54. @Autowired
  55. private TemplateJsonService templateJsonService;
  56. @Autowired
  57. private MsgUtil msgUtil;
  58. @Autowired
  59. private TaskUtil taskUtil;
  60. @Autowired
  61. private ServiceUtil serviceUtil;
  62. @Autowired
  63. private DelegateInfoService delegateInfoService;
  64. @Override
  65. public List<DelegateListVO> getList(DelegatePagination pagination) {
  66. String userId = UserProvider.getLoginUserId();
  67. String keyword = pagination.getKeyword();
  68. QueryWrapper<DelegateEntity> wrapper = new QueryWrapper<>();
  69. wrapper.lambda().eq(DelegateEntity::getUserId, userId);
  70. QueryWrapper<DelegateInfoEntity> queryWrapper = new QueryWrapper<>();
  71. if (StringUtil.isNotBlank(keyword)) {
  72. queryWrapper.lambda().like(DelegateInfoEntity::getToUserName, keyword);
  73. }
  74. List<DelegateInfoEntity> delegateInfoList = delegateInfoService.list(queryWrapper);
  75. List<String> delegateIds = delegateInfoList.stream().map(DelegateInfoEntity::getDelegateId).distinct().collect(Collectors.toList());
  76. if (CollectionUtil.isNotEmpty(delegateIds)) {
  77. if (StringUtil.isNotBlank(keyword)) {
  78. wrapper.lambda().and(e -> e.in(DelegateEntity::getId, delegateIds).or().like(DelegateEntity::getFlowName, keyword));
  79. } else {
  80. wrapper.lambda().in(DelegateEntity::getId, delegateIds);
  81. }
  82. } else {
  83. if (StringUtil.isNotBlank(keyword)) {
  84. wrapper.lambda().like(DelegateEntity::getFlowName, keyword);
  85. } else {
  86. return new ArrayList<>();
  87. }
  88. }
  89. if (ObjectUtil.equals(pagination.getType(), 1)) {
  90. wrapper.lambda().eq(DelegateEntity::getType, 0);// 委托
  91. } else {
  92. wrapper.lambda().eq(DelegateEntity::getType, 1);// 代理
  93. }
  94. wrapper.lambda().orderByAsc(DelegateEntity::getSortCode).orderByDesc(DelegateEntity::getCreatorTime);
  95. Page<DelegateEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  96. IPage<DelegateEntity> iPage = this.page(page, wrapper);
  97. List<DelegateEntity> dataList = pagination.setData(iPage.getRecords(), page.getTotal());
  98. List<DelegateListVO> voList = new ArrayList<>();
  99. long time = new Date().getTime();
  100. List<String> ids = dataList.stream().map(DelegateEntity::getId).distinct().collect(Collectors.toList());
  101. List<DelegateInfoEntity> list = delegateInfoService.getList(ids);
  102. for (DelegateEntity delegate : dataList) {
  103. DelegateListVO vo = JsonUtil.getJsonToBean(delegate, DelegateListVO.class);
  104. List<DelegateInfoEntity> infoList = list.stream()
  105. .filter(e -> ObjectUtil.equals(e.getDelegateId(), delegate.getId())).collect(Collectors.toList());
  106. List<String> toUserNameList = infoList.stream().map(DelegateInfoEntity::getToUserName).collect(Collectors.toList());
  107. vo.setToUserName(String.join(",", toUserNameList));
  108. long rejectCount = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 2)).count();
  109. long acceptCount = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 1)).count();
  110. if (time >= vo.getEndTime() || rejectCount == infoList.size()) {// 已失效,1、所有人都拒绝;2、到达结束时间或终止委托
  111. vo.setStatus(2);
  112. } else if (time >= vo.getStartTime() && acceptCount > 0) {// 生效中,对方接受且到达开始时间的状态
  113. vo.setStatus(1);
  114. } else {// 未生效,两种场景1:对方已接受但未达到开始时间状态为未生效,2、对方未接受状态为未生效
  115. vo.setStatus(0);
  116. }
  117. if (acceptCount > 0) {
  118. vo.setIsEdit(false);
  119. }
  120. voList.add(vo);
  121. }
  122. return voList;
  123. }
  124. @Override
  125. public DelegateEntity getInfo(String id) {
  126. if (StringUtil.isBlank(id)) {
  127. return null;
  128. }
  129. return this.getById(id);
  130. }
  131. @Override
  132. public void create(DelegateCrForm fo) {
  133. String userId = UserProvider.getLoginUserId();
  134. DelegateEntity entity = JsonUtil.getJsonToBean(fo, DelegateEntity.class);
  135. entity.setId(RandomUtil.uuId());
  136. entity.setSortCode(RandomUtil.parses());
  137. entity.setCreatorUserId(userId);
  138. this.save(entity);
  139. delegateInfoService.create(fo, entity);
  140. }
  141. @Override
  142. public boolean update(DelegateEntity entity, DelegateUpForm fo) {
  143. BeanUtil.copyProperties(fo, entity, CopyOptions.create().setIgnoreNullValue(true));
  144. UserInfo userInfo = UserProvider.getUser();
  145. entity.setLastModifyTime(new Date());
  146. entity.setLastModifyUserId(userInfo.getUserId());
  147. delegateInfoService.update(fo, entity);
  148. return this.updateById(entity);
  149. }
  150. @Override
  151. public boolean updateStop(String id, DelegateEntity entity) {
  152. UserInfo userInfo = UserProvider.getUser();
  153. entity.setId(id);
  154. entity.setLastModifyTime(new Date());
  155. entity.setLastModifyUserId(userInfo.getUserId());
  156. DelegateModel delegate = new DelegateModel();
  157. List<DelegateInfoEntity> infoList = delegateInfoService.getList(entity.getId());
  158. delegate.setToUserIds(infoList.stream().map(DelegateInfoEntity::getToUserId).collect(Collectors.toList()));
  159. delegate.setType(FlowNature.EndMsg);
  160. delegate.setUserInfo(userInfo);
  161. boolean isDelegate = ObjectUtil.equals(entity.getType(), 0);
  162. delegate.setDelegate(isDelegate);
  163. msgUtil.delegateMsg(delegate);
  164. return this.updateById(entity);
  165. }
  166. @Override
  167. public void delete(DelegateEntity entity) {
  168. this.removeById(entity.getId());
  169. }
  170. @Override
  171. public List<String> getToUser(String userId, String flowId) {
  172. Date thisTime = DateUtil.getNowDate();
  173. QueryWrapper<DelegateEntity> queryWrapper = new QueryWrapper<>();
  174. queryWrapper.lambda().eq(DelegateEntity::getType, 1);
  175. queryWrapper.lambda().le(DelegateEntity::getStartTime, thisTime).ge(DelegateEntity::getEndTime, thisTime);
  176. if (StringUtil.isNotEmpty(userId)) {
  177. queryWrapper.lambda().eq(DelegateEntity::getUserId, userId);
  178. }
  179. List<DelegateEntity> list = this.list(queryWrapper);
  180. List<DelegateEntity> listRes = new ArrayList<>();
  181. if (StringUtil.isNotEmpty(flowId)) {
  182. for (DelegateEntity item : list) {
  183. if (StringUtil.isNotEmpty(item.getFlowId())) {
  184. String[] split = item.getFlowId().split(",");
  185. if (Arrays.asList(split).contains(flowId)) {
  186. listRes.add(item);
  187. }
  188. } else {//为空是全部流程
  189. listRes.add(item);
  190. }
  191. }
  192. } else {
  193. listRes = list;
  194. }
  195. List<String> toUser = new ArrayList<>();
  196. if (CollectionUtil.isNotEmpty(listRes)) {
  197. List<String> ids = listRes.stream().map(DelegateEntity::getId).distinct().collect(Collectors.toList());
  198. List<DelegateInfoEntity> infoList = delegateInfoService.getList(ids);
  199. toUser = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 1))
  200. .map(DelegateInfoEntity::getToUserId).collect(Collectors.toList());
  201. }
  202. return toUser;
  203. }
  204. @Override
  205. public List<DelegateEntity> getByToUserId(String toUserId) {
  206. return this.getByToUserId(toUserId, 1);
  207. }
  208. // 根据 被委托人/代理人id 获取列表
  209. @Override
  210. public List<DelegateEntity> getByToUserId(String toUserId, Integer type) {
  211. List<DelegateInfoEntity> infoList = delegateInfoService.getByToUserId(toUserId);
  212. List<String> ids = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 1))
  213. .map(DelegateInfoEntity::getDelegateId).distinct().collect(Collectors.toList());
  214. List<DelegateEntity> list = new ArrayList<>();
  215. if (CollectionUtil.isNotEmpty(ids)) {
  216. Date thisTime = DateUtil.getNowDate();
  217. QueryWrapper<DelegateEntity> queryWrapper = new QueryWrapper<>();
  218. queryWrapper.lambda().in(DelegateEntity::getId, ids).eq(DelegateEntity::getType, type)
  219. .le(DelegateEntity::getStartTime, thisTime).ge(DelegateEntity::getEndTime, thisTime);
  220. list = this.list(queryWrapper);
  221. }
  222. return list;
  223. }
  224. @Override
  225. public ListVO<CandidateUserVo> getUserList(String templateId) throws WorkFlowException {
  226. String userId = UserProvider.getLoginUserId();
  227. TemplateEntity template = templateService.getById(templateId);
  228. if (null == template) {
  229. TemplateJsonEntity jsonEntity = templateJsonService.getById(templateId);
  230. if (null != jsonEntity) {
  231. template = templateService.getById(jsonEntity.getTemplateId());
  232. }
  233. }
  234. if (null == template) {
  235. throw new WorkFlowException(MsgCode.WF122.get());
  236. }
  237. if (!ObjectUtil.equals(template.getStatus(), TemplateStatueEnum.up.getCode())) {
  238. throw new WorkFlowException(MsgCode.WF140.get());
  239. }
  240. List<String> userIds = this.getDelegateUser(userId, template);
  241. Pagination pagination = new Pagination();
  242. pagination.setPageSize(10000);
  243. List<CandidateUserVo> jsonToList = taskUtil.getUserModel(userIds, pagination);
  244. if (jsonToList.isEmpty()) {
  245. throw new WorkFlowException(MsgCode.WF125.get());
  246. }
  247. ListVO<CandidateUserVo> vo = new ListVO<>();
  248. vo.setList(jsonToList);
  249. return vo;
  250. }
  251. public List<String> getDelegateUser(String userId, TemplateEntity template) throws WorkFlowException {
  252. List<String> ids = new ArrayList<>();
  253. List<DelegateEntity> delegateList = this.getByToUserId(userId, 0);
  254. if (CollectionUtil.isNotEmpty(delegateList)) {
  255. int permissionCount = 0;
  256. for (DelegateEntity delegateEntity : delegateList) {
  257. String flowId = delegateEntity.getFlowId();
  258. if (StringUtil.isNotBlank(flowId)) {
  259. if (flowId.contains(template.getId())) {
  260. if (ObjectUtil.equals(template.getVisibleType(), FlowNature.All)) {
  261. ids.add(delegateEntity.getUserId());
  262. continue;
  263. }
  264. List<String> launchPermission = serviceUtil.getPermission(delegateEntity.getUserId());
  265. if (launchPermission.contains(template.getId())) {
  266. ids.add(delegateEntity.getUserId());
  267. } else {
  268. permissionCount++;
  269. }
  270. }
  271. } else {
  272. // 全部流程
  273. if (ObjectUtil.equals(template.getVisibleType(), FlowNature.All)) {
  274. ids.add(delegateEntity.getUserId());
  275. continue;
  276. }
  277. List<String> launchPermission = serviceUtil.getPermission(delegateEntity.getUserId());
  278. if (launchPermission.contains(template.getId())) {
  279. ids.add(delegateEntity.getUserId());
  280. } else {
  281. permissionCount++;
  282. }
  283. }
  284. }
  285. if (CollectionUtil.isEmpty(ids) && delegateList.size() == permissionCount) {
  286. throw new WorkFlowException(MsgCode.WF129.get());
  287. }
  288. }
  289. return ids;
  290. }
  291. /**
  292. * 获取当前用户所有发起委托列表
  293. */
  294. @Override
  295. public List<DelegateEntity> getLaunchDelagateList() {
  296. String userId = UserProvider.getLoginUserId();
  297. Date thisTime = DateUtil.getNowDate();
  298. List<DelegateInfoEntity> infoList = delegateInfoService.getByToUserId(userId);
  299. List<String> ids = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 1))
  300. .map(DelegateInfoEntity::getDelegateId).distinct().collect(Collectors.toList());
  301. if (CollectionUtil.isEmpty(ids)) {
  302. return new ArrayList<>();
  303. }
  304. QueryWrapper<DelegateEntity> queryWrapper = new QueryWrapper<>();
  305. queryWrapper.lambda().in(DelegateEntity::getId, ids)
  306. .le(DelegateEntity::getStartTime, thisTime).ge(DelegateEntity::getEndTime, thisTime)
  307. .eq(DelegateEntity::getType, 0);
  308. return this.list(queryWrapper);
  309. }
  310. @Override
  311. public List<DelegateEntity> selectSameParamAboutDelaget(DelegateCrForm fo) {
  312. List<DelegateInfoEntity> infoList = delegateInfoService.getByToUserIds(fo.getToUserId());
  313. if (CollectionUtil.isEmpty(infoList)) {
  314. return new ArrayList<>();
  315. }
  316. List<String> ids = infoList.stream().map(DelegateInfoEntity::getDelegateId).distinct().collect(Collectors.toList());
  317. QueryWrapper<DelegateEntity> queryWrapper = new QueryWrapper<>();
  318. queryWrapper.lambda().eq(DelegateEntity::getUserId, fo.getUserId()).in(DelegateEntity::getId, ids)
  319. .eq(DelegateEntity::getType, Integer.valueOf(fo.getType()))
  320. .gt(DelegateEntity::getEndTime, new Date());
  321. return this.list(queryWrapper);
  322. }
  323. @Override
  324. public List<DelegateEntity> getList() {
  325. String userId = UserProvider.getLoginUserId();
  326. QueryWrapper<DelegateEntity> queryWrapper = new QueryWrapper<>();
  327. queryWrapper.lambda().and(t ->
  328. t.eq(DelegateEntity::getCreatorUserId, userId).or().eq(DelegateEntity::getUserId, userId));
  329. return this.baseMapper.selectList(queryWrapper);
  330. }
  331. @Override
  332. public void notarize(DelegateInfoEntity delegateInfo) {
  333. delegateInfoService.updateById(delegateInfo);
  334. // 判断全部拒绝,更新时间
  335. DelegateEntity entity = this.getById(delegateInfo.getDelegateId());
  336. List<DelegateInfoEntity> infoList = delegateInfoService.getList(entity.getId());
  337. boolean isAllReject = infoList.stream().allMatch(e -> ObjectUtil.equals(e.getStatus(), 2));
  338. if (isAllReject) {
  339. Date date = new Date();
  340. entity.setStartTime(date);
  341. entity.setEndTime(date);
  342. this.updateById(entity);
  343. }
  344. if (!ObjectUtil.equals(delegateInfo.getStatus(), 0)) {
  345. UserInfo userInfo = UserProvider.getUser();
  346. List<String> toUserIds = ImmutableList.of(entity.getUserId());
  347. SentMessageForm flowMsgModel = new SentMessageForm();
  348. flowMsgModel.setToUserIds(toUserIds);
  349. flowMsgModel.setUserInfo(userInfo);
  350. flowMsgModel.setTemplateId("PZXTLG022");
  351. Map<String, Object> parameterMap = new HashMap<>();
  352. parameterMap.put(FlowConstant.MANDATOR, userInfo.getUserName());
  353. parameterMap.put(FlowConstant.TITLE, ObjectUtil.equals(entity.getType(), 0) ? "委托" : "代理");
  354. parameterMap.put(FlowConstant.ACTION, ObjectUtil.equals(delegateInfo.getStatus(), 1) ? "接受" : "拒绝");
  355. flowMsgModel.setParameterMap(parameterMap);
  356. Integer delegateType = FlowNature.EndMsg.equals(entity.getType()) ? 0 : ObjectUtil.equals(entity.getType(), 0) ? 1 : 3;
  357. Map<String, String> contentMsg = new HashMap<>();
  358. contentMsg.put("type", delegateType + "");
  359. flowMsgModel.setContentMsg(contentMsg);
  360. flowMsgModel.setFlowType(2);
  361. flowMsgModel.setType(2);
  362. List<SentMessageForm> messageListAll = new ArrayList<>();
  363. messageListAll.add(flowMsgModel);
  364. serviceUtil.sendDelegateMsg(messageListAll);
  365. }
  366. }
  367. }