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

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.industrypolicy.domain.BizSubsidyMatter;

/**
 * 项目与补贴-补贴入参校验。
 */
public final class SubsidyMatterValidation
{
    private SubsidyMatterValidation()
    {
    }

    public static int resolveGrantStatus(Integer grantStatus)
    {
        if (grantStatus == null)
        {
            return SubsidyMatterRules.GRANT_APPLYING;
        }
        validateGrantStatus(grantStatus);
        return grantStatus;
    }

    public static void trimFieldsForSave(BizSubsidyMatter row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getSubsidyName() != null)
        {
            row.setSubsidyName(row.getSubsidyName().trim());
        }
        if (row.getIntroduction() != null)
        {
            String intro = row.getIntroduction().trim();
            row.setIntroduction(intro.isEmpty() ? null : intro);
        }
        if (row.getDetailFileUrl() != null)
        {
            row.setDetailFileUrl(row.getDetailFileUrl().trim());
        }
        if (row.getDetailFilePath() != null)
        {
            row.setDetailFilePath(row.getDetailFilePath().trim());
        }
        if (row.getDetailFileName() != null)
        {
            row.setDetailFileName(row.getDetailFileName().trim());
        }
        if (row.getCoverFileUrl() != null)
        {
            row.setCoverFileUrl(row.getCoverFileUrl().trim());
        }
        if (row.getCoverFilePath() != null)
        {
            row.setCoverFilePath(row.getCoverFilePath().trim());
        }
        if (row.getRemark() != null)
        {
            String remark = row.getRemark().trim();
            row.setRemark(remark.isEmpty() ? null : remark);
        }
    }

    public static void validateSubsidyName(String subsidyName)
    {
        if (StringUtils.isEmpty(subsidyName) || subsidyName.length() < 1 || subsidyName.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 validateGrantStatus(Integer grantStatus)
    {
        if (grantStatus == null || grantStatus < SubsidyMatterRules.GRANT_APPLYING
                || grantStatus > SubsidyMatterRules.GRANT_GRANTED)
        {
            throw new ServiceException("发放状态须为1～2（1申请中2已发放）");
        }
    }

    public static void validateDetailFileGroup(String url, String path, String originalName)
    {
        boolean hasUrl = StringUtils.isNotEmpty(url);
        boolean hasPath = StringUtils.isNotEmpty(path);
        boolean hasName = StringUtils.isNotEmpty(originalName);
        if (!hasUrl || !hasPath || !hasName)
        {
            throw new ServiceException("补贴明细附件 url、path、name 均必填");
        }
        if (originalName.contains(","))
        {
            throw new ServiceException("补贴明细附件名称非法");
        }
        if (!SubsidyMatterRules.isDetailFileExtensionAllowed(originalName))
        {
            throw new ServiceException("补贴明细附件格式不支持，允许：pdf、md、doc、docx、xls、xlsx、ppt、pptx");
        }
        if (!path.startsWith(Constants.RESOURCE_PREFIX))
        {
            throw new ServiceException("补贴明细附件存储路径非法");
        }
    }

    public static void validateCoverPair(String coverUrl, String coverPath)
    {
        boolean hasUrl = StringUtils.isNotEmpty(coverUrl);
        boolean hasPath = StringUtils.isNotEmpty(coverPath);
        if (hasUrl != hasPath)
        {
            throw new ServiceException("封面 url 与 path 须同时填写或同时为空");
        }
        if (!hasPath)
        {
            return;
        }
        if (!SubsidyMatterRules.isCoverExtensionAllowed(coverPath))
        {
            throw new ServiceException("封面格式不支持，允许：jpg、jpeg、png");
        }
    }

    public static void validateRowForSave(BizSubsidyMatter row)
    {
        trimFieldsForSave(row);
        validateSubsidyName(row.getSubsidyName());
        validateIntroduction(row.getIntroduction());
        row.setGrantStatus(resolveGrantStatus(row.getGrantStatus()));
        validateDetailFileGroup(row.getDetailFileUrl(), row.getDetailFilePath(), row.getDetailFileName());
        validateCoverPair(row.getCoverFileUrl(), row.getCoverFilePath());
    }
}
