|  | @@ -1,174 +0,0 @@
 | 
	
		
			
				|  |  | -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("转接记录同步失败");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -}
 |