Parcourir la source

飞书看课优化

xgb il y a 4 jours
Parent
commit
2cd474cd4b

+ 34 - 17
fs-service/src/main/java/com/fs/feishu/service/FeiShuService.java

@@ -108,16 +108,20 @@ public class FeiShuService {
             for (FeishuAccount account : personalAccounts) {
             for (FeishuAccount account : personalAccounts) {
                 if (account.getStatus() != 1) continue;
                 if (account.getStatus() != 1) continue;
                 Client client = buildClient(account);
                 Client client = buildClient(account);
+                String step = "";
                 try {
                 try {
+                    step = "创建文档";
                     String docId = docApiService.createDocument(client, userCourseVideo.getTitle() + "-注册");
                     String docId = docApiService.createDocument(client, userCourseVideo.getTitle() + "-注册");
+                    step = "插入链接块";
                     String authUrl = buildAuthLink(companyId, companyUserId, courseId, videoId, shortLink);
                     String authUrl = buildAuthLink(companyId, companyUserId, courseId, videoId, shortLink);
                     docApiService.createTextLinkBlock(client, docId, authUrl);
                     docApiService.createTextLinkBlock(client, docId, authUrl);
+                    step = "设置文档权限";
                     docApiService.changeDocumentPermissions(client, docId);
                     docApiService.changeDocumentPermissions(client, docId);
                     return "https://www.feishu.cn/docx/" + docId;
                     return "https://www.feishu.cn/docx/" + docId;
                 } catch (CustomException e) {
                 } catch (CustomException e) {
                     Integer code = e.getCode();
                     Integer code = e.getCode();
                     if (code == null) {
                     if (code == null) {
-                        recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, e.getMessage(), 1);
+                        recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, step, null, e.getMessage(), 1);
                         return null;
                         return null;
                     }
                     }
                     if (FeishuErrorCode.isAccountDisable(code)) {
                     if (FeishuErrorCode.isAccountDisable(code)) {
@@ -127,11 +131,11 @@ public class FeiShuService {
                         log.warn("个人账号[{}]可重试错误,切换下一个", account.getAppId());
                         log.warn("个人账号[{}]可重试错误,切换下一个", account.getAppId());
                     } else {
                     } else {
                         // 业务错误或其他,记录失败并返回
                         // 业务错误或其他,记录失败并返回
-                        recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, e.getMessage(), 1);
+                        recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, step, code, e.getMessage(), 1);
                         return null;
                         return null;
                     }
                     }
                 } catch (Exception e) {
                 } catch (Exception e) {
-                    recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, e.getMessage(), 1);
+                    recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, step, null, e.getMessage(), 1);
                     return null;
                     return null;
                 }
                 }
             }
             }
@@ -142,16 +146,20 @@ public class FeiShuService {
         int retries = clientPool.getAvailableCount();
         int retries = clientPool.getAvailableCount();
         for (int i = 0; i < retries; i++) {
         for (int i = 0; i < retries; i++) {
             Client client = clientPool.getClient();
             Client client = clientPool.getClient();
+            String step = "";
             try {
             try {
+                step = "创建文档";
                 String docId = docApiService.createDocument(client, userCourseVideo.getTitle() + "-注册");
                 String docId = docApiService.createDocument(client, userCourseVideo.getTitle() + "-注册");
+                step = "插入链接块";
                 String authUrl = buildAuthLink(companyId, companyUserId, courseId, videoId, shortLink);
                 String authUrl = buildAuthLink(companyId, companyUserId, courseId, videoId, shortLink);
                 docApiService.createTextLinkBlock(client, docId, authUrl);
                 docApiService.createTextLinkBlock(client, docId, authUrl);
+                step = "设置文档权限";
                 docApiService.changeDocumentPermissions(client, docId);
                 docApiService.changeDocumentPermissions(client, docId);
                 return "https://www.feishu.cn/docx/" + docId;
                 return "https://www.feishu.cn/docx/" + docId;
             } catch (CustomException e) {
             } catch (CustomException e) {
                 Integer code = e.getCode();
                 Integer code = e.getCode();
                 if (code == null) {
                 if (code == null) {
-                    recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, e.getMessage(), 1);
+                    recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, step, null, e.getMessage(), 1);
                     return null;
                     return null;
                 }
                 }
                 if (FeishuErrorCode.isAccountDisable(code)) {
                 if (FeishuErrorCode.isAccountDisable(code)) {
@@ -160,15 +168,15 @@ public class FeiShuService {
                 } else if (FeishuErrorCode.isRateLimit(code) || FeishuErrorCode.isInternalRetryable(code)) {
                 } else if (FeishuErrorCode.isRateLimit(code) || FeishuErrorCode.isInternalRetryable(code)) {
                     log.warn("公共账号可重试错误,切换下一个");
                     log.warn("公共账号可重试错误,切换下一个");
                 } else {
                 } else {
-                    recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, e.getMessage(), 1);
+                    recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, step, code, e.getMessage(), 1);
                     return null;
                     return null;
                 }
                 }
             } catch (Exception e) {
             } catch (Exception e) {
-                recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, e.getMessage(), 1);
+                recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, step, null, e.getMessage(), 1);
                 return null;
                 return null;
             }
             }
         }
         }
