package com.sys.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import jxl.common.Logger;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

public class Tool {
	static Logger  log = Logger.getLogger(Tool.class);
	public static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	//public static String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
	public static JSONObject getRequestObject(HttpServletRequest request) throws Exception {
        StringBuilder json = new StringBuilder();
        BufferedReader reader =request.getReader();
        String line = null;
        while((line = reader.readLine()) != null){
            json.append(line);
        }
        reader.close();
        return JSONObject.parseObject(json.toString());
    }
	static{
		dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
	}
	
	public static String uuid(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	//判断是否为数字，包含浮点
	public static boolean isNumber(String str){  
	        String reg = "^[0-9]+(.[0-9]+)?$";  
	        return str.matches(reg);  
	} 
	
	public static boolean isPureDigital(String string) {
        return string.matches("[0-9]+"); 
    }
	
	public static String getCurrTime(){
		return dateTimeFormat.format(new Date());
	}
	public static String getCurrDate(){
		return dateFormat.format(new Date());
	}
	/**
	 * 当前日期+1秒
	 * @return
	 */
	public static String getCurrTime2(){
		Calendar c = Calendar.getInstance();
		c.add(Calendar.SECOND, 1);
		return dateTimeFormat.format(c.getTime());
	}
	public static Date formatTime(String format,String time) {
	    SimpleDateFormat sdf = new SimpleDateFormat(format);  
	    try {
			return  sdf.parse(time);
		} catch (ParseException e) {
			log.error("时间转换异常",e);
		}  
	    return null;
	}
	public static boolean isOverlap(String start1, String end1,String start2, String end2) {    
        Date s1 = null;    
        Date e1 = null;    
        Date s2 = null;    
        Date e2 = null;    
        try {    
            s1 = dateTimeFormat.parse(start1);    
            e1 = dateTimeFormat.parse(end1);    
            s2 = dateTimeFormat.parse(start2);    
            e2 = dateTimeFormat.parse(end2);    
        } catch (ParseException e) { 
        	log.error("时间转换异常",e);
            return false;    
        }    
        if(e2.getTime() > s1.getTime() &&s2.getTime()<e1.getTime()) {
        		return true;
        }else{
        		return false;	
        }
                
    }

	/**
	 * 除小数点后面的  0
	 * @param value
	 * @return
	 */
	public static BigDecimal getStripZeros(BigDecimal value){
		if(value==null){
			return null;
		}
		int scale = value.scale();
		if(scale==0){
			return value;
		}
		String str = value.toString();
		int scale_ = scale;
		char[] chars = str.toCharArray();
		for (int i = chars.length-1; i >=0; i--) {
			if(chars[i]=='0'){
				scale_--;
			}else{
				break;
			}
		}
		value = value.setScale(scale_);
		return value;
	}
	public static String newID(){
		return UUID.randomUUID().toString().replace("-","");
	}

	public static String getDeptName(String deptId){
		String sql = "select * from ct_jx_ratingdept where ratingdept_id = ?";
		return Db.findFirst(sql,deptId).getStr("ratingdept_name");
	}
	public static String getDeptdesc (String deptId){
		String sql = "select * from ct_jx_ratingdept where ratingdept_id = ?";
		return Db.findFirst(sql,deptId).getStr("ratingdept_desc");
	}
}
