package com.mortals.xhx.daemon.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.ph.model.PhQueueStatEntity;
import com.mortals.xhx.module.ph.service.PhQueueStatService;
import com.mortals.xhx.module.pj.model.PjEvaluateStatEntity;
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 java.util.Date;
import java.util.List;

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


    @Autowired
    private ISiteFeign siteFeign;
    @Autowired
    private PjEvaluateStatService pjEvaluateStatService;
    @Autowired
    private PhQueueStatService phQueueStatService;
    @Autowired
    private StatService statService;


    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("站点评价统计任务");
        //统计最近一周
        for (int i = 0; i < 4; i++) {
            DateTime beforeDate = DateUtil.offsetDay(new Date(), -i);
            int year = beforeDate.year();
            int month = beforeDate.month() + 1;
            int day = beforeDate.dayOfMonth();
/*        int year = DateUtil.date().year();
        int month = DateUtil.date().month() + 1;
        int day = DateUtil.date().dayOfMonth();*/

            SitePdu sitePdu = new SitePdu();
            sitePdu.setSize(-1);
            Rest<RespData<List<SitePdu>>> resp = siteFeign.list(sitePdu);
            if (resp.getCode() == 1) {
                List<SitePdu> sitePduList = resp.getData().getData();
                for (SitePdu site : sitePduList) {

                    PjEvaluateStatEntity sitestatEntity = new PjEvaluateStatEntity();
                    sitestatEntity.initAttrValue();
                    sitestatEntity.setSiteId(site.getId());
                    sitestatEntity.setSiteName(site.getSiteName());
                    sitestatEntity.setSiteCode(site.getSiteCode());

                    sitestatEntity.setYear(year);
                    sitestatEntity.setMonth(month);
                    sitestatEntity.setDay(day);
                    //设置年月日
                    pjEvaluateStatService.updateSitePjStat(sitestatEntity, null);

                    //设置排号
                    PhQueueStatEntity phQueueStatEntity = new PhQueueStatEntity();
                    phQueueStatEntity.initAttrValue();
                    phQueueStatEntity.setSiteId(site.getId());
                    phQueueStatEntity.setSiteName(site.getSiteName());
                    phQueueStatEntity.setSiteCode(site.getSiteCode());

                    phQueueStatEntity.setYear(year);
                    phQueueStatEntity.setMonth(month);
                    phQueueStatEntity.setDay(day);
                    phQueueStatService.updateSitePhStat(phQueueStatEntity, null);


                    StatEntity statEntity = new StatEntity();
                    statEntity.initAttrValue();
                    statEntity.setSiteId(site.getId());
                    statEntity.setSiteName(site.getSiteName());
                    statEntity.setSiteCode(site.getSiteCode());

                    statEntity.setYear(year);
                    statEntity.setMonth(month);
                    statEntity.setDay(day);
                    //设置年月日
                    statService.updateSiteStat(statEntity, null);

                }
            }

        }
    }


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

    }


}
