package com.steering.mes.mcp.heatTreatment;

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.Vrp.FrmVrpInStore;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 热处理上料实绩
 * 
 * @author lzh 2015-7-20
 */
public class HttLoadingService extends DBCommon {

	/***
	 * 查询是否存在上料之后的数据
	 * 
	 * @param plan
	 * @return
	 */
	public CoreReturnObject getQueryInfoPlan(String gx, String planNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("HttLoadingService.getQueryInfoPlan");
		return this.getData(sql, new Object[] { gx, planNo });
	}

	/**
	 * 查询相同炉计划ID下实绩状态
	 * 
	 * @param planNo
	 *            计划号
	 * @return
	 */
	public CoreReturnObject getQueryLoadingResult(String heatPlanNo,
			String plinCode) {
		return this.getDataBySqlID("HttLoadingService.getQueryLoadingResult",
				new Object[] { heatPlanNo, plinCode });
	}

	/***
	 *输入上料支数带出重量
	 * 
	 * @param plan
	 * @return
	 */
	public CoreReturnObject getQueryWeight(String numb, String heatPlanID,
			String batchNo, String groupNo) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("HttLoadingService.getQueryWeight");
		return this.getData(sql, new Object[] { batchNo, groupNo, heatPlanID,
				Integer.parseInt(numb) + 1 });
	}

	/**
	 * 查询相同炉计划ID下的待上料支数的和
	 * 
	 * @param planNo
	 *            计划号
	 * @return
	 */
	public CoreReturnObject getquerymaternum(String heatno) {
		return this.getDataBySqlID("HttLoadingService.getquerymaternum",
				new Object[] { heatno });
	}

	/**
	 * 每支材料对应的出库单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            出库类型
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String querySendOut(Connection conn, String strlist,
			String plineCode) throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("HttLoadingService.queryOutList");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("OUTSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 每支材料对应的出库流水号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            出库单号
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String querySendOutList(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("HttLoadingService.queryOut");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("OUTSTOCK_SQE");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/***
	 * 查询是否上料行
	 * 
	 * @param plan
	 * @return
	 */
	public CoreReturnObject getQueryOrderRCLS(String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("HttLoadingService.getQueryOrderRCLS");
		return this.getData(sql, new Object[] { plineCode, plineCode });
	}

	/**
	 * 查询上料实绩支数总和
	 * 
	 * @param planNo
	 *            计划号
	 * @return
	 */
	public CoreReturnObject getLoadResult1(String heatNo) {
		return this.getDataBySqlID("HttLoadingService.GetQuery1",
				new Object[] { heatNo });
	}

	/**
	 * 查询上料实绩支数总和
	 * 
	 * @param planNo
	 *            计划号
	 * @return
	 */
	public CoreReturnObject getLoadResult1(String heatNo, Integer ProcessSeq) {
		return this.getDataBySqlID("HttLoadingService.GetQuery2", new Object[] {
				heatNo, ProcessSeq });
	}

	/**
	 * 保存
	 * 
	 * @param load
	 * @return
	 */
	public CoreReturnObject saveHttLoadingResult(String loadStr,
			String hertSqe, String slgx, String plinCode, String groupbatchno,
			ArrayList<String> list3, ArrayList<String> list4) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		 //  TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");

		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMdd",
				Locale.CHINA);
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMM",
				Locale.CHINA);
		String systime = dateFormat.format(now);
		String BalYearMothDay = dateFormat1.format(now);
		String BalYearMoth = ""; //改25号结转
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			 //  TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务

			// PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			String sqlupdateplnc = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.updatePlnZyRclc");// 修改热处理计划表从表
			PreparedStatement patupdateplnc = connection
					.prepareStatement(sqlupdateplnc);
			String sqlupdate = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.updatePlnZyRclM");// 修改热处理计划表
			PreparedStatement patupdate = connection
					.prepareStatement(sqlupdate);
			String addPlnInfo = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.AddComInfoResult");// 新增公共信息
			PreparedStatement patsPlan = connection
					.prepareStatement(addPlnInfo);
			String deleteButt = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.deleteButter");// 删除位置表
			PreparedStatement patsButt = connection
					.prepareStatement(deleteButt);
			String addport = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.AddPortVrp");// 新增管号队列
			PreparedStatement patsport = connection.prepareStatement(addport);

			String UpdatePortValidflag = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.UpdatePortValidflag");// 新增管号队列
			PreparedStatement patUpdatePortValidflag = connection
					.prepareStatement(UpdatePortValidflag);

			String UpdatePortBalYearMonth = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.UpdatePortBalYearMonth");// 新增管号队列
			PreparedStatement patUpdatePortBalYearMonth = connection
					.prepareStatement(UpdatePortBalYearMonth);

			String updatePath = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.UpdatePath");// 修改队列表工艺路径
			PreparedStatement pastupdatePath = connection
					.prepareStatement(updatePath);
			String addOut = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.AddOutList");// 新增出库记录
			PreparedStatement patsAddOut = connection.prepareStatement(addOut);
			String addOutlist = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.AddOutListQuan");// 新增产权出库
			PreparedStatement patsAddOutlist = connection
					.prepareStatement(addOutlist);

			String deteleBcMat = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.DeteleBcMat");// 删除物料主表
			PreparedStatement deteleBcMatPath = connection
					.prepareStatement(deteleBcMat);

			HttLoadingResultEntity load = EntityTool.getEntityByJSONStr(
					loadStr, HttLoadingResultEntity.class);
			BalYearMoth = com.steering.common.StringTool.GetBalYear(com.steering.common.StringTool.GetSupplyUnit(load.getPlineCode())); //改25号结转

			// 获取热处理计划实体了
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					load.getHeatPlanNo());
			// 获取热处理计划从表实体类
			PlnZyRclCEntity plnC = PlanService.getSendHttPlanCInfoC(connection,
					load.getHeatPlanNo(), hertSqe);

			load.setHeatPlanSqe(hertSqe);

			String BatchNo = "";
			String BatchFlag = "0";
			Integer RealProcess = Integer.parseInt(pln.getRealProcessSeq());
			if (pln.getProcessSeq().equals(pln.getRealProcessSeq())) {
				if (pln.getProcessCodeC().equals("B024")) {
					BatchNo = plnC.getBatchNo(); /*
												 * PlanService.GetPlineCodeFlag(connection
												 * ,pln.getPlineCode()) + "999";
												 */
					BatchFlag = "1";
				}
			} else {
				BatchNo = PlanService.GetPlineCodeFlag(connection, pln
						.getPlineCode())
						+ "000";
				BatchFlag = "1";
			}

			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("上料支数不能为0！");
				return core;
			}

			if (count < detCount) {
				core.setV_errMsg("废品支数不能大于上料支数！");
				return core;
			}

			if (pln == null)
				return core;

			if (pln.getPlanstatus().equals("00")) {
				core.setV_errMsg("该计划未下发！");
				return core;
			}

			if (pln.getPlanstatus().equals("30")) {
				core.setV_errMsg("该计划已关闭！");
				return core;
			}
			String ifMonitNo = "";
			if (pln.getIfMonitor().equals("是")) {
				ifMonitNo = "1";
			} else {
				ifMonitNo = "0";
			}

			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.qureyGxComInfo");// 查询 工序是否是结炉状态
			PreparedStatement psta1 = connection.prepareStatement(sql);
			psta1.setString(1, pln.getHeatPlanNo());
			// psta1.setString(2, plinCode);
			psta1.setString(2, slgx);
			psta1.setString(3, load.getProcessSeq());
			ResultSet rs = psta1.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}
			if (cutFalg.equals("2")) {
				core.setV_errMsg("已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta1.close();
			String resultNo = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.queryLoadRESULT");// 查询最大实绩编号
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, plinCode + slgx + matter1.format(dt));
			psta.setObject(2, matter.format(dt));
			psta.setObject(3, plinCode);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();

			String sendOut = querySendOut(connection, "800801", plinCode);
			// 获取出库流水号
			String butessMatNo = querySendOutList(connection, sendOut);
			int outNo = Integer.parseInt(butessMatNo.substring(butessMatNo
					.length() - 4, butessMatNo.length()));

			String sendOutlist = querySendOut(connection, "800809", plinCode);
			// 获取出库流水号
			String butessMatNolist = querySendOutList(connection, sendOutlist);
			int outNolist = Integer.parseInt(butessMatNolist.substring(
					butessMatNolist.length() - 4, butessMatNolist.length()));

			double wegth = 0;
			int snum = 0;
			String stationname = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.queryStationName");// 查询工序点配置界面工序名称
			psta = connection.prepareStatement(stationname);
			psta.setObject(1, pln.getPlineCode());
			rs = psta.executeQuery();
			if (rs.next()) {
				snum = Integer.parseInt(rs.getString("NUM"));
			}
			rs.close();
			psta.close();
			// String gyPath = "1-0-0-0-0-0-0-0-0-0-0-0";
			String gyPath = "";
			if (snum == 2) {
				gyPath = "1-0";
			} else if (snum == 3) {
				gyPath = "1-0-0";
			} else if (snum == 4) {
				gyPath = "1-0-0-0";
			} else if (snum == 5) {
				gyPath = "1-0-0-0-0";
			} else if (snum == 6) {
				gyPath = "1-0-0-0-0-0";
			} else if (snum == 7) {
				gyPath = "1-0-0-0-0-0-0";
			} else if (snum == 8) {
				gyPath = "1-0-0-0-0-0-0-0";
			} else if (snum == 9) {
				gyPath = "1-0-0-0-0-0-0-0-0";
			} else if (snum == 10) {
				gyPath = "1-0-0-0-0-0-0-0-0-0";
			} else if (snum == 11) {
				gyPath = "1-0-0-0-0-0-0-0-0-0-0";
			}

			String deleteMat = "";

			String StorageNo = PlanService.GetStorageIn(pln.getPlineCode(),
					connection);
			String OutStorageNo = PlanService.GetStorageOut(pln.getPlineCode(),
					connection);

			if (Integer.parseInt(pln.getRealProcessSeq()) <= 1) {
				deleteMat = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.CopyBcMat");// 复制物料主表到MAT_BC_BACKUP表

				String sqlMatNo = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.queryMatZc");// 查询新增物料信息
				psta = connection.prepareStatement(sqlMatNo);
				psta.setObject(1, plnC.getJugdeApplyCode());
				psta.setObject(2, pln.getFeedHeatNo());
				psta.setObject(3, plnC.getBatchNo());
				psta.setObject(4, groupbatchno);
				psta.setObject(5, load.getHeatPlanNo());
				psta.setObject(6, StorageNo);
				psta.setObject(7, count + 1);
				rs = psta.executeQuery();
			} else {

				deleteMat = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.CopyBcMat2");// 复制物料主表到MAT_BC_BACKUP表

				// 多次热处理的料 通过本工序的判定炉号 和本工序的炉计划ID 在库存找料
				String sqlMatNo = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.queryMatZc2");// 查询新增物料信息
				psta = connection.prepareStatement(sqlMatNo);
				psta.setObject(1, pln.getJudgeStoveNo());
				psta.setObject(2, pln.getHeatPlanNo());
				psta.setObject(3, StorageNo);
				// psta.setObject(4, plinCode);
				psta.setObject(4, count + 1);
				rs = psta.executeQuery();

			}

			PreparedStatement patsMat = connection.prepareStatement(deleteMat);

			List<MatBcMEntity> listMat = EntityTool.getEntitysByRs(rs,
					MatBcMEntity.class);
			rs.close();
			psta.close();

			if (listMat.size() != count) {
				core.setV_errMsg("库存不够,无法上足" + count + "支");
				return core;
			}

			// 是否是成品红冲回来 管号队列批号写原炉计划
			boolean isBackMatNo = false;
			String OldBatchNo = "";

			if (listMat.get(0).getPlineCode().equals("100101")
					|| listMat.get(0).getPlineCode().equals("100102")
					|| listMat.get(0).getPlineCode().equals("100103")) {

				isBackMatNo = true;

				sql = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.QueryOldBatchNo");
				psta = connection.prepareStatement(sql);
				psta.setObject(1, plnC.getJudgeStoveNo());
				psta.setObject(2, plnC.getBatchNo());
				rs1 = psta.executeQuery();
				if (rs1.next()) {
					OldBatchNo = rs1.getString("BATCH_NO");
				}
				rs1.close();
				psta.close();

			}

			int CountInstorage = 0;
			Double TWt = 0d;

			for (MatBcMEntity lvMat : listMat) {

				/*if (!"002023005".equals(lvMat.getBelongCode())
						&& "C091".equals(pln.getPlineCode())) {
					core.setV_errMsg("管所属权不属于天淮,不允许上料！清确认管是否倒运至天淮！");
					return core;
				}*/

				if ("1".equals(lvMat.getLockFlag())) {
					core.setV_errMsg("库存存在质量封锁料,无法上足" + count + "支");
					return core;
				}

				if ("1".equals(lvMat.getMngLockFlag())) {
					core.setV_errMsg("库存存在管理封锁料,无法上足" + count + "支");
					return core;
				}

				if (lvMat.getStorageNo().equals("")) {
					core.setV_errMsg("库存正在倒运中,无法上料!");
					return core;
				}

				if (!lvMat.getStorageNo().equals(StorageNo)) {
					if (pln.getIsLoadFlag().equals("1")) {

						String deleteYdmBack = CoreXmlSqlParsersFactory
								.getSql("FrmMcpLoading.deleteYdmBack");//
						PreparedStatement patsdeleteYdmBack = connection
								.prepareStatement(deleteYdmBack);

						patsdeleteYdmBack.setObject(1, pln.getPlineCode());
						patsdeleteYdmBack.setObject(2, lvMat.getMatNo());
						patsdeleteYdmBack.addBatch();
						patsdeleteYdmBack.executeBatch();

						// 上料炉号和判定炉号不一致 改成保存判定炉号的位置信息
						String insertYdmBack = CoreXmlSqlParsersFactory
								.getSql("FrmMcpLoading.insertYdmBack");//
						PreparedStatement patsinsertYdmBack = connection
								.prepareStatement(insertYdmBack);
						patsinsertYdmBack.setObject(1, pln.getPlineCode());
						patsinsertYdmBack.setObject(2, lvMat.getMatNo());
						patsinsertYdmBack.executeUpdate();
						patsinsertYdmBack.close();

					} else {

						if (Integer.parseInt(pln.getRealProcessSeq()) <= 1) {
							if (CountInstorage == 0) {
								core.setV_errMsg("库存未在投料库,无法上料");
								return core;
							} else {
								core.setV_errMsg("只有" + CountInstorage
										+ "支在投料库 ,无法上足" + count + "支");
								return core;
							}
						} else {
							if (!lvMat.getStorageNo().equals(OutStorageNo)) {
								if (CountInstorage == 0) {
									core.setV_errMsg("库存未在投料库,无法上料");
									return core;
								} else {
									core.setV_errMsg("只有" + CountInstorage
											+ "支在投料库 ,无法上足" + count + "支");
									return core;
								}
							}
						}
					}

				}
				CountInstorage++;

				TWt = TWt + Double.parseDouble(lvMat.getActWeight());

				wegth += Double.parseDouble(lvMat.getActWeight());
				patsport.setObject(1, load.getHeatPlanNo());
				patsport.setObject(2, lvMat.getMatNo());
				patsport.setObject(3, "");
				patsport.setObject(4, "");
				patsport.setObject(5, pln.getZyBatchId());
				patsport.setObject(6, pln.getIndexSeq());
				patsport.setObject(7, pln.getPlineCode());
				patsport.setObject(8, pln.getPlineName());
				patsport.setObject(9, pln.getJudgeStoveNo());
				patsport.setObject(10, isBackMatNo ? OldBatchNo : plnC
						.getBatchNo());
				patsport.setObject(11, groupbatchno);
				patsport.setObject(12, pln.getStoveNo());
				patsport.setObject(13, pln.getJudgeStoveNo());
				patsport.setObject(14, lvMat.getActDimater());
				patsport.setObject(15, lvMat.getActHeight());
				patsport.setObject(16, lvMat.getActLen());
				patsport.setObject(17, lvMat.getActLenMin());
				patsport.setObject(18, lvMat.getActLenMax());
				patsport.setObject(19, lvMat.getActCount());
				patsport.setObject(20, lvMat.getActWeight());
				patsport.setObject(21, lvMat.getActTheoryWeight());
				patsport.setObject(22, slgx);
				patsport.setObject(23, "00");
				patsport.setObject(24, gyPath);
				patsport.setObject(25, load.getLoadUser());
				patsport.setObject(26, load.getLoadShift());
				patsport.setObject(27, load.getLoadGroup());
				patsport.setObject(28, systime);
				patsport.setObject(29, BatchNo);
				patsport.setObject(30, hertSqe);
				patsport.setObject(31, pln.getFeedHeatNo());
				patsport.setObject(32, pln.getRealProcessSeq());
				patsport.setObject(33, BalYearMothDay);
				patsport.setObject(34, BalYearMoth);
				patsport.addBatch();

				patUpdatePortValidflag.setObject(1, lvMat.getMatNo());
				patUpdatePortValidflag.setObject(2, pln.getRealProcessSeq());
				patUpdatePortValidflag.addBatch();

				if (Integer.parseInt(pln.getRealProcessSeq()) > 1) {
					patUpdatePortBalYearMonth
							.setObject(1, load.getHeatPlanNo());
					patUpdatePortBalYearMonth.setObject(2, lvMat.getMatNo());
					patUpdatePortBalYearMonth.setObject(3, pln
							.getRealProcessSeq());
					patUpdatePortBalYearMonth.addBatch();
				}

				// 新增出库表
				patsAddOut.setObject(1, sendOut
						+ StringTool.formatDecimal(outNo, "0000"));
				patsAddOut.setObject(2, load.getHeatPlanNo()
						+ (RealProcess > 1 ? ("-" + RealProcess) : ""));
				patsAddOut.setObject(3, sendOut);
				patsAddOut.setObject(4, "800801");
				patsAddOut.setObject(5, "投料出库");
				patsAddOut.setObject(6, lvMat.getMatNo());
				patsAddOut.setObject(7, lvMat.getJudgeStoveNo());
				patsAddOut.setObject(8, lvMat.getBatchNo());
				patsAddOut.setObject(9, lvMat.getBatchGroudNo());
				patsAddOut.setObject(10, lvMat.getMaterialNo());
				patsAddOut.setObject(11, lvMat.getMaterialName());
				patsAddOut.setObject(12, lvMat.getProduccode());
				patsAddOut.setObject(13, lvMat.getProducname());
				patsAddOut.setObject(14, lvMat.getStdCode());
				patsAddOut.setObject(15, lvMat.getStdName());
				patsAddOut.setObject(16, lvMat.getGradecode());
				patsAddOut.setObject(17, lvMat.getGradename());
				patsAddOut.setObject(18, lvMat.getSteelcode());
				patsAddOut.setObject(19, lvMat.getSteelname());
				patsAddOut.setObject(20, lvMat.getStdStyle());
				patsAddOut.setObject(21, lvMat.getStdStyleDesc());
				patsAddOut.setObject(22, lvMat.getSpecCode());
				patsAddOut.setObject(23, lvMat.getSpecName());
				patsAddOut.setObject(24, lvMat.getModelCode());
				patsAddOut.setObject(25, lvMat.getModelDesc());
				patsAddOut.setObject(26, (lvMat.getPlineCode().equals("100101")
						|| lvMat.getPlineCode().equals("100102") || lvMat
						.getPlineCode().equals("100103")) ? (lvMat
						.getProcessCdoe().equals("D") ? "C008" : lvMat
						.getProcessCdoe().equals("E") ? "C013" : lvMat
						.getProcessCdoe().equals("F") ? "C022" : lvMat
						.getProcessCdoe().equals("G") ? "C028" : "C041")
						: lvMat.getPlineCode());
				patsAddOut.setObject(27, (lvMat.getPlineCode().equals("100101")
						|| lvMat.getPlineCode().equals("100102") || lvMat
						.getPlineCode().equals("100103")) ? (lvMat
						.getProcessCdoe().equals("D") ? "250机组" : lvMat
						.getProcessCdoe().equals("E") ? "冷轧机组" : lvMat
						.getProcessCdoe().equals("F") ? "热处理01#（管加工1号热）"
						: lvMat.getProcessCdoe().equals("G") ? "加工线01#（管加工1#线）"
								: "接箍1（美国PMC1077）") : lvMat.getPlineName());
				patsAddOut.setObject(28, lvMat.getActDimater());
				patsAddOut.setObject(29, lvMat.getActHeight());
				patsAddOut.setObject(30, String.valueOf(lvMat.getActLen()));
				patsAddOut.setObject(31, lvMat.getActLenMin());
				patsAddOut.setObject(32, lvMat.getActLenMax());
				patsAddOut.setObject(33, String.valueOf(lvMat.getActCount()));
				patsAddOut.setObject(34, String.valueOf(lvMat.getActWeight()));
				patsAddOut.setObject(35, String.valueOf(lvMat
						.getActTheoryWeight()));
				patsAddOut.setObject(36, lvMat.getBelongCode());
				patsAddOut.setObject(37, lvMat.getBelongName());
				patsAddOut.setObject(38, lvMat.getStorageNo());
				patsAddOut.setObject(39, lvMat.getLocationNo());
				patsAddOut.setObject(40, lvMat.getLaryNo());
				patsAddOut.setObject(41, lvMat.getLocNo());
				patsAddOut.setObject(42, lvMat.getStorageAttr());
				patsAddOut.setObject(43, lvMat.getStorageAttrName());
				patsAddOut.setObject(44, lvMat.getStorageTypeNo());
				patsAddOut.setObject(45, lvMat.getStorageTypeName());
				patsAddOut.setObject(46, lvMat.getManagementNo());
				patsAddOut.setObject(47, lvMat.getManagementName());
				patsAddOut.setObject(48, systime);
				patsAddOut.setObject(49, lvMat.getProBc());
				patsAddOut.setObject(50, lvMat.getProBz());
				patsAddOut.setObject(51, systime);
				patsAddOut.setObject(52, load.getLoadShift());
				patsAddOut.setObject(53, load.getLoadGroup());
				patsAddOut.setObject(54, load.getLoadUser());
				patsAddOut.setObject(55, systime);
				patsAddOut.setObject(56, lvMat.getOrdPk());
				patsAddOut.setObject(57, lvMat.getOrdLnPk());
				patsAddOut.setObject(58, lvMat.getOrdLnDlyPk());
				patsAddOut.setObject(59, lvMat.getDepartmentCode());
				patsAddOut.setObject(60, lvMat.getDepartmentDesc());
				patsAddOut.setObject(61, lvMat.getOrderNo());
				patsAddOut.setObject(62, lvMat.getOrderSeq());
				patsAddOut.setObject(63, pln.getDeliveryNo());
				patsAddOut.setObject(64, "801401");
				patsAddOut.setObject(65, "80150105");
				patsAddOut.setObject(66, lvMat.getJudgeStdCode());
				patsAddOut.setObject(67, lvMat.getJudgeStdName());
				patsAddOut.setObject(68, pln.getZyBatchId());
				patsAddOut.setObject(69, lvMat.getStoveNo());
				patsAddOut.setObject(70, plinCode);
				patsAddOut.setObject(71, plinCode);
				patsAddOut.setObject(72, pln.getOrderNo() + "/"
						+ pln.getOrderSeq());
				patsAddOut.setObject(73, BalYearMoth);
				patsAddOut.setObject(74, lvMat.getProcessCdoe());
				patsAddOut.setObject(75, lvMat.getProcessDesc());
				patsAddOut.addBatch();
				outNo++;
				// 新增出库表
				patsAddOutlist.setObject(1, sendOutlist
						+ StringTool.formatDecimal(outNolist, "0000"));
				patsAddOutlist.setObject(2, load.getHeatPlanNo()
						+ (RealProcess > 1 ? ("-" + RealProcess) : ""));
				patsAddOutlist.setObject(3, sendOutlist);
				patsAddOutlist.setObject(4, "800809");
				patsAddOutlist.setObject(5, "产权出库");
				patsAddOutlist.setObject(6, lvMat.getMatNo());
				patsAddOutlist.setObject(7, lvMat.getJudgeStoveNo());
				patsAddOutlist.setObject(8, lvMat.getBatchNo());
				patsAddOutlist.setObject(9, lvMat.getBatchGroudNo());
				patsAddOutlist.setObject(10, lvMat.getMaterialNo());
				patsAddOutlist.setObject(11, lvMat.getMaterialName());
				patsAddOutlist.setObject(12, lvMat.getProduccode());
				patsAddOutlist.setObject(13, lvMat.getProducname());
				patsAddOutlist.setObject(14, lvMat.getStdCode());
				patsAddOutlist.setObject(15, lvMat.getStdName());
				patsAddOutlist.setObject(16, lvMat.getGradecode());
				patsAddOutlist.setObject(17, lvMat.getGradename());
				patsAddOutlist.setObject(18, lvMat.getSteelcode());
				patsAddOutlist.setObject(19, lvMat.getSteelname());
				patsAddOutlist.setObject(20, lvMat.getStdStyle());
				patsAddOutlist.setObject(21, lvMat.getStdStyleDesc());
				patsAddOutlist.setObject(22, lvMat.getSpecCode());
				patsAddOutlist.setObject(23, lvMat.getSpecName());
				patsAddOutlist.setObject(24, lvMat.getModelCode());
				patsAddOutlist.setObject(25, lvMat.getModelDesc());
				patsAddOutlist.setObject(26, (lvMat.getPlineCode().equals(
						"100101")
						|| lvMat.getPlineCode().equals("100102") || lvMat
						.getPlineCode().equals("100103")) ? (lvMat
						.getProcessCdoe().equals("D") ? "C008" : lvMat
						.getProcessCdoe().equals("E") ? "C013" : lvMat
						.getProcessCdoe().equals("F") ? "C022" : lvMat
						.getProcessCdoe().equals("G") ? "C028" : "C041")
						: lvMat.getPlineCode());
				patsAddOutlist.setObject(27, (lvMat.getPlineCode().equals(
						"100101")
						|| lvMat.getPlineCode().equals("100102") || lvMat
						.getPlineCode().equals("100103")) ? (lvMat
						.getProcessCdoe().equals("D") ? "250机组" : lvMat
						.getProcessCdoe().equals("E") ? "冷轧机组" : lvMat
						.getProcessCdoe().equals("F") ? "热处理01#（管加工1号热）"
						: lvMat.getProcessCdoe().equals("G") ? "加工线01#（管加工1#线）"
								: "接箍1（美国PMC1077）") : lvMat.getPlineName());
				patsAddOutlist.setObject(28, lvMat.getActDimater());
				patsAddOutlist.setObject(29, lvMat.getActHeight());
				patsAddOutlist.setObject(30, String.valueOf(lvMat.getActLen()));
				patsAddOutlist.setObject(31, lvMat.getActLenMin());
				patsAddOutlist.setObject(32, lvMat.getActLenMax());
				patsAddOutlist.setObject(33, String
						.valueOf(lvMat.getActCount()));
				patsAddOutlist.setObject(34, String.valueOf(lvMat
						.getActWeight()));
				patsAddOutlist.setObject(35, String.valueOf(lvMat
						.getActTheoryWeight()));
				patsAddOutlist.setObject(36, lvMat.getBelongCode());
				patsAddOutlist.setObject(37, lvMat.getBelongName());
				patsAddOutlist.setObject(38, lvMat.getStorageNo());
				patsAddOutlist.setObject(39, lvMat.getLocationNo());
				patsAddOutlist.setObject(40, lvMat.getLaryNo());
				patsAddOutlist.setObject(41, lvMat.getLocNo());
				patsAddOutlist.setObject(42, lvMat.getStorageAttr());
				patsAddOutlist.setObject(43, lvMat.getStorageAttrName());
				patsAddOutlist.setObject(44, lvMat.getStorageTypeNo());
				patsAddOutlist.setObject(45, lvMat.getStorageTypeName());
				patsAddOutlist.setObject(46, lvMat.getManagementNo());
				patsAddOutlist.setObject(47, lvMat.getManagementName());
				patsAddOutlist.setObject(48, systime);
				patsAddOutlist.setObject(49, lvMat.getProBc());
				patsAddOutlist.setObject(50, lvMat.getProBz());
				patsAddOutlist.setObject(51, systime);
				patsAddOutlist.setObject(52, load.getLoadShift());
				patsAddOutlist.setObject(53, load.getLoadGroup());
				patsAddOutlist.setObject(54, load.getLoadUser());
				patsAddOutlist.setObject(55, systime);
				patsAddOutlist.setObject(56, lvMat.getOrdPk());
				patsAddOutlist.setObject(57, lvMat.getOrdLnPk());
				patsAddOutlist.setObject(58, lvMat.getOrdLnDlyPk());
				patsAddOutlist.setObject(59, lvMat.getDepartmentCode());
				patsAddOutlist.setObject(60, lvMat.getDepartmentDesc());
				patsAddOutlist.setObject(61, lvMat.getOrderNo());
				patsAddOutlist.setObject(62, lvMat.getOrderSeq());
				patsAddOutlist.setObject(63, pln.getDeliveryNo());
				patsAddOutlist.setObject(64, "801401");
				patsAddOutlist.setObject(65, "80150105");
				patsAddOutlist.setObject(66, lvMat.getJudgeStdCode());
				patsAddOutlist.setObject(67, lvMat.getJudgeStdName());
				patsAddOutlist.setObject(68, pln.getZyBatchId());
				patsAddOutlist.setObject(69, lvMat.getStoveNo());
				patsAddOutlist.setObject(70, plinCode);
				patsAddOutlist.setObject(71, plinCode);
				patsAddOutlist.setObject(72, pln.getOrderNo() + "/"
						+ pln.getOrderSeq());
				patsAddOutlist.setObject(73, BalYearMoth);
				patsAddOutlist.setObject(74, lvMat.getProcessCdoe());
				patsAddOutlist.setObject(75, lvMat.getProcessDesc());
				if (Integer.parseInt(pln.getRealProcessSeq()) <= 1
						&& !pln.getIfMatchOdd().equals("6"))
					patsAddOutlist.addBatch();
				outNolist++;

				// 删除位置表
				patsButt.setObject(1, lvMat.getMatNo());
				patsButt.addBatch();

				if (Integer.parseInt(pln.getRealProcessSeq()) <= 1) {
					// 复制物料主表到MAT_BC_BACKUP表
					patsMat.setObject(1, lvMat.getMatNo());
					patsMat.addBatch();
				} else {
					// 复制物料主表到MAT_BC_BACKUP表
					patsMat.setObject(1, pln.getRealProcessSeq());
					patsMat.setObject(2, lvMat.getMatNo());
					patsMat.addBatch();
				}

				// 删除物料主表
				deteleBcMatPath.setObject(1, lvMat.getMatNo());
				deteleBcMatPath.addBatch();
			}
			patsport.executeBatch();
			patUpdatePortValidflag.executeBatch();

			if (Integer.parseInt(pln.getRealProcessSeq()) > 1) {
				patUpdatePortBalYearMonth.executeBatch();
			}

			patsAddOut.executeBatch();
			patsAddOutlist.executeBatch();
			patsButt.executeBatch();
			patsMat.executeBatch();
			deteleBcMatPath.executeBatch();

			// 新增上料实绩
			load.setProWt(TWt + "");
			load.setResultNo(resultNo);
			load.setProPlanId(pln.getProPlanId());
			load.setGxPlanNo(pln.getGxPlanNo());
			load.setPlineCode(pln.getPlineCode());
			load.setPlineName(pln.getPlineName());
			load.setCreateTime(systime);
			load.setColMode("1");
			load.setBalYearMonth(BalYearMoth);

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.AddLoadResultNew");// 新增上料实绩
			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd,
					load);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();

			// 新增热处理公共信息
			patsPlan.setObject(1, resultNo);
			patsPlan.setObject(2, pln.getZyBatchId());
			patsPlan.setObject(3, pln.getProPlanId());
			patsPlan.setObject(4, pln.getGxPlanNo());
			patsPlan.setObject(5, pln.getOrderSource());
			patsPlan.setObject(6, pln.getOrderNo());
			patsPlan.setObject(7, pln.getOrderSeq());
			patsPlan.setObject(8, pln.getReqBlId());
			patsPlan.setObject(9, pln.getPlineCode());
			patsPlan.setObject(10, pln.getPlineName());
			patsPlan.setObject(11, slgx);
			patsPlan.setObject(12, "上料");
			patsPlan.setObject(13, pln.getGradecode());
			patsPlan.setObject(14, pln.getGradename());
			patsPlan.setObject(15, pln.getSteelcode());
			patsPlan.setObject(16, pln.getSteelname());
			patsPlan.setObject(17, pln.getOutdiameterIn());
			patsPlan.setObject(18, pln.getWallthickIn());
			patsPlan.setObject(19, "");
			patsPlan.setObject(20, "");
			patsPlan.setObject(21, "");
			patsPlan.setObject(22, "");
			patsPlan.setObject(23, "");
			patsPlan.setObject(24, "");
			patsPlan.setObject(25, "");
			patsPlan.setObject(26, "");
			patsPlan.setObject(27, pln.getOutwlId());
			patsPlan.setObject(28, pln.getOutwlDesc());
			patsPlan.setObject(29, pln.getInwlId());
			patsPlan.setObject(30, pln.getInwlDesc());
			patsPlan.setObject(31, pln.getMonitorPoint());
			patsPlan.setObject(32, ifMonitNo);
			patsPlan.setObject(33, pln.getPrdcrNo());
			patsPlan.setObject(34, load.getProducer());
			if (load.getProducer().equals("")) {
				patsPlan.setObject(35, "");
			} else {
				patsPlan.setObject(35, load.getProducerTime());
			}
			patsPlan.setObject(36, pln.getStationRoad());
			patsPlan.setObject(37, pln.getCraftFileNo());
			patsPlan.setObject(38, pln.getCraftFileMan());
			patsPlan.setObject(39, pln.getCraftSeq());
			patsPlan.setObject(40, load.getProNum());
			patsPlan.setObject(41, load.getProWt());
			patsPlan.setObject(42, load.getFailNum());
			patsPlan.setObject(43, resultNo);
			patsPlan.setObject(44, load.getOfflineNum());
			patsPlan.setObject(45, load.getOfflineWt());
			patsPlan.setObject(46, load.getReNum());
			patsPlan.setObject(47, load.getReWt());
			patsPlan.setObject(48, "1");
			patsPlan.setObject(49, load.getLoadTime());
			patsPlan.setObject(50, "");
			patsPlan.setObject(51, load.getLoadUser());
			patsPlan.setObject(52, load.getLoadShift());
			patsPlan.setObject(53, load.getLoadGroup());
			patsPlan.setObject(54, "1");
			patsPlan.setObject(55, systime);
			patsPlan.setObject(56, "");
			patsPlan.setObject(57, load.getHeatPlanNo());
			patsPlan.setObject(58, pln.getJudgeStoveNo());
			patsPlan.setObject(59, plnC.getBatchNo());
			patsPlan.setObject(60, groupbatchno);
			patsPlan.setObject(61, pln.getProduccode());
			patsPlan.setObject(62, pln.getProducname());
			patsPlan.setObject(63, pln.getSpecCode());
			patsPlan.setObject(64, pln.getSpecName());
			patsPlan.setObject(65, load.getProcessSeq());
			patsPlan.addBatch();

			FrmHttBugD.AddHttWaste(connection, load.getHeatPlanNo(), resultNo,
					plinCode, load.getLoadUser(), load.getProcessSeq(), slgx,
					list3, list4);

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

			// 修改计划表从表
			patupdateplnc.setObject(1,
					pln.getRealProcessSeq().equals("1") ? load.getProNum()
							: "0");
			patupdateplnc
					.setObject(2, pln.getRealProcessSeq().equals("1") ? load
							.getProWt() : "0");
			patupdateplnc.setObject(3, pln.getHeatPlanNo());
			patupdateplnc.setObject(4, plnC.getHeatPlanSqe());
			patupdateplnc.addBatch();
			// 修改计划表
			patupdate.setObject(1, "20");
			patupdate.setObject(2, "00");
			patupdate.setObject(3, BatchFlag);
			patupdate.setObject(4, load.getHeatPlanNo());
			patupdate.addBatch();
			patupdateplnc.executeBatch();
			patupdate.executeBatch();

			// 更新运输需求上料时间
			FrmVrpInStore frmVrpInStore = new FrmVrpInStore();
