DelegateController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package jnpf.flowable.controller;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.google.common.collect.ImmutableList;
  5. import io.swagger.v3.oas.annotations.Operation;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import jakarta.validation.Valid;
  8. import jnpf.base.ActionResult;
  9. import jnpf.base.UserInfo;
  10. import jnpf.base.controller.SuperController;
  11. import jnpf.base.vo.ListVO;
  12. import jnpf.base.vo.PaginationVO;
  13. import jnpf.constant.MsgCode;
  14. import jnpf.exception.WorkFlowException;
  15. import jnpf.flowable.entity.DelegateEntity;
  16. import jnpf.flowable.entity.DelegateInfoEntity;
  17. import jnpf.flowable.model.candidates.CandidateUserVo;
  18. import jnpf.flowable.model.delegate.*;
  19. import jnpf.flowable.service.DelegateInfoService;
  20. import jnpf.flowable.service.DelegateService;
  21. import jnpf.flowable.util.ServiceUtil;
  22. import jnpf.util.JsonUtil;
  23. import jnpf.util.JsonUtilEx;
  24. import jnpf.util.StringUtil;
  25. import jnpf.util.UserProvider;
  26. import org.springframework.beans.BeanUtils;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.web.bind.annotation.*;
  29. import java.util.*;
  30. import java.util.stream.Collectors;
  31. /**
  32. * 类的描述
  33. *
  34. * @author JNPF@YinMai Info. Co., Ltd
  35. * @version 5.0.x
  36. * @since 2024/5/13 17:27
  37. */
  38. @Tag(name = "流程委托", description = "DelegateController")
  39. @RestController
  40. @RequestMapping("/api/workflow/delegate")
  41. public class DelegateController extends SuperController<DelegateService, DelegateEntity> {
  42. @Autowired
  43. private DelegateService delegateService;
  44. @Autowired
  45. private ServiceUtil serviceUtil;
  46. @Autowired
  47. private DelegateInfoService delegateInfoService;
  48. /**
  49. * 获取流程委托列表
  50. *
  51. * @param pagination 分页参数
  52. */
  53. @Operation(summary = "获取流程委托列表")
  54. @GetMapping
  55. public ActionResult list(DelegatePagination pagination) {
  56. List<DelegateListVO> voList;
  57. if (ObjectUtil.equals(pagination.getType(), 2) || ObjectUtil.equals(pagination.getType(), 4)) {
  58. voList = delegateInfoService.getList(pagination);
  59. } else {
  60. voList = delegateService.getList(pagination);
  61. }
  62. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  63. return ActionResult.page(voList, paginationVO);
  64. }
  65. /**
  66. * 委托信息列表
  67. *
  68. * @param delegateId 委托主键
  69. */
  70. @Operation(summary = "委托信息列表")
  71. @GetMapping("/Info/{delegateId}")
  72. public ActionResult getDelegateInfo(@PathVariable("delegateId") String delegateId) {
  73. List<DelegateInfoEntity> list = delegateInfoService.getList(delegateId);
  74. return ActionResult.success(JsonUtil.getJsonToList(list, DelegateListVO.class));
  75. }
  76. /**
  77. * 获取流程委托信息
  78. *
  79. * @param id 主键
  80. */
  81. @Operation(summary = "获取流程委托信息")
  82. @GetMapping("/{id}")
  83. public ActionResult info(@PathVariable("id") String id) throws WorkFlowException {
  84. DelegateEntity entity = delegateService.getInfo(id);
  85. if (null == entity) {
  86. throw new WorkFlowException(MsgCode.FA001.get());
  87. }
  88. DelegateInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, DelegateInfoVO.class);
  89. List<DelegateInfoEntity> infoList = delegateInfoService.getList(entity.getId());
  90. if (CollectionUtil.isNotEmpty(infoList)) {
  91. List<String> toUserNameList = infoList.stream().map(DelegateInfoEntity::getToUserName).collect(Collectors.toList());
  92. vo.setToUserName(String.join(",", toUserNameList));
  93. List<String> toUserIdList = infoList.stream().map(DelegateInfoEntity::getToUserId).collect(Collectors.toList());
  94. vo.setToUserId(toUserIdList);
  95. }
  96. return ActionResult.success(vo);
  97. }
  98. /**
  99. * 新建流程委托
  100. *
  101. * @param fo 参数
  102. */
  103. @Operation(summary = "新建流程委托")
  104. @PostMapping
  105. public ActionResult create(@RequestBody @Valid DelegateCrForm fo) {
  106. // 超管和管理员不能新增委托/代理(即:只有普通用户身份才能新建委托/代理,提示:“管理员不能新建委托/代理”)
  107. if (!serviceUtil.isCommonUser(UserProvider.getLoginUserId())) {
  108. return ActionResult.fail(MsgCode.WF130.get());
  109. }
  110. if (StringUtil.isBlank(fo.getUserId())) {
  111. UserInfo userInfo = UserProvider.getUser();
  112. fo.setUserId(userInfo.getUserId());
  113. fo.setUserName(userInfo.getUserName() + "/" + userInfo.getUserAccount());
  114. }
  115. boolean isDelegate = ObjectUtil.equals(fo.getType(), "0");
  116. if (fo.getToUserId().contains(fo.getUserId())) {
  117. return ActionResult.fail(isDelegate ? MsgCode.WF017.get() : MsgCode.WF137.get());
  118. }
  119. // 受托人/代理人不能选择admin和本人
  120. List<String> toUserList = fo.getToUserId();
  121. String admin = serviceUtil.getAdmin();
  122. for (String toUser : toUserList) {
  123. if (ObjectUtil.equals(toUser, admin)) {
  124. return ActionResult.fail(MsgCode.WF131.get());
  125. }
  126. }
  127. if (this.alreadyDelegate(fo, null)) {
  128. return ActionResult.fail(isDelegate ? MsgCode.WF018.get() : MsgCode.WF144.get());
  129. }
  130. DelegateCrForm reverse = new DelegateCrForm();
  131. BeanUtils.copyProperties(fo, reverse);
  132. reverse.setUserIdList(fo.getToUserId());
  133. reverse.setToUserId(ImmutableList.of(fo.getUserId()));
  134. if (this.alreadyDelegate(reverse, null)) {
  135. return ActionResult.fail(isDelegate ? MsgCode.WF019.get() : MsgCode.WF145.get());
  136. }
  137. delegateService.create(fo);
  138. return ActionResult.success(MsgCode.SU001.get());
  139. }
  140. /**
  141. * 更新流程委托
  142. *
  143. * @param id 主键
  144. * @param fo 参数
  145. */
  146. @Operation(summary = "更新流程委托")
  147. @PutMapping("/{id}")
  148. public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid DelegateUpForm fo) throws WorkFlowException {
  149. DelegateEntity entity = delegateService.getInfo(id);
  150. if (null == entity) {
  151. throw new WorkFlowException(MsgCode.FA001.get());
  152. }
  153. fo.setUserId(entity.getUserId());
  154. boolean isDelegate = ObjectUtil.equals(fo.getType(), "0");
  155. if (fo.getToUserId().contains(fo.getUserId())) {
  156. return ActionResult.fail(isDelegate ? MsgCode.WF017.get() : MsgCode.WF137.get());
  157. }
  158. if (this.alreadyDelegate(fo, id)) {
  159. return ActionResult.fail(isDelegate ? MsgCode.WF018.get() : MsgCode.WF144.get());
  160. }
  161. // 判断是否有人接受
  162. List<DelegateInfoEntity> infoList = delegateInfoService.getList(id);
  163. if (CollectionUtil.isNotEmpty(infoList)) {
  164. DelegateInfoEntity delegateInfoEntity = infoList.stream().filter(e -> ObjectUtil.equals(e.getStatus(), 1)).findFirst().orElse(null);
  165. if (null != delegateInfoEntity) {
  166. return ActionResult.fail(MsgCode.WF132.get());
  167. }
  168. }
  169. DelegateCrForm reverse = new DelegateCrForm();
  170. BeanUtils.copyProperties(fo, reverse);
  171. reverse.setUserIdList(fo.getToUserId());
  172. reverse.setToUserId(ImmutableList.of(fo.getUserId()));
  173. if (this.alreadyDelegate(reverse, id)) {
  174. return ActionResult.fail(isDelegate ? MsgCode.WF019.get() : MsgCode.WF145.get());
  175. }
  176. if (delegateService.update(entity, fo)) {
  177. return ActionResult.success(MsgCode.SU004.get());
  178. }
  179. return ActionResult.success(MsgCode.FA002.get());
  180. }
  181. // 判断是否已有委托
  182. private boolean alreadyDelegate(DelegateCrForm form, String id) {
  183. List<DelegateEntity> delegateEntities = new ArrayList<>();
  184. if (CollectionUtil.isNotEmpty(form.getUserIdList())) {
  185. for (String userId : form.getUserIdList()) {
  186. form.setUserId(userId);
  187. List<DelegateEntity> list = delegateService.selectSameParamAboutDelaget(form);
  188. delegateEntities.addAll(list);
  189. }
  190. } else {
  191. delegateEntities = delegateService.selectSameParamAboutDelaget(form);
  192. }
  193. for (DelegateEntity delegate : delegateEntities) {
  194. if (delegate.getId().equals(id)) {
  195. continue;
  196. }
  197. //时间交叉
  198. if ((form.getStartTime() <= delegate.getStartTime().getTime() && form.getEndTime() >= delegate.getStartTime().getTime()) ||
  199. (form.getStartTime() >= delegate.getStartTime().getTime() && form.getStartTime() <= delegate.getEndTime().getTime())) {
  200. if (StringUtil.isEmpty(form.getFlowId())) {
  201. return true;
  202. } else {
  203. if (StringUtil.isEmpty(delegate.getFlowId())) {
  204. return true;
  205. } else {
  206. List<String> split = Arrays.asList(delegate.getFlowId().split(","));
  207. List<String> split1 = Arrays.asList(form.getFlowId().split(","));
  208. for (String srt : split) {
  209. if (split1.contains(srt)) {
  210. return true;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. return false;
  218. }
  219. /**
  220. * 结束委托
  221. *
  222. * @param id 主键
  223. */
  224. @Operation(summary = "结束委托")
  225. @PutMapping("/Stop/{id}")
  226. public ActionResult stop(@PathVariable("id") String id) {
  227. Calendar calendar = Calendar.getInstance();
  228. calendar.setTime(new Date());
  229. calendar.add(Calendar.SECOND, -1);
  230. Date date = calendar.getTime();
  231. DelegateEntity entity = delegateService.getInfo(id);
  232. if (null != entity) {
  233. entity.setStartTime(date);
  234. entity.setEndTime(date);
  235. delegateService.updateStop(id, entity);
  236. return ActionResult.success(MsgCode.SU008.get());
  237. }
  238. return ActionResult.fail(MsgCode.FA002.get());
  239. }
  240. /**
  241. * 删除流程委托
  242. *
  243. * @param id 主键
  244. */
  245. @Operation(summary = "删除流程委托")
  246. @DeleteMapping("/{id}")
  247. public ActionResult delete(@PathVariable("id") String id) {
  248. DelegateEntity entity = delegateService.getInfo(id);
  249. if (null != entity) {
  250. delegateService.delete(entity);
  251. delegateInfoService.delete(entity.getId());
  252. return ActionResult.success(MsgCode.SU003.get());
  253. }
  254. return ActionResult.fail(MsgCode.FA003.get());
  255. }
  256. /**
  257. * 获取委托人
  258. * 根据被委托人查询可发起的流程列表
  259. */
  260. @Operation(summary = "获取委托人")
  261. @GetMapping("/UserList")
  262. public ActionResult getUserListByFlowId(@RequestParam("templateId") String templateId) throws WorkFlowException {
  263. ListVO<CandidateUserVo> userList = delegateService.getUserList(templateId);
  264. return ActionResult.success(userList);
  265. }
  266. /**
  267. * 确认
  268. *
  269. * @param id 委托信息主键
  270. * @param type 类型,1.接受 2.拒绝
  271. */
  272. @Operation(summary = "确认")
  273. @PostMapping("/Notarize/{id}")
  274. public ActionResult accept(@PathVariable("id") String id, @RequestParam("type") Integer type) throws WorkFlowException {
  275. DelegateInfoEntity delegateInfo = delegateInfoService.getById(id == null ? "" : id);
  276. if (null == delegateInfo) {
  277. throw new WorkFlowException(MsgCode.FA001.get());
  278. }
  279. delegateInfo.setStatus(type);
  280. delegateService.notarize(delegateInfo);
  281. return ActionResult.success(MsgCode.SU004.get());
  282. }
  283. }