package com.yida.data.common.core.utils;

import cn.hutool.core.util.StrUtil;

/**
 * 脱敏工具类
 */
public class DesensitizeUtil {

    public static final String REPLACE_CHAR = "*";

    /**
     * 保留前后字符
     *
     * @param origin 原始字符
     * @param left   前面保留位数
     * @param right  后面保留位数
     * @return
     */
    public static String arround(String origin, Integer left, Integer right) {
        return origin.substring(0, left).concat(right(origin, right).substring(left, origin.length()));
    }

    /**
     * 保留前面字符
     *
     * @param origin 原始字符
     * @param left   前面保留位数
     * @return
     */
    public static String left(String origin, Integer left) {
        return StrUtil.padAfter(origin.substring(0, left), origin.length(), REPLACE_CHAR);
    }

    /**
     * 保留后面字符
     *
     * @param origin 原始字符
     * @param right  后面保留位数
     * @return
     */
    public static String right(String origin, Integer right) {
        int originLength = origin.length();
        return StrUtil.padPre(origin.substring(originLength - right, originLength), originLength, REPLACE_CHAR);
    }

    public static void main(String[] args) {
        String s = "13281290871";
        System.out.println(left(s, 3));
        System.out.println(right(s, 4));
        System.out.println(arround(s, 3, 4));
    }
}
