Parcourir la source

中车-附件同步文件库文件夹

xmy il y a 1 mois
Parent
commit
f975b99414

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

@@ -534,10 +534,23 @@ public class ZhongcheServiceImpl implements ZhongcheService {
             return;
         }
 
+        Map fileLibrary = searchProjectFileLibrary(projectId, operatorId);
+        if (Objects.isNull(fileLibrary)) {
+            log.info("项目未启用文件库,跳过附件同步, projectId:{}", projectId);
+            return;
+        }
+
         Map body = new HashMap();
         body.put("projectId", projectId);
         body.put("fileTokens", fileTokens);
 
+        String productionFolderId = findProductionFolderId(fileLibrary);
+        if (Strings.isNotBlank(productionFolderId)) {
+            body.put("parentId", productionFolderId);
+        } else {
+            log.info("未找到根目录文件夹04-生产制造,附件将同步到文件库根目录, projectId:{}", projectId);
+        }
+
         TBR response = (TBR) UtilHttp.doPost(
                 "https://open.teambition.com/api/v3/work/create",
                 initHeaderToken(operatorId),
@@ -551,6 +564,51 @@ public class ZhongcheServiceImpl implements ZhongcheService {
         log.info("已同步 {} 个附件到项目文件库, projectId:{}", fileTokens.size(), projectId);
     }
 
+    private Map searchProjectFileLibrary(String projectId, String operatorId) {
+        Map query = new HashMap();
+        query.put("projectId", projectId);
+        query.put("q", "04-生产制造");
+        query.put("onlyWorks", false);
+        query.put("pageSize", 50);
+
+        TBR response = (TBR) UtilHttp.doGet(
+                "https://open.teambition.com/api/v3/work/search",
+                initHeaderToken(operatorId),
+                query,
+                TBR.class);
+        if (Objects.isNull(response)) {
+            throw new IllegalStateException("查询项目文件库失败:未收到 Teambition 响应");
+        }
+        if (!"200".equals(response.getCode())) {
+            if (isFileLibraryDisabled(response.getErrorMessage())) {
+                return null;
+            }
+            response.assertSuccess();
+        }
+        if (!(response.getResult() instanceof Map)) {
+            throw new IllegalStateException("查询项目文件库失败:响应结果格式错误");
+        }
+        return (Map) response.getResult();
+    }
+
+    private String findProductionFolderId(Map fileLibrary) {
+        List<Map> collections = UtilMap.getList(fileLibrary, "collections");
+        for (Map collection : collections) {
+            String title = UtilMap.getString(collection, "title");
+            if ("04-生产制造".equals(title)) {
+                return UtilMap.getString(collection, "id");
+            }
+        }
+        return null;
+    }
+
+    private boolean isFileLibraryDisabled(String errorMessage) {
+        return StringUtils.contains(errorMessage, "文件库")
+                && (StringUtils.contains(errorMessage, "未开启")
+                || StringUtils.contains(errorMessage, "未启用")
+                || StringUtils.contains(errorMessage, "未开通"));
+    }
+
     /**
      * 上传文件(核心方法)
      * @param filePath 本地文件路径(如"2.png")

+ 5 - 4
mjava-zhongche/src/main/resources/application-dev.yml

@@ -19,6 +19,7 @@ dingtalk:
   corpId: ding187f52da89b1e5c8a1320dcb25e91351
   aesKey:
   token:
+
   operator:
 
 aliwork:
@@ -26,10 +27,10 @@ aliwork:
   systemToken: HOE66T71Z9H2PLVHM6EU85Q7H2AJ2YV2GRKKMWL1
 
 teambition:
-  AppID: 69c4f0f024a34a74e1c06d61
-  AppSecret: X77qcnv2AICiv70R41YZxGVFXRrFGCta
-  OperatorId: 67ce737aaec2f7379c7bbf6f
+  AppID: 6a98d68e9122a8bf6f8c5dfe
+  AppSecret: ElXVfDIyOJiktuDhWoFHhvmiftAfrjsj
+  OperatorId: 622ee3450cf3bb5e1a486f1f
   TenantId: 658a6467d87ead477d2d3f9d
 
 file:
-  path: d:\\
+  path: D:/zhongche/files/

+ 10 - 4
mjava-zhongche/src/main/resources/application-prod.yml

@@ -25,11 +25,17 @@ aliwork:
   appType: APP_SL9V2KUH6GBEISLNXXTJ
   systemToken: HOE66T71Z9H2PLVHM6EU85Q7H2AJ2YV2GRKKMWL1
 
+#teambition:
+#  AppID: 69c4f0f024a34a74e1c06d61
+#  AppSecret: X77qcnv2AICiv70R41YZxGVFXRrFGCta
+#  OperatorId: 67ce737aaec2f7379c7bbf6f
+#  TenantId: 658a6467d87ead477d2d3f9d
+
 teambition:
-  AppID: 69c4f0f024a34a74e1c06d61
-  AppSecret: X77qcnv2AICiv70R41YZxGVFXRrFGCta
-  OperatorId: 67ce737aaec2f7379c7bbf6f
+  AppID: 6a98d68e9122a8bf6f8c5dfe
+  AppSecret: ElXVfDIyOJiktuDhWoFHhvmiftAfrjsj
+  OperatorId: 622ee3450cf3bb5e1a486f1f
   TenantId: 658a6467d87ead477d2d3f9d
 
 file:
-  path: /home/server/zhongche/files/
+  path: /home/server/zhongche/files/