ZhongcheServiceImpl.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.malk.zhongche.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.malk.server.aliwork.YDConf;
  4. import com.malk.server.aliwork.YDParam;
  5. import com.malk.server.common.McR;
  6. import com.malk.server.teambition.TBConf;
  7. import com.malk.service.aliwork.YDClient;
  8. import com.malk.service.dingtalk.DDClient;
  9. import com.malk.service.dingtalk.DDClient_Contacts;
  10. import com.malk.service.teambition.TBClient;
  11. import com.malk.utils.UtilMap;
  12. import com.malk.zhongche.service.ZhongcheService;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.logging.log4j.util.Strings;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.text.SimpleDateFormat;
  18. import java.time.Instant;
  19. import java.time.ZoneId;
  20. import java.time.ZonedDateTime;
  21. import java.time.format.DateTimeFormatter;
  22. import java.util.*;
  23. @Slf4j
  24. @Service
  25. public class ZhongcheServiceImpl implements ZhongcheService {
  26. @Autowired
  27. private TBClient tbClient;
  28. @Autowired
  29. private YDClient ydClient;
  30. @Autowired
  31. private DDClient_Contacts ddClient_contacts;
  32. @Autowired
  33. private DDClient ddClient;
  34. @Autowired
  35. private TBConf tbConf;
  36. @Override
  37. public McR getTaskInfo(String taskId,String projectId) {
  38. Map taskInfo = tbClient.queryTaskDetail(taskId,null,null).get(0);
  39. log.info("taskInfo:{}", JSONObject.toJSONString(taskInfo));
  40. List<Map> customfields = UtilMap.getList(taskInfo, "customfields");
  41. List<String> wtls = new ArrayList<>();//问题类型
  42. for (Map customfield : customfields) {
  43. String cfId = UtilMap.getString(customfield, "cfId");
  44. switch (cfId){
  45. case "6941202e92136abe2d9de68f"://问题类型
  46. List<Map> value = UtilMap.getList(customfield, "value");
  47. for (Map map : value) {
  48. wtls.add(UtilMap.getString(map, "title"));
  49. }
  50. break;
  51. case "6969da08b9d8a458d706212d"://完成状态
  52. break;
  53. case "6969dc054cd1174e86aa213a"://延期次数
  54. break;
  55. }
  56. }
  57. String executorId = UtilMap.getString(taskInfo, "executorId");//执行者id
  58. Map ddUser = new HashMap();
  59. if (Strings.isNotBlank(executorId)){
  60. Map ddUserInfo = tbClient.idMapQuery(executorId, true).get(0);
  61. Map extra = UtilMap.getMap(ddUserInfo, "extra");
  62. String ddUserId = UtilMap.getString(extra, "userId");//执行人钉钉userid
  63. Map userInfoById = ddClient_contacts.getUserInfoById(ddClient.getAccessToken(),ddUserId);
  64. String ddUserName = UtilMap.getString(userInfoById, "name");//执行人钉钉姓名
  65. ddUser.put("label",ddUserName);
  66. ddUser.put("value",ddUserId);
  67. ddUser.put("emplId",ddUserId);
  68. }
  69. Long startDate = extracted(UtilMap.getString(taskInfo, "startDate"));//任务开始时间
  70. Long dueDate = extracted(UtilMap.getString(taskInfo, "dueDate"));//任务截止时间
  71. String tfsId = UtilMap.getString(taskInfo, "tfsId");//任务状态id
  72. Map body = new HashMap();
  73. body.put("tfsIds", tfsId);
  74. Map tfsInfo = tbClient.queryProjectCustomFlowStatus(projectId, body).get(0);
  75. String tfsName = UtilMap.getString(tfsInfo, "name");//任务状态
  76. String parentTaskId = UtilMap.getString(taskInfo, "parentTaskId");
  77. String parentTaskName = "";
  78. String rootTaskName = "";
  79. if (Strings.isNotBlank(parentTaskId)){
  80. //获取tb任务上级任务
  81. Map parentTaskInfo = tbClient.queryTaskDetail(parentTaskId, null, null).get(0);
  82. parentTaskName = UtilMap.getString(parentTaskInfo, "content");
  83. //获取tb顶级任务
  84. Map rootTaskInfo = getRootTask(parentTaskId);
  85. rootTaskName = UtilMap.getString(rootTaskInfo, "content");
  86. }
  87. Map result = new HashMap();
  88. result.put("wtls", wtls);
  89. result.put("ddUser", ddUser);
  90. result.put("startDate", startDate);
  91. result.put("dueDate", dueDate);
  92. result.put("tfsName", tfsName);
  93. result.put("parentTaskName", parentTaskName);
  94. result.put("rootTaskName", rootTaskName);
  95. return McR.success(result);
  96. }
  97. @Override
  98. public McR updateTaskDelayInfo(Map map) {
  99. String formInstId = UtilMap.getString(map, "formInstId");
  100. Map formData = ydClient.queryData(YDParam.builder()
  101. .formInstanceId(formInstId)
  102. .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
  103. String projectId = UtilMap.getString(formData, "textField_mkkrgg8u");//项目id
  104. String taskId = UtilMap.getString(formData, "textField_mkkrgg8y");//任务id
  105. long dueDate = UtilMap.getLong(formData, "dateField_mn757w7y");//预计完成时间
  106. List<String> questionTypes = UtilMap.getList(formData, "multiSelectField_mn757w7v");//任务类型
  107. Map taskInfo = tbClient.queryTaskDetail(taskId, null, null).get(0);
  108. List<Map> customfields = UtilMap.getList(taskInfo, "customfields");
  109. int delayNum = 0;//延期次数
  110. for (Map customfield : customfields) {
  111. String cfId = UtilMap.getString(customfield, "cfId");
  112. switch (cfId){
  113. case "6941202e92136abe2d9de68f"://问题类型
  114. break;
  115. case "6969da08b9d8a458d706212d"://完成状态
  116. break;
  117. case "6969dc054cd1174e86aa213a"://延期次数
  118. List<Map> values = UtilMap.getList(customfield, "value");
  119. if (Objects.nonNull(values) && !values.isEmpty()){
  120. Map value = values.get(0);
  121. delayNum = Integer.parseInt(UtilMap.getString(value, "title"));
  122. break;
  123. }
  124. }
  125. }
  126. //更新任务截止时间
  127. // 1. 转换为 Instant(UTC 时间点)
  128. Instant instant = Instant.ofEpochMilli(dueDate);
  129. // 2.1 转换为 UTC 时间(比上海时间早 8 小时)
  130. ZonedDateTime utcTime = instant.atZone(ZoneId.of("UTC"));
  131. DateTimeFormatter utcFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
  132. String utcDateTime = utcTime.format(utcFormatter);
  133. System.out.println("UTC date-time (比上海早8小时): " + utcDateTime); // 输出: 2026-03-20T01:30:00Z
  134. tbClient.updateTaskDueDate(taskId, utcDateTime, tbConf.getOperatorId());
  135. //更新任务延期次数
  136. Map body = new HashMap();
  137. body.put("customfieldId", "6969dc054cd1174e86aa213a");
  138. Map value = new HashMap();
  139. value.put("title",delayNum + 1 + "");
  140. body.put("value",Arrays.asList(value));
  141. Map map1 = tbClient.updateTaskCustomField(taskId, tbConf.getOperatorId(), body);
  142. log.info("map1:{}",JSONObject.toJSONString(map1));
  143. //更新任务问题类型
  144. Map body2 = new HashMap();
  145. body2.put("customfieldId", "6941202e92136abe2d9de68f");
  146. List<Map> value2 = new ArrayList<>();
  147. for (String questionType : questionTypes) {
  148. Map map3 = new HashMap();
  149. map3.put("title", questionType);
  150. value2.add(map3);
  151. }
  152. body2.put("value",value2);
  153. Map map2 = tbClient.updateTaskCustomField(taskId, tbConf.getOperatorId(), body2);
  154. log.info("map1:{}",JSONObject.toJSONString(map1));
  155. return McR.success();
  156. }
  157. private Map getRootTask(String taskId) {
  158. Map taskInfo = tbClient.queryTaskDetail(taskId, null, null).get(0);
  159. String parentTaskId = UtilMap.getString(taskInfo, "parentTaskId");
  160. if (Strings.isNotBlank(parentTaskId)){
  161. return getRootTask(parentTaskId);
  162. }else {
  163. return taskInfo;
  164. }
  165. }
  166. //转换时区并返回时间戳
  167. private static Long extracted(String dateStr) {
  168. if (Strings.isNotBlank(dateStr)){
  169. // 1. 解析为 Instant(UTC 时间)
  170. Instant instant = Instant.parse(dateStr);
  171. // 2. 转换为目标时区(Asia/Shanghai, UTC+8)
  172. ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai");
  173. ZonedDateTime shanghaiTime = instant.atZone(shanghaiZone);
  174. // 3. 转换为时间戳(毫秒)
  175. long timestamp = shanghaiTime.toInstant().toEpochMilli();
  176. // 输出结果
  177. System.out.println("UTC 时间: " + instant);
  178. System.out.println("上海时间: " + shanghaiTime);
  179. System.out.println("时间戳(毫秒): " + timestamp);
  180. return timestamp;
  181. }else {
  182. return null;
  183. }
  184. }
  185. }