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

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.prosperity.domain.vo.AchievementAnnualReportVo;
import com.ruoyi.web.modules.prosperity.domain.vo.AchievementReportYearsVo;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityAchievementMapper;
import com.ruoyi.web.modules.prosperity.service.IAchievementReportService;
import com.ruoyi.web.modules.prosperity.support.AchievementReportAggregator;
import com.ruoyi.web.modules.prosperity.support.AchievementReportValidation;

/**
 * 共同富裕成果年度报告服务实现。
 */
@Service
public class AchievementReportServiceImpl implements IAchievementReportService
{
    @Autowired
    private BizCommonProsperityAchievementMapper achievementMapper;

    @Override
    public AchievementAnnualReportVo getAnnualReport(String statYear)
    {
        int year = AchievementReportValidation.parseStatYear(statYear);
        return AchievementReportAggregator.buildReport(year,
                achievementMapper.selectByStatYear(String.valueOf(year)));
    }

    @Override
    public AchievementReportYearsVo listAvailableYears()
    {
        List<String> yearStrs = achievementMapper.selectDistinctStatYears();
        List<Integer> years = new ArrayList<>();
        if (yearStrs != null)
        {
            for (String y : yearStrs)
            {
                if (y != null && y.matches("^\\d{4}$"))
                {
                    years.add(Integer.parseInt(y));
                }
            }
        }
        AchievementReportYearsVo vo = new AchievementReportYearsVo();
        vo.setYears(years);
        return vo;
    }
}
