wzy 1 周之前
父节点
当前提交
9ffb9bdb9d

+ 6 - 0
mjava-zhongche/src/main/java/com/malk/zhongche/controller/ZhongcheController.java

@@ -26,4 +26,10 @@ public class ZhongcheController {
     public McR updateTaskDelayInfo(@RequestBody Map map){
         return zhongcheService.updateTaskDelayInfo(map);
     }
+
+    //更新父任务任务类型
+    @PostMapping("/updateParentTaskType")
+    public McR updateParentTaskType(@RequestBody Map map){
+        return zhongcheService.updateParentTaskType(map);
+    }
 }

+ 4 - 0
mjava-zhongche/src/main/java/com/malk/zhongche/service/ZhongcheService.java

@@ -9,4 +9,8 @@ public interface ZhongcheService {
 
     McR updateTaskDelayInfo(Map map);
 
+    McR updateParentTaskType(Map map);
+
+    void updateLastTaskSfc();
+
 }

+ 152 - 0
mjava-zhongche/src/main/java/com/malk/zhongche/service/impl/ZhongcheServiceImpl.java

@@ -1,6 +1,8 @@
 package com.malk.zhongche.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.algorithms.Algorithm;
 import com.malk.server.aliwork.YDConf;
 import com.malk.server.aliwork.YDParam;
 import com.malk.server.common.McR;
@@ -9,9 +11,12 @@ 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.UtilHttp;
 import com.malk.utils.UtilMap;
+import com.malk.utils.UtilToken;
 import com.malk.zhongche.service.ZhongcheService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.util.Strings;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -199,6 +204,83 @@ public class ZhongcheServiceImpl implements ZhongcheService {
         return McR.success();
     }
 
+    @Override
+    public McR updateParentTaskType(Map map) {
+        log.info("更新父任务任务类型:{}",JSONObject.toJSONString(map));
+        String taskId = UtilMap.getString(map, "taskId");
+
+        Map taskInfo = tbClient.queryTaskDetail(taskId, null, null).get(0);
+
+        String parentTaskId = UtilMap.getString(taskInfo, "parentTaskId");
+
+        Map parentTaskInfo = tbClient.queryTaskDetail(parentTaskId, null, null).get(0);
+
+        String projectId = UtilMap.getString(parentTaskInfo, "projectId");
+        String sfcId = UtilMap.getString(parentTaskInfo, "sfcId");
+
+        //获取项目任务类型
+        List<Map> result = searchProjectSfc(projectId);
+
+        String sfcId2 = "";//任务(企业)
+        String sfcId3 = "";//末级任务(企业)
+
+        for (Map resultMap : result) {
+            String name = UtilMap.getString(resultMap, "name");
+            String id = UtilMap.getString(resultMap, "id");
+
+            switch (name){
+                case "任务(企业)":sfcId2 = id;break;
+                case "末级任务(企业)":sfcId3 = id;break;
+                default:break;
+            }
+        }
+
+        //若父任务任务类型为末级任务(企业),更新为任务(企业)
+        if (sfcId.equals(sfcId3)){
+            updateTaskSfc(parentTaskId, sfcId2);
+        }
+
+        return McR.success();
+    }
+
+    @Override
+    public void updateLastTaskSfc() {
+        List<String> projectIds = searchProject();
+
+        for (String projectId : projectIds) {
+            if (!"6969d8a34991ad9a71ce4ec5".equals(projectId)) continue;//宜搭测试项目
+
+            //查询项目下所有非顶级任务
+            List<Map> taskList = tbClient.queryProjectTaskList(projectId, UtilMap.map("q, pageSize", "taskLayer = exceptTopLevel", 1000), null);
+
+            String sfcId = "";//末级任务(企业)
+            //获取项目任务类型
+            List<Map> result = searchProjectSfc(projectId);
+            for (Map resultMap : result) {
+                String name = UtilMap.getString(resultMap, "name");
+                String id = UtilMap.getString(resultMap, "id");
+
+                switch (name){
+                    case "末级任务(企业)":sfcId = id;break;
+                    default:break;
+                }
+            }
+
+            //遍历所有任务,查询末级任务
+            for (Map task : taskList) {
+                String taskId = UtilMap.getString(task, "id");
+
+                //查询该任务是否有子任务
+                List<Map> sonTaskList = tbClient.queryTaskDetail(null, null, taskId);
+
+                if (sonTaskList.isEmpty()){
+                    //更新任务类型为末级任务(企业)
+                    updateTaskSfc(taskId, sfcId);
+                }
+            }
+        }
+    }
+
     private Map getRootTask(String taskId) {
         Map taskInfo = tbClient.queryTaskDetail(taskId, null, null).get(0);
 
@@ -235,4 +317,74 @@ public class ZhongcheServiceImpl implements ZhongcheService {
         }
 
     }
