package com.steering.mes.mcp.Mch;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.MchCrackDetectionEntity;
import com.steering.mes.mcp.entity.MchCutResultEntity;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgxMEntity;
import com.steering.mes.mcp.entity.PortMchBatchSampleResultEntity;

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 java.util.Locale;

/**
 * 分切实绩
 * 
 * @author SZL
 *
 */
public class FrmCutResult extends DBCommon {
	private String perStuts = "";

	/**
	 * 查询分切实绩
	 */
	public CoreReturnObject getQueryExpandResult(String heatNo, String BatchNo) {
		return this.getDataBySqlID("FrmCutResult.getQueryExpandResult",
				new Object[] { heatNo,BatchNo});
	}

	/**
	 * 查询相同炉计划ID下分切实绩状态
	 */
	public CoreReturnObject getQueryExpandResultStuts(String heatNo,
			String plinCode) {
//		String sql = CoreXmlSqlParsersFactory.getSql("FrmCutResult.getQueryExpandResultStuts");
//		sql += " AND T.HEAT_PLAN_NO='" + heatNo+"' ";
//		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCode);
//		return this.getData(sql);
		return this.getDataBySqlID("FrmCutResult.getQueryExpandResultStuts",
				new Object[] { heatNo, plinCode });
	}

	/**
	 * 查询管号队列不合格的支数
	 */
	public CoreReturnObject getQuerynum(String heatNo, String plineCode,
			String groupno) {
		return this.getDataBySqlID("FrmCutResult.queryFailNUM", new Object[] {
				heatNo, plineCode, groupno });
	}

	/**
	 * 查询实绩里不同分切数的支数
	 */
	public CoreReturnObject getQueryNum(String heatNo, String outnumcut) {
		return this.getDataBySqlID("FrmCutResult.getquerynum", new Object[] {
				heatNo, outnumcut });
	}

	/**
	 * 查询分切实绩合格支数的和
	 * 
	 * @param heatNo
	 * @return
	 */
	public CoreReturnObject getQueryExpandResult1(String heatNo) {
		return this.getDataBySqlID("FrmCutResult.getQueryExpandResult1",
				new Object[] { heatNo });
	}



	public CoreReturnObject DoAdd(String loadStr, String gx,ArrayList<String> ScrappedList, ArrayList<String> SuspiciousList){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false); //



			String sqlupdatepln = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.updatePlnZyRclM2");// 修改加工计划表
			PreparedStatement patupdatepln = connection
					.prepareStatement(sqlupdatepln);

			String addPlnInfo = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.AddComInfoResultNew");// 新增公共信息
			PreparedStatement patsPlan = connection
					.prepareStatement(addPlnInfo);


			String MarkPMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.MarkPortMch1");// 标记无效
			PreparedStatement patsMarkPMatNo = connection
					.prepareStatement(MarkPMatNo);


			String AddPortMch = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.AddPortMch");// 增加子管队列
			PreparedStatement patsAddPortMch = connection
					.prepareStatement(AddPortMch);


			String AddPortMch2 = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.AddPortMch2");// 增加子管队列
			PreparedStatement patsAddPortMch2 = connection
					.prepareStatement(AddPortMch2);

			MchCutResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchCutResultEntity.class);


			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			String resultNo = "";
			int count = 0;
			int detCount = 0;


			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}
			if(StringTool.isInteger(Load.getProNum())){
				count = Integer.parseInt(Load.getProNum());
			}


			if(count<=0){
				core.setV_errMsg("请输入正确的支数！");
				return core;
			}

			if(count * Integer.parseInt(pln.getOutnumCut())<detCount){
				core.setV_errMsg("废品支数不能大于支数！");
				return core;
			}


			//获取最大实绩编号
			sql =   CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.queryResultNo");
			psta = connection.prepareStatement(sql);
			psta.setObject(1, pln.getPlineCode() + gx );
			psta.setObject(2, pln.getPlineCode() + gx );
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();


			Load.setResultNo(resultNo);

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.AddResult");// 新增上料实绩

			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, Load);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();


			// 新增加工公共信息
			patsPlan.setObject(1, resultNo);
			patsPlan.setObject(2, Load.getPlineCode());
			patsPlan.setObject(3, Load.getPlineName());
			patsPlan.setObject(4, gx);
			patsPlan.setObject(5, gx);
			patsPlan.setObject(6, Load.getBatchNo());
			patsPlan.setObject(7, Load.getProNum());
			patsPlan.setObject(8, Load.getProWt());
			patsPlan.setObject(9, Load.getFailNum());
			patsPlan.setObject(10, Load.getFailWt());
			patsPlan.setObject(11, Load.getProTime());
			patsPlan.setObject(12, Load.getProShift());
			patsPlan.setObject(13, Load.getProGroup());
			patsPlan.setObject(14, Load.getProUser());
			patsPlan.setObject(15, Load.getProUser());
			patsPlan.setObject(16, Load.getHeatPlanNo());
			patsPlan.addBatch();


			patupdatepln.setObject(1,"20");
			patupdatepln.setObject(2,Load.getHeatPlanNo());
			patupdatepln.setObject(3,Load.getBatchNo());
			patupdatepln.addBatch();

			patAdd.executeBatch();
			patsPlan.executeBatch();
			patupdatepln.executeBatch();


			String sqlMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.QueryCanCut");// 查询新增物料信息
			psta = connection.prepareStatement(sqlMatNo);
			psta.setObject(1, Load.getHeatPlanNo());
			psta.setObject(2, Load.getBatchNo());
			psta.setObject(3, Load.getProNum());
			rs = psta.executeQuery();
			List<PortMchBatchSampleResultEntity> listMats = EntityTool.getEntitysByRs(rs, PortMchBatchSampleResultEntity.class);
			rs.close();
			psta.close();


			if(listMats.size()!= Integer.parseInt(Load.getProNum())){
				core.setV_errMsg("无足够的可分切的未入库合格管！");
				return core;
			}

			for (PortMchBatchSampleResultEntity MatInfo :listMats
				 ) {
				patsMarkPMatNo.setObject(1,"0");
				patsMarkPMatNo.setObject(2,MatInfo.getHeatPlanNo());
				patsMarkPMatNo.setObject(3,MatInfo.getMatNo());
				patsMarkPMatNo.addBatch();
				patsMarkPMatNo.executeBatch();

				for (int i = 0; i < Integer.parseInt(MatInfo.getOutnumCut()) ; i++) {
					if(i==0){

						patsAddPortMch2.setObject(1,i+1);
						patsAddPortMch2.setObject(2,i+1);
						patsAddPortMch2.setObject(3,MatInfo.getHeatPlanNo());
						patsAddPortMch2.setObject(4,MatInfo.getMatNo());
						patsAddPortMch2.addBatch();
					}else {

						patsAddPortMch.setObject(1, i + 1);
						patsAddPortMch.setObject(2, i + 1);
						patsAddPortMch.setObject(3, MatInfo.getHeatPlanNo());
						patsAddPortMch.setObject(4, MatInfo.getMatNo());
						patsAddPortMch.addBatch();
					}

				}
				patsAddPortMch2.executeBatch();
				patsAddPortMch.executeBatch();
			}

			FrmMchBugD.AddMchWaste(connection,Load.getHeatPlanNo(),resultNo,Load.getPlineCode(),Load.getProUser(),gx,SuspiciousList, ScrappedList);

			core.setV_errMsg("新增成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject DoDelete(String loadStr,String gx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat1 = new SimpleDateFormat(
				"yyyyMM", Locale.CHINA);
		String systime1 = dateFormat1.format(now);
		PreparedStatement psta = null;

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(new Date());

		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			String DeleteComInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.deleteVrpResult");// 删除公共信息
			PreparedStatement patsDeleteComInfo = connection
					.prepareStatement(DeleteComInfoSql);

			String DeleteResultSql = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.DeleteResult");// 删除实绩
			PreparedStatement patsDeleteResult = connection
					.prepareStatement(DeleteResultSql);

			String MarkPMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.MarkPortMch1");// 标记母管有效
			PreparedStatement patsMarkPMatNo = connection
					.prepareStatement(MarkPMatNo);


			String DeletePortMch = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.DeletePortMch");// 删除子管队列
			PreparedStatement patsDeletePortMch = connection
					.prepareStatement(DeletePortMch);

			MchCrackDetectionEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchCrackDetectionEntity.class);

			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo(),Load.getBatchNo());

			if(!pln.getPlineCode().equals(Load.getPlineCode())){
				core.setV_errMsg("不能删除其他产线生成的实绩！");
				return core;
			}


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			patsDeleteComInfo.setObject(1,Load.getResultNo());
			patsDeleteComInfo.addBatch();
			patsDeleteComInfo.executeBatch();



			patsDeleteResult.setObject(1,Load.getResultNo());
			patsDeleteResult.addBatch();
			patsDeleteResult.executeBatch();

			int detCount =0;
			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}

			FrmMchBugD.DeleteMchWaste(connection, Load.getHeatPlanNo(),Load.getResultNo(), Load.getPlineCode(),gx,detCount);



			String sqlMatNo = CoreXmlSqlParsersFactory
					.getSql("FrmCutResult.QueryCanCut2");// 查询新增物料信息
			psta = connection.prepareStatement(sqlMatNo);
			psta.setObject(1, Load.getHeatPlanNo());
			psta.setObject(2, Load.getBatchNo());
			psta.setObject(3, Load.getProNum());
			rs = psta.executeQuery();
			List<PortMchBatchSampleResultEntity> listMats = EntityTool.getEntitysByRs(rs, PortMchBatchSampleResultEntity.class);
			rs.close();
			psta.close();


			if(listMats.size()!= Integer.parseInt(Load.getProNum())){
				core.setV_errMsg("数据异常, 找不到部分原管号的信息！");
				return core;
			}

			for (PortMchBatchSampleResultEntity MatInfo :listMats
					) {
				patsMarkPMatNo.setObject(1,"1");
				patsMarkPMatNo.setObject(2,MatInfo.getHeatPlanNo());
				patsMarkPMatNo.setObject(3,MatInfo.getMatNo());
				patsMarkPMatNo.addBatch();
				patsMarkPMatNo.executeBatch();

				 sqlMatNo = CoreXmlSqlParsersFactory
						.getSql("FrmCutResult.QueryChildPortMch");// 查询新增物料信息
				psta = connection.prepareStatement(sqlMatNo);
				psta.setObject(1, Load.getHeatPlanNo());
				psta.setObject(2, Load.getBatchNo());
				psta.setObject(3, MatInfo.getMatNo());
				rs = psta.executeQuery();
				List<PortMchBatchSampleResultEntity> listChildMats = EntityTool.getEntitysByRs(rs, PortMchBatchSampleResultEntity.class);
				rs.close();
				psta.close();

				for (PortMchBatchSampleResultEntity ChildInfo  :listChildMats
					 ) {
					if(!ChildInfo.getMatStatus().equals("00")){
						core.setV_errMsg("子管不是合格状态,无法撤销分切！");
						return core;
					}

					if(ChildInfo.getInstoreFlag().equals("1")){
						core.setV_errMsg("子管已入库,无法撤销分切！");
						return core;
					}

					patsDeletePortMch.setObject(1,MatInfo.getHeatPlanNo());
					patsDeletePortMch.setObject(2,ChildInfo.getMatNo());
					patsDeletePortMch.addBatch();
				}
				patsDeletePortMch.executeBatch();

			}




			core.setV_errMsg("删除成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}




	/***
	 * 结炉
	 * 
	 * @param zYgNo
	 * @return
	 */
	public CoreReturnObject updateDoBeside(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.doBeside");// 修改工序状态为结炉
			PreparedStatement updateSqlPsta = connection
					.prepareStatement(updateSql);
			String updatePln = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.doPlnBeside");// 修改计划表结炉状态
			PreparedStatement updatePlnPsta = connection
					.prepareStatement(updatePln);
			// int count = 0;
			String cutFalg1 = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.qureyGxComInfo");// 查询工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			psta.setString(2, parm.get(1));
			psta.setObject(3, Integer.parseInt(parm.get(2)) - 1);
			rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg1 = rs.getString("STATION_STATUS");
			}
			if (cutFalg1.equals("1")) {
				cro.setV_errMsg("上一道工序没有结炉,不允许此工序结炉!");
				return cro;
			}
			rs.close();
			psta.close();
			String cutFalg = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.qureyGxComInfo");// 查询工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			psta.setString(2, parm.get(1));
			psta.setString(3, parm.get(2));
			rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			} else {
				cro.setV_errMsg("无当前工序实绩,无法进行结炉!");
				return cro;
			}
			if (cutFalg.equals("2")) {
				cro.setV_errMsg("已结炉,不允许此操作！");
				return cro;
			}
			rs.close();
			psta.close();
			updateSqlPsta.setString(1, parm.get(0));
			updateSqlPsta.setString(2, parm.get(1));
			updateSqlPsta.setString(3, parm.get(2));
			updateSqlPsta.addBatch();

			// 修改计划结炉状态
			updatePlnPsta.setObject(1, "20");
			updatePlnPsta.setObject(2, parm.get(0));
			updatePlnPsta.addBatch();

			updateSqlPsta.executeBatch();
			updatePlnPsta.executeBatch();
			cro.setV_errMsg("结炉成功！");
			connection.commit();
		} 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 zYgNo
	 * @return
	 */
	public CoreReturnObject undoBeside(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.deleteBeside");// 修改工序状态撤销结炉
			PreparedStatement updateSqlPsta = connection
					.prepareStatement(updateSql);
			String updatePln = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.doPlnBeside");// 修改计划表结炉状态
			PreparedStatement updatePlnPsta = connection
					.prepareStatement(updatePln);
			String sqlloadings = CoreXmlSqlParsersFactory
					.getSql("FrmMchInOnlineStore.queryperstu");// 查询信息表除了本工序之外最大的工序
			PreparedStatement pstas = connection.prepareStatement(sqlloadings);
			pstas.setObject(1, parm.get(0));
			pstas.setObject(2, parm.get(2));
			ResultSet rss = pstas.executeQuery();
			if (rss.next()) {
				perStuts = rss.getString("STATION_CODE");
				// if(perStuts.equals("0"))
				// {
				// cro.setV_errMsg("无工序，不允许此操作!");
				// return cro;
				// }else
				if (perStuts.equals("701301")) {
					perStuts = "00";
				} else if (perStuts.equals("701302")) {
					perStuts = "01";
				} else if (perStuts.equals("701303")) {
					perStuts = "02";
				} else if (perStuts.equals("701304")) {
					perStuts = "03";
				} else if (perStuts.equals("701305")) {
					perStuts = "04";
				} else if (perStuts.equals("701306")) {
					perStuts = "05";
				} else if (perStuts.equals("701307")) {
					perStuts = "06";
				} else if (perStuts.equals("701308")) {
					perStuts = "07";
				} else if (perStuts.equals("701309")) {
					perStuts = "08";
				} else if (perStuts.equals("701310")) {
					perStuts = "09";
				} else if (perStuts.equals("701311")) {
					perStuts = "10";
				}
			}
			rss.close();
			pstas.close();

			String cutFalg1 = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.qureyGxComInfo");// 查询工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			psta.setString(2, parm.get(1));
			psta.setObject(3, Integer.parseInt(parm.get(2)) + 1);
			rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg1 = rs.getString("STATION_STATUS");
			}
			if (cutFalg1.equals("2")) {
				cro.setV_errMsg("下一道工序没有撤销结炉,不允许此工序撤销结炉!");
				return cro;
			}
			rs.close();
			psta.close();
			String cutFalg = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.qureyGxComInfo");// 查询工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			psta.setString(2, parm.get(1));
			psta.setString(3, parm.get(2));
			rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			} else {
				cro.setV_errMsg("无当前工序实绩,无法进行结炉!");
				return cro;
			}
			if (cutFalg.equals("1")) {
				cro.setV_errMsg("未结炉,不允许此操作！");
				return cro;
			}
			rs.close();
			psta.close();
			// 判断是否已经交库了
			String status = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.qureyGxOnline");// 查询公共信息表有没有交库工序
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			rs = psta.executeQuery();
			if (rs.next()) {
				status = rs.getString("COUNT");
			}
			if (!status.equals("0")) {
				cro.setV_errMsg("该炉已经交库,无法撤销结炉!");
				return cro;
			}
			rs.close();
			psta.close();
			updateSqlPsta.setString(1, parm.get(0));
			updateSqlPsta.setString(2, parm.get(1));
			updateSqlPsta.setString(3, parm.get(2));
			updateSqlPsta.addBatch();

			// 修改计划结炉状态
			updatePlnPsta.setObject(1, perStuts);
			updatePlnPsta.setObject(2, parm.get(0));
			updatePlnPsta.addBatch();
			updatePlnPsta.executeBatch();
			updateSqlPsta.executeBatch();

			cro.setV_errMsg("撤销结炉成功!");
			connection.commit();
		} 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 conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSqlDoc(Connection conn, String strlist, String memo)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat matter2 = new SimpleDateFormat("yyMMdd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmThreadingResult.getInListDoc");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, memo);
		psta.setString(2, matter2.format(dt));
		psta.setString(3, matter1.format(dt));
		psta.setString(4, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 每支材料对应的入库流水号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSql(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmThreadingResult.getInListDoc1");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("INSTOCK_SQE");
		}
		rs.close();
		psta.close();
		return seq;
	}
}
