package com.system.scheduler;

import com.system.api.KingDeeApi;
import com.system.api.KingDeeK3CloudApi;
import com.system.model.InitConnectParam;
import com.system.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author Inori
 */
@Configuration
@EnableScheduling
public class KingDeeLoginScheduler {

    private static final Logger log = LoggerFactory.getLogger(KingDeeLoginScheduler.class);

    @Autowired
    private KingDeeApi kingDeeApi;


    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void configureTasks() {
        InitConnectParam initConnectParam = kingDeeApi.getInitConnectParam();

        if (StringUtil.isNotNull(initConnectParam)) {
            KingDeeK3CloudApi kingDeeK3CloudApi = new KingDeeK3CloudApi(initConnectParam.getK3CloudUrl());
            Boolean flag;
            try {
                flag = kingDeeK3CloudApi.login(initConnectParam.getDbid(), initConnectParam.getUid(), initConnectParam.getPwd(), initConnectParam.getLang());
            } catch (Exception e) {
                flag = false;
                log.info("金蝶云登录失败！");
            }

            if (flag) {
                log.info("金蝶云登录成功！");
            } else {
                log.info("金蝶云登录失败！");
            }

            kingDeeApi.setFlag(flag);
            kingDeeApi.setKingDeeK3CloudApi(kingDeeK3CloudApi);
        } else {
            kingDeeApi.setFlag(false);
        }
    }


}
