McTbEventImpl.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.malk.tuosi.event;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.malk.delegate.TBEvent;
  4. import com.malk.server.common.McException;
  5. import com.malk.service.teambition.TBClient;
  6. import com.malk.utils.UtilMap;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.context.annotation.Primary;
  10. import org.springframework.scheduling.annotation.Async;
  11. import org.springframework.stereotype.Service;
  12. import java.util.*;
  13. @Primary
  14. @Service
  15. @Slf4j
  16. public class McTbEventImpl implements TBEvent {
  17. @Autowired
  18. private TBClient tbClient;
  19. // @Async
  20. @Override
  21. public void callBackTask(JSONObject eventJson) {
  22. String event = eventJson.getString("event");
  23. if(event.equals("v3.task.node.status.update")){
  24. JSONObject data = eventJson.getJSONObject("data");
  25. String taskId = data.getString("taskId");
  26. String status = data.getString("status");
  27. String nodeId = data.getString("nodeId");
  28. String creatorId = data.getString("creatorId");
  29. // Map task = tbClient.queryTaskDetail(taskId,null,null).get(0);
  30. // if(!"6970a3ff4f887b90bfdbe7e3".equals(String.valueOf(task.get("projectId")))){
  31. // return;
  32. // }
  33. // if(!Arrays.asList("622ee3450cf3bb5e1a486f1f","61a9e8a36355609fb6383d40").contains(creatorId)){
  34. // return;
  35. // }
  36. if(status.equals("finish")){
  37. List<Map> list=tbClient.queryNodeList(taskId);
  38. Map node = _getNodeDetail(list,nodeId);
  39. if(String.valueOf(node.get("name")).contains("接单")){
  40. List<String> assigneeIds = UtilMap.getList(node,"assigneeIds");
  41. String startDate = UtilMap.getString(node,"startDate");
  42. String dueDate = UtilMap.getString(node,"dueDate");
  43. Map nextMap=_getNextNode(list,nodeId);
  44. if(nextMap!=null){
  45. String nextId = UtilMap.getString(nextMap,"id");
  46. tbClient.updateNodeById(taskId,nextId,assigneeIds,startDate,dueDate,creatorId);
  47. }
  48. }
  49. }else if(status.equals("begin")){
  50. List<Map> list=tbClient.queryNodeList(taskId);
  51. // Map node = _getNodeDetail(list,nodeId);
  52. // if(String.valueOf(node.get("name")).contains("接单")){
  53. nextNode(taskId,list,nodeId,creatorId);
  54. // }
  55. }
  56. }
  57. }
  58. private void nextNode(String taskId,List<Map> list,String nodeId,String creatorId){
  59. Map nextMap=_getNextNode(list,nodeId);
  60. if(nextMap!=null){
  61. String nextId = UtilMap.getString(nextMap,"id");
  62. tbClient.updateNodeById(taskId,nextId,new ArrayList<>(),null,null,creatorId);
  63. nextNode(taskId,list,nextId,creatorId);
  64. }
  65. }
  66. @Override
  67. public void callBackProject(JSONObject eventJson) {
  68. }
  69. @Async
  70. @Override
  71. public void callBackWorktime(JSONObject eventJson) {
  72. }
  73. private Map _getNodeDetail(List<Map> list, String nodeId) {
  74. Optional optional = list.stream().filter(item -> nodeId.equals(item.get("id"))).findAny();
  75. McException.assertAccessException(!optional.isPresent(), nodeId + ": 节点不存在");
  76. return (Map) optional.get();
  77. }
  78. private Map _getNextNode(List<Map> list, String nodeId) {
  79. for(Map map : list) {
  80. List<String> prevIds = UtilMap.getList(map,"prevIds");
  81. if(prevIds.contains(nodeId)){
  82. return map;
  83. }
  84. }
  85. return null;
  86. }
  87. }