package com.ruoyi.loanpricing.service;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LoanPricingSensitiveDisplayService
{
    public String maskCustName(String custName)
    {
        if (!StringUtils.hasText(custName))
        {
            return custName;
        }
        if (custName.contains("公司") && custName.length() > 4)
        {
            return custName.substring(0, 2) + "*".repeat(custName.length() - 4) + custName.substring(custName.length() - 2);
        }
        if (custName.length() == 1)
        {
            return custName;
        }
        return custName.substring(0, 1) + "*".repeat(custName.length() - 1);
    }

    public String maskIdNum(String idNum)
    {
        if (!StringUtils.hasText(idNum))
        {
            return idNum;
        }
        if (idNum.startsWith("91") && idNum.length() == 18)
        {
            return idNum.substring(0, 2) + "*".repeat(13) + idNum.substring(idNum.length() - 3);
        }
        if (idNum.matches("\\d{17}[\\dXx]"))
        {
            return idNum.substring(0, 4) + "*".repeat(8) + idNum.substring(idNum.length() - 4);
        }
        if (idNum.length() > 5)
        {
            return idNum.substring(0, 2) + "*".repeat(idNum.length() - 5) + idNum.substring(idNum.length() - 3);
        }
        return "*".repeat(idNum.length());
    }
}
