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

import java.util.Calendar;
import java.util.Date;
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.BizIndustryPolicyPublicity;

/**
 * 产业政策宣传入参校验。
 */
public final class IndustryPolicyPublicityValidation
{
    private IndustryPolicyPublicityValidation()
    {
    }

    public static void trimFieldsForSave(BizIndustryPolicyPublicity row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getPolicyName() != null)
        {
            row.setPolicyName(row.getPolicyName().trim());
        }
        if (row.getIntroduction() != null)
        {
            String intro = row.getIntroduction().trim();
            row.setIntroduction(intro.isEmpty() ? null : intro);
        }
        if (row.getPolicyDesc() != null)
        {
            row.setPolicyDesc(row.getPolicyDesc().trim());
        }
        if (row.getPolicyFileUrl() != null)
        {
            row.setPolicyFileUrl(row.getPolicyFileUrl().trim());
        }
        if (row.getPolicyFilePath() != null)
        {
            row.setPolicyFilePath(row.getPolicyFilePath().trim());
        }
        if (row.getPolicyFileName() != null)
        {
            row.setPolicyFileName(row.getPolicyFileName().trim());
        }
        if (row.getCoverFileUrl() != null)
        {
            row.setCoverFileUrl(row.getCoverFileUrl().trim());
        }
        if (row.getCoverFilePath() != null)
        {
            row.setCoverFilePath(row.getCoverFilePath().trim());
        }
    }

    public static void validatePolicyName(String policyName)
    {
        if (StringUtils.isEmpty(policyName) || policyName.length() < 1 || policyName.length() > 20)
        {
            throw new ServiceException("政策名称须为1～20个字符");
        }
    }

    public static void validatePolicyLevel(Integer policyLevel)
    {
        if (policyLevel == null || policyLevel < 1 || policyLevel > 4)
        {
            throw new ServiceException("政策层级须为1～4（1国家级2省级3市级4县级）");
        }
    }

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

    public static void validatePolicyDesc(String policyDesc)
    {
        if (StringUtils.isEmpty(policyDesc) || policyDesc.length() > 50)
        {
            throw new ServiceException("政策描述须为1～50个字符");
        }
    }

    public static void validateIssueDate(Date issueDate)
    {
        if (issueDate == null)
        {
            throw new ServiceException("出台日期不能为空");
        }
        Calendar today = Calendar.getInstance();
        truncateToDay(today);
        Calendar issue = Calendar.getInstance();
        issue.setTime(issueDate);
        truncateToDay(issue);
        if (issue.after(today))
        {
            throw new ServiceException("出台日期不能为未来日期");
        }
    }

    public static void validatePolicyFileGroup(String url, String path, String originalName)
    {
        boolean hasUrl = StringUtils.isNotEmpty(url);
        boolean hasPath = StringUtils.isNotEmpty(path);
        boolean hasName = StringUtils.isNotEmpty(originalName);
        if (hasUrl == hasPath && hasPath == hasName)
        {
            if (!hasUrl)
            {
                return;
            }
            if (originalName.contains(","))
            {
                throw new ServiceException("政策文件名称非法");
            }
            if (!IndustryPolicyPublicityRules.isPolicyFileExtensionAllowed(originalName))
            {
                throw new ServiceException("政策文件格式不支持，允许：pdf、md、doc、docx、xls、xlsx、ppt、pptx");
            }
            if (!path.startsWith(Constants.RESOURCE_PREFIX))
            {
                throw new ServiceException("政策文件存储路径非法");
            }
            return;
        }
        throw new ServiceException("政策文件 url、path、name 须同时填写或同时为空");
    }

    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 (!IndustryPolicyPublicityRules.isCoverExtensionAllowed(coverPath))
        {
            throw new ServiceException("封面格式不支持，允许：jpg、jpeg、png");
        }
    }

    public static void validateRowForSave(BizIndustryPolicyPublicity row)
    {
        trimFieldsForSave(row);
        validatePolicyName(row.getPolicyName());
        validatePolicyLevel(row.getPolicyLevel());
        validateIntroduction(row.getIntroduction());
        validateIssueDate(row.getIssueDate());
        validatePolicyDesc(row.getPolicyDesc());
        validatePolicyFileGroup(row.getPolicyFileUrl(), row.getPolicyFilePath(), row.getPolicyFileName());
        validateCoverPair(row.getCoverFileUrl(), row.getCoverFilePath());
    }

    private static void truncateToDay(Calendar cal)
    {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
}
