package com.fs.app.taskService.impl;

import com.fs.app.taskService.SyncQwExternalContactService;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.StringUtils;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qwApi.domain.QwExternalContactResult;
import com.fs.qwApi.service.QwApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SyncQwExternalContactServiceImpl implements SyncQwExternalContactService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;
    @Autowired
    private QwApiService qwApiService;
    @Override
    public R syncQwExternalContactUnionid() {
        // 测试环境需要在sql加上:and corp_id='ww51717e2b71d5e2d3'
        // 查询这次同步的最大id
        Long maxId = qwExternalContactMapper.selectSyncMaxId();
        log.info("同步最大id值:"+maxId);
        if (maxId == null) {
            return R.ok("无需同步");
        }
        Long recordId = 0L;
        String recordIdStr = redisCache.getCacheObject("syncQwExternalContactUnionId");
        if (StringUtils.isNotEmpty(recordIdStr)) {
            try {
                recordId = Long.parseLong(recordIdStr);
            } catch (NumberFormatException e) {
                log.info("Failed to parse recordId from redis: {}", recordIdStr);
                recordId = 0L;
            }
        }
        log.info("开始同步的recordId值:"+recordId);
        // 循环同步直到recordId等于maxId
        while (recordId < maxId) {
            // 每次查询500条数据
            List<QwExternalContact> qwExternalContacts = qwExternalContactMapper.selectSyncData(recordId, maxId);
            if (qwExternalContacts.isEmpty()) {
                break;
            }
            List<QwExternalContact> batchList = new ArrayList<>();
            // 调用接口
            for (QwExternalContact info : qwExternalContacts) {
                QwExternalContactResult externalcontact = qwApiService.getExternalcontact(info.getExternalUserId(), info.getCorpId());
                if (null!=externalcontact && null!=externalcontact.getExternal_contact() && null!=externalcontact.getExternal_contact().getUnionid() ) {
                    info.setUnionid(externalcontact.getExternal_contact().getUnionid());
                    batchList.add(info);
                }
            }
            if (!batchList.isEmpty()) {
                for (QwExternalContact qwExternalContact : batchList) {
                    qwExternalContactMapper.batchUpdateUnionId(qwExternalContact);
                }
            }else{
                log.info("集合为空：{recordId->"+recordId+";syncId->"+qwExternalContacts.get(qwExternalContacts.size() - 1).getId()+"}");
            }
            // 更新recordId为本次处理的最后一条记录的id
            recordId = qwExternalContacts.get(qwExternalContacts.size() - 1).getId();
            // 更新redis中的记录值
            redisCache.setCacheObject("syncQwExternalContactUnionId", recordId.toString());
        }
        log.info("同步成功,同步完之后的recordId:"+recordId);
        return R.ok();
    }
}
