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

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

/**
 * 生长与出栏标准入参校验（标准类型为资讯类别编码；名称 1～20）。
 */
public final class GrowthSlaughterStandardValidation
{
    private GrowthSlaughterStandardValidation()
    {
    }

    public static void validateStandardName(String standardName)
    {
        if (StringUtils.isEmpty(standardName) || standardName.length() < 1 || standardName.length() > 20)
        {
            throw new ServiceException("标准名称须为1～20个字符");
        }
    }

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

    public static void validateStandardType(String standardTypeCode)
    {
        if (StringUtils.isEmpty(standardTypeCode))
        {
            throw new ServiceException("标准类型不能为空");
        }
        if (standardTypeCode.length() > 32)
        {
            throw new ServiceException("标准类型编码过长");
        }
    }

    public static void validateContentAttachment(String originalName, String storePath)
    {
        if (StringUtils.isEmpty(originalName) || originalName.contains(","))
        {
            throw new ServiceException("正文附件名称非法");
        }
        if (!GrowthSlaughterStandardRules.isContentExtensionAllowed(originalName))
        {
            throw new ServiceException("正文附件格式不支持");
        }
        if (StringUtils.isEmpty(storePath) || !storePath.startsWith(Constants.RESOURCE_PREFIX))
        {
            throw new ServiceException("正文存储路径非法");
        }
    }

    public static void validateCoverPath(String coverPath)
    {
        if (StringUtils.isEmpty(coverPath))
        {
            return;
        }
        if (!GrowthSlaughterStandardRules.isCoverExtensionAllowed(coverPath))
        {
            throw new ServiceException("封面格式不支持，允许：jpg、jpeg、png");
        }
    }
}
