package com.malk.zhongche.service.impl; import com.alibaba.fastjson.JSONObject; import com.malk.server.aliwork.YDConf; import com.malk.server.aliwork.YDParam; import com.malk.server.common.McR; import com.malk.server.teambition.TBConf; import com.malk.service.aliwork.YDClient; import com.malk.service.dingtalk.DDClient; import com.malk.service.dingtalk.DDClient_Contacts; import com.malk.service.teambition.TBClient; import com.malk.utils.UtilMap; import com.malk.zhongche.service.ZhongcheService; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.util.Strings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.*; @Slf4j @Service public class ZhongcheServiceImpl implements ZhongcheService { @Autowired private TBClient tbClient; @Autowired private YDClient ydClient; @Autowired private DDClient_Contacts ddClient_contacts; @Autowired private DDClient ddClient; @Autowired private TBConf tbConf; @Override public McR getTaskInfo(String taskId,String projectId) { Map taskInfo = tbClient.queryTaskDetail(taskId,null,null).get(0); log.info("taskInfo:{}", JSONObject.toJSONString(taskInfo)); List customfields = UtilMap.getList(taskInfo, "customfields"); List wtls = new ArrayList<>();//问题类型 for (Map customfield : customfields) { String cfId = UtilMap.getString(customfield, "cfId"); switch (cfId){ case "6941202e92136abe2d9de68f"://问题类型 List value = UtilMap.getList(customfield, "value"); for (Map map : value) { wtls.add(UtilMap.getString(map, "title")); } break; case "6969da08b9d8a458d706212d"://完成状态 break; case "6969dc054cd1174e86aa213a"://延期次数 break; } } String executorId = UtilMap.getString(taskInfo, "executorId");//执行者id Map ddUser = new HashMap(); if (Strings.isNotBlank(executorId)){ Map ddUserInfo = tbClient.idMapQuery(executorId, true).get(0); Map extra = UtilMap.getMap(ddUserInfo, "extra"); String ddUserId = UtilMap.getString(extra, "userId");//执行人钉钉userid Map userInfoById = ddClient_contacts.getUserInfoById(ddClient.getAccessToken(),ddUserId); String ddUserName = UtilMap.getString(userInfoById, "name");//执行人钉钉姓名 ddUser.put("label",ddUserName); ddUser.put("value",ddUserId); ddUser.put("emplId",ddUserId); } Long startDate = extracted(UtilMap.getString(taskInfo, "startDate"));//任务开始时间 Long dueDate = extracted(UtilMap.getString(taskInfo, "dueDate"));//任务截止时间 String tfsId = UtilMap.getString(taskInfo, "tfsId");//任务状态id Map body = new HashMap(); body.put("tfsIds", tfsId); Map tfsInfo = tbClient.queryProjectCustomFlowStatus(projectId, body).get(0); String tfsName = UtilMap.getString(tfsInfo, "name");//任务状态 String parentTaskId = UtilMap.getString(taskInfo, "parentTaskId"); String parentTaskName = ""; String rootTaskName = ""; if (Strings.isNotBlank(parentTaskId)){ //获取tb任务上级任务 Map parentTaskInfo = tbClient.queryTaskDetail(parentTaskId, null, null).get(0); parentTaskName = UtilMap.getString(parentTaskInfo, "content"); //获取tb顶级任务 Map rootTaskInfo = getRootTask(parentTaskId); rootTaskName = UtilMap.getString(rootTaskInfo, "content"); } Map result = new HashMap(); result.put("wtls", wtls); result.put("ddUser", ddUser); result.put("startDate", startDate); result.put("dueDate", dueDate); result.put("tfsName", tfsName); result.put("parentTaskName", parentTaskName); result.put("rootTaskName", rootTaskName); return McR.success(result); } @Override public McR updateTaskDelayInfo(Map map) { String formInstId = UtilMap.getString(map, "formInstId"); Map formData = ydClient.queryData(YDParam.builder() .formInstanceId(formInstId) .build(), YDConf.FORM_QUERY.retrieve_id).getFormData(); String projectId = UtilMap.getString(formData, "textField_mkkrgg8u");//项目id String taskId = UtilMap.getString(formData, "textField_mkkrgg8y");//任务id long dueDate = UtilMap.getLong(formData, "dateField_mn757w7y");//预计完成时间 List questionTypes = UtilMap.getList(formData, "multiSelectField_mn757w7v");//任务类型 Map taskInfo = tbClient.queryTaskDetail(taskId, null, null).get(0); List customfields = UtilMap.getList(taskInfo, "customfields"); int delayNum = 0;//延期次数 for (Map customfield : customfields) { String cfId = UtilMap.getString(customfield, "cfId"); switch (cfId){ case "6941202e92136abe2d9de68f"://问题类型 break; case "6969da08b9d8a458d706212d"://完成状态 break; case "6969dc054cd1174e86aa213a"://延期次数 List values = UtilMap.getList(customfield, "value"); if (Objects.nonNull(values) && !values.isEmpty()){ Map value = values.get(0); delayNum = Integer.parseInt(UtilMap.getString(value, "title")); break; } } } //更新任务截止时间 // 1. 转换为 Instant(UTC 时间点) Instant instant = Instant.ofEpochMilli(dueDate); // 2.1 转换为 UTC 时间(比上海时间早 8 小时) ZonedDateTime utcTime = instant.atZone(ZoneId.of("UTC")); DateTimeFormatter utcFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); String utcDateTime = utcTime.format(utcFormatter); System.out.println("UTC date-time (比上海早8小时): " + utcDateTime); // 输出: 2026-03-20T01:30:00Z tbClient.updateTaskDueDate(taskId, utcDateTime, tbConf.getOperatorId()); //更新任务延期次数 Map body = new HashMap(); body.put("customfieldId", "6969dc054cd1174e86aa213a"); Map value = new HashMap(); value.put("title",delayNum + 1 + ""); body.put("value",Arrays.asList(value)); Map map1 = tbClient.updateTaskCustomField(taskId, tbConf.getOperatorId(), body); log.info("map1:{}",JSONObject.toJSONString(map1)); //更新任务问题类型 Map body2 = new HashMap(); body2.put("customfieldId", "6941202e92136abe2d9de68f"); List value2 = new ArrayList<>(); for (String questionType : questionTypes) { Map map3 = new HashMap(); map3.put("title", questionType); value2.add(map3); } body2.put("value",value2); Map map2 = tbClient.updateTaskCustomField(taskId, tbConf.getOperatorId(), body2); log.info("map1:{}",JSONObject.toJSONString(map1)); return McR.success(); } private Map getRootTask(String taskId) { Map taskInfo = tbClient.queryTaskDetail(taskId, null, null).get(0); String parentTaskId = UtilMap.getString(taskInfo, "parentTaskId"); if (Strings.isNotBlank(parentTaskId)){ return getRootTask(parentTaskId); }else { return taskInfo; } } //转换时区并返回时间戳 private static Long extracted(String dateStr) { if (Strings.isNotBlank(dateStr)){ // 1. 解析为 Instant(UTC 时间) Instant instant = Instant.parse(dateStr); // 2. 转换为目标时区(Asia/Shanghai, UTC+8) ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai"); ZonedDateTime shanghaiTime = instant.atZone(shanghaiZone); // 3. 转换为时间戳(毫秒) long timestamp = shanghaiTime.toInstant().toEpochMilli(); // 输出结果 System.out.println("UTC 时间: " + instant); System.out.println("上海时间: " + shanghaiTime); System.out.println("时间戳(毫秒): " + timestamp); return timestamp; }else { return null; } } }