package com.sonic.lion.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class MaskUtils {

    /**
     * 隐藏邮箱信息
     *
     * @param email
     * @return
     */
    public static String maskEmail(String email) {
        if (StringUtils.isEmpty(email)) return "";

        String prefix = "";
        try {
            if (email.indexOf("@") > 5) {
                prefix = email.substring(0, 3);
            } else {
                prefix = email.substring(0, 1);
            }

            String middle = "";
            if (prefix.length() < email.indexOf("@")) {
                for (int i = prefix.length(); i < email.indexOf("@"); i++) {
                    middle += "*";
                }
            }

            String suffix = email.substring(email.indexOf("@"));
            return prefix + middle + suffix;
        } catch (Exception e) {
            log.error(
                    String.format("email encrypt fail, param:%s, errorMsg:%s", email, e.getMessage()), e
            );
            return "";
        }
    }

    /**
     * 隐藏身份证号码
     *
     * @param id
     * @return
     */
    public static String maskBankCardNo(String id) {
        return "****" + id.substring(id.length() - 4);
    }

}