+
+    private static final Long EXPIRES_IN = 7200000L;
+
+    private String getAccessToken() {
+            String accessToken = UtilToken.get("invalid-token-teambition");
+            if (StringUtils.isNotBlank(accessToken)) {
+                return accessToken;
+            } else {
+                Algorithm algorithm = Algorithm.HMAC256(tbConf.getAppSecret());
+                long timestamp = System.currentTimeMillis();
+                Date issuedAt = new Date(timestamp);
+                Date expiresAt = new Date(timestamp + EXPIRES_IN);
+                accessToken = JWT.create().withClaim("_appId", tbConf.getAppID()).withIssuedAt(issuedAt).withExpiresAt(expiresAt).sign(algorithm);
+                log.info("响应token, {}", accessToken);
+                UtilToken.put("invalid-token-teambition", accessToken, EXPIRES_IN);
+                return accessToken;
+            }
+
+    }
+
+    private Map<String, String> initHeaderToken() {
+        Map header = new HashMap();
+        header.put("Authorization", getAccessToken());
+        header.put("X-Tenant-Id", tbConf.getTenantId());
+        header.put("X-Tenant-Type", "organization");
+        return header;
+    }
+
+    // 操作者ID: 若是查询, 以操作人视角作为权限
+    private Map<String, String> initHeaderToken(String operatorId) {
+        Map header = initHeaderToken();
+        header.put("x-operator-id", operatorId);
+        return header;
+    }
+
+    //搜索项目任务类型
+    private List<Map> searchProjectSfc(String projectId) {
+        String s = UtilHttp.doGet("https://open.teambition.com/api/v3/project/" + projectId + "/scenariofieldconfig/search", initHeaderToken(), UtilMap.map("objectTypes", "task"));
+        Map result = (Map) JSONObject.parse(s);
+
+        log.info("result:{}",result);
+
+        List<Map> list = UtilMap.getList(result, "result");
+
+        return list;
+    }
+
+    //更新任务的任务类型
+    private Map updateTaskSfc(String taskId, String sfcId) {
+        String s = UtilHttp.doPut("https://open.teambition.com/api/v3/task/" + taskId + "/sfc/update", initHeaderToken(tbConf.getOperatorId()), null, UtilMap.map("sfcId", sfcId));
+
+        Map result = (Map) JSONObject.parse(s);
+
+        log.info("result:{}",result);
+
+        return result;
+    }
+
+    //通过TQL搜索项目
+    private List<String> searchProject() {
+        String s = UtilHttp.doGet("https://open.teambition.com/api/project/search", initHeaderToken(tbConf.getOperatorId()), UtilMap.map("pageSize", 100));
+
+        Map result = (Map) JSONObject.parse(s);
+
+        log.info("result:{}",result);
+
+        List<String> list = UtilMap.getList(result, "result");
+
+        return list;
+    }
 }

+ 19 - 0
mjava-zhongche/src/test/java/com/malk/zhongche/ZcTest.java

@@ -1,4 +1,23 @@
 package com.malk.zhongche;
 
+import com.malk.zhongche.service.ZhongcheService;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@Slf4j
+@SpringBootTest
+@RunWith(SpringRunner.class)
 public class ZcTest {
+    @Autowired
+    private ZhongcheService zhongcheService;
+
+    @Test
+    //刷新所有项目末级任务的任务类型
+    public void test(){
+        zhongcheService.updateLastTaskSfc();
+    }
 }