package com.steering.mes.mcp.Vrp;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.*;
import com.steering.mes.mcp.Vrp.FrmVrpInStore;

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

public class FrmHttInStore extends DBCommon {

	/***
	 * 查询热处理炉计划
	 * 
	 * @param parmList
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject querySinglePlan(ArrayList<String> parmList,
			String[] plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmHttInStore.querySinglePlan");
		if (!parmList.get(0).equals("")) {
			sql += " AND T.DOWNTIME >=TO_DATE('" + parmList.get(0)
					+ "', 'YYYY-MM-DD HH24:MI:SS')AND T.DOWNTIME <=TO_DATE('"
					+ parmList.get(1) + "', 'YYYY-MM-DD HH24:MI:SS') ";
		}
		if (!parmList.get(2).equals("")) {
			sql += " AND T.ZY_BATCH_ID = '" + parmList.get(2) + "'";
		}
		if (!parmList.get(3).equals("")) {
			sql += " AND T.JUDGE_STOVE_NO = '" + parmList.get(3) + "'";
		}
		if (parmList.get(4).equals("1")) {
			sql += " AND ((T.BESIDE_STATUS >='90' AND T.PLANSTATUS > '10') OR (REAL_PROCESS_SEQ > 1 AND PLANSTATUS <= 10))";
		} else {
			sql += " AND (T.BESIDE_STATUS <>'90' OR T.BESIDE_STATUS IS NULL) AND  T.PLANSTATUS > '10' AND PER_STATUS>='00' ";
		}
		sql += " AND T.PLANSTATUS > '00'  AND T.PLANSTATUS < '40'";
		// sql += "AND T.PER_STATUS IS  NOT NULL'"
		sql += CoreConverter.toSqlConditionString(
				"NVL(T.PLINE_CODE_W,PLINE_CODE)", plineCode);
		sql += " ORDER BY T.DOWNTIME ASC";
		return this.getData(sql);
	}

	/***
	 * 查询交库实绩表
	 * 
	 * @param parmList
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryInList(ArrayList<String> parmList,
			String[] plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmHttInStore.doQueryInList");
		if (!parmList.get(0).equals("")) {
			sql += " AND TRUNC(T.IN_TIME_F)>=TO_DATE('" + parmList.get(0)
					+ "','YYYY-MM-DD') AND TRUNC(T.IN_TIME_F)<= TO_DATE('"
					+ parmList.get(1) + "','YYYY-MM-DD')";
		}
		if (!parmList.get(2).equals("")) {
			sql += " AND T.LOT_NO = '" + parmList.get(2) + "'";
		}
		if (!parmList.get(3).equals("")) {
			sql += " AND T.JUDGE_STOVE_NO = '" + parmList.get(3) + "'";
		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		sql += " group by t.JUDGE_STOVE_NO,t.BATCH_NO,t.INSTOCK_DOC,t.PRODUCCODE,t.PRODUCNAME,t.STD_CODE,t.STD_NAME,t.STEELCODE,t.STEELNAME,t.STD_STYLE,t.STD_STYLE_DESC,t.SPEC_CODE,t.SPEC_NAME,t.JUDGE_STD_CODE,t.JUDGE_STD_NAME,t.GRADECODE,t.GRADENAME,t.BAL_YEAR_MONTH,t.PIPMEMO,t.BATCH_GROUD_NO,t.MATERIAL_NO,t.MATERIAL_NAME,t.MAT_STATUS,";
		sql += " t.MODEL_DESC,t.ACT_DIMATER,t.ACT_HEIGHT,t.ACT_LEN_MIN,t.ACT_LEN_MAX,t.BELONG_CODE,t.BELONG_NAME,t.MEMO,t.CREATE_NAME,t.ORDER_NO,t.ORDER_SEQ,t.DELIVERY_NO,"
				+ "t.MODEL_CODE,t.PRODUCT_FLAG,t.PLINE_CODE,t.PLINE_NAME,t.ACT_LEN,t.GX_PLAN_NO,"
				+ "t.PRO_PLAN_ID,T.LOT_NO, B.STORAGE_NO";
		sql += " order by t.instock_doc desc";
		return this.getData(sql);
	}

	/***
	 * 查询交库管号队列信息
	 * 
	 * @param heatPlanNo
	 * @param judgeNo
	 * @return
	 */
	public CoreReturnObject doQueryVrpRack(String heatPlanNo, String judgeNo) {
		return this.getDataBySqlID("FrmHttInStore.doQueryVrpRack",
				new Object[] { judgeNo, heatPlanNo, judgeNo });
	}

	/***
	 * 查询指定炉批支数
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryStausRack(String heatPlanNo, String judgeNo,
			String phyBatchNo, String isSampleFlag) {
		String sampleFlag = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmHttInStore.doQueryStausRack");
		if (isSampleFlag.equals("样管")) {
			sampleFlag = "1";
		} else {
			sampleFlag = "0";
		}
		return this.getData(sql, new Object[] { heatPlanNo, judgeNo,
				phyBatchNo, sampleFlag });
	}

	/***
	 * 查询指定炉批输入支数重量
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryStausRackWgt(String heatPlanNo,
			String judgeNo, String phyBatchNo, String isSampleFlag,
			String actCount) {
		String sampleFlag = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmHttInStore.doQueryStausRackWgt");
		if (isSampleFlag.equals("样管")) {
			sampleFlag = "1";
		} else {
			sampleFlag = "0";
		}
		return this.getData(sql, new Object[] { heatPlanNo, judgeNo,
				phyBatchNo, sampleFlag, actCount });
	}

	/***
	 * 热处理下线交库
	 * 
	 * @param parmList
	 * @param plnOrderTity
	 * @return
	 */
	public CoreReturnObject doSave(ArrayList<String> parmList,
			String plnZyTity, String userName, String mangMentNo,
			String userOrder, String userClass) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat dd = new SimpleDateFormat("yyyyMMdd");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String yearMothDay = dd.format(now);
		String inDoc = "";// 入库单号
		String groupNo = "";// 组号
		String storageNo = "";// 仓库号
		String strInBound = "800701";
		String belongCode = "";
		String belongName = "";
		int inCount = 0;// 入库总支数

