package cn.com.qmth.stmms.biz.school.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.com.qmth.stmms.biz.config.model.SystemAuth;
import cn.com.qmth.stmms.biz.config.service.SystemAuthService;
import cn.com.qmth.stmms.biz.config.service.impl.SystemCache;
import cn.com.qmth.stmms.biz.lock.LockService;
import cn.com.qmth.stmms.biz.school.service.SchoolService;
import cn.com.qmth.stmms.common.enums.LockType;
import cn.com.qmth.stmms.common.enums.SystemAuthType;

/**
 * 机构同步定时任务
 *
 */

@Component
public class OrgCornService {

    protected static final Logger log = LoggerFactory.getLogger(OrgCornService.class);

    @Autowired
    SchoolService schoolService;

    @Autowired
    private LockService lockService;

    @Autowired
    private SystemAuthService authService;

    @Autowired
    private SystemCache authCache;

    /**
     * 自动同步
     */
    @Scheduled(cron = "${school.updateMinute}")
    public void cronUpdateOrg() {
        SystemAuth systemAuth = authService.findOne();
        if (systemAuth != null && SystemAuthType.ONLINE.equals(systemAuth.getType()) && authCache.isAuth()) {
            lockService.trylock(LockType.SCHOOL, "");
            try {
                schoolService.updateOrg();
            } catch (Exception e) {
                log.error("OrgCornTask error", e);
            }
            lockService.unlock(LockType.SCHOOL, "");
        }
    }
}
