package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DataUtil;
import com.mortals.framework.util.ThreadPool;
import com.mortals.xhx.common.code.StatTypeEnum;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.thread.StatThread;
import com.mortals.xhx.module.access.model.AccessQuery;
import com.mortals.xhx.module.access.service.AccessService;
import com.mortals.xhx.module.ph.model.PhQueueStatEntity;
import com.mortals.xhx.module.ph.model.PhQueueStatQuery;
import com.mortals.xhx.module.ph.service.PhQueueStatService;
import com.mortals.xhx.module.pj.model.PjEvaluateStatEntity;
import com.mortals.xhx.module.pj.model.PjEvaluateStatQuery;
import com.mortals.xhx.module.pj.service.PjEvaluateStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 站点评价统计任务
 *
 * @author: zxfei
 * @date: 2022/3/9 15:19
 */
@Slf4j
@Service("SiteStatAllTask")
public class SiteStatAllTaskImpl implements ITaskExcuteService {

    @Autowired
    private AccessService accessService;


    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("站点评价统计任务");
        //统计最近4天
        Long compare = DataUtil.converStr2Long(task.getExcuteParam(), 4L);
        statByDate(compare);


    }

    private void statByDate(Long compare) {
        List<SitePdu> sitePduList = accessService.getStatSiteList(new AccessQuery());
        DateTime attendStart = DateUtil.offsetDay(new Date(), -compare.intValue());
        log.info("开始统计站点[{}]数量",sitePduList.size());
        for (SitePdu site : sitePduList) {
            if (ObjectUtils.isEmpty(site.getId())) continue;
            StatThread statThread = new StatThread(attendStart, compare, site, null,
                    Arrays.asList(StatTypeEnum.STAT_ALL.getValue()));
            ThreadPool.getInstance().execute(statThread);
        }
    }

    @Override
    public void stopTask(ITask task) throws AppException {

    }

    public static void main(String[] args) {


        for (int i = 0; i < 7; i++) {
            DateTime beforeDate = DateUtil.offsetDay(new Date(), -i);
            int year = beforeDate.year();
            int month = beforeDate.month() + 1;
            int day = beforeDate.dayOfMonth();

            System.out.println(year + "年" + month + "月" + day + "日");

        }



/*        int year = DateUtil.date().year();
        int month = DateUtil.date().month() + 1;
        int day = DateUtil.date().dayOfMonth();

        //  System.out.println(year + "年" + month + "月" + day + "日");


        System.out.println(dateTime.toString("yyyy-MM-dd"));*/

    }


}
