package com.ruoyi.web.modules.news.support;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;

/**
 * 养殖资讯列表/详情常量与 type→源表路由（v_farming_news）。
 */
public final class FarmingNewsRules
{
    public static final int DEFAULT_PAGE_NUM = 1;

    public static final int DEFAULT_PAGE_SIZE = 10;

    public static final int MAX_PAGE_SIZE = 50;

    public static final int PUBLISH_STATUS_PUBLISHED = 1;

    public static final String NOT_FOUND_MSG = "该资讯已下线或不存在";

    public static final String MSG_ID_REQUIRED = "资讯主键不能为空";

    public static final String MSG_TYPE_ROUTE_INVALID = "资讯类型无效，无法定位数据来源";

    /** type 对应的源业务表 */
    public enum SourceTable
    {
        BREEDING_STANDARD,
        FEEDING_STANDARD,
        EQUIPMENT_WORK_STANDARD,
        GROWTH_SLAUGHTER_STANDARD,
        TECH_PROJECT_GUIDE,
        INDUSTRY_POLICY_PUBLICITY,
        SUBSIDY_PROJECT,
        SUBSIDY_MATTER,
        COMMON_PROSPERITY_PROJECT
    }

    private FarmingNewsRules()
    {
    }

    /**
     * 按资讯类别 type 解析源表。
     * <ul>
     *   <li>001001～001004 → biz_breeding_standard</li>
     *   <li>001005～001006 → biz_feeding_standard</li>
     *   <li>001007～001010 → biz_equipment_work_standard</li>
     *   <li>001011～001012 → biz_growth_slaughter_standard</li>
     *   <li>002001～002004 → biz_tech_project_guide</li>
     *   <li>003001 → biz_industry_policy_publicity</li>
     *   <li>003002 → biz_subsidy_project</li>
     *   <li>003003 → biz_subsidy_matter</li>
     *   <li>003004 → biz_common_prosperity_project</li>
     * </ul>
     */
    public static SourceTable resolveSourceTable(String typeCode)
    {
        if (StringUtils.isEmpty(typeCode))
        {
            throw new ServiceException(FarmingNewsValidation.MSG_TYPE_REQUIRED);
        }
        String t = typeCode.trim();
        if (inRange(t, "001001", "001004"))
        {
            return SourceTable.BREEDING_STANDARD;
        }
        if (inRange(t, "001005", "001006"))
        {
            return SourceTable.FEEDING_STANDARD;
        }
        if (inRange(t, "001007", "001010"))
        {
            return SourceTable.EQUIPMENT_WORK_STANDARD;
        }
        if (inRange(t, "001011", "001012"))
        {
            return SourceTable.GROWTH_SLAUGHTER_STANDARD;
        }
        if (inRange(t, "002001", "002004"))
        {
            return SourceTable.TECH_PROJECT_GUIDE;
        }
        if ("003001".equals(t))
        {
            return SourceTable.INDUSTRY_POLICY_PUBLICITY;
        }
        if ("003002".equals(t))
        {
            return SourceTable.SUBSIDY_PROJECT;
        }
        if ("003003".equals(t))
        {
            return SourceTable.SUBSIDY_MATTER;
        }
        if ("003004".equals(t))
        {
            return SourceTable.COMMON_PROSPERITY_PROJECT;
        }
        throw new ServiceException(MSG_TYPE_ROUTE_INVALID);
    }

    public static boolean isPublished(Integer publishStatus)
    {
        return publishStatus != null && publishStatus == PUBLISH_STATUS_PUBLISHED;
    }

    private static boolean inRange(String code, String fromInclusive, String toInclusive)
    {
        return code.compareTo(fromInclusive) >= 0 && code.compareTo(toInclusive) <= 0;
    }
}
