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

import java.io.File;
import java.util.regex.Pattern;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.web.modules.circulation.domain.BizTradeMarket;

/**
 * 交易市场管理入参校验。
 */
public final class TradeMarketValidation
{
    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    private TradeMarketValidation()
    {
    }

    public static int resolveOperateStatus(Integer operateStatus)
    {
        if (operateStatus == null)
        {
            return TradeMarketRules.OPERATE_STATUS_NORMAL;
        }
        validateOperateStatus(operateStatus);
        return operateStatus;
    }

    public static void trimFieldsForSave(BizTradeMarket row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getMarketName() != null)
        {
            row.setMarketName(row.getMarketName().trim());
        }
        if (row.getDetailAddress() != null)
        {
            row.setDetailAddress(row.getDetailAddress().trim());
        }
        if (row.getPersonInCharge() != null)
        {
            row.setPersonInCharge(row.getPersonInCharge().trim());
        }
        if (row.getContactPhone() != null)
        {
            row.setContactPhone(row.getContactPhone().trim());
        }
        if (row.getPhotoFileUrl() != null)
        {
            row.setPhotoFileUrl(row.getPhotoFileUrl().trim());
        }
        if (row.getPhotoFilePath() != null)
        {
            row.setPhotoFilePath(row.getPhotoFilePath().trim());
        }
    }

    public static void validateMarketName(String marketName)
    {
        if (StringUtils.isEmpty(marketName) || marketName.length() < 1 || marketName.length() > 10)
        {
            throw new ServiceException("市场名称须为1～10个字符");
        }
    }

    public static void validateDetailAddress(String detailAddress)
    {
        if (StringUtils.isEmpty(detailAddress) || detailAddress.length() < 1 || detailAddress.length() > 200)
        {
            throw new ServiceException("详细地址须为1～200个字符");
        }
    }

    public static void validatePersonInCharge(String personInCharge)
    {
        if (StringUtils.isEmpty(personInCharge) || personInCharge.length() < 1 || personInCharge.length() > 10)
        {
            throw new ServiceException("负责人须为1～10个字符");
        }
    }

    public static void validateContactPhone(String phone)
    {
        if (StringUtils.isEmpty(phone) || !PHONE_PATTERN.matcher(phone.trim()).matches())
        {
            throw new ServiceException("请输入正确的手机号码");
        }
    }

    public static void validateOperateStatus(Integer operateStatus)
    {
        if (operateStatus == null || operateStatus < TradeMarketRules.OPERATE_STATUS_NORMAL
                || operateStatus > TradeMarketRules.OPERATE_STATUS_CANCELLED)
        {
            throw new ServiceException("运营状态须为1～3（1正常2停业3注销）");
        }
    }

    public static void validatePhotoPair(String photoUrl, String photoPath)
    {
        boolean hasUrl = StringUtils.isNotEmpty(photoUrl);
        boolean hasPath = StringUtils.isNotEmpty(photoPath);
        if (hasUrl != hasPath)
        {
            throw new ServiceException("照片 url 与 path 须同时填写或同时为空");
        }
        if (!hasPath)
        {
            return;
        }
        if (!TradeMarketRules.isPhotoExtensionAllowed(photoPath))
        {
            throw new ServiceException("仅支持 jpg、jpeg、png");
        }
        if (!photoPath.startsWith(Constants.RESOURCE_PREFIX))
        {
            throw new ServiceException("照片存储路径非法");
        }
        validatePhotoFileSize(photoPath);
    }

    private static void validatePhotoFileSize(String resourcePath)
    {
        String relative = FileUtils.stripPrefix(resourcePath);
        while (relative.startsWith("/") || relative.startsWith("\\"))
        {
            relative = relative.substring(1);
        }
        String full = RuoYiConfig.getProfile() + File.separator + relative.replace("/", File.separator);
        File file = new File(full);
        if (file.isFile() && file.length() > TradeMarketRules.MAX_PHOTO_BYTES)
        {
            throw new ServiceException("图片大小不能超过 10MB");
        }
    }

    public static void validateRowForSave(BizTradeMarket row)
    {
        trimFieldsForSave(row);
        validateMarketName(row.getMarketName());
        validateDetailAddress(row.getDetailAddress());
        validatePersonInCharge(row.getPersonInCharge());
        validateContactPhone(row.getContactPhone());
        row.setOperateStatus(resolveOperateStatus(row.getOperateStatus()));
        validatePhotoPair(row.getPhotoFileUrl(), row.getPhotoFilePath());
    }
}