		String gpreqNum = "";// 需求支
		String matGpNum = "";// 已分配到订单支数
		int gpreqCount = 0;// 未分配支数
		int interNum = 0;// 整数支
		int remianNum = 0;// 余数支

		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			PlnZyRclMEntity1 plnZyEntity = EntityTool.getEntityByJSONStr(
					plnZyTity, PlnZyRclMEntity1.class);
			inDoc = getInListDoc(connection, strInBound);
			storageNo = getStorageNo(connection, plnZyEntity.getPlineCode());
			if (storageNo.equals("")) {
				throw new Exception("没有下线仓库，请先维护下线仓库。");
			}
			ArrayList<String> sqllist = new ArrayList<String>();
			sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.selectBelong");
			sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				belongCode = rs.getString("DEPARTMENT_CODE");
				belongName = rs.getString("DEPARTMENT_DESC");
			}
			rs.close();
			psta.close();

			String ordPk = "";
			String ordLnPk = "";
			sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.selectOrdPk");
			sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				ordPk = rs.getString("ORD_PK");
				ordLnPk = rs.getString("ORD_LN_PK");
			}
			rs.close();
			psta.close();
			for (String str : parmList) {
				PortHttBatchSampleResultEntity1 portVrpEntity = EntityTool
						.getEntityByJSONStr(str,
								PortHttBatchSampleResultEntity1.class);
				groupNo = creatGroup(connection, plnZyTity, str);
				// String inListSeq = querySendSql(connection, inDoc); // 入库流水号
				// int n = Integer.parseInt(inListSeq.substring(
				// inListSeq.length() - 4, inListSeq.length()));
				MatBcMEntity matBcEntity = new MatBcMEntity();
				// matBcEntity.setTestCount(String.valueOf(n));
				matBcEntity.setInstockDoc(inDoc);
				matBcEntity.setInstockTypeCode(strInBound);
				matBcEntity.setInstockTypeName("产出入库");

				matBcEntity.setStorageNo(storageNo);
				matBcEntity.setLocationNo(storageNo + "10101");
				matBcEntity.setLaryNo("1");

				matBcEntity.setBatchGroudNo(groupNo);
				// if(!plnZyEntity.getIsendgx().equals("0") ||
				// !plnZyEntity.getRealProcessSeq().equals(plnZyEntity.getProcessSeq()))
				// {
				// matBcEntity.setProductFlag("801401");
				// matBcEntity.setMatStatus("80150101");
				// }else
				// {
				// matBcEntity.setProductFlag("801402");
				// matBcEntity.setMatStatus("80150201");成品
				// }
				if (!plnZyEntity.getDecision().equals("A")
						|| !plnZyEntity.getRealProcessSeq().equals(
								plnZyEntity.getProcessSeq())) {
					matBcEntity.setProductFlag("801401");
					matBcEntity.setMatStatus("80150101");

				} else {
					matBcEntity.setProductFlag("801402");// 成品
					matBcEntity.setMatStatus("80150201");
				}
				matBcEntity.setJudgeStoveNo(plnZyEntity.getJudgeStoveNo());
				matBcEntity.setMaterialNo(plnZyEntity.getOutwlId());
				matBcEntity.setMaterialName(plnZyEntity.getOutwlDesc());
				matBcEntity.setProduccode(plnZyEntity.getProduccode());
				matBcEntity.setProducname(plnZyEntity.getProducname());
				matBcEntity.setSteelcode(plnZyEntity.getSteelcode());
				matBcEntity.setSteelname(plnZyEntity.getSteelname());
				matBcEntity.setStdStyle(plnZyEntity.getStdStyle());
				matBcEntity.setStdStyleDesc(plnZyEntity.getStdStyleDesc());
				matBcEntity.setSpecCode(plnZyEntity.getSpecCode());
				matBcEntity.setSpecName(plnZyEntity.getSpecName());
				matBcEntity.setModelCode(plnZyEntity.getModelCode());
				matBcEntity.setModelDesc(plnZyEntity.getModelDesc());
				matBcEntity.setGradecode(plnZyEntity.getGradecode());
				matBcEntity.setGradename(plnZyEntity.getGradename());
				matBcEntity.setStdCode("");
				// matBcEntity.setStdName(plnZyEntity.getProdStandard()); //生产标准
				matBcEntity.setStdName(plnZyEntity.getOutputStandard()); // 输出标准
				matBcEntity.setProOrderNo(plnZyEntity.getOrderNo() + "/"
						+ plnZyEntity.getOrderSeq());
				matBcEntity.setProcessCdoe("F");
				matBcEntity.setProcessDesc("热处理");
				matBcEntity.setNextProcessCdoe(plnZyEntity.getRealProcessSeq()
						.equals(plnZyEntity.getProcessSeq()) ? plnZyEntity
						.getNextProcessCode() : "F");
				matBcEntity.setNextProcessDesc("");
				matBcEntity.setHeatPlanNoNext(plnZyEntity.getRealProcessSeq()
						.equals(plnZyEntity.getProcessSeq()) ? "" : plnZyEntity
						.getHeatPlanNo());
				matBcEntity.setPlineCode(plnZyEntity.getPlineCode());
				matBcEntity.setPlineName(plnZyEntity.getPlineName());
				matBcEntity.setMatOriginCode("801302");
				matBcEntity.setMatOriginName("产线");
				matBcEntity.setActDimater(plnZyEntity.getOutdiameter());
				matBcEntity.setActHeight(plnZyEntity.getWallthick());
				matBcEntity.setActLenMin(plnZyEntity.getLengthmin());
				matBcEntity.setActLenMax(plnZyEntity.getLengthmax());
				matBcEntity.setBelongCode(belongCode);
				matBcEntity.setBelongName(belongName);
				matBcEntity.setCreateName(userName);
				matBcEntity.setCreateTime(systime);
				matBcEntity.setBalYearMonth(yearMoth);
				matBcEntity.setJudgeStdCode("");
				matBcEntity.setJudgeStdName(plnZyEntity.getOutputStandard());
				matBcEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
				matBcEntity.setQcmStaus("00");
				matBcEntity.setHeatPlanNo(plnZyEntity.getHeatPlanNo());
				matBcEntity.setActCount(portVrpEntity.getActCount());
				matBcEntity.setProOrderId(plnZyEntity.getProPlanId());
				matBcEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
				matBcEntity.setPhyBatchNo(portVrpEntity.getPhyBatchNo());
				matBcEntity.setIsSamplePipe(portVrpEntity.getIsSamplePipe());
				matBcEntity.setCrafNo(plnZyEntity.getCraftFileNo());
				matBcEntity.setCraftSeq(plnZyEntity.getCraftSeq());
				matBcEntity.setOrdLnDlyPk(plnZyEntity.getOrdLnDlyPk());
				matBcEntity.setOrderNo(plnZyEntity.getOrderNo());
				matBcEntity.setOrderSeq(plnZyEntity.getOrderSeq());
				matBcEntity.setOrderSeqNo(plnZyEntity.getDeliveryNo());
				matBcEntity.setMatGowhereCode(plnZyEntity.getGowhereCode());
				matBcEntity.setMatGowhereName(plnZyEntity.getGowhereName());
				matBcEntity.setStoveNo(plnZyEntity.getStoveNo());
				matBcEntity.setFlagStoveNoZg(portVrpEntity.getFlagStoveNo());
				matBcEntity.setTarget(plnZyEntity.getDecision());
				matBcEntity.setInBc(userOrder);
				matBcEntity.setInBz(userClass);
				matBcEntity.setHeatPlanNoOld(plnZyEntity.getHeatPlanNo());
				matBcEntity.setMemo(portVrpEntity.getRemark());
				matBcEntity.setActLen(portVrpEntity.getActLen());
				;
				if (plnZyEntity.getIfProduct().equals("1")) {
					matBcEntity.setJudgeStdDes(plnZyEntity.getCic() + "+"
							+ plnZyEntity.getPic() + "+" + plnZyEntity.getSic()
							+ "+" + plnZyEntity.getDic());
				} else {
					matBcEntity.setJudgeStdDes("" + "+" + plnZyEntity.getPic()
							+ "+" + plnZyEntity.getSic() + "+"
							+ plnZyEntity.getDic());
				}
				matBcEntity.setCic(plnZyEntity.getCic());
				if (ordPk == null || ordPk.equals("")) {
					matBcEntity.setOrdPk("");
				} else {
					matBcEntity.setOrdPk(ordPk);
				}
				if (ordLnPk == null || ordLnPk.equals("")) {
					matBcEntity.setOrdLnPk("");
				} else {
					matBcEntity.setOrdLnPk(ordLnPk);
				}

				String insertBcSql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.insertMatBcM");// 插入库存、交易
				insertBcSql = EntityTool.fillSqlByEntity(insertBcSql,
						matBcEntity);
				sqllist.add(insertBcSql);

				HttComInfoEntity1 vrpEntity = new HttComInfoEntity1();
				vrpEntity.setPlineCode(plnZyEntity.getPlineCode());
				vrpEntity.setPlineName(plnZyEntity.getPlineName());
				vrpEntity.setHeatPlanNo(plnZyEntity.getHeatPlanNo());
				vrpEntity.setZyBatchId(plnZyEntity.getZyBatchId());
				vrpEntity.setProPlanId(plnZyEntity.getProPlanId());
				vrpEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
				vrpEntity.setIfMonitorRcl(plnZyEntity.getIfMonitor());
				vrpEntity.setPrdcrNo(plnZyEntity.getPrdcrNo());
				vrpEntity.setMonitordemand("");
				vrpEntity.setCraftFileNo(plnZyEntity.getCraftFileNo());
				vrpEntity.setCraftSeq(plnZyEntity.getCraftSeq());
				vrpEntity.setCraftFileMan(plnZyEntity.getCraftFileMan());
				vrpEntity.setJudgeStoveNo(plnZyEntity.getJudgeStoveNo());
				vrpEntity.setBatchNo(portVrpEntity.getPhyBatchNo());
				vrpEntity.setBatchGroudNo(groupNo);
				vrpEntity.setGradecode(plnZyEntity.getGradecode());
				vrpEntity.setGradename(plnZyEntity.getGradename());
				vrpEntity.setSteelcode(plnZyEntity.getSteelcode());
				vrpEntity.setSteelname(plnZyEntity.getSteelname());
				vrpEntity.setSpecCode(plnZyEntity.getSpecCode());
				vrpEntity.setSpecName(plnZyEntity.getSpecName());
				vrpEntity.setLen(plnZyEntity.getLengthIn());
				vrpEntity.setDimater(plnZyEntity.getOutdiameterIn());
				vrpEntity.setHeight(plnZyEntity.getWallthickIn());
				vrpEntity.setPlnLen(portVrpEntity.getActLen());
				vrpEntity.setPlnLenMin(plnZyEntity.getLengthmin());
				vrpEntity.setPlnLenMax(plnZyEntity.getLengthmax());
				vrpEntity.setLenMin(plnZyEntity.getLengthminIn());
				vrpEntity.setLenMax(plnZyEntity.getLengthmaxIn());
				vrpEntity.setLenCutHead("");// 切头
				vrpEntity.setLenCutEnd("");// 切尾
				vrpEntity.setActSpecCode(plnZyEntity.getSpecCode());
				vrpEntity.setActSpecName(plnZyEntity.getSpecName());
				vrpEntity.setActDimater(plnZyEntity.getOutdiameter());
				vrpEntity.setActHeight(plnZyEntity.getWallthick());
				vrpEntity.setActLen(portVrpEntity.getActLen());
				vrpEntity.setActLenMin(plnZyEntity.getLengthmin());
				vrpEntity.setActLenMax(plnZyEntity.getLengthmax());
				vrpEntity.setActMaterialNo(plnZyEntity.getOutwlId());
				vrpEntity.setActMaterialName(plnZyEntity.getOutwlDesc());
				vrpEntity.setInwlId(plnZyEntity.getInwlId());
				vrpEntity.setInwlDesc(plnZyEntity.getInwlDesc());
				vrpEntity.setProNum(portVrpEntity.getActCount());
				vrpEntity.setProWt(portVrpEntity.getActWeight());
				vrpEntity.setStationStatus("1");
				vrpEntity.setCreateTime(systime);
				vrpEntity.setRroTime(systime);
				vrpEntity.setProUser(userName);
				vrpEntity.setProGroup(userClass);
				vrpEntity.setProShift(userOrder);
				vrpEntity.setColOperator(userName);
				vrpEntity.setBalYearMonth(yearMoth);
				vrpEntity.setInstockDoc(inDoc);
				vrpEntity.setProYearMonthDay(yearMothDay);
				String inserVrpComInfoSql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.insertVrpComInfo");// 插入公共表
				inserVrpComInfoSql = EntityTool.fillSqlByEntity(
						inserVrpComInfoSql, vrpEntity);
				sqllist.add(inserVrpComInfoSql);

				inCount = inCount
						+ Integer.parseInt(portVrpEntity.getActCount());
			}
			if (!plnZyEntity.getNextProcessCode().equals("")
					&& plnZyEntity.getRealProcessSeq().equals(
							plnZyEntity.getProcessSeq())) {
				if (plnZyEntity.getNextProcessCode().equals("G")) {
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.selectPlnZyJg");
				} else if (plnZyEntity.getNextProcessCode().equals("H")) {
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.selectPlnZyJgm");
				}
				sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();

				List<PlnZyRclMEntity1> plnOrderZy = EntityTool.getEntitysByRs(
						rs, PlnZyRclMEntity1.class);
				rs.close();
				psta.close();
				String codeBl = "";
				if (plnZyEntity.getNextProcessCode().equals("H")) {
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.selectPlnZyJgmProp");
					sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
					psta = connection.prepareStatement(sql);
					rs = psta.executeQuery();
					if (rs.next()) {
						codeBl = rs.getString("CODE_JG_BL");
					}
					rs.close();
					psta.close();
				}
				for (int i = 0; i < plnOrderZy.size(); i++) {
					// 该订单下已分配多少支
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.selectMatGpMNumZg");
					sql = EntityTool.fillSqlByEntity(sql, plnOrderZy.get(i));
					psta = connection.prepareStatement(sql);
					rs = psta.executeQuery();
					if (rs.next()) {
						matGpNum = rs.getString("ACT_COUNT");
					}
					rs.close();
					psta.close();
					if (plnZyEntity.getNextProcessCode().equals("G")) {
						gpreqCount = Integer.parseInt(plnOrderZy.get(i)
								.getPlnInsingleNum())
								- Integer.parseInt(matGpNum);
					} else if (plnZyEntity.getNextProcessCode().equals("H")) {
						gpreqCount = Integer.parseInt(plnOrderZy.get(i)
								.getPlanBlNum())
								- Integer.parseInt(matGpNum);
					}

					MatBcMEntity matEntity = new MatBcMEntity();
					matEntity.setJudgeStoveNo(plnZyEntity.getJudgeStoveNo());
					matEntity.setProOrderId(plnZyEntity.getProPlanId());
					matEntity.setGxPlanNo(plnZyEntity.getGxPlanNo());
					matEntity.setHeatPlanNo(plnZyEntity.getHeatPlanNo());
					matEntity
							.setProPlanIdNext(plnOrderZy.get(i).getProPlanId());
					matEntity.setGxPlanNoNext(plnOrderZy.get(i).getGxPlanNo());
					matEntity.setPlineCode(plnZyEntity.getPlineCode());
					// matEntity.setCodeJgBl(plnOrderZy.get(i).getCodeJgBl());
					matEntity.setCodeJgBl(codeBl);
					matEntity.setHeatPlanNoNext(plnOrderZy.get(i)
							.getHeatPlanNo());

					if (i == plnOrderZy.size() - 1) {
						matEntity.setActCount(String.valueOf(inCount));
					} else if (inCount < gpreqCount) {
						matEntity.setActCount(String.valueOf(inCount));
					} else {
						matEntity.setActCount(String.valueOf(gpreqCount));
					}
					String updateMatBcMSql = CoreXmlSqlParsersFactory
							.getSql("FrmVrpInStore.updateMatBcM");
					updateMatBcMSql = EntityTool.fillSqlByEntity(
							updateMatBcMSql, matEntity);
					sqllist.add(updateMatBcMSql);

					inCount = inCount - gpreqCount;
					if (inCount <= 0 && i + 1 < plnOrderZy.size()) {
						break;
					}

				}
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			// //查询上料等于废品加下线，不允许有离线
			// int LoadingPiece = 0;
			// int InstorePiece = 0;
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.QueryMatbcmPiece");//交库后查库存表支数
			// sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
			// psta = connection.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if(rs.next())
			// {
			// InstorePiece =
			// Integer.parseInt(rs.getString("REAL_OUTDOUBLE_NUM"));
			// }
			// rs.close();
			// psta.close();
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.QueryLoadingPiece");//上料支数,下线支数
			// sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
			// psta = connection.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if(rs.next())
			// {
			// LoadingPiece =
			// Integer.parseInt(rs.getString("REAL_INDOUBLE_NUM"));
			// }
			// rs.close();
			// psta.close();
			// int OfflinePiece = 0;
			// int RePiece = 0;
			// int groupPiece = 0;
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.QueryOfflinePiece");//离线和返线支
			// sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
			// psta = connection.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if(rs.next())
			// {
			// OfflinePiece = Integer.parseInt(rs.getString("OFFLINE_NUM"));
			// RePiece = Integer.parseInt(rs.getString("RE_NUM"));
			// groupPiece = Integer.parseInt(rs.getString("GROUP_NUM"));
			// }
			// rs.close();
			// psta.close();
			int PortPiece = 0;
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmHttInStore.QueryVrpPortFailPiece");// 各实绩废品和
			sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				PortPiece = Integer.parseInt(rs.getString("ACT_COUNT"));
			}
			rs.close();
			psta.close();
			String load = "0";
			// if(LoadingPiece == InstorePiece + OfflinePiece - RePiece
			// +PortPiece)
			// {
			// //cro.setV_errMsg("上料支数和下线支数已平,请尽快结炉!");
			// load = "1";
			// }
			if (PortPiece == 0) {
				load = "1";
			}

			// 运输需求
			FrmVrpInStore frmVrpInStore = new FrmVrpInStore();
