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

import java.util.regex.Pattern;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.industryservice.support.PastureValidation;
import com.ruoyi.web.modules.trading.domain.BizDistributor;

/**
 * 承销商入参校验。
 */
public final class DistributorValidation
{
    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    private static final Pattern ID_CARD_PATTERN = Pattern.compile(
            "^[1-9]\\d{5}(19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]$");

    private static final Pattern USCC_PATTERN = Pattern.compile("^[0-9A-Z]{18}$");

    private DistributorValidation()
    {
    }

    public static void trimFieldsForSave(BizDistributor row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getDistributorName() != null)
        {
            row.setDistributorName(row.getDistributorName().trim());
        }
        if (row.getIdentityNo() != null)
        {
            row.setIdentityNo(row.getIdentityNo().trim().toUpperCase());
        }
        if (row.getContactPhone() != null)
        {
            row.setContactPhone(row.getContactPhone().trim());
        }
        if (row.getBusinessLocation() != null)
        {
            String loc = row.getBusinessLocation().trim();
            row.setBusinessLocation(loc.isEmpty() ? null : loc);
        }
        if (row.getRemark() != null)
        {
            String remark = row.getRemark().trim();
            row.setRemark(remark.isEmpty() ? null : remark);
        }
    }

    public static void validateDistributorName(String distributorName)
    {
        if (StringUtils.isEmpty(distributorName) || distributorName.length() < 1 || distributorName.length() > 20)
        {
            throw new ServiceException("承销商名称须为1～20个字符");
        }
    }

    public static void validateDistributorType(Integer distributorType)
    {
        if (distributorType == null
                || (distributorType != DistributorRules.TYPE_INDIVIDUAL
                        && distributorType != DistributorRules.TYPE_ENTERPRISE))
        {
            throw new ServiceException("承销商类型须为1～2（1个体户2企业）");
        }
    }

    public static void validateIdentityNo(String identityNo, Integer distributorType)
    {
        if (StringUtils.isEmpty(identityNo))
        {
            throw new ServiceException("身份识别号不能为空");
        }
        validateDistributorType(distributorType);
        String normalized = identityNo.trim().toUpperCase();
        if (distributorType == DistributorRules.TYPE_INDIVIDUAL)
        {
            if (!ID_CARD_PATTERN.matcher(normalized).matches()
                    || !PastureValidation.isIdCardChecksumValid(normalized))
            {
                throw new ServiceException("请输入正确的身份证号");
            }
        }
        else
        {
            if (!USCC_PATTERN.matcher(normalized).matches())
            {
                throw new ServiceException("请输入正确的主体识别号");
            }
        }
    }

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

    public static void validateBusinessLocation(String businessLocation)
    {
        if (businessLocation != null && businessLocation.length() > 50)
        {
            throw new ServiceException("经营地点不能超过50个字符");
        }
    }

    public static void validateSalesDestination(Integer salesDestination)
    {
        if (salesDestination == null || salesDestination < DistributorRules.SALES_DESTINATION_LOCAL_CITY
                || salesDestination > DistributorRules.SALES_DESTINATION_OUT_PROVINCE)
        {
            throw new ServiceException("销售去向须为1～3（1本市2本省3外省）");
        }
    }

    public static void validateRowForSave(BizDistributor row)
    {
        trimFieldsForSave(row);
        validateDistributorName(row.getDistributorName());
        validateDistributorType(row.getDistributorType());
        validateIdentityNo(row.getIdentityNo(), row.getDistributorType());
        validateContactPhone(row.getContactPhone());
        validateBusinessLocation(row.getBusinessLocation());
        validateSalesDestination(row.getSalesDestination());
    }

    public static void validateForAssignAccount(BizDistributor db)
    {
        if (db == null)
        {
            throw new ServiceException("记录不存在或已删除");
        }
        if (db.getAccountAssigned() != null
                && db.getAccountAssigned() == TradePartnerAccountRules.ACCOUNT_ASSIGNED_YES)
        {
            throw new ServiceException(TradePartnerAccountRules.MSG_ALREADY_ASSIGNED_DISTRIBUTOR);
        }
    }
}
