package com.jzdsh.util;

import com.jfinal.kit.PathKit;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URL;
import java.text.Collator;
import java.util.*;

/**
 * 发送请求帮助类
 *
 * @author YaoShiHang
 */
public class CommonUtil {

    public static Map< String, Object > cache = new HashMap<>( );

    //用来产生随机验证码的
    private static Random rand = new Random( );
    private static String[] str = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    /**
     * 获取几位随机数
     *
     * @param num 位数
     * @return
     */
    public static String getRandom(int num) {
        //存放产生的随机数
        String sms;
        //生成六位数的随机数
        StringBuffer buf = new StringBuffer( );
        for (int i = 0; i < num; i++) {
            buf.append(str[rand.nextInt(10)]);
        }
        sms = buf.toString( );
        return sms;
    }

    /**
     * 获取uuid
     *
     * @return
     */
    public static String getUUID( ) {
        return UUID.randomUUID( ).toString( ).trim( ).replace("-", "").toLowerCase( );
    }

    /**
     * list集合按字典顺序排序
     *
     * @param list
     * @return
     */
    public static List chineseSort(List< String > list) {
        if (list == null || list.isEmpty( )) {
            return list;
        }
        Collections.sort(list, (o1, o2) -> {
            Comparator< Object > com = Collator.getInstance(Locale.CHINA);
            return com.compare(o1, o2);
        });
        return list;
    }

    public static String[] splitIgnoreCase(String str) {
        return str.split("[,，]+");
    }

    /**
     * 对象序列化成字节码数据
     *
     * @param obj
     * @return
     */
    public static byte[] setSerialize(Object obj) {
        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream( );
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(byteOutStream);
            oos.writeObject(obj);
            oos.close( );
            return byteOutStream.toByteArray( );
        } catch (Exception e) {
            e.printStackTrace( );
        } finally {
            try {
                oos.close( );
            } catch (IOException e) {
                e.printStackTrace( );
            }
        }
        return null;
    }

    /**
     * 反序列化  字节码文件转对象
     *
     * @param bytes
     * @return
     */
    public static Object getSerialize(byte[] bytes) {
        ByteArrayInputStream byteInStream = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(byteInStream);
            return ois.readObject( );
        } catch (Exception e) {
            e.printStackTrace( );
        } finally {
            try {
                ois.close( );
            } catch (IOException e) {
                e.printStackTrace( );
            }
        }
        return null;
    }

    /**
     * 添加到缓存中
     *
     * @param key
     * @param value
     */
    public static void setCache(String key, Object value) {
        cache.put(key, value);
    }

    /**
     * 从缓存中取出
     *
     * @param key
     * @return
     */
    public static Object getCache(String key) {
        return cache.get(key);
    }



    public static String wxImgSave(String img) {
        if (StringUtils.isBlank(img)) {return "";}
        String uuidJpg = null;
        InputStream is = null;
        OutputStream out = null;
        try {
            URL url = new URL(img);
            is = url.openStream( );

            File file = new File(PathKit.getWebRootPath()+"/upload/logo");
            if (!file.exists( )) {
                file.mkdirs( );
            }
            uuidJpg = CommonUtil.getUUID( ) + ".jpg";
            out = new FileOutputStream(file + "/" + uuidJpg);

            byte[] bytes = new byte[1024];
            int l = 0;
            while ((l = is.read(bytes)) > 0) {
                out.write(bytes, 0, l);
                out.flush( );
            }

        } catch (IOException e) {
            System.out.println("微信头像 保存失败!" + e);
        } finally {
            if (out != null) {
                try {
                    out.close( );
                } catch (IOException e) {
                    e.printStackTrace( );
                }
            }
            if (is != null) {
                try {
                    is.close( );
                } catch (IOException e) {
                    e.printStackTrace( );
                }
            }
        }
        return CommonCons.PROJECT_NAME + "/" + CommonCons.LOG_PICTURE + "/" + uuidJpg;
    }


}
