Ver código fonte

Merge remote-tracking branch 'origin/master'

ct 1 dia atrás
pai
commit
0eea51dd66

+ 7 - 1
fs-company/src/main/java/com/fs/company/controller/qw/QwExternalContactTransferLogController.java

@@ -14,6 +14,7 @@ import com.fs.framework.service.TokenService;
 import com.fs.qw.domain.QwExternalContactTransferLog;
 import com.fs.qw.param.QwExternalContactTransferLogParam;
 import com.fs.qw.service.IQwExternalContactTransferLogService;
+import com.fs.qw.service.impl.AsyncQwExternalContactTransferService;
 import com.fs.qw.vo.QwExternalContactTransferLogListVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -41,6 +42,9 @@ public class QwExternalContactTransferLogController extends BaseController
     @Autowired
     private CompanyDeptServiceImpl companyDeptService;
 
+    @Autowired
+    private AsyncQwExternalContactTransferService transferService;
+
     /**
      * 查询转接记录列表
      */
@@ -116,8 +120,10 @@ public class QwExternalContactTransferLogController extends BaseController
     @GetMapping("/sync/{corpId}")
     public R syncTag(@PathVariable("corpId") String corpId)
     {
+        transferService.syncQwExternalContactTransferLog(corpId);
 
-        return qwExternalContactTransferLogService.syncQwExternalContactTransferLog(corpId);
+//        return qwExternalContactTransferLogService.syncQwExternalContactTransferLog(corpId);
+        return R.ok();
     }
     /**
      * 获取转接记录详细信息

+ 174 - 0
fs-service/src/main/java/com/fs/qw/service/impl/AsyncQwExternalContactTransferService.java

@@ -0,0 +1,174 @@
+package com.fs.qw.service.impl;
+
+
+import com.fs.course.domain.FsUserCourse;
+import com.fs.qw.domain.QwExternalContact;
+import com.fs.qw.domain.QwExternalContactTransferLog;
+import com.fs.qw.mapper.QwExternalContactTransferLogMapper;
+import com.fs.qw.service.IQwExternalContactService;
+import com.fs.qw.service.IQwExternalContactTransferLogService;
+import com.fs.qwApi.domain.QwGetTransferResult;
+import com.fs.qwApi.param.QwGetTransferParam;
+import com.fs.qwApi.service.QwApiService;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Slf4j
+@Service
+@AllArgsConstructor
+public class AsyncQwExternalContactTransferService {
+
+    @Autowired
+    private QwExternalContactTransferLogMapper qwExternalContactTransferLogMapper;
+
+    @Autowired
+    private QwApiService qwApiService;
+
+    @Autowired
+    private IQwExternalContactService iQwExternalContactService;
+
+    @Autowired
+    private IQwExternalContactTransferLogService iQwExternalContactTransferLogService;
+
+    @Async("scheduledExecutorService")
+    public void syncQwExternalContactTransferLog(String corpId) {
+        try {
+            QwExternalContactTransferLog qwExternalContactTransferLog = new QwExternalContactTransferLog();
+            qwExternalContactTransferLog.setStatus(2);
+            qwExternalContactTransferLog.setCorpId(corpId);
+            List<QwExternalContactTransferLog> qwExternalContactTransferLogs = qwExternalContactTransferLogMapper.selectQwExternalContactTransferLogList(qwExternalContactTransferLog);
+
+            Map<String, List<QwExternalContactTransferLog>> groupedLogs = qwExternalContactTransferLogs.stream()
+                    .collect(Collectors.groupingBy(log ->
+                            log.getCorpId() + "/-/" + log.getTakeoverUserId() + "/-/" + log.getHandoverUserId()
+                    ));
+
+            for (Map.Entry<String, List<QwExternalContactTransferLog>> entry : groupedLogs.entrySet()) {
+
+
+                try {
+                    // 拆分key
+                    String[] keyParts = entry.getKey().split("/-/");
+                    String keyPartsCorpId = keyParts[0];
+                    String keyPartsTakeoverUserId = keyParts[1];
+                    String keyPartsHandoverUserId = keyParts[2];
+
+                    List<QwExternalContactTransferLog> transferLogListEntry = entry.getValue();
+
+
+                    QwGetTransferParam getTransferParam = new QwGetTransferParam();
+                    getTransferParam.setTakeover_userid(keyPartsTakeoverUserId);
+                    getTransferParam.setHandover_userid(keyPartsHandoverUserId);
+
+
+                    // 第一次请求游标为空
+                    String cursor = null;
+                    List<QwGetTransferResult.Customer> allCustomers = new ArrayList<>();
+
+                    int page = 0;
+                    do {
+                        page++;
+
+                        // 设置游标(第一次请求可能不需要设置或设为空)
+                        if (cursor != null) {
+                            getTransferParam.setCursor(cursor);
+                        }
+
+                        // 调用接口
+                        QwGetTransferResult transfer = qwApiService.getTransfer(getTransferParam, keyPartsCorpId);
+
+                        // 检查接口返回是否成功
+                        if (transfer.getErrcode() != 0) {
+                            log.error("获取转移记录失败: errcode={}, errmsg={}, takeover_userid={}, handover_userid={}",
+                                    transfer.getErrcode(), transfer.getErrmsg(),
+                                    keyPartsTakeoverUserId, keyPartsHandoverUserId);
+                            break;
+                        }
+
+                        // 获取当前页数据
+                        List<QwGetTransferResult.Customer> customers = transfer.getCustomer();
+                        if (customers != null && !customers.isEmpty()) {
+                            allCustomers.addAll(customers);
+                            log.info("第 {} 页获取到 {} 条转移记录", page, customers.size());
+                        } else {
+                            log.info("第 {} 页没有数据", page);
+                            // 如果没有数据,也退出循环
+                            break;
+                        }
+
+                        // 更新游标,准备下一次请求
+                        cursor = transfer.getNext_cursor();
+
+                        // 添加短暂延时,避免请求过快
+                        try {
+                            Thread.sleep(200);
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                            break;
+                        }
+
+                        // 如果next_cursor为空或没有更多数据,则退出循环
+                    } while (cursor != null && !cursor.isEmpty() && !"0".equals(cursor));
+
+                    List<QwExternalContactTransferLog> transferLogList=new ArrayList<>();
+
+                    List<QwExternalContact> contactList = new ArrayList<>();
+
+
+                    Map<String, QwGetTransferResult.Customer> allCustomersMap = allCustomers.stream()
+                            .collect(Collectors.toMap(
+                                    QwGetTransferResult.Customer::getExternal_userid,
+                                    customer -> customer
+                            ));
+
+                    for (QwExternalContactTransferLog transferLog : transferLogListEntry) {
+
+                        QwGetTransferResult.Customer customer = allCustomersMap.get(transferLog.getExternalUserId());
+
+                        if (customer != null && customer.getStatus() != 2) {
+                            transferLog.setStatus(customer.getStatus());
+                            transferLogList.add(transferLog);
+
+                            QwExternalContact qwExternalContact = new QwExternalContact();
+                            qwExternalContact.setId(transferLog.getExternalContactId());
+                            qwExternalContact.setTransferStatus(customer.getStatus());
+
+                            if (customer.getStatus() == 1) {
+                                qwExternalContact.setUserId(transferLog.getTakeoverUserId());
+                                qwExternalContact.setQwUserId(transferLog.getQwUserId());
+                                qwExternalContact.setCompanyUserId(transferLog.getCompanyUserId());
+                            }
+                            contactList.add(qwExternalContact);
+                        }
+                    }
+
+                    if (!transferLogList.isEmpty()){
+                        log.info("更新{}条外部联系人转移日志",transferLogList.size());
+                        iQwExternalContactTransferLogService.updateBatchById(transferLogList,300);
+                    }
+                    if (!contactList.isEmpty()){
+                        log.info("更新{}条外部联系人",contactList.size());
+                        iQwExternalContactService.updateBatchById(contactList,300);
+                    }
+                }catch (Exception e){
+                    log.error("查询转接记录失败:"+entry);
+                }
+
+
+            }
+
+            log.info("转接记录同步成功");
+        } catch (Exception e){
+            e.printStackTrace();
+            log.info("转接记录同步失败");
+        }
+    }
+}

+ 1 - 1
fs-service/src/main/java/com/fs/qw/service/impl/QwExternalContactTransferLogServiceImpl.java

@@ -129,7 +129,7 @@ public class QwExternalContactTransferLogServiceImpl extends ServiceImpl<QwExter
                         log.getCorpId() + "/-/" + log.getTakeoverUserId() + "/-/" + log.getHandoverUserId()
                 ));
 
-        for (Map.Entry<String, List<QwExternalContactTransferLog>> entry : groupedLogs.entrySet()) {
+                for (Map.Entry<String, List<QwExternalContactTransferLog>> entry : groupedLogs.entrySet()) {
 
 
             try {

+ 1 - 1
fs-service/src/main/resources/mapper/course/FsCourseWatchLogMapper.xml

@@ -172,7 +172,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         select l.log_id,l.user_id,uc.course_name,v.title as video_name,u.nick_name as fsNickName, u.avatar as fsAvatar,
         l.log_type,SEC_TO_TIME(l.duration) as duration,c.company_name,l.camp_period_time,l.finish_time,
         cu.nick_name as company_user_name ,l.send_type,l.create_time, qu.qw_user_name,qec.name as external_user_name
-        from · l
+        from fs_course_watch_log l
         left join fs_user_course_video v on v.video_id = l.video_id
         left join fs_user_course uc on uc.course_id = l.course_id
         left join fs_user u on u.user_id = l.user_id