package com.steering.mes.signature;

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 FrmReportManage extends DBCommon {

	/**
	 * 查询
	 * 
	 * @param plineNo
	 * @param name
	 * @return
	 */
	public CoreReturnObject queryReportManage(String plineNo, String name) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmReportManage.queryReportManage");
		// sql += CoreConverter.toSqlConditionString("PLINE_CODE", plineNo);
		if (!name.equals("admin")) {
			sql += " AND PLINE_CODE='" + plineNo + "'";
		}
		sql += " ORDER BY PLINE_CODE,REPORT_NAME";
		return this.getData(sql);
	}

	/**
	 * 新增轧管报表文件管理
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject addReportManage(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("FrmReportManage.addReportManage");
			PreparedStatement pstaaddsql = connection.prepareStatement(addsql);

			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);
				String plineCode = list.get(0).toString();
				String reportNo = list.get(1).toString();
				String reportNoNew = list.get(2).toString();
				String reportNoTime = list.get(3).toString();
				String reportName = list.get(4).toString();
				String remark = list.get(5).toString();
				String colName = list.get(6).toString();

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmReportManage.selectPcodeReportName"); // 根据产线、报表名称查询是否已有数据
				psta = connection.prepareStatement(sql);
				psta.setString(1, plineCode);
				psta.setString(2, reportName);
				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, plineCode);
				pstaaddsql.setString(2, reportNo);
				pstaaddsql.setString(3, reportNoNew);
				pstaaddsql.setString(4, reportNoTime);
				pstaaddsql.setString(5, reportName);
				pstaaddsql.setString(6, colName);
				pstaaddsql.setString(7, remark);
				pstaaddsql.setString(8, list.get(7).toString());
				pstaaddsql.setString(9, list.get(8).toString());
				pstaaddsql.setString(10, list.get(9).toString());
				pstaaddsql.setString(11, list.get(10).toString());
				pstaaddsql.setString(12, list.get(11).toString());
				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;
	}

	/**
	 * 删除
	 * 
	 * @param pram
	 * @return
	 */
	public CoreReturnObject deleteReportManage(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("FrmReportManage.deleteReportManage"));
			parmlist.add(pram.get(i).toArray());
		}
		return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
				sqllist, parmlist); // 执行事务处理（多个非查询， 自动提交、回滚）
	}

	/**
	 * 修改
	 * 
	 * @param pram
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject updateReportManage(ArrayList<Object> pram) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		try {
			// connection = ds.getConnection();
			connection.setAutoCommit(false); // 取消自动提交事务
			String addsql = CoreXmlSqlParsersFactory
					.getSql("FrmReportManage.updateReportManage");
			psta = connection.prepareStatement(addsql);

			for (int j = 0; j < pram.size(); j++) {
				ArrayList<Object> list = (ArrayList<Object>) pram.get(j);

				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());
				psta.setString(7, list.get(6).toString());
				psta.setString(8, list.get(7).toString());
				psta.setString(9, list.get(8).toString());
				psta.setString(10, list.get(9).toString());
				psta.setString(11, list.get(10).toString());
				psta.setString(12, list.get(11).toString());
				psta.addBatch();
			}
			psta.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;
	}

}
