package com.steering.mes.signature;

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

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;

/**
 * 库里剔废
 * 
 * @author Administrator
 * 
 */
public class FrmStorageScrap extends DBCommon {

	/**
	 * 查询库存信息
	 * 
	 * @param jugeNo
	 * @param process
	 * @return
	 */
	public CoreReturnObject queryMat(String jugeNo, String process) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmStorageScrap.queryMat");
		return this.getData(sql, new Object[] { jugeNo, process });
	}

	/**
	 * 库里剔废
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject scrape(ArrayList<String> parm, String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		int acount = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String scrapeSql = CoreXmlSqlParsersFactory
					.getSql("FrmStorageScrap.scrape");
			PreparedStatement scrapePsta = connection
					.prepareStatement(scrapeSql);
			for (String str : parm) {
				MatBcMEntity matBcTity = EntityTool.getEntityByJSONStr(str,
						MatBcMEntity.class);
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmStorageScrap.selectByMatNo");// 查库存
				psta = connection.prepareStatement(sql);
				psta.setString(1, matBcTity.getMatNo());
				psta.setString(2, matBcTity.getJudgeStoveNo());
				psta.setString(3, matBcTity.getProcessCdoe());
				psta.setString(4, "22");
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (acount > 0) {
					cro
							.setV_errMsg("材料号：" + matBcTity.getMatNo()
									+ "废品不能重复剔除!");
					return cro;
				}
				scrapePsta.setString(1, matBcTity.getMatNo());
				scrapePsta.setString(2, matBcTity.getJudgeStoveNo());
				scrapePsta.setString(3, matBcTity.getProcessCdoe());
				scrapePsta.setString(4, userName);
				scrapePsta.setString(5, matBcTity.getScrapReason());
				scrapePsta.addBatch();
			}
			scrapePsta.executeBatch();
			cro.setV_errMsg("剔废成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/**
	 * 撤销剔废
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject scrapeBack(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		int acount = 0;
		String SCRAP_FRIM_FLAG = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String scrapeSql = CoreXmlSqlParsersFactory
					.getSql("FrmStorageScrap.scrapeBack");
			PreparedStatement scrapePsta = connection
					.prepareStatement(scrapeSql);
			for (String str : parm) {
				MatBcMEntity matBcTity = EntityTool.getEntityByJSONStr(str,
						MatBcMEntity.class);
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmStorageScrap.selectByMatNo");// 查库存
				psta = connection.prepareStatement(sql);
				psta.setString(1, matBcTity.getMatNo());
				psta.setString(2, matBcTity.getJudgeStoveNo());
				psta.setString(3, matBcTity.getProcessCdoe());
				psta.setString(4, "00");
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (acount > 0) {
					cro.setV_errMsg("材料号：" + matBcTity.getMatNo()
							+ "合格管不用撤销剔废!");
					return cro;
				}

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmStorageScrap.selectByMatNo2");// 查库存信息
				psta = connection.prepareStatement(sql);
				psta.setString(1, matBcTity.getMatNo());
				psta.setString(2, matBcTity.getJudgeStoveNo());
				psta.setString(3, matBcTity.getProcessCdoe());
				rs = psta.executeQuery();
				if (rs.next()) {
					SCRAP_FRIM_FLAG = rs.getString("SCRAP_FRIM_FLAG");
				}
				rs.close();
				psta.close();
				if (SCRAP_FRIM_FLAG.equals("2") || SCRAP_FRIM_FLAG.equals("1")) {
					cro.setV_errMsg("已审核的废管不能撤销剔废!");
					return cro;
				}
				scrapePsta.setString(1, matBcTity.getMatNo());
				scrapePsta.setString(2, matBcTity.getJudgeStoveNo());
				scrapePsta.setString(3, matBcTity.getProcessCdoe());
				scrapePsta.addBatch();
			}
			scrapePsta.executeBatch();
			cro.setV_errMsg("撤销剔废成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

}
