package com.steering.pss.ydm.pipemanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.ydm.entity.*;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;

public class FrmIpMonitor extends DBCommon{

	public CoreReturnObject getIpMonitor(ArrayList list)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmIpMonitor.getLoadIpMonitor");
		if(list.size()!=0) {
			if (!(list.get(0) == null || list.get(0).toString().isEmpty())) {
				sql = sql + " and USERID= '" + list.get(0) + "'";
			}
			if (!(list.get(1) == null || list.get(1).toString().isEmpty())) {
				sql = sql + " and username= '" + list.get(1) + "'";
			}
			if (!(list.get(2) == null || list.get(2).toString().isEmpty())) {
				sql = sql + " and userip= '" + list.get(2) + "'";
			}
		}
		//sql += CoreConverter.toSqlConditionString("T1.SALE_AREA_NO", arr);
		 return this.getData(sql);
	}
	public CoreReturnObject DeleteLoadIpMonitor(ArrayList<String> list) {

		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();


		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			for (int i = 0; i < list.size(); i++) {
				LoadipmonitorEntity LoadipmonitorEntity = EntityTool.getEntityByJSONStr(list.get(i),LoadipmonitorEntity.class);


				String sqlUpdateDefault = CoreXmlSqlParsersFactory
						.getSql("FrmIpMonitor.DeleteLoadIpMonitor");
				sqlUpdateDefault = EntityTool.fillSqlByEntity(sqlUpdateDefault, LoadipmonitorEntity);
				PreparedStatement patUpdateDefault = connection.prepareStatement(sqlUpdateDefault);
				patUpdateDefault.addBatch();
				patUpdateDefault.executeBatch();
			}

			core.setV_errMsg("删除成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;



	}
	public CoreReturnObject AddDefault(ArrayList<String> list) {

		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();


		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			for (int i = 0; i < list.size(); i++) {
				LoadipmonitorEntity LoadipmonitorEntity = EntityTool.getEntityByJSONStr(list.get(i),LoadipmonitorEntity.class);


				String sqlUpdateDefault = CoreXmlSqlParsersFactory
						.getSql("FrmIpMonitor.AddLoadIpMonitor");
				sqlUpdateDefault = EntityTool.fillSqlByEntity(sqlUpdateDefault, LoadipmonitorEntity);
				PreparedStatement patUpdateDefault = connection.prepareStatement(sqlUpdateDefault);
				patUpdateDefault.addBatch();
				patUpdateDefault.executeBatch();
			}
			core.setV_errMsg("新增成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;


	}
	public CoreReturnObject getLoadRecord(ArrayList list)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmIpMonitor.getLoadRecord");

			sql= sql+  "and  t1.ldatetime  >  to_date('" + list.get(0)+"', 'yyyy-MM-dd hh24:mi:ss')"
					 +  " and  t1.ldatetime <   to_date('"+ list.get(1)+"', 'yyyy-MM-dd hh24:mi:ss')  " +
					"   order by    ldatetime desc " ;

		//sql += CoreConverter.toSqlConditionString("T1.SALE_AREA_NO", arr);
		return this.getData(sql);
	}

}
