package com.changfa.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class CommunalTool {


    /**
     * 转换数据中的null为""(空串)
     *
     * @param obj       转换之前的数据
     * @param beanClass 转换之后的数据类型
     * @return 返回Object
     * @throws Exception
     */
    public static Object objectToData(Object obj, Class<?> beanClass) throws Exception {
        return JSONObject.parseObject(objectToString(obj), beanClass);
    }

    /**
     * Map中的null转换成""(空串)
     *
     * @param map 转换之前的数据
     * @return 返回 Map<String,Object>
     * @throws Exception
     */
    public static Map<String, Object> objectToMap(Map<String, Object> map) throws Exception {
        return JSONObject.parseObject(objectToString(map), Map.class);
    }

    /**
     * 把list中的null转换成""(空串)
     *
     * @param list 转换之前的数据
     * @return 返回list
     * @throws Exception
     */
    public static List objectToList(List list) throws Exception {
        return JSONObject.parseObject(objectToString(list), List.class);
    }

    // fistJson 的一个过滤器
    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    static DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v instanceof Date) {
                return simpleDateFormat.format(v);
            } else {
                if (v == null) {
                    return "";
                } else {
                    return v;
                }
            }
        }
    };

    /**
     * 将对象转换为json格式的字符串
     *
     * @param obj
     * @return String
     */
    public static String objectToString(Object obj) {
        // JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
        return JSON.toJSONString(obj, filter, SerializerFeature.WriteNonStringKeyAsString, SerializerFeature.WriteNullStringAsEmpty);
    }

    /**
     * 将图片转成base64 字符串
     *
     * @param path 文件路径
     * @return
     * @throws Exception
     */
    public static String encodeBase64Picture(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        String result = new BASE64Encoder().encode(buffer);
        if (StringUtils.isNotBlank(result)) {
            result = "data:image/png;base64," + result;
        } else {
            result = null;
        }
        return result;
    }

    /**
     * 时间转换  yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getTransformationDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 时间转换  yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getTimeDate(Date date) {
        int minte = getMinute(date);
        minte = minte % 5;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date afterDate = new Date(date.getTime() + 300000);
        System.out.println(sdf.format(afterDate));
        return "1";
    }

    /**
     * 功能描述：返回分
     *
     * @param date
     *            日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取日期年份
     * @param date 日期
     * @return
     */
    public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
    public static String getTime(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        return df.format(date).substring(12, 16);
    }

}

