package com.steering.pss.plnsaleord.planStove;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.planStove.entity.MatBcTrackEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;
import com.steering.pss.plnsaleord.stove.corePlanFurnProM;
import com.steering.pss.plnsaleord.stove.entity.*;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

/**
 * 管坯材料信息
 * 
 * @author teng 2017.11.21
 */
public class FrmPlanMaterialRcl extends DBCommon {
	/**
	 * 根据轧管投入产出信息
	 * 
	 * @return
	 */
	public CoreReturnObject queryBaseMaterialDPl(String inwlId, String outwlId,
			String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmPlanMaterialRcl.queryBaseMaterialDPl");
		return this.getData(sql, new Object[] { inwlId, outwlId, plineCode });
	}

	/**
	 * 坯子利库
	 * 
	 * @param jslist
	 * @param userName
	 *            利库人
	 * @param proPlanId
	 *            订单号
	 * @param gxPlanNo
	 *            排产序号
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doAddLibrary(ArrayList<ArrayList<String>> jslist,
			String userName, String proPlanId, String gxPlanNo)
			throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			ResultSet rs = null;
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 查询热处理信息
			PlnOrderRclSEntity plnRclS = FrmPlanMaterialDbk.getPlanOrderS(conn,
					proPlanId, gxPlanNo, PlnOrderRclSEntity.class, "F");
			if (plnRclS == null) {
				cro.setV_errMsg("未找到热处理订单(" + proPlanId + "/" + gxPlanNo
						+ ")信息!");
				return cro;
			}
			// 判断是否排产订单下发
			// if (plnOrderS.getIfStoveIssued().equals("1")) {
			// cro.setV_errMsg("轧管订单(" + proPlanId + "/" + gxPlanNo
			// + ")已下发,不能利该轧管订单,如需利该轧管订单,请拆分订单!");
			// return cro;
			// }
			// int lkNum = 0;
			// double lkWt = 0;
			int seq = 1;
			// 循环库存信息
			for (int i = 0; i < jslist.size(); i++) {
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"yyyyMMddHHmmss");
				DecimalFormat df = new DecimalFormat("0000000000");
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String judgeStoveNo = list.get(0);
				String batchNo = list.get(1);
				String batchGroupBy = list.get(2);
				String materialNo = list.get(3);
				int takeCount = Integer.parseInt(list.get(4));// 利库支
				String proPlanIdNo = list.get(5);// 本工序订单号
				// String UpproPlanIdNo = list.get(6);// 上工序订单号
				String specCic = list.get(7);
				String storageNo = list.get(8);
				String plineCode = list.get(9);
				List<MatBcMEntityPln> matGpMlist = FrmPlanMaterialDbk
						.getQureyMatGpM(conn, judgeStoveNo, batchNo,
								batchGroupBy, materialNo, takeCount, "", "F","","",storageNo,plineCode);
				if (matGpMlist == null) {
					cro.setV_errMsg("管坯物料已发生改变,请刷新!");
					return cro;
				}
				MatBcTrackEntity matTrack = FrmPlanMaterialDbk
						.getQureyMatTrack(conn, judgeStoveNo, batchNo,
								batchGroupBy, takeCount, "0", proPlanIdNo,
								MatBcTrackEntity.class, "add","","");
				if (matTrack == null) {
					cro.setV_errMsg("管坯物料已发生改变,请刷新!");
					return cro;
				}
				double actWt = 0;
				String jugdeApplyCode = "";// 是否有申请判定单号
				int countAppCode = 0;

				String stoveNo = "";
				String upperHeatplanId = "";
				String upProessCode = "";
				String inwlId = "";
				String inwlDesc = "";
				String lengthIn = "";
				String lengthmaxIn = "";
				String lengthminIn = "";
				String outdiameterIn = "";
				String wallthickIn = "";
				String flagStoveNo = "";
				String gradeName = "";
				for (MatBcMEntityPln matGpM : matGpMlist) {
					countAppCode++;
					gradeName = matGpM.getGradename();
					jugdeApplyCode = matGpM.getJugdeApplyCode();
					stoveNo = matGpM.getStoveNo();
					flagStoveNo = matGpM.getFlagStoveNoZg();
					inwlId = matGpM.getMaterialNo();
					inwlDesc = matGpM.getMaterialName();
					upProessCode = matGpM.getProcessCdoe();
					upperHeatplanId = matGpM.getHeatPlanNo();
					lengthIn = matGpM.getActLen();
					lengthmaxIn = matGpM.getActLenMax();
					lengthminIn = matGpM.getActLenMin();
					outdiameterIn = matGpM.getActDimater();
					wallthickIn = matGpM.getActHeight();
					//actWt += Double.parseDouble(matGpM.getActWeight());
					actWt = Double.parseDouble(matGpM.getActWeight());
					if (!matGpM.getProductFlag().equals("801401")) {
						cro.setV_errMsg("材料类别不是在制品,不能利库!");
						return cro;
					}
					if (!matGpM.getMatStatus().equals("80150102")
							&& !matGpM.getInstancyFlag().equals("1")) {
						cro.setV_errMsg("材料状态不是产出已判定状态并且不是紧急放行状态,不能利库");
						return cro;
					}
					if (!matGpM.getQcmStaus().equals("00")) {
						cro.setV_errMsg("交库状态不是合格状态,不能利库!");
						return cro;
					}


					// 修改坯子库存信息
					String sqlUpdateMat = CoreXmlSqlParsersFactory
							.getSql("FrmPlanMaterialDbk.UpdateMatGp");
					ps = conn.prepareStatement(sqlUpdateMat);
					ps.setObject(1, "80150103");
					ps.setObject(2, proPlanId);
					ps.setObject(3, gxPlanNo);
					ps.setObject(4, "1");
					ps.setObject(5, "1");
					ps.setObject(6, "F");
					ps.setObject(7, "热处理");
					ps.setObject(8, matGpM.getMatNo());
					ps.executeUpdate();
					ps.close();



					String resumeSeq = dateFormat.format(now) + df.format(seq);
					// 生成利库日志
					matTrack.setResumeSeq(resumeSeq);
					matTrack.setEventId("900102");
					matTrack.setEventDesc("利库");
					matTrack.setFormName("com.steering.pss.plnsaleord.planStove.FrmPlanMaterialRcl");
					matTrack.setCallFuncCoding("doAddLibrary");
					matTrack.setKeyvalue1("List<MatBcMEntityPln> matGpMlist");
					matTrack.setKeyvalue1Desc("物料主表关键字实体类集合");
					matTrack.setKeyvalue2("String userName");
					matTrack.setKeyvalue2Desc2("操作人");
					matTrack.setProcessCdoe("F");
					matTrack.setMemo("");
					matTrack.setCreateName(userName);
					matTrack.setCreateTime(dateTime);
					seq++;
					// 新增利库记录
					String sqlAddTrack = EntityTool.fillSqlByEntity(
							CoreXmlSqlParsersFactory
									.getSql("FrmPlanMaterialDbk.addMatBcTrack"),
							matTrack);
					ps = conn.prepareStatement(sqlAddTrack);
					ps.executeUpdate();
					ps.close();

					int countM = 0;
					String heatPlanNo = "";
					String planStatus = "00";
					// 根据判定炉号，订单号查询是否存在虚拟炉计划
					String sqlQuery = "SELECT COUNT(*) COUNT,T.HEAT_PLAN_NO,T.PLANSTATUS,T.IF_MATCH_ODD FROM PLN_ZY_RCL_M T WHERE T.FEED_HEAT_NO=? "
							+ " AND T.PRO_PLAN_ID=? AND T.GX_PLAN_NO=? GROUP BY T.HEAT_PLAN_NO,T.PLANSTATUS,T.IF_MATCH_ODD";
					ps = conn.prepareStatement(sqlQuery);
					ps.setObject(1, judgeStoveNo);
					ps.setObject(2, proPlanId);
					ps.setObject(3, gxPlanNo);
					rs = ps.executeQuery();
					while (rs.next()) {
						heatPlanNo = rs.getString("HEAT_PLAN_NO");
						planStatus = rs.getString("PLANSTATUS");
						if (rs.getString("IF_MATCH_ODD").equals("1")) {
							countM = rs.getInt("COUNT");
							if (!planStatus.equals("00")
									&& !planStatus.equals("10")) {
								cro.setV_errMsg("该热处理订单(" + proPlanId + "/"
										+ gxPlanNo + "),判定炉号(" + judgeStoveNo
										+ ")已存在炉计划,并已上料,如需利库,请先撤销上料或拆分订单!");
								return cro;
							}
						} else {
							cro.setV_errMsg("该热处理订单(" + proPlanId + "/" + gxPlanNo
									+ "),判定炉号(" + judgeStoveNo
									+ ")已存在不是利库炉计划,如需利库,请先撤销炉计划!");
							return cro;
						}
					}
					rs.close();
					ps.close();
					// 如果存在同炉,同订单号只执行一次新增虚拟炉计划
					if (countM == 0) {
						PlnZyRclMEntity plnRclM = new PlnZyRclMEntity();
						heatPlanNo = corePlanFurnProM
								.getMaxHeatPlanNo(conn, plnRclS.getProPlanId(),
										plnRclS.getProPlanId(), "F");
						// 获取炉计划顺序号
						int gxProSeq = corePlanFurnProM.getMaxGxPlnSeq(conn,
								plnRclS.getPlineCode(), "F");

						// 订单内炉顺序号
						int lotNo = corePlanFurnProM.getMaxLotNoSqe(conn,
								plnRclS.getProPlanId(), plnRclS.getGxPlanNo(), "F");
						plnRclM.setLotNoSqe(String.valueOf(lotNo));
						plnRclM.setDecision(plnRclS.getDecision());
						plnRclM.setStoveNo(stoveNo);
						plnRclM.setFeedHeatNo(judgeStoveNo);
						if(plineCode.equals("C072") && !plnRclS.getPlineCode().equals("C091"))
						{
							plnRclM.setJudgeStoveNo("");
							plnRclM.setFlagStoveNo("");
						}else
						{
							plnRclM.setJudgeStoveNo(judgeStoveNo);
							plnRclM.setFlagStoveNo(judgeStoveNo);
						}
						plnRclM.setUpperHeatplanId(upperHeatplanId);
						plnRclM.setPlanstatus("00");
						plnRclM.setUpperProcessCode(upProessCode);
						plnRclM.setPlineCode(plnRclS.getPlineCode());
						plnRclM.setPlineName(plnRclS.getPlineName());
						plnRclM.setProcessCodeC(plnRclS.getProcessCodeC());
						plnRclM.setProcessDescC(plnRclS.getProcessDescC());
						String outwlId = "";
						String outwlDesc = "";
						String materialNocp = "";
						String materialdesccp = "";
						// 根据投入物料，制程号，交货行pK,工艺序号查询本工序产出物料
						String sqlBom = CoreXmlSqlParsersFactory
								.getSql("FrmPlanMaterialDbk.queryBaseMaterialFPl");
						sqlBom += " AND T.F_IN_MATERIAL_NO=?";
						ps = conn.prepareStatement(sqlBom);
						ps.setObject(1, plnRclS.getOrdLnDlyPk());
						ps.setObject(2, plnRclS.getIndexSeq());
						ps.setObject(3, plnRclS.getCraftSeq());
						ps.setObject(4, inwlId);
						rs = ps.executeQuery();
						if (rs.next()) {
							outwlId = rs.getString("OUT_MATERIAL_NO");
							outwlDesc = rs.getString("OUT_MATERIAL_DESC");
							materialNocp = rs.getString("MATERIAL_NO");
							materialdesccp = rs.getString("MATERIAL_DESC");
							// plnRclM.setSpecCode(rs.getString("SPEC_CODE_C"));
							// plnRclM.setSpecName(rs.getString("SPEC_NAME_C"));
							plnRclM.setBom(rs.getString("BOM"));
							plnRclM.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
						} else {
							outwlId = plnRclS.getOutwlId();
							outwlDesc = plnRclS.getOutwlDesc();

							plnRclM.setBom(plnRclS.getBom());
							// cro.setV_errMsg("未找到判定炉号(" + judgeStoveNo
							// + ")在热处理的产出物料!");
							// return cro;
						}
						rs.close();
						ps.close();

						if (!plnRclS.getDecision().equals("A")) {
							// 通过制程判定是否末工序
							if (plnRclS.getWholeBacklogDesc().contains("加工")) {
								plnRclM.setNextProcessCode("G");
							} else if (plnRclS.getWholeBacklogDesc().contains("接箍")) {
								plnRclM.setNextProcessCode("H");
							} else {
								if (plnRclS.getOrdLnDlyPk().contains("ZZBY-J")) {
									plnRclM.setNextProcessCode("H");
								} else if (plnRclS.getOrdLnDlyPk().contains(
										"ZZBY-D")) {
									plnRclM.setNextProcessCode("G");
								} else {
									plnRclM.setNextProcessCode("");
								}
							}
							plnRclM.setIsendgx("0");
							plnRclM.setOutwlId(outwlId);
							plnRclM.setOutwlDesc(outwlDesc);
						} else {
							plnRclM.setNextProcessCode("");
							plnRclM.setIsendgx("1");
							plnRclM.setOutwlId(materialNocp);
							plnRclM.setOutwlDesc(materialdesccp);
						}

						plnRclM.setIfMatchOdd("1");
						plnRclM.setIfDoHeatPlan("1");
						plnRclM.setProPlanId(plnRclS.getProPlanId());
						plnRclM.setGxPlanNo(plnRclS.getGxPlanNo());
						plnRclM.setGradecode(matTrack.getGradecode());
						plnRclM.setGradename(matTrack.getGradename());
						plnRclM.setSteelcode(plnRclS.getSteelcode());
						plnRclM.setSteelname(plnRclS.getSteelname());
						plnRclM.setProduccode(plnRclS.getProduccode());
						plnRclM.setProducname(plnRclS.getProducname());
						plnRclM.setSpecCode(plnRclS.getSpecCode());
						plnRclM.setSpecName(plnRclS.getSpecName());
						plnRclM.setInwlId(inwlId);
						plnRclM.setInwlDesc(inwlDesc);

						plnRclM.setModelCode(plnRclS.getModelCode());
						plnRclM.setModelDesc(plnRclS.getModelDesc());
						// 单倍支吨
						plnRclM.setPlnInsingleNum(String.valueOf(takeCount));
						plnRclM.setPlnInsingleWt(String.valueOf(actWt));
						plnRclM.setPlnOutsingleNum(String.valueOf(takeCount));
						double burnoutRate = 0;
						// 查询产线烧损
						String sql = "SELECT NVL(T.BURNOUT_RATE,0)/100 BURNOUT_RATE FROM COM_BASE_PLINE T WHERE T.PLINE_CODE='"
								+ plnRclS.getPlineCode() + "'";
						ps = conn.prepareStatement(sql);
						rs = ps.executeQuery();
						if (rs.next()) {
							burnoutRate = rs.getDouble("BURNOUT_RATE");
						}
						rs.close();
						ps.close();
						double yjWt = actWt - actWt * burnoutRate;
						plnRclM.setPlnOutsingleW(String.valueOf(yjWt));

						String sqlZg = "";

						sqlZg = CoreXmlSqlParsersFactory
								.getSql("PlanFurnProduction.QueryWgZg");
						ps = conn.prepareStatement(sqlZg);
						ps.setObject(1, plnRclS.getOrdLnDlyPk());
						rs = ps.executeQuery();
						if (rs.next()) {
							plnRclM.setProdStandard(rs.getString("PROD_STANDARD"));
							plnRclM.setOutputStandard(rs
									.getString("OUTPUT_STANDARD"));
							plnRclM.setSecCraftFileNo(rs
									.getString("SEC_CRAFT_FILE_NO"));
							plnRclM.setZyBatchIdZg(rs.getString("ZY_BATCH_ID"));
						}
						rs.close();
						ps.close();
						// }
						plnRclM.setStdCode(plnRclS.getStdCode());
						plnRclM.setStdName(plnRclS.getStdName());
						plnRclM.setStdStyle(plnRclS.getStdStyle());
						plnRclM.setStdStyleDesc(plnRclS.getStdStyleDesc());
						plnRclM.setOutdiameterIn(outdiameterIn);
						plnRclM.setWallthickIn(wallthickIn);
						plnRclM.setLengthIn(lengthIn);
						plnRclM.setLengthmaxIn(lengthmaxIn);
						plnRclM.setLengthminIn(lengthminIn);
						plnRclM.setOrderSource(plnRclS.getOrderSource());
						plnRclM.setOrderNo(plnRclS.getOrderNo());
						plnRclM.setOrderSeq(plnRclS.getOrderSeq());
						plnRclM.setDeliveryNo(plnRclS.getDeliveryNo());
						plnRclM.setOrdLnDlyPk(plnRclS.getOrdLnDlyPk());
						plnRclM.setReqBlId(plnRclS.getReqBlId());
						plnRclM.setIfExport(plnRclS.getIfExport());
						plnRclM.setPriority(plnRclS.getPriority());
						plnRclM.setPriorityMemo(plnRclS.getPriorityMemo());
						plnRclM.setProMonth(plnRclS.getProMonth());
						plnRclM.setIfMonitor(plnRclS.getIfMonitorRcl());
						plnRclM.setPrdcrNo(plnRclS.getPrdcrNo());
						plnRclM.setMonitorPoint(plnRclS.getMonitorPoint());
						plnRclM.setWholeBacklogDesc(plnRclS.getWholeBacklogDesc());
						plnRclM.setStationRoad(plnRclS.getStationRoad());

						plnRclM.setCic(specCic);

						if (plnRclS.getSic() == null || plnRclS.getSic().equals("")) {
							plnRclM.setSic(plnRclS.getSic());
						} else {
							plnRclM.setSic(plnRclS.getSic().split(",")[0]);
						}
						if (plnRclS.getDic() == null || plnRclS.getDic().equals("")) {
							plnRclM.setDic(plnRclS.getDic());
						} else {
							plnRclM.setDic(plnRclS.getDic().split(",")[0]);
						}
						if (plnRclS.getPic() == null || plnRclS.getPic().equals("")) {
							plnRclM.setPic(plnRclS.getPic());
						} else {
							plnRclM.setPic(plnRclS.getPic().split(",")[0]);
						}
						plnRclM.setCraftFileMan(plnRclS.getCraftFileMan());
						plnRclM.setCraftFileNo(plnRclS.getCraftFileNo());
						plnRclM.setCraftSeq(plnRclS.getCraftSeq());
						plnRclM.setOutdiameter(plnRclS.getOutdiameter());
						plnRclM.setWallthick(plnRclS.getWallthick());
						// 查询合同最大最小长度
						double lenMax = 0;
						double lenMin = 0;
						String ordLen = "SELECT T.LENGTHMAX,T.LENGTHMIN FROM PLN_SALEORD T WHERE T.ORD_LN_DLY_PK=?";
						ps = conn.prepareStatement(ordLen);
						ps.setObject(1, plnRclS.getOrdLnDlyPk());
						rs = ps.executeQuery();
						if (rs.next()) {
							lenMax = rs.getDouble("LENGTHMAX");
							lenMin = rs.getDouble("LENGTHMIN");
						}
						rs.close();
						ps.close();
						plnRclM.setLengthmax(String.valueOf(lenMax));
						plnRclM.setLengthmin(String.valueOf(lenMin));
						plnRclM.setCreateName(userName);
						plnRclM.setCreateTime(dateTime);
						plnRclM.setProDay(dateTime.substring(0, 10)
								.replace("-", ""));
						plnRclM.setBalYearMonth(dateTime.substring(0, 7).replace(
								"-", ""));
						plnRclM.setIndexSeq(plnRclS.getIndexSeq());
						plnRclM.setHeatPlanNo(heatPlanNo);
						plnRclM.setGxProSeq(String.valueOf(gxProSeq));
						// 获取热处理循环次数
						String sqlSeq = CoreXmlSqlParsersFactory
								.getSql("FrmPlanMaterialRcl.QueryProcessSeq");
						ps = conn.prepareStatement(sqlSeq);
						ps.setObject(1, plnRclS.getProPlanId());
						ps.setObject(2, plnRclS.getGxPlanNo());
						rs = ps.executeQuery();
						if (rs.next()) {
							plnRclM.setProcessSeq(rs.getString("PROCESS_SEQ"));
						} else {
							plnRclM.setProcessSeq("1");
						}
						rs.close();
						ps.close();
						plnRclM.setRealProcessSeq("1");
						String ifProduct = "0";
						int counnum = StoveHelper.getQueryStation(conn,
								plnRclS.getProPlanId(), plnRclS.getGxPlanNo(), "F");
						if (counnum > 0) {
							ifProduct = "1";
						}
						plnRclM.setIfProduct(ifProduct);
						// 新增热处理虚拟炉计划
						String addPlnGxZg = EntityTool.fillSqlByEntity(
								CoreXmlSqlParsersFactory
										.getSql("PlanFurnProduction.addPlnZyRcl"),
								plnRclM);
						ps = conn.prepareStatement(addPlnGxZg);
						ps.executeUpdate();
						ps.close();
						// 新增生产批号
						StoveHelper.updatePlnRclGroup(conn, plnRclM.getProPlanId(),
								plnRclM.getGxPlanNo(), plnRclM.getPic());
					}
					int countC = 0;
					String heatPlanSqe = "";
					// 判定是否存在判定炉号，判定单号，同炉计划ID数据
					String sqlC = "SELECT COUNT(*) COUNT1,T.HEAT_PLAN_SQE FROM PLN_ZY_RCL_C T WHERE "
							+ "T.HEAT_PLAN_NO=? AND NVL(T.JUGDE_APPLY_CODE,'/')=NVL(?,'/') AND T.FEED_HEAT_NO=? AND T.BATCH_NO=? AND T.BATCH_GROUD_NO=?"
							+ " GROUP BY T.HEAT_PLAN_SQE";
					ps = conn.prepareStatement(sqlC);
					ps.setObject(1, heatPlanNo);
					ps.setObject(2, jugdeApplyCode);
					ps.setObject(3, judgeStoveNo);
					ps.setObject(4, batchNo);
					ps.setObject(5, batchGroupBy);
					rs = ps.executeQuery();
					if (rs.next()) {
						countC = rs.getInt("COUNT1");
						heatPlanSqe = rs.getString("HEAT_PLAN_SQE");
					}
					rs.close();
					ps.close();

					if (countC == 0) {
						PlnZyRclCEntity plnC = new PlnZyRclCEntity();
						// 获取炉计划顺序号
						heatPlanSqe = corePlanFurnProM.getHeatPlanSeq(conn,
								heatPlanNo, "F");
						plnC.setHeatPlanNo(heatPlanNo);
						plnC.setJugdeApplyCode(jugdeApplyCode);

						plnC.setFeedHeatNo(judgeStoveNo);

						if(plineCode.equals("C072") && !plnRclS.getPlineCode().equals("C091"))
						{
							plnC.setJudgeStoveNo("");
							plnC.setFlagStoveNo("");
						}else
						{
							plnC.setJudgeStoveNo(judgeStoveNo);
							plnC.setFlagStoveNo(judgeStoveNo);
						}
						plnC.setBatchNo(batchNo);
						plnC.setBatchGroudNo(batchGroupBy);
						plnC.setStoveNo(stoveNo);
						plnC.setProPlanId(plnRclS.getProPlanId());
						plnC.setGxPlanNo(plnRclS.getGxPlanNo());
						plnC.setPlineCode(plnRclS.getPlineCode());
						plnC.setPlineName(plnRclS.getPlineName());
						plnC.setOutdiameterIn(outdiameterIn);
						plnC.setWallthickIn(wallthickIn);
						plnC.setLengthIn(lengthIn);
						plnC.setLengthmaxIn(lengthmaxIn);
						plnC.setLengthminIn(lengthminIn);
						plnC.setPlanIndoubleNum(String.valueOf(matGpM.getActCount()));
						plnC.setPlanIndoubleWt(String.valueOf(actWt));

						plnC.setHeatPlanSqe(heatPlanSqe);
						// 新增
						String addPlnGxZg = EntityTool.fillSqlByEntity(
								CoreXmlSqlParsersFactory
										.getSql("PlanFurnProduction.addPlnZyRclC"),
								plnC);
						ps = conn.prepareStatement(addPlnGxZg);
						ps.executeUpdate();
						ps.close();
					} else {
						// 修改炉计划实表
						String updateC = "UPDATE PLN_ZY_RCL_C T SET T.PLAN_INDOUBLE_NUM=T.PLAN_INDOUBLE_NUM+TO_NUMBER(?),"
								+ "T.PLAN_INDOUBLE_WT=T.PLAN_INDOUBLE_WT+TO_NUMBER(?) ";
						updateC += " WHERE T.HEAT_PLAN_NO=? AND T.HEAT_PLAN_SQE=?";
						ps = conn.prepareStatement(updateC);
						ps.setObject(1, matGpM.getActCount());
						ps.setObject(2, actWt);
						ps.setObject(3, heatPlanNo);
						ps.setObject(4, heatPlanSqe);
						ps.executeUpdate();
						ps.close();
					}

					if (countM > 0) {
						// 修改炉计划主表量
						String sqlUpdate = CoreXmlSqlParsersFactory
								.getSql("FrmPlanMaterialRcl.updatePlnZgM");
						ps = conn.prepareStatement(sqlUpdate);
						ps.setObject(1, heatPlanNo);
						ps.setObject(2, heatPlanNo);
						ps.executeUpdate();
						ps.close();
					}
					// 修改坯子库存下工序炉计划ID
					 sqlUpdateMat = "UPDATE MAT_BC_M T SET T.HEAT_PLAN_NO_NEXT=? WHERE T.PRO_PLAN_ID_NEXT=? AND T.GX_PLAN_NO_NEXT=? AND T.JUDGE_STOVE_NO=?";
					ps = conn.prepareStatement(sqlUpdateMat);
					ps.setObject(1, heatPlanNo);
					ps.setObject(2, proPlanId);
					ps.setObject(3, gxPlanNo);
					ps.setObject(4, judgeStoveNo);
					ps.executeUpdate();
					ps.close();
				}

			}
			cro.setV_errMsg("利库成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}

	/**
	 * 坯子脱单（删除订单号）
	 * 
	 * @param jslist
	 * @param userName
	 *            脱单人
	 * @return
	 * @throws ParseException
	 */
	public CoreReturnObject doDeleteMonomer(
			ArrayList<ArrayList<String>> jslist, String userName)
			throws ParseException {
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		try {
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			PreparedStatement ps = null;
			// ResultSet rs = null;
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			DecimalFormat df = new DecimalFormat("0000000000");
			// 获取数据库当前时间
			String dateTime = FrmSteelMakingPlanChange.getSystemTime(conn);
			// 循环库存信息
			int seq = 1;
			for (int i = 0; i < jslist.size(); i++) {

				Date now = new Date();
				ArrayList<String> list = (ArrayList<String>) jslist.get(i);
				String judgeStoveNo = list.get(0);
				String batchNo = list.get(1);
				String batchGroupBy = list.get(2);
				String materialNo = list.get(3);
				int takeCount = Integer.parseInt(list.get(4));// 利库支
				String proPlanIdNo = list.get(5);

				String plineCode = list.get(6);
				List<MatBcMEntityPln> matGpMlist = FrmPlanMaterialDbk
						.getQureyMatGpM(conn, judgeStoveNo, batchNo,
								batchGroupBy, materialNo, takeCount,
								proPlanIdNo, "F","","","",plineCode);
				if (matGpMlist == null) {
					cro.setV_errMsg("管坯物料已发生改变,请刷新!");
					return cro;
				}
				MatBcTrackEntity matTrack = FrmPlanMaterialDbk
						.getQureyMatTrack(conn, judgeStoveNo, batchNo,
								batchGroupBy, takeCount, "0", proPlanIdNo,
								MatBcTrackEntity.class, "delete","","");

				for (MatBcMEntityPln matGpM : matGpMlist) {
					// if (!matGpM.getProductFlag().equals("801401")) {
					// cro.setV_errMsg("材料类别不是成品,不能脱单!");
					// return cro;
					// }
					// if (!matStatus.equals("80150103")
					// && !matStatus.equals("80150102")) {
					// cro.setV_errMsg("材料状态不是产出待判或产出已判定状态,不能脱单");
					// return cro;
					// }

					// if (matGpM.getHangingFlag().equals("1")) {
					// cro.setV_errMsg("你选择的管坯物料已生成实际炉计划,不能脱单，如需脱单,请先删除炉计划!");
					// return cro;
					// }
					String proPlanZg = matGpM.getProPlanIdNext() + "/"
							+ matGpM.getGxPlanNoNext();
					if (proPlanZg.equals("/")) {
						cro.setV_errMsg("你选择的管坯物料未分配其他热处理订单,不能脱单!");
						return cro;
					}
					// 修改坯子库存信息
					String sqlUpdateMat = CoreXmlSqlParsersFactory
							.getSql("FrmPlanMaterialDbk.UpdateMatGp");
					ps = conn.prepareStatement(sqlUpdateMat);
					ps.setObject(1, "80150102");
					ps.setObject(2, "");
					ps.setObject(3, "");
					ps.setObject(4, "0");
					ps.setObject(5, "0");
					ps.setObject(6, "");
					ps.setObject(7, "");
					ps.setObject(8, matGpM.getMatNo());
					ps.executeUpdate();
					ps.close();
				}
				String resumeSeq = dateFormat.format(now) + df.format(seq);
				// 生成利库日志
				matTrack.setResumeSeq(resumeSeq);
				matTrack.setEventId("900101");
				matTrack.setEventDesc("热处理脱单");
				matTrack.setFormName("com.steering.pss.plnsaleord.planStove.FrmPlanMaterialRcl");
				matTrack.setCallFuncCoding("doDeleteMonomer");
				matTrack.setKeyvalue1("List<MatBcMEntityPln> matGpMlist");
				matTrack.setKeyvalue1Desc("物料主表关键字实体类集合");
				matTrack.setKeyvalue2("String userName");
				matTrack.setKeyvalue2Desc2("操作人");
				matTrack.setKeyvalue3("doDeleteMonomer");
				matTrack.setKeyvalue3Desc3("删除订单号");
				matTrack.setProcessCdoe("F");
				matTrack.setMemo("");
				matTrack.setCreateName(userName);
				matTrack.setCreateTime(dateTime);
				seq++;
				String sqlAddTrack = EntityTool.fillSqlByEntity(
						CoreXmlSqlParsersFactory
								.getSql("FrmPlanMaterialDbk.addMatBcTrack"),
						matTrack);
				ps = conn.prepareStatement(sqlAddTrack);
				ps.executeUpdate();
				ps.close();

			}
			cro.setV_errMsg("脱单成功!");
			conn.commit();
		} catch (SQLException sqle) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errCode(-1);
			cro.setV_errMsg(sqle.getMessage());
		} finally {
			try {
				if (conn != null && !conn.isClosed()) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cro;
	}
}
