package com.nmgs.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import oracle.sql.CLOB;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class PubTools {
   static SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   static SimpleDateFormat dfYYD=new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat dfYMD = new SimpleDateFormat("yyyyMMdd");



    /**
     * 判断字符串是否为空
     * yyp 20190305
     * @param anObject 字符串
     * @return true :是空，false:不是空
     */
    public static boolean isNull(String anObject ) {
        if(null==anObject||"".equals(anObject)||"null".equals(anObject)){
            return true;
        }else{
            return false;
        }
    }

    public static boolean isEmpty(Object anObject ) {
        if (anObject == null) return true;
        else if (anObject instanceof CharSequence) return ((CharSequence) anObject).length() == 0;
        else if (anObject instanceof Collection) return ((Collection) anObject).isEmpty();
        else if (anObject instanceof Map) return ((Map) anObject).isEmpty();
        //else if (anObject.getClass().isArray()) return Array.getLength(anObject) == 0;

        return false;
    }

    /**
     *	根据⾝份证号获取年龄
     *
     * @param IDCard
     * @return
     */
    public static Integer getAge(String IDCard) {
        String age ="0";
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH)+1;
        int dayNow = cal.get(Calendar.DATE);
        String IDCardNew=((IDCard.length()==15)?IDCard.substring(0, 6)+"19"+IDCard.substring(6)+"0":IDCard);
        System.out.println("IDCardNew==="+IDCardNew);
        int year = Integer.valueOf(IDCardNew.substring(6,10));
        int month = Integer.valueOf(IDCard.substring(10,12));
        int day = Integer.valueOf(IDCard.substring(12,14));
        System.out.println("year==="+year+"====month==="+month+"====day==="+day);
        if ((month < monthNow) || (month == monthNow && day<= dayNow) ){
            age = String.valueOf(yearNow - year);
        }else {
            age = String.valueOf(yearNow - year-1);
        }
        return Integer.parseInt(age);
    }

    public static JSONArray revertList(JSONObject json){
        JSONArray listArray=new JSONArray();
        listArray.add(json);
        return listArray;
    }

    public static String getCurrentDate(){
        return df.format(new Date());
    }

    public static String getCurrentDateNoStr(){
        SimpleDateFormat df2=new SimpleDateFormat("yyyyMMddHHmmss");
        return df2.format(new Date());
    }
    public static String getCurrentDateYYD(){
        return dfYYD.format(new Date());
    }

    public static String getCurrentDateYMD() {
        return dfYMD.format(new Date());
    }
    //拼接字符串
    public static String StringToClob(String text) {
        int content_length = text.length();
        StringBuilder sb_temp = new StringBuilder();
        int n = 1;
        int max = content_length / 1999 + 1;
        while (n < max) {
            String temp = "";
            temp = text.substring((n - 1) * 1999, n * 1999);
            sb_temp.append("to_clob('" + temp + "')||");
            n++;
        }
        sb_temp.append("to_clob('" + text.substring((n - 1) * 1999, content_length) + "')");
        return sb_temp.toString();
    }


    //计算两个字符串日期相差秒数

    /**
     * 计算两个日期差值
     * @param startTime 开始日期
     * @param endTime 结束日期
     * @param diffType 差值类型（D 天数 ，S 秒数,H  小时，M 分钟）
     * @return
     */
    public static Long dateDiff(String startTime,String endTime,String diffType) throws ParseException {
        long nd = 1000 * 24 * 60 * 60; //一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数据
        long nm = 1000 * 60; //一分钟毫秒数
        long ns = 1000;
        long diff;
        //获得两个时间的毫秒时间差异
        diff = df.parse(endTime).getTime() - df.parse(startTime).getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        if("D".equals(diffType)){
            return day;
        }else if("H".equals(diffType)){
            return hour;
        }else if("M".equals(diffType)){
            return min;
        }else if("S".equals(diffType)){
            return sec;
        }else{
            return 0l;
        }
    }

    /**
     * double  保留一位小数
     * @param number
     * @return
     */
    public static double doubleSave1(double number){
        BigDecimal bd = BigDecimal.valueOf(number);
        bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);
        double roundedNumber = bd.doubleValue();
        return roundedNumber;
    }

    /**
     * 获取二维码内容，16进制转十进制
     * @param hexStr
     * @return
     */
    public static String hexToString(String hexStr){
        String[] split = hexStr.replace("|", ",").split(",");
        Long dataStr = Long.valueOf(split[0], 16);
        BigInteger bigint=new BigInteger(split[1], 16);
        int series=bigint.intValue();
        BigInteger bigint2=new BigInteger(split[2], 16);
        int rebateInteger=bigint2.intValue();
        BigInteger bigint3=new BigInteger(split[3], 16);
        int checkStr=bigint.intValue();
        return dataStr+"|"+series+"|"+rebateInteger+"|"+checkStr;
    }

    private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    public static List list=new ArrayList();
    public static String generateRandomCode(int CODE_LENGTH) {
        StringBuilder sb = new StringBuilder(CODE_LENGTH);
        Random random = new Random();
        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }
        if(list.contains(sb.toString())){
            return generateRandomCode(6);
        }else{
            list.add(sb.toString());
            return sb.toString();
        }
    }

    /**
     * 随机生成6位不重复的字符串
     * @return
     */
    public static String getlinkNo() {
        String linkNo = "";
        // 用字符数组的方式随机
        String model = "0123ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] m = model.toCharArray();
        for (int j = 0; j < 6; j++) {
            char c = m[(int) (Math.random() * 30)];
            // 保证六位随机数之间没有重复的
            if (linkNo.contains(String.valueOf(c))) {
                j--;
                continue;
            }
            linkNo = linkNo + c;
        }
        return linkNo;
    }

    public static void main(String[] args) {
        System.out.println(getCurrentDateYMD());
    }
    public static String ClobToString(Object  clob) {
        String reString = "";
        try{
            if(clob instanceof CLOB){
                reString = ((CLOB)clob).getSubString((long) 1, (int) ((CLOB)clob).length());
            }else if(clob instanceof Blob){
                reString = clob.toString();
            } else if (clob instanceof String) {
                reString = clob.toString();
            }
        }catch (Exception e){
            reString="";
        }
        return reString;
    }
}


