package com.zm.order.tool.server.controller;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *
 * </p>
 *
 * @author nianchang
 * @date 2023/11/11 2:29 下午
 */
public class DataDesensiHandler {

    public static void main(String[] args) {
        // 处理addr_info字段
        String addrInfo = "{\"acceptName\":\"留易(先生)\",\"area\":0,\"areaName\":\"\",\"city\":0,\"cityName\":\"\",\"doorNumber\":\"\",\"hide\":0,\"id\":929524970741305345,\"label\":0,\"lat\":\"22.76644\",\"lng\":\"113.207947\",\"mobile\":\"13217642070_1404\",\"orderNo\":\"D00169954562271020876\",\"province\":0,\"provinceName\":\"\",\"sellerId\":216145,\"sex\":0,\"street\":\"【隐私保护】顾客地址已隐藏\",\"userId\":-2102233505}\n";
        String newAddrInfo = handleAddrInfo(addrInfo);
        // 验证重复调用
        newAddrInfo = handleAddrInfo(newAddrInfo);

        System.out.println("addr_info 处理前：" + addrInfo);
        System.out.println("addr_info 处理后：" + newAddrInfo);

        // 处理buyer_remarks字段
        String remarks = "加热，加热！要烫点，捂手都要的 收餐人隐私号 15527923420_9542，手机号 180****7637 顾客需要餐具,备份隐私号：[\\\"15618048937_1651\\\"]";
        String newRemarks = handleBuyerRemarks(remarks);
        // 验证重复调用
        newRemarks = handleBuyerRemarks(newRemarks);
        System.out.println("\nbuyer_remarks 处理前：" + remarks);
        System.out.println("buyer_remarks 处理后：" + newRemarks);

        // 处理performance_info字段
        String performance_info = "{\"reserveTime\":1699615200000,\"receiverMobile\":\"15646513646_8896\"}";
        String newPerInfo = handlePerformanceInfo(performance_info);
        // 验证重复调用
        newPerInfo = handlePerformanceInfo(newPerInfo);

        System.out.println("\nperformance_info 处理前：" + performance_info);
        System.out.println("performance_info 处理后：" + newPerInfo);
    }

    /**
     * 处理买家备注
     * @param remarks
     * @return
     */
    public static String handleBuyerRemarks(String remarks) {
        if (StringUtils.isEmpty(remarks)) {
            return remarks;
        }
        try {
            String result = remarks;
            Pattern pattern = Pattern.compile("\\d*[\\*_]+\\d*");
            Matcher matcher = pattern.matcher(remarks);
            while (matcher.find()) {
                String str = matcher.group();
                String newString = desensiMobile(str);
                result = result.replace(str, newString);
            }
            return result;
        } catch (Throwable r) {
            r.printStackTrace();
            return "";
        }
    }

    /**
     * 处理地址信息
     * 【收货地址】隐藏区/县级以下部分的地址
     * @param addrInfo
     * @return
     */
    public static String handleAddrInfo(String addrInfo) {
        if (StringUtils.isEmpty(addrInfo)) {
            return addrInfo;
        }
        try {
            Map<String, Object> info = JSON.parseObject(addrInfo, Map.class);
            if (Objects.nonNull(info.get("acceptName"))) {
                info.put("acceptName", desensiNickname(info.get("acceptName").toString()));
            }
            if (Objects.nonNull(info.get("mobile"))) {
                info.put("mobile", desensiMobile(info.get("mobile").toString()));
            }
            info.put("street", "");
            return JSON.toJSONString(info);
        } catch (Throwable r) {
            r.printStackTrace();
            return "";
        }
    }


    /**
     * 处理履约信息
     * @param performance
     * @return
     */
    public static String handlePerformanceInfo(String performance) {
        if (StringUtils.isEmpty(performance)) {
            return performance;
        }
        try {
            Map<String, Object> info = JSON.parseObject(performance, Map.class);
            if (Objects.nonNull(info.get("receiverMobile"))) {
                info.put("receiverMobile", desensiMobile(info.get("receiverMobile").toString()));
            }
            return JSON.toJSONString(info);
        } catch (Throwable r) {
            r.printStackTrace();
            return "";
        }
    }

    /**
     * 处理手机号
     *【手机号】显示+后 4 位。如：**1050；
     *【固定号码】显示区号和后三位，如0571-**123；
     * @param mobile
     * @return
     */
    private static String desensiMobile(String mobile) {
        if (StringUtils.isEmpty(mobile) || mobile.length() < 5) {
            return mobile;
        }
        // 手机号
        if (! mobile.contains("-")) {
            return "**" + StringUtils.right(mobile, 4);
        }
        // 固定号码
        String[] data = mobile.split("-");
        String tmp = data[0] + "-";
        if (mobile.length() > 8) {
            tmp += "**" + StringUtils.right(mobile, 3);
        }
        return tmp;
    }

    /**
     * 处理昵称
     * 【美团昵称】显示首/尾各1位，中间加例如：a1；
     * @param nickname
     * @return
     */
    private static String desensiNickname(String nickname) {
        if (StringUtils.isEmpty(nickname)) {
            return nickname;
        }
        if (nickname.length() <= 2) {
            return nickname.substring(0, 1) + "*";
        }
        return nickname.substring(0, 1) + StringUtils.right(nickname, 1);
    }
}