-        recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, "所有公共账号不可用", 1);
+        recordLinkError(videoId, companyId, courseId, companyUserId, shortLink, "降级", null, "所有公共账号不可用", 1);
         return null;
         return null;
     }
     }
 
 
@@ -203,11 +211,15 @@ public class FeiShuService {
             for (FeishuAccount account : personalAccounts) {
             for (FeishuAccount account : personalAccounts) {
                 if (account.getStatus() != 1) continue;
                 if (account.getStatus() != 1) continue;
                 Client client = buildClient(account);
                 Client client = buildClient(account);
+                String step = "";
                 try {
                 try {
+                    step = "创建文档";
                     String docId = docApiService.createDocument(client, userCourseVideo.getTitle());
                     String docId = docApiService.createDocument(client, userCourseVideo.getTitle());
+                    step = "插入iframe块";
                     String courseUrl = buildCourseNewLink(param.getCompanyId(), param.getCompanyUserId(),
                     String courseUrl = buildCourseNewLink(param.getCompanyId(), param.getCompanyUserId(),
                             param.getCourseId(), param.getVideoId(), param.getUserId(), param.getLink(), questionFlag);
                             param.getCourseId(), param.getVideoId(), param.getUserId(), param.getLink(), questionFlag);
                     docApiService.createIframeBlock(client, docId, courseUrl);
                     docApiService.createIframeBlock(client, docId, courseUrl);
+                    step = "设置文档权限";
                     docApiService.changeDocumentPermissions(client, docId);
                     docApiService.changeDocumentPermissions(client, docId);
                     String result = "https://www.feishu.cn/docx/" + docId;
                     String result = "https://www.feishu.cn/docx/" + docId;
                     redisCache.setCacheObject(cacheKey, result, 24, TimeUnit.HOURS);
                     redisCache.setCacheObject(cacheKey, result, 24, TimeUnit.HOURS);
@@ -216,7 +228,7 @@ public class FeiShuService {
                     Integer code = e.getCode();
                     Integer code = e.getCode();
                     if (code == null) {
                     if (code == null) {
                         recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
                         recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
-                                param.getCompanyUserId(), param.getLink(), e.getMessage(), 2);
+                                param.getCompanyUserId(), param.getLink(), step, null, e.getMessage(), 2);
                         return null;
                         return null;
                     }
                     }
                     if (FeishuErrorCode.isAccountDisable(code)) {
                     if (FeishuErrorCode.isAccountDisable(code)) {
@@ -226,12 +238,12 @@ public class FeiShuService {
                         log.warn("个人账号[{}]可重试错误,切换下一个", account.getAppId());
                         log.warn("个人账号[{}]可重试错误,切换下一个", account.getAppId());
                     } else {
                     } else {
                         recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
                         recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
-                                param.getCompanyUserId(), param.getLink(), e.getMessage(), 2);
+                                param.getCompanyUserId(), param.getLink(), step, code, e.getMessage(), 2);
                         return null;
                         return null;
                     }
                     }
                 } catch (Exception e) {
                 } catch (Exception e) {
                     recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
                     recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
-                            param.getCompanyUserId(), param.getLink(), e.getMessage(), 2);
+                            param.getCompanyUserId(), param.getLink(), step, null, e.getMessage(), 2);
                     return null;
                     return null;
                 }
                 }
             }
             }
