package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.common.Rest;
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.AccessTypeEnum;
import com.mortals.xhx.common.code.StatTypeEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.keys.RedisCacheKeys;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.thread.StatThread;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.access.model.AccessEntity;
import com.mortals.xhx.module.access.model.AccessQuery;
import com.mortals.xhx.module.access.model.AccessStatLogEntity;
import com.mortals.xhx.module.access.model.AccessSystemEntity;
import com.mortals.xhx.module.access.service.AccessService;
import com.mortals.xhx.module.access.service.AccessStatLogService;
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 com.mortals.xhx.module.stat.model.StatEntity;
import com.mortals.xhx.module.stat.service.StatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StopWatch;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

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

    @Autowired
    private PjEvaluateStatService pjEvaluateStatService;
    @Autowired
    private PhQueueStatService phQueueStatService;
    @Autowired
    private ICacheService cacheService;

    @Autowired
    private AccessService accessService;


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

        statByDate(compare);
        log.info("站点统计缓存任务");
        //统计排号与评价 部门，等字典顺序记录次数
        updateSiteCache();
    }

    private void statByDate(Long compare) {
        List<SitePdu> sitePduList = accessService.getStatSiteList(new AccessQuery());
        DateTime attendStart = DateUtil.offsetDay(new Date(), -compare.intValue());
        for (SitePdu site : sitePduList) {
            if (ObjectUtils.isEmpty(site.getId())) continue;
            StatThread statThread = new StatThread(attendStart, compare, site, null,
                    Arrays.asList(StatTypeEnum.STAT_PH.getValue(), StatTypeEnum.STAT_PJ.getValue()));
            ThreadPool.getInstance().execute(statThread);
        }
    }

    private void updateSiteCache() {
        List<SitePdu> sitePduList = accessService.getStatSiteList(new AccessQuery());

        for (SitePdu site : sitePduList) {
            updatePjSectionCache(site);
            updatePjWindowCache(site);
            updatePjHallNameCache(site);

            updatePhQueueBusniessCache(site);
            updatePhQueueSectionNameCache(site);
            updatePhQueueWindowsCache(site);
            updatePhQueueHallNameCache(site);

        }
        int total = sitePduList.size();
        log.info("开始统计站点[{}]数量",sitePduList.size());
        cacheService.set(RedisKey.KEY_SITE_STAT_COUNT_CHCHE, total);
        cacheService.set(RedisKey.KEY_SITE_STAT_TIME_CHCHE, DateUtil.formatDateTime(new Date()));

    }

    private void updatePhQueueBusniessCache(SitePdu site) {
        PhQueueStatQuery phQueueStatQuery = new PhQueueStatQuery();
        phQueueStatQuery.setSiteId(site.getId());
        phQueueStatQuery.setBusinessNotList(Arrays.asList(""));
        List<PhQueueStatEntity> phQueueStatEntities = phQueueStatService.find(phQueueStatQuery, null);
        if (!ObjectUtils.isEmpty(phQueueStatEntities)) {
            cacheService.del(RedisKey.KEY_SITE_QUEUE_BUSNIESS_CHCHE + site.getId());
            for (PhQueueStatEntity phQueueStatEntity : phQueueStatEntities) {
                cacheService.hset(RedisKey.KEY_SITE_QUEUE_BUSNIESS_CHCHE + site.getId(), phQueueStatEntity.getBusiness(), phQueueStatEntity.getPhCount());
            }
        }
    }

    private void updatePhQueueSectionNameCache(SitePdu site) {
        PhQueueStatQuery phQueueStatQuery = new PhQueueStatQuery();
        phQueueStatQuery.setSiteId(site.getId());
        phQueueStatQuery.setSectionNameNotList(Arrays.asList(""));
        List<PhQueueStatEntity> phQueueStatEntities = phQueueStatService.find(phQueueStatQuery, null);
        if (!ObjectUtils.isEmpty(phQueueStatEntities)) {
            cacheService.del(RedisKey.KEY_SITE_QUEUE_SECTIONNAME_CHCHE + site.getId());
            for (PhQueueStatEntity phQueueStatEntity : phQueueStatEntities) {
                cacheService.hset(RedisKey.KEY_SITE_QUEUE_SECTIONNAME_CHCHE + site.getId(), phQueueStatEntity.getSectionName(), phQueueStatEntity.getPhCount());
            }
        }
    }

    private void updatePhQueueWindowsCache(SitePdu site) {
        PhQueueStatQuery phQueueStatQuery = new PhQueueStatQuery();
        phQueueStatQuery.setSiteId(site.getId());
        phQueueStatQuery.setWindowFromnumNotList(Arrays.asList(""));
        List<PhQueueStatEntity> phQueueStatEntities = phQueueStatService.find(phQueueStatQuery, null);
        if (!ObjectUtils.isEmpty(phQueueStatEntities)) {
            cacheService.del(RedisKey.KEY_SITE_QUEUE_WINDOW_CHCHE + site.getId());
            for (PhQueueStatEntity phQueueStatEntity : phQueueStatEntities) {
                cacheService.hset(RedisKey.KEY_SITE_QUEUE_WINDOW_CHCHE + site.getId(), phQueueStatEntity.getWindowFromnum(), phQueueStatEntity.getPhCount());
            }
        }
    }

    private void updatePhQueueHallNameCache(SitePdu site) {
        PhQueueStatQuery phQueueStatQuery = new PhQueueStatQuery();
        phQueueStatQuery.setSiteId(site.getId());
        phQueueStatQuery.setHallNameNotList(Arrays.asList(""));
        List<PhQueueStatEntity> phQueueStatEntities = phQueueStatService.find(phQueueStatQuery, null);
        if (!ObjectUtils.isEmpty(phQueueStatEntities)) {
            cacheService.del(RedisKey.KEY_SITE_QUEUE_HALLNAME_CHCHE + site.getId());
            for (PhQueueStatEntity phQueueStatEntity : phQueueStatEntities) {
                cacheService.hset(RedisKey.KEY_SITE_QUEUE_HALLNAME_CHCHE + site.getId(), phQueueStatEntity.getHallName(), phQueueStatEntity.getPhCount());
            }
        }
    }


    private void updatePjSectionCache(SitePdu site) {
        PjEvaluateStatQuery pjEvaluateStatQuery = new PjEvaluateStatQuery();
        pjEvaluateStatQuery.setSiteId(site.getId());
        pjEvaluateStatQuery.setSectionNameNotList(Arrays.asList(""));
        List<PjEvaluateStatEntity> pjEvaluateStatEntities = pjEvaluateStatService.find(pjEvaluateStatQuery, null);
        if (!ObjectUtils.isEmpty(pjEvaluateStatEntities)) {
            cacheService.del(RedisKey.KEY_SITE_PJ_SECTIONNAME_CHCHE + site.getId());
            for (PjEvaluateStatEntity pjEvaluateStatEntity : pjEvaluateStatEntities) {
                cacheService.hset(RedisKey.KEY_SITE_PJ_SECTIONNAME_CHCHE + site.getId(), pjEvaluateStatEntity.getSectionName(), pjEvaluateStatEntity.getPjCount());
            }
        }
    }

    private void updatePjWindowCache(SitePdu site) {
        PjEvaluateStatQuery pjEvaluateStatQuery = new PjEvaluateStatQuery();
        pjEvaluateStatQuery.setSiteId(site.getId());
        pjEvaluateStatQuery.setWindowFromnumNotList(Arrays.asList(""));
        List<PjEvaluateStatEntity> pjEvaluateStatEntities = pjEvaluateStatService.find(pjEvaluateStatQuery, null);
        if (!ObjectUtils.isEmpty(pjEvaluateStatEntities)) {
            cacheService.del(RedisKey.KEY_SITE_PJ_WINDOW_CHCHE + site.getId());
            for (PjEvaluateStatEntity pjEvaluateStatEntity : pjEvaluateStatEntities) {
                cacheService.hset(RedisKey.KEY_SITE_PJ_WINDOW_CHCHE + site.getId(), pjEvaluateStatEntity.getWindowFromnum(), pjEvaluateStatEntity.getPjCount());
            }
        }
    }

    private void updatePjHallNameCache(SitePdu site) {
        PjEvaluateStatQuery pjEvaluateStatQuery = new PjEvaluateStatQuery();
        pjEvaluateStatQuery.setSiteId(site.getId());
        pjEvaluateStatQuery.setHallNameNotList(Arrays.asList(""));
        List<PjEvaluateStatEntity> pjEvaluateStatEntities = pjEvaluateStatService.find(pjEvaluateStatQuery, null);
        if (!ObjectUtils.isEmpty(pjEvaluateStatEntities)) {
            cacheService.del(RedisKey.KEY_SITE_PJ_HALLNAME_CHCHE + site.getId());
            for (PjEvaluateStatEntity pjEvaluateStatEntity : pjEvaluateStatEntities) {
                cacheService.hset(RedisKey.KEY_SITE_PJ_HALLNAME_CHCHE + site.getId(), pjEvaluateStatEntity.getHallName(), pjEvaluateStatEntity.getPjCount());
            }
        }
    }


    @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"));*/

    }


}
