package com.shining.cloud.task;

import cn.hutool.core.date.DateUtil;
import com.njcn.pojo.commons.AreaGeneralData;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.UserUtil;
import com.shining.cloud.mapper.report.ShiningReportMapper;
import com.shining.cloud.service.report.ShiningReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class ReportTask {
    @Autowired
    private ShiningReportService shiningReportService;

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

    @Autowired
    private UserUtil userUtil;

    @Resource
    private ShiningReportMapper shiningReportMapper;



    @Scheduled(cron = "0 1 0 ? * MON") //每周一0时执行
    public void weeklyReport() { //周报   恐时间有延迟，提前一分钟
        Date now = new Date(new Date().getTime()-300000);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.beginOfWeek(now));
        Date startTime = calendar.getTime();

        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(DateUtil.endOfWeek(now));
        Date endTime = calendarEnd.getTime();
        createPointReport(startTime, endTime, 1);
    }

    @Scheduled(cron = "0 0 0 1 * ?")
    public void monthReport() { //月报
        Date endTime = new Date(new Date().getTime() - 180000); //恐时间有延迟，减掉一分钟
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.beginOfMonth(endTime));
        Date startTime = calendar.getTime();
        List<String> list = getUserGuid();

        createPointReport(startTime, endTime, 2);

        if (list != null) {
            for (String userId : list) {
                createMonthReport(userId, startTime, endTime);
            }
        }
    }

    public void createMonthReport(String userId, Date startTime, Date endTime) {
        try {
            shiningReportService.customReport(userId, null, 1, startTime, endTime, 2, null);
        } catch (Exception e) {
            logger.error("查询报告发生异常，异常是"+e.getMessage());
        }
    }

    public void createPointReport(Date startTime, Date endTime, Integer reportType) {
        List<Integer> listLineId = new ArrayList<>();
        List<String> listUserIndex = getUserGuid();

        for (String userIndex : listUserIndex) {
            listLineId.addAll(getLineId(userIndex));
        }

        HashSet<Integer> hashSet = new HashSet<>(listLineId);

        for (Integer integer : hashSet) {
            try {
                shiningReportService.customReport("", Long.valueOf(integer), 2, startTime, endTime, reportType, null);
            } catch (Exception e) {
                logger.error("查看报告发生异常，异常是"+e.getMessage());
            }
        }
    }

    //获取所有app主用户
    public List<String> getUserGuid() {
        return shiningReportMapper.getUserGuid();
    }

    //获取所有监测点
    public List<Integer> getLineId(String userId) {
        User user = new User();
        List<Integer> listLine = new ArrayList<>();
        user.setUserIndex(userId);
        List<AreaGeneralData> list = userUtil.getappAreaGeneral(user);

        for (AreaGeneralData areaGeneralData : list) {
            listLine.addAll(areaGeneralData.getLineIndexs());
        }

        return listLine;
    }
}