package com.tax.sign.job;

import com.tax.sign.utils.TaxnoUtil;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.common.cache.distributelock.ScheduledControll;
import com.yxcheng.token.constants.RedisContants;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.UUIDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by 曹小红 on 2018-03-23.
 */
@Component
public class SyncTaxnoServiceJob {

    private static final Logger logger = LoggerFactory.getLogger(SyncTaxnoServiceJob.class);

    @Value("${taxno.sync.url}")
    private String taxnoUrl;
    @Value("${limit.taxno.list}")
    private String taxnoLimtList;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ScheduledControll scheduledControll;

//    @Scheduled(cron = "${taxno.sync.date}")
    public void SyncTaxnoJob() {

        if(!scheduledControll.getScheduledLock(RedisContants.SYNC_TAXNO_SCHEDULED_LOCK,RedisContants.SYNC_TAXNO_SCHEDULED_EXPIRE)){
            logger.info("SyncTaxnoServiceJob，不执行");
            return;
        }


        // 获取当前时间，用于记录
        long start = System.currentTimeMillis();
        String uuid = UUIDGenerator.generate();
        logger.info(uuid + " the SyncTaxnoServiceJob task number is:" + Thread.currentThread().getName());
        List<String> taxnoList = TaxnoUtil.getTaxnoList(taxnoUrl);
        for(String taxno:taxnoList) {
            setTaxnoToRedis("bwgf-limit", taxno);
        }
        logger.info(uuid + " SyncTaxnoServiceJob lost time is :" + (System.currentTimeMillis()-start) + "ms, the taxno number is : " + taxnoList.size());
    }

    public void setTaxnoToRedis(String userName, String taxno) {
        redisService.set(RedisContants.SIGN_TAXNO_LIST + userName + ":" + taxno, DateUtils.getDataString(DateUtils.datetimeFormat));
    }

}
