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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.mall.constant.MallConfigConstants;
import com.ruoyi.web.modules.mall.dto.MallSettingSaveDTO;

/**
 * 商城设置校验
 */
public final class MallConfigSupport
{
    private MallConfigSupport()
    {
    }

    public static void validateSave(MallSettingSaveDTO dto)
    {
        if (dto == null)
        {
            throw new ServiceException("参数不能为空");
        }
        if (dto.getAutoConfirmDays() == null
                || dto.getAutoConfirmDays() < MallConfigConstants.MIN_AUTO_CONFIRM_DAYS
                || dto.getAutoConfirmDays() > MallConfigConstants.MAX_AUTO_CONFIRM_DAYS)
        {
            throw new ServiceException(MallConfigConstants.MSG_AUTO_CONFIRM_DAYS_INVALID);
        }
        if (dto.getAftersaleLimitDays() == null
                || dto.getAftersaleLimitDays() < MallConfigConstants.MIN_AFTERSALE_LIMIT_DAYS
                || dto.getAftersaleLimitDays() > MallConfigConstants.MAX_AFTERSALE_LIMIT_DAYS)
        {
            throw new ServiceException(MallConfigConstants.MSG_AFTERSALE_DAYS_INVALID);
        }
        if (!MallConfigConstants.FLAG_YES.equals(dto.getShowSales())
                && !MallConfigConstants.FLAG_NO.equals(dto.getShowSales()))
        {
            throw new ServiceException(MallConfigConstants.MSG_SHOW_FLAG_INVALID);
        }
        if (StringUtils.isEmpty(dto.getDefaultAvatar()))
        {
            throw new ServiceException(MallConfigConstants.MSG_DEFAULT_AVATAR_REQUIRED);
        }
        validateOptionalUrl(dto.getIcpLink());
        validateOptionalUrl(dto.getPsbLink());
    }

    public static boolean isYes(String flag)
    {
        return MallConfigConstants.FLAG_YES.equals(flag);
    }

    public static boolean hasFilingPair(String no, String link)
    {
        return StringUtils.isNotEmpty(no) && StringUtils.isNotEmpty(link);
    }

    private static void validateOptionalUrl(String url)
    {
        if (StringUtils.isEmpty(url))
        {
            return;
        }
        String trimmed = url.trim();
        if (!trimmed.startsWith("http://") && !trimmed.startsWith("https://"))
        {
            throw new ServiceException(MallConfigConstants.MSG_FILING_LINK_INVALID);
        }
    }
}
