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.DeptInfoSyncService;
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 DeptInfoSyncJob {
    @Resource
    private DocusProperties docusProperties;
    @Autowired
    private Map<String, DeptInfoSyncService> deptInfoSyncServiceMap = new ConcurrentHashMap<>();

    @XxlJob("DeptInfoSyncJob")
    public void deptInfoSync() {
        String syncServicePrefix = docusProperties.getSyncServicePrefix();
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {} 科室基础数据同步开始了!", syncServicePrefix);
        String serviceName = syncServicePrefix + SyncConstant.DEPT_INFO_SYNC_SERVICE_SUFFIX;
        DeptInfoSyncService deptInfoSyncService = deptInfoSyncServiceMap.get(serviceName);
        if (deptInfoSyncService == null) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  科室基础数据同步服务 {} 未找到!", serviceName);
            return;
        }
        try {
            deptInfoSyncService.fullSync();
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {} 科室基础数据同步结束了!", syncServicePrefix);
        } catch (Exception ex) {
            log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> " + syncServicePrefix + " 科室基础数据同步出现异常!" + ex.getMessage(), ex);
        }
    }


}
