package com.sifang.modules.customer.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.utils.DateUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.*;

public class ClientUtils {




    public static String getClientCode(String clientId){
        if(StringUtil.isBlank(clientId)){

        }
        StringBuilder sb= new StringBuilder();
        sb.append(DateUtils.getDate("yyyyMMdd"))
                .append(clientId)
                .append(getCharAndNumr(2));
        sb.append("00").append(DateUtils.getDate("HHmmssSSS"));
        return sb.toString();
    }


    /**
     * Java实体类转Map
    */
    public static Map<String, Object> entityToMap(String jsonString){
        Map<String, Object> map =Maps.newHashMap();
        Gson gson = new Gson();
        map = gson.fromJson(jsonString, map.getClass());
        return map;
    }


    /**
     * 方法3：生成随机数字和字母组合
     * @param length
     * @return
     */

    public static String getCharAndNumr(int length) {

        StringBuffer valSb = new StringBuffer();

        for (int i = 0; i < length; i++) {

            String charOrNum = Math.round(Math.random()) % 2 == 0 ? "char" : "num"; // 输出字母还是数字

            if ("char".equalsIgnoreCase(charOrNum)) {

                // 字符串

                int choice = Math.round(Math.random()) % 2 == 0 ? 65 : 97;  // 取得大写字母还是小写字母

                valSb.append((char) (choice + Math.round(Math.random()*25)));

            } else if ("num".equalsIgnoreCase(charOrNum)) {

                // 数字

                valSb.append(String.valueOf(Math.round(Math.random()*9)));

            }

        }

        return valSb.toString();

    }


    /**
     * Java实体类转Map
     * @param obj
     * @return
     */
    public static Map<String, Object> entityToMap2(Object obj){
        Map<String, Object> map = Maps.newHashMap();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    if(value!= "" && value!= null){
                        map.put(key, value);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取文件长度
     * @param file
     */
    public static long getFileSize1(File file) {
        if (file.exists() && file.isFile()) {
            String fileName = file.getName();
            System.out.println("文件"+fileName+"的大小是："+file.length());
            return file.length();
        }
        return 0;
    }

    /**
     * 根据java.io.*的流获取文件大小
     * @param file
     */
    public static int getFileSize2(File file){
        FileInputStream fis  = null;
        try {
            if(file.exists() && file.isFile()){
                String fileName = file.getName();
                fis = new FileInputStream(file);
                System.out.println("文件"+fileName+"的大小是："+fis.available()+"\n");
                return fis.available();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(null!=fis){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    public static void main(String[] args) {

     System.out.println("325135112555As59a".toUpperCase());

    }
    public static void getMapToMultiMap(Map<String,Object> maps , LinkedMultiValueMap<String, Object> params){
        Set<String> keys = maps.keySet();
        for (String key : keys) {
            Object val = maps.get(key);
            if (val != null) {
                if (isArray(val) || (val instanceof List) || val instanceof Set) {
                    List<String> lists = extractList(val);
                    for (String el : lists) {
                        params.add(key, el);
                    }
                } else {
                    params.set(key, val.toString());
                }
            }
        }
    }

    public static boolean isArray(Object obj) {
        return obj != null && obj.getClass().isArray();
    }

    public static List<String> extractList(Object val) {
        List<String> ret = new ArrayList<>();
        if (isArray(val)) {
            for (int i = 0, n = Array.getLength(val); i < n; i++) {
                Object o = Array.get(val, i);
                if (o != null) {
                    ret.add(o.toString());
                }
            }
        } else if (val instanceof List) {
            List list = (List) val;
            for (Object el : list) {
                if (el != null) {
                    ret.add(el.toString());
                }
            }
        } else if (val instanceof Set) {
            Set set = (Set) val;
            for (Object el : set) {
                if (el != null) {
                    ret.add(el.toString());
                }
            }
        }
        return ret;
    }

}
