package com.zc.business.controller;

import com.google.gson.Gson;
import com.zc.business.domain.DcHolidays;
import com.zc.business.domain.DcNoStakeWarningTable;
import com.zc.business.enums.UniversalEnum;
import com.zc.business.service.IDcGantryStatisticsDataService;
import com.zc.business.service.IDcHolidaysService;
import com.zc.business.service.IDcTollStationStatisticsDataService;
import com.zc.business.service.impl.DcNoStakeWarningTableServiceImpl;
import com.zc.common.core.websocket.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 *
 */


@Component
public class DcTrafficFlowWarning {
    @Resource
    private DcNoStakeWarningTableServiceImpl dcNoStakeWarningTableService;
    @Autowired
    private IDcHolidaysService dcHolidaysService;
    @Autowired
    private IDcGantryStatisticsDataService dcGantryStatisticsDataService;
    @Autowired
    private IDcTollStationStatisticsDataService iDcTollStationStatisticsDataService;

   //@Scheduled(cron = "0/2 * * * * ?")
 @Scheduled(cron = "0 0 9 * * ?")
    public void DcTrafficFlow() throws ParseException {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年份
        int currentYear = currentDate.getYear();
        DcHolidays dcHolidays = new DcHolidays();
        dcHolidays.setDate(currentYear);
        //查询当前年份假期
        List<DcHolidays> dcHolidaysList = dcHolidaysService.selectDcHolidaysList(dcHolidays);
        // 计算当前日期加上七天的日期
        LocalDate dateAfterSevenDays = currentDate.plusDays(UniversalEnum.SEVEN.getNumber());
        // 格式化日期输出(可选)
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(UniversalEnum.TIME_FORMAT_YEARS_MONTH_DAY.getValue());
        DateTimeFormatter formatterStr = DateTimeFormatter.ofPattern(UniversalEnum.PURE_CHINESE_CHARACTER_TIME_FORMATS_YEARS_MONTH_DAY.getValue());
      String formattedDate = dateAfterSevenDays.format(formatter);
        String formattedDateStr = dateAfterSevenDays.format(formatterStr);
        // String formattedDate ="2024-05-01";

        for (DcHolidays holidays : dcHolidaysList) {
            String vacation = holidays.getVacation();
            String[] dates = vacation.split(UniversalEnum.THERE_S_A_LOT_GOING_ON.getValue());
            //判断当前日期+ 7天 与 日期 数据对比
            if (formattedDate.equals(dates[UniversalEnum.ZERO.getNumber()])) {
                //当前假期名称
                String name = holidays.getName();
                dcHolidays.setDate(currentYear - UniversalEnum.ONE.getNumber());

                DcHolidays dcHolidaysLast = dcHolidaysService.selectDcHolidaysListNameByDate(currentYear - 1, name);
                String vacation1 = dcHolidaysLast.getVacation();


                String[] date = vacation1.split(UniversalEnum.THERE_S_A_LOT_GOING_ON.getValue());
                Map<String, Map<String, List<Map<String, String>>>> mapListOne = new LinkedHashMap<>();

                Map<String, List<Map<String, String>>> map = new LinkedHashMap<>();
                Map<String, List<Map<String, String>>> map2 = new LinkedHashMap<>();
                Map<String, List<Map<String, String>>> map3 = new LinkedHashMap<>();
                Map<String, List<Map<String, String>>> map4 = new LinkedHashMap<>();
                for (int i1 = UniversalEnum.ZERO.getNumber(); i1 < date.length; i1++) {
                    //交通流
                    List<Map<String, String>> mapList = dcGantryStatisticsDataService.realTimeTrafficFlow(date[i1], UniversalEnum.ONE.getValue(), UniversalEnum.FOUR.getValue());
                    List<Map<String, String>> mapList2 = dcGantryStatisticsDataService.realTimeTrafficFlow(date[i1], UniversalEnum.THREE.getValue(), UniversalEnum.FOUR.getValue());
                    //收费站
                    // access_type 1 入口 2出口
                    // period_type 时间颗粒度
                    List<Map<String, String>> dcTollStationStatisticsDataTwo = iDcTollStationStatisticsDataService.TollBoothStatistics(date[i1], UniversalEnum.TWO.getValue(), UniversalEnum.FOUR.getValue());
                    List<Map<String, String>> dcTollStationStatisticsData = iDcTollStationStatisticsDataService.TollBoothStatistics(date[i1], UniversalEnum.ONE.getValue(), UniversalEnum.FOUR.getValue());

                    // 计算当前日期加上七天后的日期
                    LocalDate dateAfterSevenDay = currentDate.plusDays(UniversalEnum.SEVEN.getNumber() + i1);
                    DateTimeFormatter formattedDateLst = DateTimeFormatter.ofPattern(UniversalEnum.TIME_FORMAT_YEARS_MONTH_DAY.getValue());
                    String formatted = dateAfterSevenDay.format(formattedDateLst);
                    map.put(formatted, mapList);
                    map2.put(formatted, mapList2);
                    map3.put(formatted, dcTollStationStatisticsData);
                    map4.put(formatted, dcTollStationStatisticsDataTwo);

                }
/*                //根据时间进行升序排序
                Map<String, List<Map<String, String>>> sortedMap = map3.entrySet().stream()
                        .sorted(Map.Entry.comparingByKey())
                        .collect(Collectors.toMap(
                                Map.Entry::getKey,
                                Map.Entry::getValue,
                                (oldValue, newValue) -> oldValue, // 如果有重复的key,默认保留旧值
                                LinkedHashMap::new // 为了保持排序,使用LinkedHashMap
                        ));

// 用排序后的Map替换原来的map3
                map3 = sortedMap;*/
                mapListOne.put(UniversalEnum.ONE.getValue(), map);//菏泽方向
                mapListOne.put(UniversalEnum.THREE.getValue(), map2);//济南方向
                mapListOne.put(UniversalEnum.FOUR.getValue(), map3);//收费站入口
                mapListOne.put(UniversalEnum.FIVE.getValue(), map4);//收费站出口

                // 创建Gson对象
                Gson gson = new Gson();
                // 将Map转换为JSON字符串
                String jsonString = gson.toJson(mapListOne);
                DcNoStakeWarningTable dcNoStakeWarningTable = new DcNoStakeWarningTable();

                // 计算当前日期加上七天的日期
                LocalDate dateAfterSevenDa = currentDate.plusDays(UniversalEnum.SEVEN.getNumber()+date.length);
                // 计算当前日期加上七天的日期
                // 格式化日期输出(可选)
                DateTimeFormatter formatte = DateTimeFormatter.ofPattern(UniversalEnum.PURE_CHINESE_CHARACTER_TIME_FORMATS_MONTH_DAY.getValue());
                String formattedDa = dateAfterSevenDa.format(formatte);

                dcNoStakeWarningTable.setWarningDescription("预计"+ formattedDateStr+UniversalEnum.SHORT_BAR.getValue()+formattedDa+",高速车流量剧增。平台预警提醒您:提前调度路管人员前往匝道等交通事故多发路段巡查,以便及时救援。");
                dcNoStakeWarningTable.setWarningType(UniversalEnum.ONE.getValue());
                dcNoStakeWarningTable.setOtherConfig(jsonString);
                dcNoStakeWarningTable.setWarningTime(new Date());
//存入记录
                dcNoStakeWarningTableService.addDcNoStakeWarningTable(dcNoStakeWarningTable);
                Long id = dcNoStakeWarningTable.getId();
                Map<String, Object> contentMap = new HashMap<>();
                contentMap.put("content",  "预计"+ formattedDateStr+UniversalEnum.SHORT_BAR.getValue()+formattedDa+",高速车流量剧增。平台预警提醒您:提前调度路管人员前往匝道等交通事故多发路段巡查,以便及时救援。");
                contentMap.put("id", id);
                WebSocketService.broadcast("trafficFlowDataEarlyWarning", contentMap);

                System.out.println(jsonString);
            }
        }
    }
}