package com.goldenwater.yjjt.service.impl;

import com.goldenwater.yjjt.domain.YjjtDailyStatistics;
import com.goldenwater.yjjt.domain.YjjtStatisticsDTO;
import com.goldenwater.yjjt.domain.YjjtYearlyDataStatistics;
import com.goldenwater.yjjt.mapper.YjjtStatisticsMapper;
import com.goldenwater.yjjt.service.IYjjtStatisticsService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("yjjtStatisticsService")
public class YjjtStatisticsServiceImpl implements IYjjtStatisticsService {

    @Resource
    private YjjtStatisticsMapper yjjtStatisticsMapper;

    @Override
    public List<YjjtDailyStatistics> getDailyStatistics() {
        return yjjtStatisticsMapper.selectDailyStatistics();
    }

    @Override
    public List<YjjtDailyStatistics> getWeeklyStatistics() {
        return yjjtStatisticsMapper.selectWeeklyStatistics();
    }

    @Override
    public List<YjjtDailyStatistics> getMonthlyStatistics() {
        return yjjtStatisticsMapper.selectMonthlyStatistics();
    }

    @Override
    public List<YjjtYearlyDataStatistics> getYearlyStatistics(String year) {
        List<YjjtYearlyDataStatistics> rawData = yjjtStatisticsMapper.selectYearlyStatisticsData(year);
        return processYearlyData(rawData);
    }

    @Override
    public List<YjjtYearlyDataStatistics> getYearlySlStatistics(String year) {
        List<YjjtYearlyDataStatistics> rawData = yjjtStatisticsMapper.selectYearlySlStatisticsData(year);
        return processYearlyData(rawData);
    }

    @Override
    public List<YjjtYearlyDataStatistics> getYearlySzStatistics(String year) {
        List<YjjtYearlyDataStatistics> rawData = yjjtStatisticsMapper.selectYearlySzStatisticsData(year);
        return processYearlyData(rawData);
    }

    /**
     * 处理年度统计数据，按年份分组并汇总各单位数据
     */
    private List<YjjtYearlyDataStatistics> processYearlyData(List<YjjtYearlyDataStatistics> rawData) {
        Map<String, List<YjjtYearlyDataStatistics>> groupedData = rawData.stream()
                .collect(Collectors.groupingBy(YjjtYearlyDataStatistics::getYear));

        List<YjjtYearlyDataStatistics> result = new ArrayList<>();

        for (Map.Entry<String, List<YjjtYearlyDataStatistics>> entry : groupedData.entrySet()) {
            String yearKey = entry.getKey();
            List<YjjtYearlyDataStatistics> yearData = entry.getValue();

            YjjtYearlyDataStatistics merged = new YjjtYearlyDataStatistics();
            merged.setYear(yearKey);
            merged.setWxDataCount(0);
            merged.setWxMonitorCount(0);
            merged.setJsDataCount(0);
            merged.setJsMonitorCount(0);
            merged.setShDataCount(0);
            merged.setShMonitorCount(0);

            for (YjjtYearlyDataStatistics item : yearData) {
                String addvcd = item.getAddvcd();
                Integer dataCount = item.getDataCount() != null ? item.getDataCount() : 0;
                Integer monitorCount = item.getMonitorCount() != null ? item.getMonitorCount() : 0;

                if ("WX".equals(addvcd)) {
                    merged.setWxDataCount(dataCount);
                    merged.setWxMonitorCount(monitorCount);
                } else if ("JS".equals(addvcd)) {
                    merged.setJsDataCount(dataCount);
                    merged.setJsMonitorCount(monitorCount);
                } else if ("SH".equals(addvcd)) {
                    merged.setShDataCount(dataCount);
                    merged.setShMonitorCount(monitorCount);
                }
            }

            result.add(merged);
        }

        result.sort((a, b) -> b.getYear().compareTo(a.getYear()));
        return result;
    }

    @Override
    public List<YjjtStatisticsDTO> getUnitYearlyStatistics(String unitCode) {
        return yjjtStatisticsMapper.selectUnitYearlyStatistics(unitCode);
    }

    @Override
    public List<YjjtStatisticsDTO> getStationCountByYear(String unitCode) {
        return yjjtStatisticsMapper.selectStationCountByYear(unitCode);
    }

    @Override
    public int getTodayDataCount(String unitCode) {
        return yjjtStatisticsMapper.selectTodayDataCount(unitCode);
    }

    @Override
    public int getWeekDataCount(String unitCode) {
        return yjjtStatisticsMapper.selectWeekDataCount(unitCode);
    }

    @Override
    public int getMonthDataCount(String unitCode) {
        return yjjtStatisticsMapper.selectMonthDataCount(unitCode);
    }
}
