package com.steering.pss.judge.DAL;

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

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

import com.steering.comm.db.DBCommon;

public class QcmJyEquipment extends DBCommon {

	
	/**
	 * 查询检测报告设备表
	 * @return
	 */
	public CoreReturnObject FindEquipmentInfo(ArrayList<Object> params){
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyEquipment.FindEquipmentInfo");
		if(params.size()!=0){
			if (!params.get(0).equals("")) {
				sql += " and items = '" + params.get(0) + "'";
			}else if(!params.get(1).equals("")){
				sql += "and result = '"+ params.get(1)+"'";
			}
		}
		
		return this.getData(sql);
	}
	
	/**
	 * 
	 * @param params
	 * @return
	 */
	public CoreReturnObject FindEquipmentInResult(String params){
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyEquipment.FindEquipmentInResult");
		if(params!=null){
				sql += "and items = '"+ params +"'";
			
		}
		
		return this.getData(sql);
	}
	/**
	 * 增加
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject AddEquipment(ArrayList<Object> pram){
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		String sql = "";
		ResultSet rs = null;
		String acount = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("QcmJyEquipment.AddEquipment");
			PreparedStatement pstaaddsql = connection.prepareStatement(addsql);

			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);
				String ITEMS = list.get(0).toString();
				String RESULT = list.get(1).toString();
				String CREATE_NAME = list.get(2).toString();
				sql = CoreXmlSqlParsersFactory
						.getSql("QcmJyEquipment.selectEquipmentInfobyitems"); // 根据 设备名称  和 设备型号查询是否已有数据
				psta = connection.prepareStatement(sql);
				psta.setString(1, ITEMS);
				psta.setString(2, RESULT);
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = rs.getString("acount");
				}
				rs.close();
				psta.close();
				if (acount.equals("1")) {
					cro.setV_errMsg("设备型号"+RESULT+"已有，请勿重复增加!");
					return cro;
				}

				pstaaddsql.setString(1, ITEMS);
				pstaaddsql.setString(2, RESULT);
				pstaaddsql.setString(3, CREATE_NAME);
				pstaaddsql.addBatch();
			}
			pstaaddsql.executeBatch();
			cro.setV_errMsg("新增成功！");
			connection.commit();
		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			cro.setV_errCode(-1);
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}


	//修改
	@SuppressWarnings("unchecked")
	public CoreReturnObject UpdateEquipment(ArrayList<Object> pram){
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement pstas = null;
		String sql ="";
		ResultSet rs = null;
		String acount = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("QcmJyEquipment.UpdateEquipment");
			pstas = connection.prepareStatement(addsql);
			
			
			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);
				
				sql = CoreXmlSqlParsersFactory
						.getSql("QcmJyEquipment.selectEquipmentInfobyitems"); // 根据 设备名称  和 设备型号查询是否已有数据
				
				PreparedStatement psta = connection.prepareStatement(sql);
				psta.setString(1, list.get(0).toString());
				psta.setString(2, list.get(1).toString());
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = rs.getString("acount");
				}
//				rs.close();
//				psta.close();
				if (acount.equals("1")) {
					cro.setV_errMsg("设备型号"+list.get(1).toString()+"已存在，暂且不能修改!");
					return cro;
				}
				
				pstas.setString(1, list.get(0).toString());
				pstas.setString(2, list.get(1).toString());
				pstas.setString(3, list.get(2).toString());
				pstas.setString(4, list.get(3).toString());
				pstas.addBatch();
			}
			pstas.executeBatch();
			cro.setV_errMsg("修改成功！");
			connection.commit();

		} catch (Exception e) {
			cro.setV_errMsg(e.getMessage());
			cro.setV_errCode(-1);
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
		
	}
	
	/**
	 * 删除检验报表设备信息
	 * @param pram
	 * @return
	 */
	public CoreReturnObject deleteEquipment(ArrayList<ArrayList<String>> pram) {
		ArrayList<String> sqllist = new ArrayList<String>();
		ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
		for (int i = 0; i < pram.size(); i++) {
			sqllist.add(CoreXmlSqlParsersFactory
					.getSql("QcmJyEquipment.deleteEquipment"));
			parmlist.add(pram.get(i).toArray());
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
	}
	
	
	
	
}