//			TZH 24.4.24 注销物流接口
//			frmVrpInStore.updateTmsDemandM(connection, connection2, cro,
//					plnZyEntity.getJudgeStoveNo(), plnZyEntity.getHeatPlanNo(),
//					"XX");
			if (cro.getV_errMsg().equals("")) {
				cro.setV_errMsg("交库成功!");
				cro.setResult(load);
			}
			connection.commit();
//			TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			System.out.println(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 cro;
	}

	/***
	 * 撤销交库
	 * 
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject cancelPipe(ArrayList<String> parmList) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		int appCount = 0;
		String appCode = "";
		String HeatPlanNo = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			ArrayList<String> sqllist = new ArrayList<String>();
			for (String str : parmList) {
				YdmBcInlistEntity ydmEntity = EntityTool.getEntityByJSONStr(
						str, YdmBcInlistEntity.class);
				if (!ydmEntity.getStatus().equals("未倒运")) {
					cro.setV_errMsg("不在产出库,不允许撤销交库!");
					return cro;
				}

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.selectNode");// 查询是否已结炉
				sql = EntityTool.fillSqlByEntity(sql, ydmEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					appCount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (appCount > 0) {
					cro.setV_errMsg("该炉已结炉,不允许撤销交库!");
					return cro;
				}
				// int value = 0;
				// sql =
				// CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.SelectApplyNoF");//查询申请判定表是否有这炉数据
				// sql = EntityTool.fillSqlByEntity(sql, ydmEntity);
				// psta = connection.prepareStatement(sql);
				// rs = psta.executeQuery();
				// if(rs.next())
				// {
				// value = Integer.parseInt(rs.getString("COUNT"));
				// }
				// rs.close();
				// psta.close();
				// if(value>0)
				// {
				// cro.setV_errMsg("已经申请判定，不允许撤销交库!");
				// return cro;
				// }
				String liku = "";
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpInStore.SelectMatGpStorageF");// 查询在库物料支数
				sql = EntityTool.fillSqlByEntity(sql, ydmEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					appCount = Integer.parseInt(rs.getString("ACOUNT"));
					appCode = rs.getString("JUGDE_APPLY_CODE");
					liku = rs.getString("LOCK_LIKU");
				}
				rs.close();
				psta.close();
				if (appCode != null && !appCode.equals("")) {
					cro.setV_errMsg("已经申请判定,不允许撤销交库!");
					return cro;
				}
				if (appCount != Integer.parseInt(ydmEntity.getActCount())) {
					cro.setV_errMsg("产出库找不到足够的库存!");
					return cro;
				}
				if (!liku.equals("0")) {
					cro.setV_errMsg("该炉已经利库,不允许撤销交库!");
					return cro;
				}
				String deleteInListSql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.DeleteInList");
				deleteInListSql = EntityTool.fillSqlByEntity(deleteInListSql,
						ydmEntity);
				sqllist.add(deleteInListSql);

			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			// 运输需求
			YdmBcInlistEntity ydmEntity2 = EntityTool.getEntityByJSONStr(
					parmList.get(0), YdmBcInlistEntity.class);
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmHttInStore.selectHeatPlanNo");// 查询炉计划
			sql = EntityTool.fillSqlByEntity(sql, ydmEntity2);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				HeatPlanNo = rs.getString("HEAT_PLAN_NO");
			}
			rs.close();
			psta.close();
			FrmVrpInStore frmVrpInStore = new FrmVrpInStore();
//			TZH 24.4.24 注销物流接口
//			frmVrpInStore.updateTmsDemandM(connection, connection2, cro,
//					ydmEntity2.getJudgeStoveNo(), HeatPlanNo, "XX");
			if (cro.getV_errMsg().equals("")) {
				cro.setV_errMsg("撤销成功!");
			}
			connection.commit();
//			TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			System.out.println(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 cro;

	}

	/***
	 * 结炉
	 * 
	 * @param plnTity
	 * @return
	 */
	public CoreReturnObject doBeside(String plnTity) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		int count = 0;
		String nodeFlag = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			ArrayList<String> sqllist = new ArrayList<String>();
			PlnZyRclMEntity1 plnEntity = EntityTool.getEntityByJSONStr(plnTity,
					PlnZyRclMEntity1.class);
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmHttInStore.selectLxCount");
			sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				count = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			psta.close();
			if (count > 0) {
				cro.setV_errMsg("存在待处理品，不允许结炉!");
				return cro;
			}

			// /**
			// * 管号队列合格已入库的支数与判定支数相等 炉计划关闭
			// */
			// int doPiece = 0;
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.QuerydoJudgePiece");//查询已判定支数
			// sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			// psta = connection.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if(rs.next())
			// {
			// doPiece = Integer.parseInt(rs.getString("ACT_COUNT"));
			// }
			// rs.close();
			// psta.close();
			// //查询上料等于废品加下线，不允许有离线
			// int LoadingPiece = 0;
			// int InstorePiece = 0;
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.QueryMatbcmPiece");//交库后查库存表支数
			// sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			// psta = connection.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if(rs.next())
			// {
			// InstorePiece =
			// Integer.parseInt(rs.getString("REAL_OUTDOUBLE_NUM"));
			// }
			// rs.close();
			// psta.close();
			// if(InstorePiece == doPiece)
			// {
			// if(!plnEntity.getPlanstatus().equals("40"))
			// {
			// String updateSql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.UpdatePlanStatus");
			// updateSql = EntityTool.fillSqlByEntity(updateSql, plnEntity);
			// sqllist.add(updateSql);
			// }
			// }
			//			
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.QueryLoadingPiece");//上料支数,下线支数
			// sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			// psta = connection.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if(rs.next())
			// {
			// LoadingPiece =
			// Integer.parseInt(rs.getString("REAL_INDOUBLE_NUM"));
			// }
			// rs.close();
			// psta.close();
			// int OfflinePiece = 0;
			// int RePiece = 0;
			// int groupPiece = 0;
			// sql =
			// CoreXmlSqlParsersFactory.getSql("FrmHttInStore.QueryOfflinePiece");//离线和返线支
			// sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			// psta = connection.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if(rs.next())
			// {
			// OfflinePiece = Integer.parseInt(rs.getString("OFFLINE_NUM"));
			// RePiece = Integer.parseInt(rs.getString("RE_NUM"));
			// groupPiece = Integer.parseInt(rs.getString("GROUP_NUM"));
			// }
			// rs.close();
			// psta.close();
			String actCount = "";
			// 如果该工序都是废品结炉后变成生产完成
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmHttInStore.queryPortStuts");// 查询管号队列状态
			sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				actCount = rs.getString("ACT_COUNT");
			}
			rs.close();
			psta.close();
			if (Integer.parseInt(actCount) == 0) {
				String updateSqlPlnStuts = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.plnupdateStuts");// 结炉修改计划工序状态为生产完成
				updateSqlPlnStuts = EntityTool.fillSqlByEntity(
						updateSqlPlnStuts, plnEntity);
				sqllist.add(updateSqlPlnStuts);
			}
			int PortPiece = 0;
			// 获取镦拔扩计划从表实体类
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmHttInStore.QueryVrpPortFailPiece");// 各实绩废品和
			sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				PortPiece = Integer.parseInt(rs.getString("ACT_COUNT"));
			}
			rs.close();
			psta.close();
			// if(LoadingPiece != InstorePiece + OfflinePiece - RePiece
			// +PortPiece)
			// {
			// cro.setV_errMsg("上料支数和下线支数不平,不允许结炉!");
			// return cro;
			// }
			if (PortPiece > 0) {
				cro.setV_errMsg("上料支数和下线支数不平,不允许结炉!");
				return cro;
			}
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmHttInStore.selectNodeFlag");
			sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				nodeFlag = rs.getString("BESIDE_STATUS");
			}
			rs.close();
			psta.close();

			String likuCode = "";
			// 查询库存标志位
			sql = CoreXmlSqlParsersFactory.getSql("FrmHttInStore.selectMatBc");
			sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				likuCode = rs.getString("LIKU_PROCESS_CODE");
			}
			rs.close();
			psta.close();
			if (likuCode == null || likuCode.equals("")) {
				if (nodeFlag == null
						|| Integer.parseInt(nodeFlag) < (plnEntity
								.getRealProcessSeq().equals(
										plnEntity.getProcessSeq())
								&& !plnEntity.getPlineCode().equals("C015")&& !plnEntity.getPlineCode().equals("C091") ? 80
								: 70)) {
					cro.setV_errMsg("上工序未结炉,不允许结炉!");
					return cro;
				}
			}

			if (plnEntity.getRealProcessSeq().equals(plnEntity.getProcessSeq())) {
				String updateSql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.UpdateNodeFlag");
				updateSql = EntityTool.fillSqlByEntity(updateSql, plnEntity);
				sqllist.add(updateSql);
			} else {
				String updateSql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.UpdateNodeFlag1");
				updateSql = EntityTool.fillSqlByEntity(updateSql, plnEntity);
				sqllist.add(updateSql);
			}

			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			// 运输需求
			FrmVrpInStore frmVrpInStore = new FrmVrpInStore();
