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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.architecture.domain.BizBreedingStandard;
import com.ruoyi.web.modules.architecture.domain.BizEquipmentWorkStandard;
import com.ruoyi.web.modules.architecture.domain.BizFeedingStandard;
import com.ruoyi.web.modules.architecture.domain.BizGrowthSlaughterStandard;
import com.ruoyi.web.modules.architecture.mapper.BizBreedingStandardMapper;
import com.ruoyi.web.modules.architecture.mapper.BizEquipmentWorkStandardMapper;
import com.ruoyi.web.modules.architecture.mapper.BizFeedingStandardMapper;
import com.ruoyi.web.modules.architecture.mapper.BizGrowthSlaughterStandardMapper;
import com.ruoyi.web.modules.industrypolicy.domain.BizIndustryPolicyPublicity;
import com.ruoyi.web.modules.industrypolicy.domain.BizSubsidyMatter;
import com.ruoyi.web.modules.industrypolicy.domain.BizSubsidyProject;
import com.ruoyi.web.modules.industrypolicy.mapper.BizIndustryPolicyPublicityMapper;
import com.ruoyi.web.modules.industrypolicy.mapper.BizSubsidyMatterMapper;
import com.ruoyi.web.modules.industrypolicy.mapper.BizSubsidyProjectMapper;
import com.ruoyi.web.modules.innovation.domain.BizTechProjectGuide;
import com.ruoyi.web.modules.innovation.mapper.BizTechProjectGuideMapper;
import com.ruoyi.web.modules.news.domain.FarmingNewsListVo;
import com.ruoyi.web.modules.news.domain.FarmingNewsQuery;
import com.ruoyi.web.modules.news.domain.FarmingNewsRow;
import com.ruoyi.web.modules.news.mapper.FarmingNewsMapper;
import com.ruoyi.web.modules.news.service.IFarmingNewsService;
import com.ruoyi.web.modules.news.support.FarmingNewsDetailSupport;
import com.ruoyi.web.modules.news.support.FarmingNewsQuerySupport;
import com.ruoyi.web.modules.news.support.FarmingNewsRules;
import com.ruoyi.web.modules.news.support.FarmingNewsValidation;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectMapper;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectOpImageMapper;

@Service
public class FarmingNewsServiceImpl implements IFarmingNewsService
{
    @Autowired
    private FarmingNewsMapper farmingNewsMapper;

    @Autowired
    private BizBreedingStandardMapper bizBreedingStandardMapper;

    @Autowired
    private BizFeedingStandardMapper bizFeedingStandardMapper;

    @Autowired
    private BizEquipmentWorkStandardMapper bizEquipmentWorkStandardMapper;

    @Autowired
    private BizGrowthSlaughterStandardMapper bizGrowthSlaughterStandardMapper;

    @Autowired
    private BizTechProjectGuideMapper bizTechProjectGuideMapper;

    @Autowired
    private BizIndustryPolicyPublicityMapper bizIndustryPolicyPublicityMapper;

    @Autowired
    private BizSubsidyProjectMapper bizSubsidyProjectMapper;

    @Autowired
    private BizSubsidyMatterMapper bizSubsidyMatterMapper;

    @Autowired
    private BizCommonProsperityProjectMapper bizCommonProsperityProjectMapper;

    @Autowired
    private BizCommonProsperityProjectOpImageMapper bizCommonProsperityProjectOpImageMapper;

    @Override
    public List<FarmingNewsListVo> selectFarmingNewsList(FarmingNewsQuery query)
    {
        FarmingNewsQuery effective = FarmingNewsQuerySupport.normalize(query);
        List<FarmingNewsRow> rows = farmingNewsMapper.selectFarmingNewsList(effective);
        if (rows == null || rows.isEmpty())
        {
            return Collections.emptyList();
        }
        List<FarmingNewsListVo> result = new ArrayList<>(rows.size());
        for (FarmingNewsRow row : rows)
        {
            result.add(toListVo(row));
        }
        return result;
    }

    @Override
    public Object selectFarmingNewsById(Long id, String type)
    {
        FarmingNewsValidation.validateId(id);
        FarmingNewsValidation.validateType(type);
        String typeCode = type.trim();
        FarmingNewsRules.SourceTable source = FarmingNewsRules.resolveSourceTable(typeCode);
        switch (source)
        {
            case BREEDING_STANDARD:
                BizBreedingStandard breeding = bizBreedingStandardMapper.selectBizBreedingStandardById(id);
                FarmingNewsDetailSupport.assertVisibleBreeding(breeding, typeCode);
                return breeding;
            case FEEDING_STANDARD:
                BizFeedingStandard feeding = bizFeedingStandardMapper.selectBizFeedingStandardById(id);
                FarmingNewsDetailSupport.assertVisibleFeeding(feeding, typeCode);
                return feeding;
            case EQUIPMENT_WORK_STANDARD:
                BizEquipmentWorkStandard equipment = bizEquipmentWorkStandardMapper.selectBizEquipmentWorkStandardById(id);
                FarmingNewsDetailSupport.assertVisibleEquipment(equipment, typeCode);
                return equipment;
            case GROWTH_SLAUGHTER_STANDARD:
                BizGrowthSlaughterStandard growth = bizGrowthSlaughterStandardMapper.selectBizGrowthSlaughterStandardById(id);
                FarmingNewsDetailSupport.assertVisibleGrowth(growth, typeCode);
                return growth;
            case TECH_PROJECT_GUIDE:
                BizTechProjectGuide tech = bizTechProjectGuideMapper.selectBizTechProjectGuideById(id);
                FarmingNewsDetailSupport.assertVisibleTech(tech, typeCode);
                return tech;
            case INDUSTRY_POLICY_PUBLICITY:
                BizIndustryPolicyPublicity policy = bizIndustryPolicyPublicityMapper.selectBizIndustryPolicyPublicityById(id);
                FarmingNewsDetailSupport.assertVisiblePolicy(policy);
                return policy;
            case SUBSIDY_PROJECT:
                BizSubsidyProject subsidyProject = bizSubsidyProjectMapper.selectBizSubsidyProjectById(id);
                FarmingNewsDetailSupport.assertVisibleSubsidyProject(subsidyProject);
                return subsidyProject;
            case SUBSIDY_MATTER:
                BizSubsidyMatter subsidyMatter = bizSubsidyMatterMapper.selectBizSubsidyMatterById(id);
                FarmingNewsDetailSupport.assertVisibleSubsidyMatter(subsidyMatter);
                return subsidyMatter;
            case COMMON_PROSPERITY_PROJECT:
                BizCommonProsperityProject prosperity = bizCommonProsperityProjectMapper.selectBizCommonProsperityProjectById(id);
                FarmingNewsDetailSupport.assertVisibleProsperity(prosperity);
                prosperity.setOperationImages(bizCommonProsperityProjectOpImageMapper.selectByProjectId(id));
                if (prosperity.getOperationImages() != null)
                {
                    prosperity.setOpImageCount(prosperity.getOperationImages().size());
                }
                return prosperity;
            default:
                throw new IllegalStateException("unsupported source: " + source);
        }
    }

    private static FarmingNewsListVo toListVo(FarmingNewsRow row)
    {
        FarmingNewsListVo vo = new FarmingNewsListVo();
        vo.setId(row.getId());
        vo.setTitle(row.getTitle());
        vo.setIntroduction(row.getIntroduction());
        vo.setType(row.getType());
        vo.setCoverFileUrl(row.getCoverFileUrl());
        vo.setContentFileUrl(row.getContentFileUrl());
        vo.setPublishTime(row.getPublishTime());
        return vo;
    }
}