//			frmVrpInStore.updateTmsDemandMLoadTime(connection, connection2,
//					core, load.getHeatPlanNo(), "F");
			if (core.getV_errMsg().equals("")) {
				core.setV_errMsg("上料成功！");
			}

			// 财务系统接口数据处理
			// FmisTool.addBcOutList(connection,core,load.getHeatPlanNo(),resultNo,"F");

			core.setResult(1);
			connection.commit();
			 //  TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
					 //  TZH 24.4.24 注销物流接口
//					connection2.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
			 //  TZH 24.4.24 注销物流接口
//			if (connection2 != null) {
//				try {
//					connection.rollback(); // 回滚事务
//					connection2.rollback(); // 回滚事务
//				} catch (Exception e2) {
//				}
//			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
				 //  TZH 24.4.24 注销物流接口
//				if (connection2 != null && !connection2.isClosed()) {
//					connection2.close();
//				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 撤销上料
	 * 
	 * @param planNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public CoreReturnObject returnHttLoadingResult(String loadStr,
			String hertSqe, String slgx, String userName, String FeedStoveNo) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		 //  TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			 //  TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			String sqlDelete = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.ReturnHttLoadingResult");// 删除上料实绩
			PreparedStatement patDelete = connection
					.prepareStatement(sqlDelete);
			String sqlupdateplnc = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.deletePlnZyRclc");// 删除时修改热处理计划从表
			PreparedStatement patupdateplnc = connection
					.prepareStatement(sqlupdateplnc);
			String sqlupdate = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.deletePlnZyRclM");// 删除时修改热处理计划表
			PreparedStatement patupdate = connection
					.prepareStatement(sqlupdate);
			String updatePlnInfo = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.deleteComInfoResult");// 删除热处理公共信息
			PreparedStatement patsupdatePlan = connection
					.prepareStatement(updatePlnInfo);
			String deleteButt = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.addButter");// 新增位置表
			PreparedStatement patsButt = connection
					.prepareStatement(deleteButt);
			String updateMat = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.updateMatZcPlanStuts");// 修改库存状态
			PreparedStatement patsUpMat = connection
					.prepareStatement(updateMat);
			String addOut = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.DeleteOutList");// 删除出库记录投料出库
			PreparedStatement patsDeleteOut = connection
					.prepareStatement(addOut);
			String addOutList = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.DeleteOutListQuan");// 删除出库记录产权出库
			PreparedStatement patsDeleteOutList = connection
					.prepareStatement(addOutList);
			String addport = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.DeletePortVrp");// 删除管号队列
			PreparedStatement patsport = connection.prepareStatement(addport);

			String UpdatePortValidflag2 = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.UpdatePortValidflag2");// 删除管号队列
			PreparedStatement patUpdatePortValidflag2 = connection
					.prepareStatement(UpdatePortValidflag2);

			String deleteOffline = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.DeleteOffline");// 清空离线实绩
			PreparedStatement patsdeleteOffline = connection
					.prepareStatement(deleteOffline);

			String addPlnInfo1 = CoreXmlSqlParsersFactory
					.getSql("HeatTreatmentService.AddComInfoResult1");// 查询材料状态和工序点编码
			PreparedStatement patsPlan1 = connection
					.prepareStatement(addPlnInfo1);
			String sql4 = CoreXmlSqlParsersFactory
					.getSql("FrmVrpLoading.deleteFmis");// 删除财务系统接口数据
			PreparedStatement deleteFmis = connection.prepareStatement(sql4);

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

			HttLoadingResultEntity load = EntityTool.getEntityByJSONStr(
					loadStr, HttLoadingResultEntity.class);
			String systime1 = com.steering.common.StringTool.GetBalYear(com.steering.common.StringTool.GetSupplyUnit(load.getPlineCode())); //改25号结转
			if (!load.getHeatPlanSqe().equals("")) {

				hertSqe = load.getHeatPlanSqe();
			}
			// 财务系统接口数据处理
			// FmisTool.deleteBcOutList(connection,core,load.getHeatPlanNo(),load.getResultNo(),"F");

			// 获取热处理计划实体了
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					load.getHeatPlanNo());
			// 获取热处理计划从表实体类
			PlnZyRclCEntity plnC = PlanService.getSendHttPlanCInfoC(connection,
					load.getHeatPlanNo(), hertSqe);

			// 获取热处理计划实体了
			PlnOrderRclSEntity httOrder = PlanService.getHttOrder(connection,
					pln.getProPlanId(), pln.getGxPlanNo());

			if (pln == null)
				return core;
			if (pln.getPlanstatus().equals("30")) {
				core.setV_errMsg("该计划已生产完成！");
				return core;
			}

			if (httOrder == null) {
				core.setV_errMsg("无订单计划！");
				return core;
			}

			/*
			 * if(!httOrder.getExeStatus().equals("0")
			 * &&!httOrder.getExeStatus().equals("1")) {
			 * core.setV_errMsg("订单处于订单处于生产完成/关闭状态,无法撤销上料实绩！"); return core; }
			 */

			if (!pln.getRealProcessSeq().equals(load.getProcessSeq())) {
				core.setV_errMsg("该上料记录是上次热处理的实绩,无法删除！");
				return core;
			}

			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.qureyGxComInfo");// 查询工序是否是结炉状态
			PreparedStatement psta = connection.prepareStatement(sql);
			psta.setString(1, load.getHeatPlanNo());
			// psta.setString(2, load.getPlineCode());
			psta.setString(2, slgx);
			psta.setString(3, load.getProcessSeq());
			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();

			int ProcessSeq = 1;
			if (StringTool.isInteger(load.getProcessSeq())) {
				ProcessSeq = Integer.parseInt(load.getProcessSeq());
			}

			int MaxSeq = 1;
			sql = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.queryMaxProcess");
			psta = connection.prepareStatement(sql);
			psta.setString(1, load.getHeatPlanNo());
			rs = psta.executeQuery();
			if (rs.next()) {
				MaxSeq = rs.getInt("PROCESS_SEQ");
			}
			if (MaxSeq > ProcessSeq) {
				core.setV_errMsg("请先删除最大循环次数的上料实绩！");
				return core;
			}
			rs.close();
			psta.close();

			if (MaxSeq == 1) {
				String SYNC_FLAG = "0";
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpLoading.SelectSyncFlag");// 查询财务系统同步状态
				psta = connection.prepareStatement(sql);
				psta.setObject(1, pln.getJudgeStoveNo());
				psta.setObject(2, plnC.getBatchNo());
				psta.setObject(3, load.getHeatPlanNo());
				psta.setObject(4, load.getProNum());
				rs = psta.executeQuery();
				if (rs.next()) {
					SYNC_FLAG = rs.getString("SYNC_FLAG");
				}
				rs.close();
				psta.close();
				if (SYNC_FLAG.equals("1")) {
					core.setV_errMsg("财务系统同步出库信息，请联系财务撤销同步信息!");
					return core;
				}
				// 删除财务系统接口数据
				deleteFmis.setObject(1, pln.getJudgeStoveNo());
				deleteFmis.setObject(2, plnC.getBatchNo());
				deleteFmis.setObject(3, load.getHeatPlanNo());
				deleteFmis.setObject(4, load.getProNum());
				deleteFmis.addBatch();
				deleteFmis.executeBatch();
			}

			if (!pln.getPlineCode().equals(load.getPlineCode())) {
				core.setV_errMsg("不允许删除其他产线上料实绩,若需删除,请离线委内回原产线进行删除！");
				return core;
			}

			int detCount = 0;
			int count1 = 0;

			if (StringTool.isInteger(load.getFailNum())) {
				detCount = Integer.parseInt(load.getFailNum());
			}
			if (StringTool.isInteger(load.getProNum())) {
				count1 = Integer.parseInt(load.getProNum());
			}

			FrmHttBugD.DeleteHttWaste(connection, load.getHeatPlanNo(), load
					.getResultNo(), load.getPlineCode(), load.getProcessSeq(),
					slgx, detCount);

			// 删除上料实绩
			patDelete.setObject(1, load.getResultNo());
			patDelete.addBatch();

			// 删除上料公共实绩
			patsupdatePlan.setObject(1, load.getResultNo());
			patsupdatePlan.addBatch();

			patDelete.executeBatch();
			patsupdatePlan.executeBatch();

			String BatchNo = "";
			if (ProcessSeq <= 1) {
				BatchNo = plnC.getBatchNo(); /*
											 * PlanService.GetPlineCodeFlag(connection
											 * ,pln.getPlineCode()) + "999";
											 */

			} else {
				BatchNo = PlanService.GetPlineCodeFlag(connection, pln
						.getPlineCode())
						+ "000";
			}

			List<YdmBcOutlistEntity> listOut = new ArrayList<YdmBcOutlistEntity>();
			String sqlMatNo = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.queryYdmOut");// 查询出库信息
			psta = connection.prepareStatement(sqlMatNo);
			psta.setObject(1, FeedStoveNo);
			psta.setObject(2, BatchNo);
			psta.setObject(3, pln.getHeatPlanNo()
					+ (ProcessSeq > 1 ? ("-" + ProcessSeq) : ""));
			psta.setObject(4, count1 + 1);
			rs = psta.executeQuery();
			listOut = EntityTool.getEntitysByRs(rs, YdmBcOutlistEntity.class);
			rs.close();
			psta.close();

			if (listOut.size() != count1) {
				core.setV_errMsg("数据异常,无法找到出库记录！");
				return core;
			}

			PreparedStatement patsAddBackup = null;
			PreparedStatement deteleBackupPath = null;

			if (ProcessSeq <= 1) {
				String addBackup = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.CopyBackup");// 复制MAT_BC_BACKUP表到物料主表
				patsAddBackup = connection.prepareStatement(addBackup);

				String deteleBackup = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.DeteleBackup");// 删除MAT_BC_BACKUP表
				deteleBackupPath = connection.prepareStatement(deteleBackup);
			} else {
				String addBackup = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.CopyBackup2");// 复制MAT_BC_BACKUP表到物料主表
				patsAddBackup = connection.prepareStatement(addBackup);

				String deteleBackup = CoreXmlSqlParsersFactory
						.getSql("HttLoadingService.DeteleBackup2");// 删除MAT_BC_BACKUP表
				deteleBackupPath = connection.prepareStatement(deteleBackup);
			}

			for (YdmBcOutlistEntity lvMat : listOut) {
				PortHttBatchSampleResultEntity PortHttBatchSampleResultEntity = PlanService
						.getPortHttBatchSampleResultEntity(pln.getHeatPlanNo(),
								lvMat.getMatNo(), ProcessSeq, connection);
				if (!PortHttBatchSampleResultEntity.getMatStatus().equals("00")) {
					if (PortHttBatchSampleResultEntity.getMatStatus().equals(
							"10")
							&& PortHttBatchSampleResultEntity.getOpProcess()
									.equals("600201")) {

					} else {
						core.setV_errMsg("后续工序已经做了离线操作或者剔除,无法撤销！");
						return core;
					}
				}

				if (!lvMat.getBalYearMonth().equals(systime1)) {
					core.setV_errMsg("上料结算日期和撤销上料结算日期不在同一个月，不允许撤销上料!");
					return core;
				}

				if (PortHttBatchSampleResultEntity.getInstoreFlag().equals("1")) {
					core.setV_errMsg("有物料已经做了交库,无法撤销！");
					return core;
				}

				// 修改库存状态
				patsUpMat.setObject(1, "80150103");
				patsUpMat.setObject(2, lvMat.getMatNo());
				patsUpMat.addBatch();

				List<YdmBcButtressEntity> back = new ArrayList<YdmBcButtressEntity>();
				String sqlBack = CoreXmlSqlParsersFactory
						.getSql("FrmMcpLoading.selectBack");// 查询出库信息
				psta = connection.prepareStatement(sqlBack);
				psta.setObject(1, lvMat.getMatNo());
				psta.setObject(2, pln.getPlineCode());
				rs = psta.executeQuery();
				back = EntityTool.getEntitysByRs(rs, YdmBcButtressEntity.class);
				rs.close();
				psta.close();

				if (back != null && back.size() > 0) {
					// 新增位置表
					patsButt.setObject(1, back.get(0).getMatNo());
					patsButt.setObject(2, back.get(0).getJudgeStoveNo());
					patsButt.setObject(3, back.get(0).getBatchNo());
					patsButt.setObject(4, back.get(0).getBatchGroudNo());
					patsButt.setObject(5, back.get(0).getStorageNo());
					patsButt.setObject(6, back.get(0).getLocationNo());
					patsButt.setObject(7, back.get(0).getLaryNo());
					patsButt.setObject(8, back.get(0).getLocNo());
					patsButt.setObject(9, back.get(0).getStorageAttr());
					patsButt.setObject(10, back.get(0).getStorageAttrName());
					patsButt.setObject(11, back.get(0).getStorageTypeNo());
					patsButt.setObject(12, back.get(0).getStorageTypeName());
					patsButt.setObject(13, back.get(0).getManagementNo());
					patsButt.setObject(14, back.get(0).getManagementName());
					patsButt.setObject(15, userName);
					patsButt.setObject(16, systime);
					patsButt.setObject(17, back.get(0).getDepartmentCode());
					patsButt.setObject(18, back.get(0).getDepartmentDesc());
					patsButt.addBatch();
				} else {

					// 新增位置表
					patsButt.setObject(1, lvMat.getMatNo());
					patsButt.setObject(2, lvMat.getJudgeStoveNo());
					patsButt.setObject(3, lvMat.getBatchNo());
					patsButt.setObject(4, lvMat.getBatchGroudNo());
					patsButt.setObject(5, lvMat.getStorageNo());
					patsButt.setObject(6, lvMat.getLocationNo());
					patsButt.setObject(7, lvMat.getLaryNo());
					patsButt.setObject(8, lvMat.getLocNo());
					patsButt.setObject(9, lvMat.getStorageAttr());
					patsButt.setObject(10, lvMat.getStorageAttrName());
					patsButt.setObject(11, lvMat.getStorageTypeNo());
					patsButt.setObject(12, lvMat.getStorageTypeName());
					patsButt.setObject(13, lvMat.getManagementNo());
					patsButt.setObject(14, lvMat.getManagementName());
					patsButt.setObject(15, userName);
					patsButt.setObject(16, systime);
					patsButt.setObject(17, lvMat.getDepartmentCode());
					patsButt.setObject(18, lvMat.getDepartmentDesc());
					patsButt.addBatch();
				}

				// 删除出库表
				patsDeleteOut.setObject(1, lvMat.getOutstockSqe());
				patsDeleteOut.addBatch();

				patsDeleteOutList.setObject(1, lvMat.getMatNo());
				patsDeleteOutList.setObject(2, pln.getHeatPlanNo()
						+ (ProcessSeq > 1 ? ("-" + ProcessSeq) : ""));
				patsDeleteOutList.addBatch();

				// 删除管号队列
				patsport.setObject(1, pln.getHeatPlanNo());
				patsport.setObject(2, lvMat.getMatNo());
				patsport.setObject(3, load.getProcessSeq());
				patsport.addBatch();

				patUpdatePortValidflag2.setObject(1, lvMat.getMatNo());
				patUpdatePortValidflag2.setObject(2, Integer.parseInt(load
						.getProcessSeq()) - 1);
				patUpdatePortValidflag2.addBatch();

				if (ProcessSeq <= 1) {
					patsAddBackup.setObject(1, lvMat.getMatNo());
					patsAddBackup.addBatch();

					deteleBackupPath.setObject(1, lvMat.getMatNo());
					deteleBackupPath.addBatch();
				} else {
					patsAddBackup.setObject(1, lvMat.getMatNo());
					patsAddBackup.setObject(2, load.getProcessSeq());
					patsAddBackup.addBatch();

					deteleBackupPath.setObject(1, lvMat.getMatNo());
					deteleBackupPath.setObject(2, load.getProcessSeq());
					deteleBackupPath.addBatch();
				}

			}

			patsUpMat.executeBatch();
			patsButt.executeBatch();
			patsDeleteOut.executeBatch();
			patsport.executeBatch();
			patUpdatePortValidflag2.executeBatch();
			patsAddBackup.executeBatch();
			deteleBackupPath.executeBatch();
			patsDeleteOutList.executeBatch();

			// 查询计划下剩余上料支、吨
			double count = 0;
			double weghit = 0;
			int indCount = 0;
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.queryCountRclM");
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, pln.getHeatPlanNo());
			psta.setObject(2, plnC.getHeatPlanSqe());
			rs = psta.executeQuery();
			while (rs.next()) {
				count = Double.parseDouble(rs.getString("PRO_NUM"));
				weghit = Double.parseDouble(rs.getString("PRO_WT"));
				indCount = Integer.parseInt(rs.getString("COUNT"));
			}
			rs.close();
			psta.close();

			patupdateplnc.setObject(1, count);
			patupdateplnc.setObject(2, weghit);
			patupdateplnc.setObject(3, pln.getHeatPlanNo());
			patupdateplnc.setObject(4, plnC.getHeatPlanSqe());
			patupdateplnc.addBatch();

			patupdateplnc.executeBatch();

			String sqlloadingdet2 = CoreXmlSqlParsersFactory
					.getSql("FrmHttCrackDetectResult.detelefailnum2");// 删除废品缺陷实绩
			PreparedStatement pstadet2 = connection
					.prepareStatement(sqlloadingdet2);
			pstadet2.setString(1, load.getResultNo());
			pstadet2.addBatch();
			pstadet2.executeBatch();

			String sqlloadingdet = CoreXmlSqlParsersFactory
					.getSql("FrmHttCrackDetectResult.detelefailnum");// 删除废品缺陷实绩
			PreparedStatement pstadet = connection
					.prepareStatement(sqlloadingdet);
			pstadet.setString(1, load.getResultNo());
			pstadet.addBatch();
			pstadet.executeBatch();

			int pronum = 0;
			String sqlqueryplan = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.querypronum");// 查询计划从表上料支数和
			psta = connection.prepareStatement(sqlqueryplan);
			psta.setObject(1, load.getHeatPlanNo());
			psta.setObject(2, load.getProcessSeq());
			rs = psta.executeQuery();
			while (rs.next()) {
				pronum = Integer.parseInt(rs.getString("PRO_NUM"));
			}
			rs.close();
			psta.close();
			if (pronum == 0) {
				patupdate.setObject(1, "10");
				patupdate.setObject(2, load.getHeatPlanNo());
				patupdate.addBatch();
			}
			patupdate.executeBatch();

			patsdeleteOffline.setObject(1, load.getHeatPlanNo());
			patsdeleteOffline.addBatch();
			patsdeleteOffline.executeBatch();

			// 更新运输需求上料时间
			FrmVrpInStore frmVrpInStore = new FrmVrpInStore();
			 //  TZH 24.4.24 注销物流接口
