package cn.palmte.work.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;
import top.jfunc.common.crypto.symmetric.AesCrypto;
import top.jfunc.common.crypto.symmetric.Base64Crypto;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

public class Utils {


    /**
     * 解析前端的json条件，封装成map
     */
    public static ConcurrentHashMap<String, String> parseMap(String jsonObject) {
        ConcurrentHashMap<String, String> searchMap = new ConcurrentHashMap<String, String>();
        if (StringUtils.isEmpty(jsonObject)) {
            return searchMap;
        }
        try {
            JSONObject jo = JSONObject.parseObject(jsonObject);
            for (Map.Entry<String, Object> item : jo.entrySet()) {
                String value = item.getValue().toString();
                if (!StringUtils.isEmpty(value)) {
                    searchMap.put(item.getKey(), value);
                }
            }
        } catch (Exception e) {
        }
        return searchMap;
    }

    /**
     * 产生excel的名字
     */
    public static String generateExcelName(String prefix) {
        return prefix + "_" + DateKit.toStr(new Date(), "yyyyMMddHHmmss") + ".xls";
    }

    private static final Pattern PHONE_PATTERN = Pattern.compile("^1\\d{10}$");
    public static boolean isPhone(String str){
        return PHONE_PATTERN.matcher(str).matches();
    }


    /**
     * 保留两位小数
     */
    public static String format(Number number, String defaultValue){
        if (null == number) {
            return defaultValue;
        }else {
            return new java.text.DecimalFormat("#,##0.00").format(number);
        }
    }
    public static String format(Number number){
        return format(number, "0.00");
    }

    /**
     * 保留五位小数
     */
    public static String format2(Number number, String defaultValue){
        if (null == number) {
            return defaultValue;
        }else {
            return new java.text.DecimalFormat("#,##0.00###").format(number);
        }
    }
    public static String format2(Number number){
        return format(number, "0.00");
    }


    public static void main(String[] args) throws Exception {
        AesCrypto aesCrypto = new AesCrypto("CDGXQHCJ-HHYC2021017");
        String encoded = "fdKQaLHH1kt/pW3s4APoUA==";
        byte[] decode = new Base64Crypto().decrypt(encoded.getBytes());
        byte[] decrypt = aesCrypto.decrypt(decode);
        System.out.println(new String(decrypt));
    }
}