//			TZH 24.4.24 注销物流接口
//			frmVrpInStore.updateTmsDemandM(connection, connection2, cro,
//					plnEntity.getJudgeStoveNo(), plnEntity.getHeatPlanNo(),
//					"JL");
			if (cro.getV_errMsg().equals("")) {
				cro.setV_errMsg("结炉成功!");
			}
			connection.commit();
//			TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			System.out.println(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 cro;

	}

	/***
	 * 结炉回退
	 * 
	 * @param plnTity
	 * @return
	 */
	public CoreReturnObject cancelDoBeside(String plnTity) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		int count = 0;
		String nodeFlag = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			ArrayList<String> sqllist = new ArrayList<String>();
			PlnZyRclMEntity1 plnEntity = EntityTool.getEntityByJSONStr(plnTity,
					PlnZyRclMEntity1.class);
			sql = CoreXmlSqlParsersFactory.getSql("FrmHttInStore.selectQcmApp");
			sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				count = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			psta.close();
			if (count == 0) {
				cro.setV_errMsg("该炉已申请判定,不允许结炉回退!");
				return cro;
			}

			sql = CoreXmlSqlParsersFactory
					.getSql("FrmHttInStore.selectNodeFlag");
			sql = EntityTool.fillSqlByEntity(sql, plnEntity);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				nodeFlag = rs.getString("BESIDE_STATUS");
			}
			rs.close();
			psta.close();

			if ((nodeFlag == null || !nodeFlag.equals("90"))
					&& !(!plnEntity.getRealProcessSeq().equals("1") && plnEntity
							.getPlanstatus().equals("10"))) {
				cro.setV_errMsg("未结炉, 不允许撤销!");
				return cro;
			}

			if (plnEntity.getRealProcessSeq().equals(plnEntity.getProcessSeq())
					&& !plnEntity.getPlanstatus().equals("10")
					&& !plnEntity.getBesideStatus().equals("")) {
				String updateSql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.CancelupdateNodeFlag");
				updateSql = EntityTool.fillSqlByEntity(updateSql, plnEntity);
				sqllist.add(updateSql);
			} else {
				String updateSql = CoreXmlSqlParsersFactory
						.getSql("FrmHttInStore.CancelupdateNodeFlag1");
				updateSql = EntityTool.fillSqlByEntity(updateSql, plnEntity);
				sqllist.add(updateSql);
			}

			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			// 运输需求
			FrmVrpInStore frmVrpInStore = new FrmVrpInStore();