@@ -242,11 +254,15 @@ public class FeiShuService {
         int retries = clientPool.getAvailableCount();
         int retries = clientPool.getAvailableCount();
         for (int i = 0; i < retries; i++) {
         for (int i = 0; i < retries; i++) {
             Client client = clientPool.getClient();
             Client client = clientPool.getClient();
+            String step = "";
             try {
             try {
+                step = "创建文档";
                 String docId = docApiService.createDocument(client, userCourseVideo.getTitle());
                 String docId = docApiService.createDocument(client, userCourseVideo.getTitle());
+                step = "插入iframe块";
                 String courseUrl = buildCourseNewLink(param.getCompanyId(), param.getCompanyUserId(),
                 String courseUrl = buildCourseNewLink(param.getCompanyId(), param.getCompanyUserId(),
                         param.getCourseId(), param.getVideoId(), param.getUserId(), param.getLink(), questionFlag);
                         param.getCourseId(), param.getVideoId(), param.getUserId(), param.getLink(), questionFlag);
                 docApiService.createIframeBlock(client, docId, courseUrl);
                 docApiService.createIframeBlock(client, docId, courseUrl);
+                step = "设置文档权限";
                 docApiService.changeDocumentPermissions(client, docId);
                 docApiService.changeDocumentPermissions(client, docId);
                 String result = "https://www.feishu.cn/docx/" + docId;
                 String result = "https://www.feishu.cn/docx/" + docId;
                 redisCache.setCacheObject(cacheKey, result, 24, TimeUnit.HOURS);
                 redisCache.setCacheObject(cacheKey, result, 24, TimeUnit.HOURS);
@@ -255,7 +271,7 @@ public class FeiShuService {
                 Integer code = e.getCode();
                 Integer code = e.getCode();
                 if (code == null) {
                 if (code == null) {
                     recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
                     recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
-                            param.getCompanyUserId(), param.getLink(), e.getMessage(), 2);
+                            param.getCompanyUserId(), param.getLink(), step, null, e.getMessage(), 2);
                     return null;
                     return null;
                 }
                 }
                 if (FeishuErrorCode.isAccountDisable(code)) {
                 if (FeishuErrorCode.isAccountDisable(code)) {
@@ -265,17 +281,17 @@ public class FeiShuService {
                     log.warn("公共账号可重试错误,切换下一个");
                     log.warn("公共账号可重试错误,切换下一个");
                 } else {
                 } else {
                     recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
                     recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
-                            param.getCompanyUserId(), param.getLink(), e.getMessage(), 2);
+                            param.getCompanyUserId(), param.getLink(), step, code, e.getMessage(), 2);
                     return null;
                     return null;
                 }
                 }
             } catch (Exception e) {
             } catch (Exception e) {
                 recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
                 recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
-                        param.getCompanyUserId(), param.getLink(), e.getMessage(), 2);
+                        param.getCompanyUserId(), param.getLink(), step, null, e.getMessage(), 2);
                 return null;
                 return null;
             }
             }
-            }
+        }
         recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
         recordLinkError(param.getVideoId(), param.getCompanyId(), param.getCourseId(),
-                param.getCompanyUserId(), param.getLink(), "所有公共账号不可用", 2);
+                param.getCompanyUserId(), param.getLink(), "降级", null, "所有公共账号不可用", 2);
         return null;
         return null;
     }
     }
 
 
@@ -293,7 +309,8 @@ public class FeiShuService {
     }
     }
 
 
     private void recordLinkError(Long videoId, Long companyId, Long courseId,
     private void recordLinkError(Long videoId, Long companyId, Long courseId,
-                                 Long companyUserId, String link, String errorInfo, Integer type) {
+                                 Long companyUserId, String link, String step, Integer errorCode, String errorMsg, Integer type) {
+        String errorInfo = (errorCode != null ? "[code=" + errorCode + "] " : "") + "[" + step + "] " + errorMsg;
         FeishuLinkError error = new FeishuLinkError();
         FeishuLinkError error = new FeishuLinkError();
         error.setVideoId(videoId);
         error.setVideoId(videoId);
         error.setCompanyId(companyId);
         error.setCompanyId(companyId);
@@ -310,7 +327,7 @@ public class FeiShuService {
             error.setCorpId(fsCourseLink.getCorpId());
             error.setCorpId(fsCourseLink.getCorpId());
         }
         }
         feishuLinkErrorMapper.insertFeishuLinkError(error);
         feishuLinkErrorMapper.insertFeishuLinkError(error);
-        log.info("飞书链接创建失败已记录,videoId={}, link={}, error={}", videoId, link, errorInfo);
+        log.info("飞书链接创建失败已记录,step={}, videoId={}, link={}, error={}", step, videoId, link, errorInfo);
     }
     }
 
 
     private CourseConfig getCourseConfig() {
     private CourseConfig getCourseConfig() {

+ 3 - 2
fs-service/src/main/java/com/fs/feishu/service/FeishuDocApiService.java

@@ -90,13 +90,14 @@ public class FeishuDocApiService {
                                 .index(0)
                                 .index(0)
                                 .children(new Block[]{
                                 .children(new Block[]{
                                         Block.newBuilder()
                                         Block.newBuilder()
-                                                .blockType(2)
-                                                .text(Text.newBuilder()
+                                                .blockType(6)
+                                                .heading4(Text.newBuilder()
                                                         .elements(new TextElement[]{
                                                         .elements(new TextElement[]{
                                                                 TextElement.newBuilder()
                                                                 TextElement.newBuilder()
                                                                         .textRun(TextRun.newBuilder()
                                                                         .textRun(TextRun.newBuilder()
                                                                                 .content("观看课程")
                                                                                 .content("观看课程")
                                                                                 .textElementStyle(TextElementStyle.newBuilder()
                                                                                 .textElementStyle(TextElementStyle.newBuilder()
+                                                                                        .bold(true)
                                                                                         .link(Link.newBuilder()
                                                                                         .link(Link.newBuilder()
                                                                                                 .url(url)
                                                                                                 .url(url)
                                                                                                 .build())
                                                                                                 .build())