package com.unis.equipmentOnlineRateAssessment.modular.monitorNvr.utils;


import java.io.BufferedInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ParameterUtil {

    public static String getSysDateTime(){
        String temp_str="";
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        temp_str=sdf.format(dt);
        System.out.println("获取当前时间"+temp_str);
        return temp_str;
    }
    
    public static String getDateYMDHMS(Date date) {
    	String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        str=sdf.format(date);
        System.out.println("获取当前时间"+str);
        return str;
    }
    
    public static String getSysDate(){
        String temp_str="";
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        temp_str=sdf.format(dt);
        return temp_str;
    }

    public static Date string2datetime(String dateTime){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static Date string2date(String dateTime){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dateFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String date2string(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String str=sdf.format(date);
        return str;
    }

    /**
     * 获取指定时间的时间戳
     * @param datatime
     * @return
     */
    public static String getTimeStamp(String datatime){
        long timeStamp = 0;
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = format.parse(datatime);
            timeStamp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(timeStamp);
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static void main(String[] args) {
        System.out.println(stampToDate("1504689208643"));
        System.out.println(getTimeStamp(getSysDateTime()));
    }
    public static String getBHS(String BHS){
        String[] str = BHS.split(",");
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<str.length;i++){
            sb.append("'").append(str[i]).append("'").append(",");
        }
        String s=sb.toString();
        String t=s.substring(0,s.length()-1);
        return t;
    }

    //判断字段值是为空 不为空返回ture
    public static boolean isnotnull(Object name){
        return (null!=name&&!"".equals(name) && !"null".equals(name) && "null" != name);
    }
    
    //判断字段值是为空,为空返回ture
    public static boolean isnull(Object name){
        return (null==name || "".equals(name) || "null".equals(name) || "null" == name);
    }

    //判断字段值是相等  相等返回ture
    public static boolean isequal(Object name,Object value){
        if(name==value||name.equals(value)){
            return true;
        }
        return false;
    }

    /**
     * 判断list和str数组是否全等
     * @param list
     * @param str
     * @return
     */
    public static boolean isAllequal(List<String> list,String[] str){
        boolean isbz = true;
        for(int z=0;z<str.length;z++){
            if(!isequal(list.get(z),str[z])){
                isbz = false;
                break;
            }
        }
        return !isbz || !(str.length == list.size());
    }

    //判断字段值是否在某个集合里  在返回ture
    public static boolean isallequal(Object name,String[] value){
        for(int i=0;i<value.length;i++){
            if(name==value[i]||name.equals(value[i])){
                return true;
            }
        }
        return false;
    }

    /**
     * 从properties得到路径
     * @param filePath
     * @return
     */
    public static Properties readProperties(String filePath) {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(ParameterUtil.class.getResourceAsStream(filePath));
            props.load(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return props;
    }
    
    /**
     * 获取6位随机码
     * @param num 随机数位数
     * @return String 返回随机数
     */
    public static String randomCode(int num) {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }


    //修改一个list
    public static List<Map<String, Object>> slashCapitals(List<Map<String, Object>> list){
        List<Map<String, Object>> listData = new ArrayList<Map<String,Object>>();
        if(isnull(list)){
            return listData;
        }
        Map<String,Object> data = new HashMap<String, Object>();
        //循环list修改map
        for (Map<String,Object> hcData : list) {
            //循环修改一条数据
            data = slashCapitals(hcData);
            listData.add(data);
        }
        return listData;
    }

    //修改一条map数据
    public static Map<String, Object> slashCapitals(Map<String, Object> map){
        Map<String,Object> data = new HashMap<String, Object>();
        if(isnull(map)){
            return data;
        }
        //循环修改一条数据
        for (Map.Entry<String,Object> entry: map.entrySet()) {
            //字符串缓冲区
            StringBuffer sbf = new StringBuffer();
            //获取map的key值
            String strs = slashCapital(entry.getKey());
            // 转换成字符数组
            char[] ch = strs.toCharArray();
            // 首字母换小写
            ch[0] = (char) (ch[0] + 32);
            sbf.append(ch);
            //添加key和value进入新的返回map
            data.put(sbf.toString(),entry.getValue());
        }
        return data;
    }

    /** 方法说明 :带 _ 后第一个字母 转换成大写
     * @return :String
     * @author :HFanss
     **/
    public static String slashCapital(String str){
        //字符串缓冲区
        StringBuffer sbf = new StringBuffer();
        //如果字符串包含下划线
        if (str.contains("_")){
            //按下划线来切割字符串为数组
            String[] split = str.split("_");
            //循环数组操作其中的字符串
            for (int i = 0, index = split.length; i<index; i++) {
                //递归调用方法
                String upperTable = slashCapital(split[i]);
                //添加到字符串缓冲区
                sbf.append(upperTable);
            }
        }else{// 字符串不包含下划线
            // 转换成字符数组
            char[] ch = str.toCharArray();
            // 判断首字母是否是字母
            if (ch[0] >= 'a' && ch[0] <= 'z'){
                // 利用ASCII码实现大写
                ch[0] = (char) (ch[0] - 32);
            }
            // 添加进字符串缓存区
            sbf.append(ch);
        }
        // 返回
        return sbf.toString();
    }

    public static String dellOneDay(String time) {
        String add = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date timeNow = df.parse(time);
            Calendar begin=Calendar.getInstance();
            begin.setTime(timeNow);
            begin.add(Calendar.DAY_OF_MONTH,-1);
            add = df.format(begin.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return add;
    }
}