//			frmVrpInStore.updateTmsDemandM(connection, connection2, cro,
//					plnEntity.getJudgeStoveNo(), plnEntity.getHeatPlanNo(),
//					"XX");
			if (cro.getV_errMsg().equals("")) {
				cro.setV_errMsg("结炉回退成功!");
			}
			connection.commit();
//			TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			System.out.println(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 cro;

	}

	/**
	 * 离线
	 * 
	 * @param load
	 * @return
	 */
	public CoreReturnObject offLineExpandResult(
			ArrayList<HttOfflineResultDentity> list1, ArrayList<String> list,
			String plinCode, String groudno) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyyMMdd");
		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);
		String systime = dateFormat.format(now);
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String addOff = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.AddOffLine");// 新增离线实绩
			PreparedStatement patAdd = connection.prepareStatement(addOff);
			String addOffD = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.AddOffLineD");// 新增离线实绩从表
			PreparedStatement patAddD = connection.prepareStatement(addOffD);
			String updateMat = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.UpdateMatNo");// 修改材料状态
			PreparedStatement patMatNo = connection.prepareStatement(updateMat);

			String updatePath = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.UpdatePathPortVrp");// 修改队列表工艺路径,材料，工序状态
			PreparedStatement pastupdatePath = connection
					.prepareStatement(updatePath);

			String heatPlanNo = list.get(0);
			String slgx = list.get(1);
			String slgxname = list.get(2);
			String name = list.get(3);
			String time = list.get(4);
			String bc = list.get(5);
			String bz = list.get(6);
			String count = list.get(7);

			int num = 0;
			int num1 = 0;
			int num2 = 0;
			if (slgx.equals("600202")) {
				num = 1;
				num1 = 2;
				num2 = 4;
			} else if (slgx.equals("600203")) {
				num = 1;
				num1 = 4;
				num2 = 6;
			} else if (slgx.equals("600204")) {
				num = 1;
				num1 = 6;
				num2 = 8;
			} else if (slgx.equals("600205")) {
				num = 1;
				num1 = 8;
				num2 = 10;
			} else if (slgx.equals("600206")) {
				num = 1;
				num1 = 10;
				num2 = 12;
			} else if (slgx.equals("600207")) {
				num = 1;
				num1 = 12;
				num2 = 14;
			} else if (slgx.equals("600208")) {
				num = 1;
				num1 = 16;
				num2 = 18;
			} else if (slgx.equals("600209")) {
				num = 1;
				num1 = 18;
				num2 = 20;
			} else if (slgx.equals("600210")) {
				num = 1;
				num1 = 20;
				num2 = 22;
			} else if (slgx.equals("600211")) {
				num = 1;
				num1 = 22;
				num2 = 24;
			}

			// 获取墩拔扩计划实体类
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					heatPlanNo);
			// 获取墩拔扩计划从表实体类
			PlnZyRclCEntity plnC = PlanService.getSendHttPlanCInfoC(connection,
					heatPlanNo);
			if (pln == null)
				return core;
			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.qureyGxComInfo");// 查询工序结炉状态
			PreparedStatement psta1 = connection.prepareStatement(sql);
			psta1.setString(1, heatPlanNo);
			psta1.setString(2, plinCode);
			psta1.setString(3, slgx);
			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();
			List<PortHttBatchSampleResultEntity> matNo = new ArrayList<PortHttBatchSampleResultEntity>();
			String sqlloading1 = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.queryPortVrpHg");// 查询管号队列里合格的材料号
			psta = connection.prepareStatement(sqlloading1);
			psta.setObject(1, heatPlanNo);
			psta.setObject(2, groudno);
			psta.setObject(3, Integer.parseInt(count) + 1);
			ResultSet rs2 = psta.executeQuery();
			matNo = EntityTool.getEntitysByRs(rs2,
					PortHttBatchSampleResultEntity.class);
			rs2.close();
			psta.close();
			if (matNo == null) {
				core.setV_errMsg("管号队列全为样管，不能离线！");
				return core;
			}
			int batchCount = 0;
			sqlloading1 = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.queryPortVrpBatchHg");// 查询管号队列里不是样管的合格支
			psta = connection.prepareStatement(sqlloading1);
			psta.setObject(1, heatPlanNo);
			rs2 = psta.executeQuery();
			if (rs2.next()) {
				batchCount = Integer.parseInt(rs2.getString("ACT_COUNT"));
			}
			rs2.close();
			psta.close();
			if (batchCount < Integer.parseInt(count)) {
				core.setV_errMsg("管号队列材料合格支数不足,请重新输入离线支数！");
				return core;
			}

			String judge = "";
			String batch = "";
			String procode = "";
			String proname = "";
			String stecode = "";
			String stename = "";
			String gracode = "";
			String graname = "";
			String modecode = "";
			String modedesc = "";
			String stdcode = "";
			String stdname = "";
			String stdstyle = "";
			String stdstyledesc = "";
			String actdimater = "";
			String actheight = "";
			String actlen = "";
			String actlenmin = "";
			String actlenmax = "";

			int countNum = 0;
			double weightNum = 0;
			for (PortHttBatchSampleResultEntity prot : matNo) {
				countNum += Integer.parseInt(prot.getActCount());
				weightNum += Double.parseDouble(prot.getActWeight());
				// 修改管号队列状态
				pastupdatePath.setObject(1, num);
				pastupdatePath.setObject(2, num1);
				pastupdatePath.setObject(3, "2");
				pastupdatePath.setObject(4, num2);
				pastupdatePath.setObject(5, "20");
				pastupdatePath.setObject(6, "10");
				pastupdatePath.setObject(7, slgx);
				pastupdatePath.setObject(8, heatPlanNo);
				pastupdatePath.setObject(9, prot.getMatNo());
				pastupdatePath.addBatch();

				// 修改材料表状态
				patMatNo.setObject(1, "80150104");
				// patMatNo.setObject(2, "11");
				patMatNo.setObject(2, prot.getMatNo());
				patMatNo.addBatch();
				judge = prot.getJudgeStoveNo();
				batch = prot.getBatchNo();
				actdimater = prot.getActDimater();
				actheight = prot.getActHeight();
				actlen = prot.getActLen();
				actlenmin = prot.getActLenMin();
				actlenmax = prot.getActLenMax();
			}
			pastupdatePath.executeBatch();

			patMatNo.executeBatch();

			String ifMonitNo = "";
			if (pln.getIfMonitor().equals("是")) {
				ifMonitNo = "1";
			} else {
				ifMonitNo = "0";
			}
			String resultNo = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.queryOffMaxRESULT");// 查询离线最大实绩编号
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, plinCode + slgx + "L" + matter1.format(dt));
			psta.setObject(2, matter.format(dt));
			psta.setObject(3, pln.getPlineCode());
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();

			// 新增离线实绩
			for (PortHttBatchSampleResultEntity lvMat : matNo) {
				patAdd.setObject(1, resultNo);
				patAdd.setObject(2, pln.getPlineCode());
				patAdd.setObject(3, pln.getPlineName());
				patAdd.setObject(4, slgx);
				patAdd.setObject(5, slgx);
				patAdd.setObject(6, heatPlanNo);
				patAdd.setObject(7, pln.getZyBatchId());
				patAdd.setObject(8, pln.getIndexSeq());
				patAdd.setObject(9, pln.getProPlanId());
				patAdd.setObject(10, pln.getGxPlanNo());

				patAdd.setObject(11, judge);
				patAdd.setObject(12, batch);
				patAdd.setObject(13, groudno);
				patAdd.setObject(14, pln.getProduccode());
				patAdd.setObject(15, pln.getProducname());
				patAdd.setObject(16, pln.getSteelcode());
				patAdd.setObject(17, pln.getSteelname());
				patAdd.setObject(18, pln.getGradecode());
				patAdd.setObject(19, pln.getGradename());
				patAdd.setObject(20, pln.getModelCode());
				patAdd.setObject(21, pln.getModelDesc());
				patAdd.setObject(22, pln.getStdCode());
				patAdd.setObject(23, pln.getStdName());
				patAdd.setObject(24, pln.getStdStyle());
				patAdd.setObject(25, pln.getStdStyleDesc());
				patAdd.setObject(26, actdimater);
				patAdd.setObject(27, actheight);
				patAdd.setObject(28, actlen);
				patAdd.setObject(29, actlenmin);
				patAdd.setObject(30, actlenmax);
				patAdd.setObject(31, pln.getOrdLnDlyPk());
				patAdd.setObject(32, pln.getOrderNo());
				patAdd.setObject(33, pln.getOrderSeq());
				patAdd.setObject(34, pln.getDeliveryNo());
				patAdd.setObject(35, countNum);
				patAdd.setObject(36, weightNum);
				patAdd.setObject(37, name);
				patAdd.setObject(38, bc);
				patAdd.setObject(39, bz);
				patAdd.setObject(40, time);
				// 如果有离线新增到材料表
				if (!count.equals("") && !count.equals("0")) {
					PlanService.AddOffLineMat(connection, "F", name, "11",
							lvMat.getMatNo(), slgx, heatPlanNo);
				}
			}
			// 新增离线从表
			for (int i = 0; i < list1.size(); i++) {
				String resultsqe = "";
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("AppearanceService.querymaxsqe");// 查询从表最大序号
				psta = connection.prepareStatement(sqlloading);
				psta.setObject(1, matter1.format(now));
				psta.setObject(2, heatPlanNo);
				psta.setObject(3, plinCode);
				ResultSet rs3 = psta.executeQuery();
				if (rs3.next()) {
					resultsqe = rs3.getString("RESULT_SQE");
				}

				rs3.close();
				psta.close();
				HttOfflineResultDentity resultD = list1.get(i);
				patAddD.setObject(1, resultNo);
				patAddD.setObject(2, resultsqe);
				patAddD.setObject(3, heatPlanNo);
				patAddD.setObject(4, plinCode);
				patAddD.setObject(5, pln.getPlineName());
				patAddD.setObject(6, count);
				patAddD.setObject(7, resultD.getGowhereCode());
				patAddD.setObject(8, resultD.getGowhereName());
				patAddD.setObject(9, time);
				patAddD.setObject(10, slgx);
				patAddD.setObject(11, slgx);
				patAddD.addBatch();
				patAddD.executeBatch();
			}
			patAdd.addBatch();
			patAdd.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 connection
	 * @param mangMentNo
	 * @return
	 * @throws SQLException
	 */
	private String getStorageNo(Connection conn, String plineCode)
			throws SQLException {
		String storageNo = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.getStorageNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, plineCode);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			storageNo = rs.getString("STORAGE_NO");
		}
		rs.close();
		psta.close();
		return storageNo;
	}

	/**
	 * 每支材料对应的入库流水号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSql(Connection conn, String inDoc) throws Exception {
		int seq = 0;
		String inListDocSeq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.querySendSql");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, inDoc);
		psta.setString(2, inDoc);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDocSeq = rs.getString("INSTOCK_SQE");
		}
		rs.close();
		psta.close();
		return inListDocSeq;
	}

	/***
	 * 获取入库单号
	 * 
	 * @param conn
	 * @param strInbound
	 * @return
	 * @throws SQLException
	 */
	public String getInListDoc(Connection conn, String strInbound)
			throws SQLException {
		String inListDoc = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.getInListDoc1");
		PreparedStatement psta = conn.prepareStatement(sql);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDoc = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return inListDoc;
	}

	/***
	 * 生成组号
	 * 
	 * @param con
	 * @param matTity
	 * @param parm
	 * @return
	 */
	public String creatGroup(Connection con, String plnZyTity, String str)
			throws Exception {
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String strGroup = "";
		String actLenMin = "";
		String actLenMax = "";
		String[] actLenM = new String[2];
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHMMSSFFF");
		String systime = dateFormat.format(now);
		PlnZyRclMEntity milPlan = EntityTool.getEntityByJSONStr(plnZyTity,
				PlnZyRclMEntity.class);
		PortHttBatchSampleResultEntity1 portEntity = EntityTool
				.getEntityByJSONStr(str, PortHttBatchSampleResultEntity1.class);
		sql = CoreXmlSqlParsersFactory.getSql("creatGroup.selectMilInStore");
		psta = con.prepareStatement(sql);
		psta.setString(1, milPlan.getJudgeStoveNo());
		psta.setString(2, portEntity.getPhyBatchNo());
		psta.setString(3, portEntity.getActLen());
		psta.setString(4, portEntity.getActLen());
		psta.setString(5, "F");
		rs = psta.executeQuery();
		if (rs.next()) {
			strGroup = rs.getString("BATCH_GROUD_NO");
		}
		rs.close();
		psta.close();

		if (strGroup.equals("")) {
			strGroup = queryGroupNo(con, milPlan.getJudgeStoveNo(), portEntity
					.getPhyBatchNo());// 组号
			if (Double.parseDouble(milPlan.getLengthmin()) > Double
					.parseDouble(portEntity.getActLen())) {
				actLenM = lessThanActMin(portEntity.getActLen(),
						new DecimalFormat("0.000")
								.format(Double.parseDouble(milPlan
										.getLengthmin().toString())));// 长度小于下限
				actLenMin = actLenM[0];
				actLenMax = actLenM[1];
			} else if (Double.parseDouble(portEntity.getActLen()) > Double
					.parseDouble(milPlan.getLengthmax())) {
				actLenM = moreThanActMax(portEntity.getActLen(),
						new DecimalFormat("0.000")
								.format(Double.parseDouble(milPlan
										.getLengthmax().toString())));// 长度大于上限
				actLenMin = actLenM[0];
				actLenMax = actLenM[1];
			} else {
				actLenMin = milPlan.getLengthmin();
				actLenMax = milPlan.getLengthmax();
			}
			sql = CoreXmlSqlParsersFactory
					.getSql("creatGroup.InsertMilInStore");// 插入记录
			psta = con.prepareStatement(sql);
			psta.setString(1, systime + strGroup);
			psta.setString(2, milPlan.getJudgeStoveNo());
			psta.setString(3, portEntity.getPhyBatchNo());
			psta.setString(4, portEntity.getIsSamplePipe());
			psta.setString(5, "");
			psta.setString(6, actLenMin);
			psta.setString(7, actLenMax);
			psta.setString(8, strGroup);
			psta.setString(9, "F");
			psta.addBatch();
			psta.executeBatch();
			con.commit();
		}
		return strGroup;
	}

	/**
	 * 根据炉号、批号自动组号
	 * 
	 * @param conn
	 *            c
	 * @param strLcode
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryGroupNo(Connection conn, String strLcode, String strBatch)
			throws Exception {
		int count = 0;
		String groupNo = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.queryGroupNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strLcode);
		psta.setString(2, strBatch);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			groupNo = rs.getString("BATCH_GROUD_NO");
		}
		rs.close();
		psta.close();
		return groupNo;
	}

	/***
	 * 小于长度下限时
	 * 
	 * @param length
	 * @param actLenMin
	 * @return
	 */
	public String[] lessThanActMin(String length, String actLenMin) {
		String[] arr = new String[2];
		String[] lengmin = new String[2];// 下限返回的整数位 和小数位
		String[] lengmax = new String[2];// 上限
		double lengthmin = Double.parseDouble(actLenMin);
		double lengthmax = Double.parseDouble(actLenMin);

		for (int i = 0; i < 5; i++) {
			lengthmax = lengthmin;
			lengthmin = lengthmin - 1;
			lengmin = getNumber(String.valueOf(lengthmin));
			if (Double.parseDouble(lengmin[1]) > 0
					&& 800 > Double.parseDouble(lengmin[1])
					&& !lengmin[1].equals("5")) {
				lengthmin = Double.parseDouble(lengmin[0].toString() + ".5");
			} else if (Double.parseDouble(lengmin[1]) > 800) {
				lengthmin = Double.parseDouble(String.valueOf(Integer
						.parseInt(lengmin[0]) + 1)
						+ ".0");
			}
			if (Double.parseDouble(length) >= lengthmin
					&& lengthmax > Double.parseDouble(length)) {
				arr[0] = String.valueOf(lengthmin);
				arr[1] = String.valueOf(lengthmax - 0.001);
				break;
			}
		}
		return arr;
	}

	/***
	 * () 大于长度上限时
	 * 
	 * @param length
	 * @param actLenMin
	 * @return
	 */
	public String[] moreThanActMax(String length, String actLenMax) {
		String[] arr = new String[2];
		String[] lengmax = new String[2];// 上限返回的整数位 和小数位
		String[] lengmin = new String[2];// 下限
		double lengthmin = Double.parseDouble(actLenMax);
		double lengthmax = Double.parseDouble(actLenMax);

		for (int i = 0; i < 5; i++) {
			lengthmin = lengthmax;
			lengthmax = lengthmax + 1;

			lengmax = getNumber(new DecimalFormat("0.000").format(lengthmax));
			if (Integer.parseInt(lengmax[1]) > 0
					&& 500 > Integer.parseInt(lengmax[1])
					&& !lengmax[1].equals("5")) {
				lengthmax = Double.parseDouble(lengmax[0].toString() + ".0");
			} else if (Integer.parseInt(lengmax[1]) > 500
					&& Integer.parseInt(lengmax[1]) < 800) {
				lengthmax = Double.parseDouble(lengmax[0].toString() + ".5");
			} else if (Integer.parseInt(lengmax[1]) > 800) {
				lengthmax = Double.parseDouble(String.valueOf(Integer
						.parseInt(lengmax[0]))
						+ ".5");
			}

			if (Double.parseDouble(length) > lengthmin
					&& lengthmax >= Double.parseDouble(length)) {
				arr[0] = String.valueOf(lengthmin + 0.001);
				arr[1] = String.valueOf(lengthmax);
				break;
			}

		}
		return arr;
	}

	public String[] getNumber(String len) {
		String[] decimal = new String[2];
		int num = len.indexOf(".");
		decimal[0] = len.substring(0, num);
		decimal[1] = len.substring(num + 1);
		return decimal;
	}
}
