package com.fufan.mtsaas.tools;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 数据格式化
 *
 * @author Liby
 * @create 2018-01-18
 */
@Slf4j
public class DateFormatTool {

    /**
     * 数字转成String,去为0的尾数
     * @param number
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static String numberToString(Number number){
            NumberFormat nf = NumberFormat.getInstance();
            nf.setGroupingUsed(false);
            return nf.format(number);
        }
    /**
     * 金额类型格式化 去除0的尾数
     * @param bigDecimal
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static BigDecimal bigDecimalFormat(BigDecimal bigDecimal){
            NumberFormat nf = NumberFormat.getInstance();
            nf.setGroupingUsed(false);
            return new BigDecimal(nf.format(bigDecimal));
    }
    /**
     * 设置sysReqsrc、sysIp、sysOper 3个默认字段
     * @param lists
     * @param metdata
     * @param object
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static void setDefaultProperties(List lists , JSONObject metdata, Object object ){
        if (object != null){
            Class clazz = object.getClass();
            try {
                Field reqsrc = clazz.getDeclaredField("sysReqsrc");
                Field sysip = clazz.getDeclaredField("sysIp");
                Field sysOper = clazz.getDeclaredField("sysOper");
                reqsrc.setAccessible(true);
                sysip.setAccessible(true);
                sysOper.setAccessible(true);
                reqsrc.set(object,metdata.get("req_src"));
                sysip.set(object,metdata.get("ip_address"));
                sysOper.set(object,metdata.get("user"));
            }catch (Exception e){
                log.error("设置默认字段数据出错" + object.toString());
                log.error("设置默认字段数据出错",e);
            }
            return ;
        }
        for (Object obj : lists) {
            Class clazz = obj.getClass();
            try {
                Field reqsrc = clazz.getDeclaredField("sysReqsrc");
                Field sysip = clazz.getDeclaredField("sysIp");
                Field sysOper = clazz.getDeclaredField("sysOper");
                reqsrc.setAccessible(true);
                sysip.setAccessible(true);
                sysOper.setAccessible(true);
                reqsrc.set(obj,metdata.get("req_src"));
                sysip.set(obj,metdata.get("ip_address"));
                sysOper.set(obj,metdata.get("user"));
            }catch (Exception e){
                log.error("设置默认字段数据出错" + object.toString());
                log.error("设置默认字段数据出错",e);
            }
        }
    }
    /**
     * 字符串转Date
     * @param pattern 转换的格式
     * @param string  需要转换的字符串
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static Date stringToDate(String pattern, String string) {
        try {
            return new SimpleDateFormat(pattern).parse(string);
        } catch (ParseException e) {
            log.error("字符串转日期出错" + "格式：" + pattern + "日期：" + string.toString());
            log.error("字符串转日期出错",e);
        }
        return null;
    }
    /**
     * 时间转字符串
     * @param date		转换的时间
     * @param pattern	转换的格式
     * @return
     */
    public static String dateToString(String pattern,Date date) {
        try {
            return new SimpleDateFormat(pattern).format(date);
        }catch (Exception e){
            log.error("字符串转日期出错" + "格式：" + pattern + "日期：" + date);
            log.error("字符串转日期出错",e);
        }
        return null;
    }
    /**
     * 日期转换格式
     * @param pattern
     * @param date
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static Date dateFrmat(String pattern , Date date){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            String str = dateToString(pattern,date);
            Date   d = stringToDate(pattern,str);
            return d;
        }catch (Exception e){
            log.error("字符串转日期出错" + "格式：" + pattern + "日期：" + date);
            log.error("字符串转日期出错",e);
        }
        return null;
    }
    /**
     * 给一个日期加上天数 返回新日期
     * @param date
     * @param day
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static Date addDay(Date date,int day) {
        try {
            Calendar calendar =new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,day);
            date=calendar.getTime();
            return date;
        }catch (Exception e){
            log.error("日期加上某一天数出错" + "日期：" + date + "天数：" + day );
            log.error("日期加上某一天数出错",e );
        }
        return null;
    }
    /**
     * 给一个日期加上月数 返回新日期
     * @param date
     * @param month
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static Date addMonth (Date date,int month){
        try {
            Calendar calendar =new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, month);
            date=calendar.getTime();
            return date;
        }catch (Exception e){
            log.error("日期加上某一月数出错" + "日期：" + date + "月数：" + month );
            log.error("日期加上某一月数出错",e );

        }
        return null;
    }
    /**
     * 给一个日期加上年 返回新日期
     * @param date
     * @param day
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static Date addYear(Date date,int day) {
        try {
            Calendar calendar =new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.YEAR,day);
            date=calendar.getTime();
            return date;
        }catch (Exception e){
            log.error("日期加上某一年数出错" + "日期：" + date + "年数：" + day );
            log.error("日期加上某一年数出错",e );
        }
        return null;
    }
    /**
     * 判断年月日是否相等
     * @param date1
     * @param date2
     * @return
     *
     * @Time 2018-01-22
     * @author Liby
     */
    public static boolean isDate(Date date1, Date date2){
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    /**
     * 获取两个天数之间的差
     * @param start 起始日期
     * @param end 结束日期
     * @return
     *
     * @Time 2018-03-21
     * @author Liby
     */
    public static int getDays(Date start, Date end) {
        return (int) ((end.getTime() - start.getTime()) / 1000 / 60 / 60 / 24);
    }
}
