package com.steerinfo.ems.Utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import com.steerinfo.ems.usagedata.model.UsageData;

public class UsageDataUtils {
	
	// JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://172.16.10.252:3306/gov_data_uplod?useSSL=false";
    // 数据库的用户名与密码
    static final String USER = "root";
    static final String PASS = "!QAZ2wsx";
	
	/**
	 * 数据同步_晋泰信和
	 * 
	 * @param usageData
	 * @return 插入数据条数（1：表示成功，其他失败）
	 */
    public static int Insert(UsageData usageData) {
		if (usageData == null || usageData.getId() == null || usageData.getDataCode() == null
				|| usageData.getDt() == null || usageData.getUsage() == null || usageData.getCt() == null) {
			return -2;
		}
    	Connection conn = null;
        Statement stmt = null;
        try{
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            stmt = conn.createStatement();
            String id = usageData.getId();
            String dataCode = usageData.getDataCode();
            String dt = usageData.getDt();
            Double usage = usageData.getUsage().doubleValue();
            String ct = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(usageData.getCt());
            String sql = "INSERT INTO `usage_data` VALUES ("+id+", '"+dataCode+"', '"+dt+"', "+usage+", '"+ct+"', 0);";
        	return stmt.executeUpdate(sql);
        }catch(Exception e){
            e.printStackTrace();
            return -1;
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
    	int asd = Insert(null);
		System.out.println(asd);
	}
}
