package com.steering.mes.zgmil.coup;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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.common.EntityTool;
import com.steering.mes.zgmil.entity.MatBcMEntity;
import com.steering.mes.zgmil.entity.MilPlanEntity3;
import com.steering.mes.zgmil.entity.MilSlabScrapEntity;

public class FrmQueryScrapBack extends DBCommon {

	/**
	 * 查询
	 * 
	 * @param parm
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryData(ArrayList<String> parm,
			String startTim, String endTim, String flag) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmQueryScrapBack.doQueryData");
		if (flag.equals("0")) {
			sql += " AND T.FLAG = '0'";
		} else if (flag.equals("1")) {
			sql += " AND T.FLAG = '1'";
		}
		if (!startTim.equals("")) {
			sql += " AND TIME >= to_date('" + startTim
					+ "','YYYY-MM-DD HH24:MI:SS') AND TIME <= to_date('"
					+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		return this.getData(sql, parm.toArray());
	}

	/**
	 * 确认废
	 * 
	 * @param parm
	 * @param parm2
	 * @return
	 */
	public CoreReturnObject doScrap(ArrayList<String> parm,
			ArrayList<String> parm2) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmQueryScrapBack.updateResult"); // 修改管坯剔除表状态
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			String updateSql2 = CoreXmlSqlParsersFactory
					.getSql("FrmQueryScrapBack.updatePort"); // 修改进程表
			PreparedStatement updatePsta2 = connection
					.prepareStatement(updateSql2);

			for (String str : parm) {
				MilSlabScrapEntity milSlabTity = EntityTool.getEntityByJSONStr(
						str, MilSlabScrapEntity.class);
				updatePsta.setString(1, parm2.get(2));
				updatePsta.setString(2, parm2.get(1));
				updatePsta.setString(3, parm2.get(0));
				updatePsta.setString(4, "500201");
				updatePsta.setString(5, systime);
				updatePsta.setString(6, "1");
				updatePsta.setString(7, milSlabTity.getJudgeStoveNo());
				updatePsta.setString(8, milSlabTity.getMatNo());
				updatePsta.addBatch();

				updatePsta2.setString(1, "500201");
				updatePsta2.setString(2, milSlabTity.getJudgeStoveNo());
				updatePsta2.setString(3, milSlabTity.getMatNo());
				updatePsta2.setString(4, milSlabTity.getMatNo());
				updatePsta2.addBatch();
			}
			updatePsta.executeBatch();
			updatePsta2.executeBatch();
			connection.commit();
			cro.setV_errMsg("确认成功!");
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 撤销确认废品
	 * 
	 * @param parm
	 * @param parm2
	 * @return
	 */
	public CoreReturnObject doScrapBack(ArrayList<String> parm,
			ArrayList<String> parm2) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmQueryScrapBack.updateResult"); // 修改管坯剔除表状态
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			String updateSql2 = CoreXmlSqlParsersFactory
					.getSql("FrmQueryScrapBack.updatePort"); // 修改进程表
			PreparedStatement updatePsta2 = connection
					.prepareStatement(updateSql2);

			for (String str : parm) {
				MilSlabScrapEntity milSlabTity = EntityTool.getEntityByJSONStr(
						str, MilSlabScrapEntity.class);
				updatePsta.setString(1, "");
				updatePsta.setString(2, "");
				updatePsta.setString(3, "");
				updatePsta.setString(4, "0");
				updatePsta.setString(5, systime);
				updatePsta.setString(6, "0");
				updatePsta.setString(7, milSlabTity.getJudgeStoveNo());
				updatePsta.setString(8, milSlabTity.getMatNo());
				updatePsta.addBatch();

				updatePsta2.setString(1, "");
				updatePsta2.setString(2, milSlabTity.getJudgeStoveNo());
				updatePsta2.setString(3, milSlabTity.getMatNo());
				updatePsta2.setString(4, milSlabTity.getMatNo());
				updatePsta2.addBatch();
			}
			updatePsta.executeBatch();
			updatePsta2.executeBatch();
			connection.commit();
			cro.setV_errMsg("撤销成功!");
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 返库
	 * 
	 * @param parm
	 * @param parm2
	 * @return
	 */
	public CoreReturnObject retrunWarehouse(ArrayList<String> parm,
			ArrayList<String> parm2, ArrayList<ArrayList<String>> parmBack) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		String sql = "";
		int acount = 0;
		ResultSet rs = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateYearMat = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String bathYear = dateYearMat.format(now);
		String wgt = "";// 重量
		String matNo = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmQueryRidResult.updateResult"); // 修改管坯剔除表状态
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			String updateSql2 = CoreXmlSqlParsersFactory
					.getSql("FrmQueryScrapBack.updatePort"); // 修改进程表
			PreparedStatement updatePsta2 = connection
					.prepareStatement(updateSql2);

			String sql1 = CoreXmlSqlParsersFactory
					.getSql("FrmQueryRidResult.returnLibrary"); // 返库
			PreparedStatement returnLibrary = connection.prepareStatement(sql1);

