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

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

/**
 * 综合牧业知识库入参校验。
 */
public final class LivestockKnowledgeValidation
{
    private LivestockKnowledgeValidation()
    {
    }

    public static void validateKnowledgeTitle(String knowledgeTitle)
    {
        String t = knowledgeTitle == null ? null : knowledgeTitle.trim();
        if (StringUtils.isEmpty(t) || t.length() < 1 || t.length() > 20)
        {
            throw new ServiceException("请填写【知识标题】，长度为1～20个字符");
        }
    }

    public static void validateIntroduction(String introduction)
    {
        if (introduction != null && introduction.length() > 50)
        {
            throw new ServiceException("【知识简介】不能超过 50 字");
        }
    }

    /** 1=饲养管理 … 5=政策解读 */
    public static void validateKnowledgeTopic(Integer topic)
    {
        if (topic == null || topic < 1 || topic > 5)
        {
            throw new ServiceException("知识主题无效，须为1～5");
        }
    }

    public static void validateContentAttachment(String originalName, String storePath)
    {
        if (StringUtils.isEmpty(originalName) || originalName.contains(","))
        {
            throw new ServiceException("请上传知识内容");
        }
        if (!LivestockKnowledgeRules.isContentExtensionAllowed(originalName))
        {
            throw new ServiceException("仅支持 pdf/md/doc/docx/xls/xlsx/ppt/pptx，且不超过 30MB");
        }
        if (StringUtils.isEmpty(storePath) || !storePath.startsWith(Constants.RESOURCE_PREFIX))
        {
            throw new ServiceException("正文存储路径非法");
        }
    }
}
