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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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.AchievementReportValidation;
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;

/**
 * 大屏共同富裕统计服务实现。
 */
@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);
        CommonProsperityScreenDashboardVo vo = new CommonProsperityScreenDashboardVo();
        vo.setStatYear(year);
        vo.setAchievement(achievementReportService.getAnnualReport(String.valueOf(year)));
        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<>();
    }
}
