package com.docus.server.archive.job;

import com.docus.server.archive.config.DocusProperties;
import com.docus.server.archive.constans.SyncConstant;
import com.docus.server.archive.service.UserInfoSyncService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 员工用户数据同步job
 *
 * @author YongBin Wen
 * @date 2024/5/27 9:16
 */
@Component
@Slf4j
public class UserInfoSyncJob {
    @Resource
    private DocusProperties docusProperties;
    @Autowired
    private Map<String, UserInfoSyncService> userInfoSyncServiceMap = new ConcurrentHashMap<>();


    @XxlJob("UserInfoSyncJob")
    public void userInfoSync() {
        String syncServicePrefix = docusProperties.getSyncServicePrefix();
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {} 用户基础数据同步开始了!", syncServicePrefix);
        String serviceName = syncServicePrefix + SyncConstant.USER_INFO_SYNC_SERVICE_SUFFIX;
        UserInfoSyncService userInfoSyncService = userInfoSyncServiceMap.get(serviceName);
        if (userInfoSyncService == null) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  用户基础数据同步服务 {} 未找到!", serviceName);
            return;
        }
        try {
            userInfoSyncService.fullSync();
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {} 用户基础数据同步结束了!", syncServicePrefix);
        } catch (Exception ex) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> " + syncServicePrefix + " 用户基础数据同步出现异常!" + ex.getMessage(), ex);
        }
    }

}
