package com.sa.ibiz.demo.ibizutil.helper;


import org.springframework.util.StringUtils;


public class DataObject {

    /**
     * 转换对象值到时间值
     *
     * @param objValue
     * @return
     * @throws Exception
     */
    final static public java.sql.Timestamp getTimestampValue(Object objValue) throws Exception {
        if (objValue == null) return null;

        if (objValue instanceof java.sql.Timestamp) {
            java.sql.Timestamp ti = (java.sql.Timestamp) objValue;
            return ti;
        }

        if (objValue instanceof java.sql.Date) {
            java.sql.Date date = (java.sql.Date) objValue;
            return new java.sql.Timestamp(date.getTime());
        }

        if (objValue instanceof java.util.Date) {
            java.util.Date date = (java.util.Date) objValue;
            return new java.sql.Timestamp(date.getTime());
        }

//        if (objValue instanceof String) {
//            String strValue = (String) objValue;
//            strValue = strValue.trim();
//            if (StringUtils.isEmpty(strValue)) return null;
//
//            java.util.Date date = DateHelper.parse((String) objValue);
//            return new java.sql.Timestamp(date.getTime());
//        }

        if(objValue instanceof Long)
        {
            Long lValue = (Long)objValue;
            return new  java.sql.Timestamp(lValue);
        }

        if(objValue instanceof Integer)
        {
            int lValue = (int)objValue;
            return new  java.sql.Timestamp(lValue);
        }

        throw new Exception(String.format("无法转换时间[%s]",objValue));
    }

    /**
     * 转换对象值到字符串值
     *
     * @param objValue
     * @return
     * @throws Exception
     */
    final static public String getStringValue(Object objValue) throws Exception {
        return getStringValue(objValue, null);
    }

    /**
     * 转换对象值到二进制值
     *
     * @param objValue
     * @param strDefault
     * @return
     * @throws Exception
     */
    final static public String getStringValue(Object objValue, String strDefault) throws Exception {
        if (objValue == null) return strDefault;
        if (objValue instanceof String) return (String) objValue;
        return objValue.toString();
    }

    /**
     * 转换对象值到Double值
     *
     * @param objValue
     * @return
     * @throws Exception
     */
    final static public Double getDoubleValue(Object objValue) throws Exception {
        if (objValue == null) return null;

        if (objValue instanceof Double) {
            return (Double) objValue;
        }

        String strValue = objValue.toString();
        if (StringUtils.isEmpty(strValue)) return null;
        strValue = strValue.replace(",", "");
        return Double.parseDouble(strValue);
    }

}
