package com.steering.pss.judge.DAL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

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

import com.steering.comm.db.DBCommon;
import com.steering.pss.judge.model.YdmZcOutlistEntity;

public class QcmJyTestReport extends DBCommon {
	
	/**
	 * 查询Items项目列
	 * @return
	 */
	public CoreReturnObject FindItemsList(){
		String sql = CoreXmlSqlParsersFactory.getSql("QcmJyQueryFrm.FindItemsList");
		return this.getData(sql);
	}
	
	/**
	 * 查询检测报告
	 * @return
	 */
	public CoreReturnObject FindTestReport(ArrayList<Object> params){
		String sql = CoreXmlSqlParsersFactory
				.getSql("QcmJyQueryFrm.FindTestReport");
		if (!params.get(0).equals("")) {
			sql += " and ORDER_NO = '" + params.get(0) + "'";
		}
		return this.getData(sql);
	}
	/**
	 * 增加
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject AddTestReport(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 = ds.getConnection();
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("QcmJyTestReport.AddTestReport");
			PreparedStatement pstaaddsql = connection.prepareStatement(addsql);

			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);
				String ORDER_NO = list.get(0).toString();
				String ITEMS = list.get(1).toString();
				String STANDARD = list.get(2).toString();
				String RESULT = list.get(3).toString();
				String INSTRUMENT_MODEL = list.get(4).toString();
				String CREATE_NAME = list.get(5).toString();
				String REPORTED = list.get(6).toString();
				String APPROVED = list.get(7).toString();
				String REMARKS = list.get(8).toString();
				sql = CoreXmlSqlParsersFactory
						.getSql("QcmJyTestReport.selectReportName"); // 根据合同号  项目查询是否已有数据
				psta = connection.prepareStatement(sql);
				psta.setString(1, ORDER_NO);
				psta.setString(2, ITEMS);
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = rs.getString("acount");
				}
				rs.close();
				psta.close();
				if (acount.equals("1")) {
					cro.setV_errMsg("项目名称已有，不能重复增加!");
					return cro;
				}

				pstaaddsql.setString(1, ORDER_NO);
				pstaaddsql.setString(2, ITEMS);
				pstaaddsql.setString(3, STANDARD);
				pstaaddsql.setString(4, RESULT);
				pstaaddsql.setString(5, INSTRUMENT_MODEL);
				pstaaddsql.setString(6, CREATE_NAME);
				pstaaddsql.setString(7, REPORTED);
				pstaaddsql.setString(8, APPROVED);
				pstaaddsql.setString(9, REMARKS);
				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 UpdateTestReport(ArrayList<Object> pram){
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		PreparedStatement psta1 = null;
		PreparedStatement psta2 = null;
		ResultSet rs = null;
		String acount ="";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("QcmJyTestReport.UpdateTestReport");
			psta = connection.prepareStatement(addsql);

			String addsql2 = CoreXmlSqlParsersFactory
					.getSql("QcmJyTestReport.UpdateTestReport2");//新字段
			psta2 = connection.prepareStatement(addsql2);
			
			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);

				String sql = CoreXmlSqlParsersFactory
						.getSql("QcmJyTestReport.selectReportName"); // 根据合同号  项目查询是否已有数据
				psta1 = connection.prepareStatement(sql);
				psta1.setString(1, list.get(4).toString());
				psta1.setString(2, list.get(5).toString());
				rs = psta1.executeQuery();
				if (rs.next()) {
					acount = rs.getString("acount");
				}
				if (acount.equals("0")) {
					cro.setV_errMsg("该项目还未添加，不能修改!");
					return cro;
				}
				
				
				psta.setString(1, list.get(0).toString());
				psta.setString(2, list.get(1).toString());
				psta.setString(3, list.get(2).toString());
				psta.setString(4, list.get(3).toString());
				psta.setString(5, list.get(4).toString());
				psta.setString(6, list.get(5).toString());
				
				psta2.setString(1, list.get(6).toString());
				psta2.setString(2, list.get(7).toString());
				psta2.setString(3, list.get(8).toString());
				psta2.setString(4, list.get(4).toString());//合同号
				psta2.addBatch();
				psta.addBatch();
			}
			psta.executeBatch();
			psta2.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 deleteTestReport(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("QcmJyQueryFrm.deleteTestReport"));
			parmlist.add(pram.get(i).toArray());
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
	}
	
	
	
	
}