//			frmVrpInStore.updateTmsDemandMLoadTime(connection, connection2,
//					core, load.getHeatPlanNo(), "F");
			if (core.getV_errMsg().equals("")) {
				core.setV_errMsg("删除上料成功！");
			}

			core.setResult(1);
			connection.commit();
			 //  TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
					 //  TZH 24.4.24 注销物流接口
//					connection2.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
			 //  TZH 24.4.24 注销物流接口
//			if (connection2 != null) {
//				try {
//					connection.rollback(); // 回滚事务
//					connection2.rollback(); // 回滚事务
//				} catch (Exception e2) {
//				}
//			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
//				if (connection2 != null && !connection2.isClosed()) {
//					connection2.close();
//				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 修改上料
	 * 
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject updateHttLoadingResult(HttLoadingResultEntity load,
			String rentNo, String heartNO) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		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 sqlDelete = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.updateHttLoadingResult");// 修改上料实绩
			PreparedStatement patDelete = connection
					.prepareStatement(sqlDelete);
			String sqlupdate = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.updatePlnZyRclM1");// 修改时修改热处理计划表
			PreparedStatement patupdate = connection
					.prepareStatement(sqlupdate);
			String updatePlnInfo = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.updateComInfoResult");// 修改接箍公共信息
			PreparedStatement patsupdatePlan = connection
					.prepareStatement(updatePlnInfo);
			// 获取热处理计划实体了
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					heartNO);

			if (pln == null)
				return core;
			if (pln.getPlanstatus().equals("2")) {
				core.setV_errMsg("该计划已关闭！");
				return core;
			}

			// 修改上料实绩
			patDelete.setObject(1, load.getProNum());
			patDelete.setObject(2, load.getProWt());
			patDelete.setObject(3, load.getLoadTime());
			patDelete.setObject(4, load.getLoadShift());
			patDelete.setObject(5, load.getLoadGroup());
			patDelete.setObject(6, load.getLoadUser());
			patDelete.setObject(7, systime);
			patDelete.setObject(8, rentNo);
			patDelete.addBatch();

			// 修改上料公共实绩
			patsupdatePlan.setObject(1, load.getProNum());
			patsupdatePlan.setObject(2, load.getProWt());
			patsupdatePlan.setObject(3, load.getLoadTime());
			patsupdatePlan.setObject(4, load.getLoadShift());
			patsupdatePlan.setObject(5, load.getLoadGroup());
			patsupdatePlan.setObject(6, rentNo);
			patsupdatePlan.addBatch();

			patDelete.executeBatch();
			patsupdatePlan.executeBatch();

			// 查询计划下剩余上料支、吨
			double count = 0;
			double weghit = 0;
			int indCount = 0;
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.queryCountRclM");
			PreparedStatement psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, heartNO);
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				count = Double.parseDouble(rs.getString("PRO_NUM"));
				weghit = Double.parseDouble(rs.getString("PRO_WT"));
				indCount = Integer.parseInt(rs.getString("COUNT"));
			}
			rs.close();
			psta.close();

			patupdate.setObject(1, count);
			patupdate.setObject(2, weghit);
			patupdate.setObject(3, heartNO);
			patupdate.addBatch();

			patupdate.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("HttLoadingService.doBeside");// 修改工序状态为结炉
			PreparedStatement updateSqlPsta = connection
					.prepareStatement(updateSql);
			String updatePln = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.doPlnBeside");// 修改计划表结炉状态
			PreparedStatement updatePlnPsta = connection
					.prepareStatement(updatePln);
			/*
			 * String updateMatNoStuts = CoreXmlSqlParsersFactory
			 * .getSql("HttLoadingService.updateMatNoStuts");// 修改材料表状态
			 * PreparedStatement updatePstaMatNoStuts = connection
			 * .prepareStatement(updateMatNoStuts);
			 */

			String AllFailUpdatePlanStatus = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.updateAllFailStatus");//
			PreparedStatement psAllFailUpdatePlanStatus = connection
					.prepareStatement(AllFailUpdatePlanStatus);

			// 获取热处理计划实体了
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					parm.get(0));

			if (pln == null) {
				cro.setV_errMsg("无炉计划数据,,不允许此操作！");
				return cro;
			}

			// int count = 0;
			String cutFalg = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.qureyGxComInfo");// 查询工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			// psta.setString(2, parm.get(1));
			psta.setString(2, parm.get(2));
			psta.setString(3, pln.getRealProcessSeq());
			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();

			int lxCount = 0;
			sql = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.qureyGxLxCount");// 查询是否离线
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			// psta.setString(2, parm.get(1));
			psta.setString(2, parm.get(2));
			rs = psta.executeQuery();
			if (rs.next()) {
				lxCount = Integer.parseInt(rs.getString("COUNT"));
			}
			rs.close();
			psta.close();
			if (lxCount != 0) {
				cro.setV_errMsg("该工序点有离线记录,不允许此操作！");
				return cro;
			}

			updateSqlPsta.setString(1, parm.get(0));
			// updateSqlPsta.setString(2, parm.get(1));
			updateSqlPsta.setString(2, parm.get(2));
			updateSqlPsta.setString(3, pln.getRealProcessSeq());
			updateSqlPsta.addBatch();

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

			/*
			 * // 修改材料表状态 updatePstaMatNoStuts.setObject(1, parm.get(3));
			 * updatePstaMatNoStuts.setObject(2, parm.get(4));
			 * updatePstaMatNoStuts.setObject(3, parm.get(5));
			 * updatePstaMatNoStuts.setObject(4, parm.get(6));
			 * updatePstaMatNoStuts.addBatch();
			 */

			psAllFailUpdatePlanStatus.setString(1, parm.get(0));
			psAllFailUpdatePlanStatus.addBatch();
			psAllFailUpdatePlanStatus.executeBatch();

			updateSqlPsta.executeBatch();
			updatePlnPsta.executeBatch();
			// updatePstaMatNoStuts.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("HttLoadingService.deleteBeside");// 修改工序状态撤销结炉
			PreparedStatement updateSqlPsta = connection
					.prepareStatement(updateSql);
			String updatePln = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.doPlnBeside");// 修改计划表结炉状态
			PreparedStatement updatePlnPsta = connection
					.prepareStatement(updatePln);

			String AllFailUpdatePlanStatus = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.CancelAllFailStatus");
			PreparedStatement psAllFailUpdatePlanStatus = connection
					.prepareStatement(AllFailUpdatePlanStatus);

			// 获取热处理计划实体了
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					parm.get(0));

			if (pln == null) {
				cro.setV_errMsg("无炉计划数据,,不允许此操作！");
				return cro;
			}

			String cutFalg1 = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("HeatTreatmentService.qureyGxComInfoNew");// 查询工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			psta.setObject(2, Integer.parseInt(parm.get(2)) + 1);
			psta.setObject(3, Integer.parseInt(parm.get(2)) + 2);
			psta.setObject(4, Integer.parseInt(parm.get(2)) + 3);
			psta.setObject(5, pln.getRealProcessSeq());
			rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg1 = rs.getString("STATION_STATUS");
			} else {
				cro.setV_errMsg("下一道工序没有撤销结炉,不允许此工序撤销结炉!");
				return cro;
			}

			if (cutFalg1 != null && cutFalg1.equals("2")) {
				cro.setV_errMsg("下一道工序没有撤销结炉,不允许此工序撤销结炉!");
				return cro;
			}
			rs.close();
			psta.close();

			String cutFalg = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("HttLoadingService.qureyGxComInfo");// 查询工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm.get(0));
			// psta.setString(2, parm.get(1));
			psta.setString(2, parm.get(2));
			psta.setString(3, pln.getRealProcessSeq());
			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();

			updateSqlPsta.setString(1, parm.get(0));
			// updateSqlPsta.setString(2, parm.get(1));
			updateSqlPsta.setString(2, parm.get(2));
			updateSqlPsta.setString(3, pln.getRealProcessSeq());
			updateSqlPsta.addBatch();

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

			psAllFailUpdatePlanStatus.setString(1, parm.get(0));
			psAllFailUpdatePlanStatus.addBatch();
			psAllFailUpdatePlanStatus.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 planNo
	 *            计划号
	 * @return
	 */
	public CoreReturnObject getLoadResult(String heatNo) {
		return getLoadResult(heatNo, "");
	}

	/**
	 * 根据计划号查询热处理上料实绩
	 * 
	 * @param planNo
	 *            计划号
	 * @return
	 */
	public CoreReturnObject getLoadResult(String heatNo, String sqe) {
		return this.getDataBySqlID("HttLoadingService.GetLoadResultByID",
				new Object[] { heatNo, sqe });
	}
}
