package com.yihu.jw.entity.util;

import org.apache.commons.codec.binary.Base64;
import org.springframework.util.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 前后端数据传输加密工具类
 * @author monkey
 *
 */
public class AesEncryptUtils {
    //可配置到Constant中，并读取配置文件注入,16位,自己定义
    public static final String KEY = "jkzl2021ZJXL*#%a";

    //参数分别代表 算法名称/加密模式/数据填充方式
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    //AES_DECRYPT(from_base64(d.patient_name),'jkzl2021ZJXL*#%a')
    public static String jiamiMysql(String str){
        return encrypt(str,KEY);
    }

    public static String jiemiMysql(String str){
        return decrypt(str,KEY);
    }

    public static String decryptMysqlNo(String name){
        return "CAST(AES_DECRYPT(from_base64("+name+"), '"+KEY+"') AS char)";
    }

    public static String decryptMysqlNo(String name,String alias){
        return decryptMysqlNo(name)+" as "+alias+" ";
    }

    /**
     * 针对 GROUP_CONCAT 函数，逗号分隔符进行解密
     * @param str
     * @return
     */
    public static String DHFGF(String str){
        char searchChar = ',';
        int count = 0;
        char[] charArray = str.toCharArray();

        for (char item : charArray) {
            if (item == searchChar) {
                count++;
            }
        }
        StringBuffer sb = new StringBuffer();
        String[] ss = new String[count];
        if (count == 0){
            sb.append(AesEncryptUtils.decrypt(str)); ;
        }else {
            for (int i=0;i<count;i++){
                ss = str.split(",") ;
            }
            List<String> list = new ArrayList<String>();
            Collections.addAll(list,ss);
            for (int j=0;j<list.size();j++){
                str = AesEncryptUtils.decrypt(list.get(j));
                sb.append(str);
                if (j == (list.size()-1)){
                    break;
                }
                sb.append(",");
            }
        }
        return sb.toString();
    }

    /**
     * 加密
     * @param content 加密的字符串
     * @param encryptKey key值
     * @return
     * @throws Exception
     */
    public static String encrypt(String content, String encryptKey){
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128);
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
            byte[] b = cipher.doFinal(content.getBytes("utf-8"));
            // 采用base64算法进行转码,避免出现中文乱码
            return Base64.encodeBase64String(b);
        }catch (Exception e){
            e.printStackTrace();
            return content;
        }
    }

    /**
     * 解密
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptStr, String decryptKey) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128);
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
            // 采用base64算法进行转码,避免出现中文乱码
            byte[] encryptBytes = Base64.decodeBase64(encryptStr);
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
            return new String(decryptBytes);
        }catch (Exception e){
            e.printStackTrace();
            return encryptStr;
        }
    }

    public static String encrypt(String content){
        if(content == null){
            return content;
        }
        return encrypt(content, KEY);
    }
    public static String decrypt(String encryptStr){
        if(StringUtils.isEmpty(encryptStr)){
            return encryptStr;
        }
        return decrypt(encryptStr, KEY);
    }

    /**
     * 网关解密
     * @param encryptStr
     * @return
     */
    public static String agDecrypt(String encryptStr){
        if(StringUtils.isEmpty(encryptStr)){
            return encryptStr;
        }
        return decrypt(encryptStr, AGKEY);
    }

    public static String agEncrypt(String content) throws Exception {
        return encrypt(content, AGKEY);
    }

    private static final String AGKEY = "46A61629A19AE04C";

}