package com.tax.sign.job;

import com.tax.sign.model.SignDateInfo;
import com.tax.sign.service.api.SignDateInfoService;
import com.tax.sign.utils.SignUtil;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.utils.DateUtils;
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.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.Date;

@Component
public class SynTaskService {

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

    @Value("${temp.sync.date.url}")
    private String tempSyncDateUrl;

    @Autowired
    private SignDateInfoService signDateInfoService;
    @Autowired
    private RedisService redisService;

    @Async
    public Future<String> getServerDate(CountDownLatch latch, SignDateInfo signDateInfo,SignDateInfoService signDateInfoService,Date date) {
        try{
            // 获取当前时间，用于记录
            Timestamp requestTime = new Timestamp(System.currentTimeMillis());

            //logger.info("Sign server get signDateInfo zone:"+signDateInfo.getZoneCode()+",url:"+signDateInfo.getReqUrl() + ", now Date:" + signDateInfo.getServerDate());
            String syncTimeFrom = signDateInfo.getZoneStatus();
            String reqUrl = tempSyncDateUrl;
            if(syncTimeFrom.equals("1")) {
                reqUrl = signDateInfo.getReqUrl();
            }
            signDateInfo.setReqDate(new Date());
            String serverDateStr = SignUtil.getServerDate(signDateInfo.getReqUrl(), syncTimeFrom);

            if(null != serverDateStr) {
                //刷新数据库
                signDateInfo.setResDate(new Date());
                Date serverDate = DateUtils.getDate(Long.valueOf(serverDateStr));
                 if(serverDate!=null){
                    signDateInfo.setTimeDiff(DateUtils.getDiff(serverDate,date));
                }
                String formatDate = DateUtils.formatDate(serverDate, "yyyyMMddHHmmss");
                signDateInfo.setServerDate(serverDate);
                signDateInfoService.updateByPrimaryKey(signDateInfo);
                //刷新redis
                signDateInfoService.setSignDateInfoToRedis(signDateInfo);
           //     logger.info("Sign server get signDateInfo is success!zone:"+signDateInfo.getZoneCode()+",serverDateStr is : " + serverDateStr+",serverDate is :" + formatDate);
            } else {
                logger.error("Sign server get signDateInfo is error!zone:"+signDateInfo.getZoneCode()+",serverDateStr is : " + serverDateStr);
            }
            //logger.info("Sign server get signDateInfo zone:"+ signDateInfo.getZoneCode() + ", lost time is:" + (DateUtils.getDate().getTime()-requestTime.getTime()) + "ms");
        }finally {
            latch.countDown();
        }
        return  new AsyncResult<String>("SynGetServerDate4ZoneJob signDateInfo zone:"+signDateInfo.getZoneCode() + " accomplished!");

    }
}
