package com.ruoyi.web.modules.screen.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.time.YearMonth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityAchievement;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProjectOpImage;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityAchievementMapper;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectMapper;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectOpImageMapper;
import com.ruoyi.web.modules.prosperity.service.IAchievementReportService;
import com.ruoyi.web.modules.prosperity.support.AchievementReportAggregator;
import com.ruoyi.web.modules.prosperity.support.AchievementReportValidation;
import com.ruoyi.web.modules.prosperity.domain.vo.AchievementAnnualReportVo;
import com.ruoyi.web.modules.screen.domain.vo.CommonProsperityScreenDashboardVo;
import com.ruoyi.web.modules.screen.domain.vo.ScreenProjectListItemVo;
import com.ruoyi.web.modules.screen.service.ICommonProsperityScreenService;
import com.ruoyi.web.modules.screen.support.CommonProsperityScreenSupport;
import com.ruoyi.web.modules.screen.support.HomeScreenSupport;

/**
 * 大屏共同富裕统计服务实现。
 * <p>
 * 看板复用 {@link IAchievementReportService} 年度成果；项目列表仅含已发布且实施周期与统计年有交集的记录。
 */
@Service
public class CommonProsperityScreenServiceImpl implements ICommonProsperityScreenService
{
    @Autowired
    private IAchievementReportService achievementReportService;

    @Autowired
    private BizCommonProsperityAchievementMapper achievementMapper;

    @Autowired
    private BizCommonProsperityProjectMapper projectMapper;

    @Autowired
    private BizCommonProsperityProjectOpImageMapper opImageMapper;

    @Override
    public CommonProsperityScreenDashboardVo loadDashboard(String statYear)
    {
        int year = AchievementReportValidation.parseStatYear(statYear);
        YearMonth rollingAnchor = HomeScreenSupport.currentYearMonth();
        CommonProsperityScreenDashboardVo vo = new CommonProsperityScreenDashboardVo();
        vo.setStatYear(year);
        AchievementAnnualReportVo achievement = achievementReportService.getAnnualReport(String.valueOf(year));
        List<BizCommonProsperityAchievement> rollingRows = safeAchievementsBetween(
                rollingAnchor.minusMonths(11), rollingAnchor);
        achievement.setMonthly(AchievementReportAggregator.buildRollingMonthly(rollingAnchor, rollingRows));
        vo.setAchievement(achievement);
        List<BizCommonProsperityProject> projects = safePublishedForScreen(year);
        vo.setHasProjectData(!projects.isEmpty());
        vo.setProjectTypeStats(CommonProsperityScreenSupport.buildProjectTypeStats(projects));
        vo.setAvailableYears(CommonProsperityScreenSupport.buildAvailableYears(
                achievementMapper.selectDistinctStatYears(),
                projectMapper.selectDistinctStatYearsFromPublished()));
        return vo;
    }

    @Override
    public List<ScreenProjectListItemVo> listPublishedProjects(String statYear)
    {
        int year = AchievementReportValidation.parseStatYear(statYear);
        List<BizCommonProsperityProject> projects = safePublishedForScreen(year);
        if (projects.isEmpty())
        {
            return Collections.emptyList();
        }
        List<Long> ids = projects.stream().map(BizCommonProsperityProject::getId).collect(Collectors.toList());
        List<BizCommonProsperityProjectOpImage> images = opImageMapper.selectByProjectIds(ids);
        Map<Long, List<BizCommonProsperityProjectOpImage>> byProject =
                CommonProsperityScreenSupport.groupImagesByProjectId(images);
        return CommonProsperityScreenSupport.toListItems(projects, byProject);
    }

    private List<BizCommonProsperityProject> safePublishedForScreen(int statYear)
    {
        List<BizCommonProsperityProject> list = projectMapper.selectPublishedForScreen(statYear);
        return list != null ? list : new ArrayList<>();
    }

    private List<BizCommonProsperityAchievement> safeAchievementsBetween(YearMonth start, YearMonth end)
    {
        List<BizCommonProsperityAchievement> list = achievementMapper.selectByStatPeriodBetween(
                HomeScreenSupport.formatStatPeriod(start),
                HomeScreenSupport.formatStatPeriod(end));
        return list != null ? list : new ArrayList<>();
    }
}
