package flow;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Properties;

import org.apache.log4j.Logger;

public class InterfaceSql {
	private static final boolean List = false;
	private static Logger log = Logger.getLogger(InterfaceSql.class);
	
	public void inset(Interfacee oppo,com.avaya.sce.runtimecommon.SCESession mySession){
		
		log.info("插入数据库准备中 sql是："+oppo.toString());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ResultSet rs=null;
		Connection conn = null;
		PreparedStatement pre =null;
		try {
			String type = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TYPE).getStringValue();
			String driver = null;
			String username = null;
			String password = null;
			String url = null;
			// 若安装多个实例时，要确认相应的port及实例名
			if(type.equals("0")){
				log.debug("-----当前数据库为测试库-----");
				driver = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_DRIVER_2).getStringValue();
				Class.forName(driver);
				username = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_USER_2).getStringValue();
				password = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_PASSWORD_2).getStringValue();
				url = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_URL_2).getStringValue();
			}else {
				log.debug("-----当前数据库为正式库-----");
				driver = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_DRIVER_1).getStringValue();
				Class.forName(driver);
				username = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_USER_1).getStringValue();
				password = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_PASSWORD_1).getStringValue();
				url = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_URL_1).getStringValue();
			}
			
			System.out.println(url + "+url+" + username + "+username+"
					+ password + "+password+" + driver + "+driver");

			conn = null;
			Class.forName(driver);
			//System.out.println(mySession.getVariableField("创建数据库连接成功");
			log.info("创建数据库连接成功");
			log.debug("创建数据库连接成功");
			conn = DriverManager.getConnection(url, username, password);
			log.info("创建数据库连接成功----并成功登入登入账户为"+username);
			log.debug("创建数据库连接成功----并成功登入登入账户为"+username);
			pre= conn.prepareStatement("INSERT INTO T_IVR_TRADE_LOG  (BEGIN_TIME,END_TIME, ANI, DNIS, TRADE_CODE, RESPONSE_CODE, RESPONSE_MESSAGE, SPEND_TIME ,ARG1,ARG2,CALLID,ID) VALUES(?,?, ?, ?, ?, ?, ?, ?,?,?,?,newid())");
			pre.setString(1,oppo.getBEGIN_TIME());
			pre.setString(2,oppo.getEND_TIME());
/*			pre.setDate(1,(Date) df.parse(oppo.getBEGIN_TIME()));
			pre.setDate(2,(Date) df.parse(oppo.getEND_TIME()));
*/			pre.setString(3, oppo.getANI());
			pre.setString(4, oppo.getDNIS());
			pre.setString(5, oppo.getTRADE_CODE());
			pre.setString(6, oppo.getRESPONSE_CODE());
			pre.setString(7, oppo.getRESPONSE_MESSAGE());
			pre.setInt(8, oppo.getSPEND_TIME());
			pre.setString(9, oppo.getARG1());
			pre.setString(10, oppo.getARG2());
			String[] split = oppo.getBEGIN_TIME().split(" ");
			pre.setString(11, oppo.getCALLID());
			
			pre.executeUpdate();
           		
			log.info("获取插入成功正常");
			
		} catch (Exception e) {
			System.out.println("数据库连接这里出现问题："+e.getMessage());
			log.info("数据库连接这里出现问题："+e.getMessage());
			log.debug("数据库连接这里出现问题："+e.getMessage());

		}finally{
			try {
				if(pre!=null){
					pre.close();}
				if(conn!=null){
					conn.close();}
				//if(in!=null){
					//in.close();}
			} catch (Exception e) {
			
				System.out.println(e.getMessage());
				System.out.println(e.getMessage());
			}
		}


	}
}