			ArrayList<String> sqllist = new ArrayList<String>();
			for (String str : parm) {
				MilSlabScrapEntity milSlabTity = EntityTool.getEntityByJSONStr(
						str, MilSlabScrapEntity.class);
				updatePsta.setString(1, parm2.get(2));
				updatePsta.setString(2, parm2.get(1));
				updatePsta.setString(3, parm2.get(0));
				updatePsta.setString(4, "500202");
				updatePsta.setString(5, systime);
				updatePsta.setString(6, "1");
				updatePsta.setString(7, milSlabTity.getJudgeStoveNo());
				updatePsta.setString(8, milSlabTity.getMatNo());
				updatePsta.addBatch();

				updatePsta2.setString(1, "500202");
				updatePsta2.setString(2, milSlabTity.getJudgeStoveNo());
				updatePsta2.setString(3, milSlabTity.getMatNo());
				updatePsta2.setString(4, milSlabTity.getMatNo());
				updatePsta2.addBatch();
			}
			// 返库
			for (ArrayList<String> arrayBack : parmBack) {
				for (String str : arrayBack) {
					MilSlabScrapEntity milSlabTity = EntityTool
							.getEntityByJSONStr(parm.get(1),
									MilSlabScrapEntity.class);
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmQueryRidResult.SelectAcount");// 查询支数
					psta = connection.prepareStatement(sql);
					psta.setString(1, milSlabTity.getJudgeStoveNo());
					psta.setString(2, milSlabTity.getProPlanId());
					psta.setString(3, milSlabTity.getGxPlanNo());
					rs = psta.executeQuery();
					if (rs.next()) {
						acount = Integer.parseInt(rs.getString("ACOUNT"));
					}
					rs.close();
					psta.close();
					if (acount == 0) {
						cro.setV_errMsg("返库失败！");
						return cro;
					}

					returnLibrary.setString(1, milSlabTity.getJudgeStoveNo());
					returnLibrary.setString(2, milSlabTity.getProPlanId());
					returnLibrary.setString(3, milSlabTity.getGxPlanNo());
					returnLibrary.setString(4, parm2.get(3));
					returnLibrary.setString(5, parm2.get(0));
					returnLibrary.setString(6, milSlabTity.getMatNo());
					returnLibrary.addBatch();
				}

			}
			updatePsta.executeBatch();
			updatePsta2.executeBatch();
			returnLibrary.executeBatch();
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			if (cro.getV_errMsg().equals("")) {
				connection.commit();
				cro.setV_errMsg("返库成功!");
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 撤销返库
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject retrunWarehouseBack(ArrayList<String> parm,
			ArrayList<String> parm2) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		String sql = "";
		int acount = 0;
		ResultSet rs = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmQueryRidResult.updateResult"); // 修改管坯剔除表状态
			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			String updateSql2 = CoreXmlSqlParsersFactory
					.getSql("FrmQueryScrapBack.updatePort"); // 修改进程表
			PreparedStatement updatePsta2 = connection
					.prepareStatement(updateSql2);

			String sql1 = CoreXmlSqlParsersFactory
					.getSql("FrmQueryRidResult.returnLibraryBack"); // 撤销返库
			PreparedStatement returnLibraryBack = connection
					.prepareStatement(sql1);

			for (String str : parm) {
				MilSlabScrapEntity milSlabTity = EntityTool.getEntityByJSONStr(
						str, MilSlabScrapEntity.class);

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmQueryRidResult.SelectIsFC");// 验证是否是返库的炉号
				psta = connection.prepareStatement(sql);
				psta.setString(1, milSlabTity.getJudgeStoveNo());
				psta.setString(2, milSlabTity.getProPlanId());
				psta.setString(3, milSlabTity.getGxPlanNo());
				psta.setString(4, milSlabTity.getMatNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (acount == 0) {
					cro.setV_errMsg("选择的炉号：【" + milSlabTity.getJudgeStoveNo()
							+ "】没有返库，不能撤销！");
					return cro;
				}

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmQueryRidResult.SelectIsNoChang");// 验证返库的炉号是否还在库里或者用作其它
				psta = connection.prepareStatement(sql);
				psta.setString(1, milSlabTity.getNewMatNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (acount == 0) {
					cro.setV_errMsg("返库管坯已经用掉了，不能撤销！");
					return cro;
				}

				updatePsta.setString(1, "");
				updatePsta.setString(2, "");
				updatePsta.setString(3, "");
				updatePsta.setString(4, "0");
				updatePsta.setString(5, systime);
				updatePsta.setString(6, "0");
				updatePsta.setString(7, milSlabTity.getJudgeStoveNo());
				updatePsta.setString(8, milSlabTity.getMatNo());
				updatePsta.addBatch();

				updatePsta2.setString(1, "");
				updatePsta2.setString(2, milSlabTity.getJudgeStoveNo());
				updatePsta2.setString(3, milSlabTity.getMatNo());
				updatePsta2.setString(4, milSlabTity.getMatNo());
				updatePsta2.addBatch();

				returnLibraryBack.setObject(1, milSlabTity.getNewMatNo());
				returnLibraryBack.addBatch();
			}
			updatePsta.executeBatch();
			updatePsta2.executeBatch();
			returnLibraryBack.executeBatch();

			connection.commit();
			cro.setV_errMsg("撤销返库成功!");
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
