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.entity.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FrmMchInStore extends DBCommon{

	/***
	 * 查询管加工炉计划
	 * @param parmList
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject querySinglePlan(ArrayList<String> parmList,String[] plineCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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 T2.BESIDE_STATUS ='11'";
		}else
		{
			sql += " AND (T2.BESIDE_STATUS <> '11' OR T2.BESIDE_STATUS IS NULL)";
		}
		sql += " AND T2.PER_STATUS IS NOT NULL ";
		sql += CoreConverter.toSqlConditionString("NVL(T2.PLINE_CODE_W,T.PLINE_CODE)", plineCode);


		sql += " ORDER BY T.DOWNTIME,T.JUDGE_STOVE_NO ,T2.BATCH_NO ASC";
		return this.getData(sql);
	}
	/***
	 * 查询交库实绩表
	 * @param parmList
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryInList(ArrayList<String> parmList,String[] plineCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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.GX_PLAN_NO,t.PRO_PLAN_ID,T.LOT_NO, B.STORAGE_NO";
		sql += " ORDER BY  T.INSTOCK_DOC,T.JUDGE_STOVE_NO,T.BATCH_NO ";
		return this.getData(sql);
	}
	
	/***
	 * 查询交库管号队列信息
	 * @param heatPlanNo
	 * @param judgeNo
	 * @return
	 */
	public CoreReturnObject doQueryVrpRack(String heatPlanNo,String judgeNo, String batchNo)
	{
		return doQueryVrpRack(heatPlanNo,judgeNo,batchNo,"0");
	}

	/***
	 * 查询交库管号队列信息
	 * @param heatPlanNo
	 * @param judgeNo
	 * @return
	 */
	public CoreReturnObject doQueryVrpRack(String heatPlanNo,String judgeNo, String batchNo, String InstoreFlag)
	{
		return this.getDataBySqlID("FrmMchInStore.doQueryVrpRack", new Object[]{heatPlanNo,batchNo,InstoreFlag});
	}
	
	/***
	 * 查询指定炉批支数
	 * @return
	 */
	public CoreReturnObject doQueryStausRack(String heatPlanNo,String judgeNo,String phyBatchNo,String isSampleFlag)
	{
		String sampleFlag = "";
		String sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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("FrmMchInStore.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); // 取消自动提交事务
			PlnZyJgxMEntity1 plnZyEntity = EntityTool.getEntityByJSONStr(plnZyTity, PlnZyJgxMEntity1.class);
			inDoc = getInListDoc(connection, strInBound);
			storageNo = getStorageNo(connection,plnZyEntity.getPlineCode());
			if(storageNo.equals("")){
				cro.setV_errMsg("没有下线仓库，请先维护下线仓库。");
				return cro;
				//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)
			{
				PortMchBatchSampleResultEntity1 portVrpEntity = EntityTool.getEntityByJSONStr(str, PortMchBatchSampleResultEntity1.class);
				if(portVrpEntity.getGroupNo().equals("")){

				groupNo = creatGroup(connection, plnZyEntity.getOrderNo() + "/" + plnZyEntity.getOrderSeq(),
						portVrpEntity.getActLenMin(), portVrpEntity.getActLenMax(),
						plnZyEntity.getOrdLnDlyPk());

			}else{
				groupNo =portVrpEntity.getGroupNo();
			}
				if(groupNo.equals("")){
					cro.setV_errMsg("组号不能为空,请设置正确的组号规则");
					return cro;
					//throw  new Exception("组号不能为空,请设置正确的组号规则");
				}
//				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"))
				{
					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("G");
				matBcEntity.setProcessDesc("加工线");
				matBcEntity.setNextProcessCdoe(plnZyEntity.getNextProcessCode());
				matBcEntity.setNextProcessDesc("");
				matBcEntity.setPlineCode(plnZyEntity.getPlineCode());
				matBcEntity.setPlineName(plnZyEntity.getPlineName());
				matBcEntity.setMatOriginCode("801302");
				matBcEntity.setMatOriginName("产线");
				matBcEntity.setActDimater(plnZyEntity.getOutdiameter());
				matBcEntity.setActHeight(plnZyEntity.getWallthick());
				matBcEntity.setActLenMin(portVrpEntity.getActLenMin());
				matBcEntity.setActLenMax(portVrpEntity.getActLenMax());
				matBcEntity.setBelongCode(belongCode);
				matBcEntity.setBelongName(belongName);
				matBcEntity.setCreateName(userName);
				matBcEntity.setCreateTime(systime);
				matBcEntity.setBalYearMonth(yearMoth);
				matBcEntity.setJudgeStdCode("");
				//matBcEntity.setJudgeStdName(plnZyEntity.getOutputStandard());
				matBcEntity.setJudgeStdName(plnZyEntity.getCic()+"+"+plnZyEntity.getPic()+"+"+plnZyEntity.getSic()+"+"+plnZyEntity.getDic()
						+"+"+plnZyEntity.getWic()+"+"+plnZyEntity.getGic());
				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.setMemo(portVrpEntity.getRemark());
				if(plnZyEntity.getIfProduct().equals("1"))
				{
				matBcEntity.setJudgeStdDes(plnZyEntity.getCic()+"+"+plnZyEntity.getPic()+"+"+plnZyEntity.getSic()+"+"+plnZyEntity.getDic()
						+"+"+plnZyEntity.getWic()+"+"+plnZyEntity.getGic());
				}else
				{
					matBcEntity.setJudgeStdDes(""+"+"+plnZyEntity.getPic()+"+"+plnZyEntity.getSic()+"+"+plnZyEntity.getDic()
							+"+"+plnZyEntity.getWic()+"+"+plnZyEntity.getGic());
				}
				matBcEntity.setCic(plnZyEntity.getCic());
				matBcEntity.setHeatPlanNoOld(plnZyEntity.getHeatPlanNo());
				if(ordPk==null||ordPk.equals(""))
				{
					matBcEntity.setOrdPk("");
				}else{
					matBcEntity.setOrdPk(ordPk);
				}
				if(ordLnPk==null||ordLnPk.equals(""))
				{
					matBcEntity.setOrdLnPk("");
				}else{
					matBcEntity.setOrdLnPk(ordLnPk);
				}

				String QueryCount = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.doQueryCount");//插入库存、交易
				QueryCount = EntityTool.fillSqlByEntity(QueryCount, matBcEntity);
				QueryCount = EntityTool.fillSqlByEntity(QueryCount, portVrpEntity);

				psta = connection.prepareStatement(QueryCount);
				rs = psta.executeQuery();
				int Count1 = 0;
				if (rs.next()) {
					Count1 = rs.getInt("COUNT");
				}
				rs.close();
				psta.close();

				if(Count1!=Integer.parseInt(matBcEntity.getActCount())){
					cro.setV_errMsg("数据发生改变,请重新刷新数据，可能是长度不在要求范围内！");
					return cro;
					//throw  new Exception("数据发生改变,请重新刷新数据");
				}
				
			    String insertBcSql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.insertMatBcM");//插入库存、交易
			    insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, matBcEntity);
				insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, portVrpEntity);
			    sqllist.add(insertBcSql);
			    
			    
			    MchComInfoEntity1 vrpEntity = new MchComInfoEntity1();
			    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.setPrdcrNo(plnZyEntity.getPrdcrNo());
			    vrpEntity.setMonitordemand("");
			    vrpEntity.setCraftFileNo(plnZyEntity.getCraftFileNo());
			    vrpEntity.setCraftSeq(plnZyEntity.getCraftSeq());
			    vrpEntity.setCraftFileMan(plnZyEntity.getCraftFileMan());
			    vrpEntity.setJudgeStoveNo(plnZyEntity.getJudgeStoveNo());
			    vrpEntity.setBatchNo(plnZyEntity.getBatchNo());
			    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(plnZyEntity.getLengthmax()); //长度放的上限
			    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(plnZyEntity.getLengthmax()); //长度放的上限
			    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.setColOperator(userName);
			    vrpEntity.setProGroup(userClass);
			    vrpEntity.setProShift(userOrder);
			    vrpEntity.setColOperator(userName);
			    vrpEntity.setBalYearMonth(yearMoth);
			    vrpEntity.setInstockDoc(inDoc);
			    vrpEntity.setProYearMonthDay(yearMothDay);
			    String inserVrpComInfoSql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.insertVrpComInfo");//插入公共表
			    inserVrpComInfoSql = EntityTool.fillSqlByEntity(inserVrpComInfoSql, vrpEntity);
			    sqllist.add(inserVrpComInfoSql);
			    
			    inCount = inCount + Integer.parseInt(portVrpEntity.getActCount());
			}
			List<PlnZyRclMEntity1> plnOrderZy = null; 
			if(plnZyEntity.getNextProcessCode().equals("G"))
			{
				sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.selectPlnZyJg");
				sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				 plnOrderZy = EntityTool
				.getEntitysByRs(rs, PlnZyRclMEntity1.class);
				rs.close();
				psta.close();
			}else if(plnZyEntity.getNextProcessCode().equals("H"))
			{
				sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.selectPlnZyJgm");
				sql = EntityTool.fillSqlByEntity(sql, plnZyEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				 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();
			}
			if(plnOrderZy  != null)
			{
				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();

//					gpreqCount = Integer.parseInt(plnOrderZy.get(i).getPlanBlNum())
//							- Integer.parseInt(matGpNum);
					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(codeBl);

					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;
							}

					}
			}
			//更新组号生成规则
			String updateMchGroupNoSql = CoreXmlSqlParsersFactory
			.getSql("FrmVrpInStore.updateMchGroupNo");
			updateMchGroupNoSql = EntityTool.fillSqlByEntity(updateMchGroupNoSql,plnZyEntity);
			sqllist.add(updateMchGroupNoSql);
			
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			System.out.print(sqllist);
			
			// 运输需求
//			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("交库成功!");
			}
			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;
		String appCode = "";
		String HeatPlanNo = "";
		int appCount = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str : parmList)
			{
				appCount= 0;
				YdmBcInlistEntity ydmEntity = EntityTool.getEntityByJSONStr(str, YdmBcInlistEntity.class);

				if(!ydmEntity.getStatus().equals("未倒运")){
					cro.setV_errMsg("不在产出库,不允许撤销交库!");
					return cro;
	        	}

				sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.selectNode");//查询是否已结炉
				sql = EntityTool.fillSqlByEntity(sql, ydmEntity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					appCount = Integer.parseInt(rs.getString("STATION_STATUS"));
				}
				rs.close();
				psta.close();
				if(appCount == 2)
				{
					cro.setV_errMsg("该炉已结炉,不允许撤销交库!");
					return cro;
				}
				appCount= 0;

				String liku = "";
				sql = CoreXmlSqlParsersFactory.getSql("FrmVrpInStore.SelectMatGpStorageG");//查询在库物料支数
				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("FrmMchInStore.DeleteInList");
				deleteInListSql = EntityTool.fillSqlByEntity(deleteInListSql,ydmEntity);
				sqllist.add(deleteInListSql);
				
				//更新组号生成规则
				String updateMchGroupNoSql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpInStore.updateMchGroupNo");
				updateMchGroupNoSql = EntityTool.fillSqlByEntity(updateMchGroupNoSql,ydmEntity);
				sqllist.add(updateMchGroupNoSql);

			}

			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			
			// 运输需求
			YdmBcInlistEntity ydmEntity2 = EntityTool.getEntityByJSONStr(
					parmList.get(0), YdmBcInlistEntity.class);
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMchInStore.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>();
			PlnZyDbkMEntity plnEntity = EntityTool.getEntityByJSONStr(plnTity, PlnZyDbkMEntity.class);
			sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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 LoadingPiece = 0;
			int InstorePiece = 0;
			/*sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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();
			sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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("FrmMchInStore.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();
			int PortPiece = 0;
			sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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 + groupPiece +PortPiece)
			{
				cro.setV_errMsg("上料支数和下线支数不平,不允许结炉!");
				return cro;
			}*/
			/*sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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(Integer.parseInt(nodeFlag)<50)
			{
				cro.setV_errMsg("上工序未结炉,不允许结炉!");
				return cro;
			}*/
			
			String updateSql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.UpdateNodeFlag");
			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>();
			PlnZyDbkMEntity plnEntity = EntityTool.getEntityByJSONStr(plnTity, PlnZyDbkMEntity.class);
			sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.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)
			{
				sql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.selectQcmApp2");
				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;
				}
			}
			
			
			String updateSql = CoreXmlSqlParsersFactory.getSql("FrmMchInStore.CancelupdateNodeFlag");
			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(),
//					"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 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.getInListDoc");
		PreparedStatement psta = conn.prepareStatement(sql);
		psta.setString(1, strInbound);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			inListDoc = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return inListDoc;
	}
	
	/***
	 * 生成组号
	 * 
	 * @param con
	 * @param matTity
	 * @param parm
	 * @return2
	 */
	public static String creatGroup(Connection con, String OrderNo,
			String Min, String Max, String ORD_LN_DLY_PK) throws Exception {
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;

		String Grroup = "";
		 sql = CoreXmlSqlParsersFactory
				.getSql("FrmMchInStore.GetGroupByMaxMin");
		 psta = con.prepareStatement(sql);
		psta.setString(1, OrderNo);
		psta.setString(2, Min);
		psta.setString(3, Max);
		 rs = psta.executeQuery();
		if (rs.next()) {
			Grroup = rs.getString("GROUP_NO");
		}
		rs.close();
		psta.close();

		if(Grroup.equals("")){
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMchInStore.GetMaxGroup");
			psta = con.prepareStatement(sql);
			psta.setString(1, OrderNo);
			rs = psta.executeQuery();
			if (rs.next()) {
				Grroup = rs.getString("GROUP_NO");
			}
			rs.close();
			psta.close();

			Double OrderMax =0D;
			Double OrderMin = 0D;
			Double OrderSort = 0D;
			Double SingleLen = 0D;


			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMchInStore.getOrderInfo");
			psta = con.prepareStatement(sql);
			psta.setString(1, ORD_LN_DLY_PK);
			rs = psta.executeQuery();
			if (rs.next()) {
				if(rs.getString("LENGTHMAX")!=null){
					OrderMax= rs.getDouble("LENGTHMAX");
				}
				if(rs.getString("LENGTHMIN")!=null){
					OrderMin= rs.getDouble("LENGTHMIN");
				}
				if(rs.getString("SHORTEST")!=null){
					OrderSort= rs.getDouble("SHORTEST");
				}
				if(rs.getString("SIGL_LEN")!=null){
					SingleLen= rs.getDouble("SIGL_LEN");
				}

			}
			rs.close();
			psta.close();

			String MinSign = "";
			String MaxSign = "";
			if(SingleLen<=0.0001){
				if( Double.parseDouble(Max)>OrderMax)
					MaxSign = "<=";
				else
					MaxSign = "<";

				if( Double.parseDouble(Min)<OrderMin)
					MinSign = ">=";
				else
					MinSign = ">";
			}else if( OrderSort>0.0001 && Double.parseDouble(Min)<OrderMin){
			    	MaxSign = "<";
			    	MinSign = ">=";
			}else {
               //   if((OrderMin-  Double.parseDouble(Min))%SingleLen < 0.0001 && (OrderMin-  Double.parseDouble(Min))%SingleLen >- 0.0001){
				  if(Math.abs(OrderMin -  Double.parseDouble(Min) ) %  SingleLen < 0.0001 ||  Math.abs(OrderMin -  Double.parseDouble(Min) ) %  SingleLen >SingleLen- 0.0001 ){
					  MaxSign = "<=";
					  MinSign = ">=";

				  }  else{
					  MaxSign = "<";
					  MinSign = ">";
				  }

		   }
			
			 if(Math.abs(Double.parseDouble(Max) -  Double.parseDouble(Min) ) <0.0001 ){
				  MaxSign = "<=";
				  MinSign = ">=";
			  } 


					sql = CoreXmlSqlParsersFactory
					.getSql("FrmMchInStore.AddGroupRule");// 插入记录
			psta = con.prepareStatement(sql);
			psta.setString(1, OrderNo);
			psta.setString(2, Max);
			psta.setString(3,Min);
			psta.setString(4, Grroup);
			psta.setString(5, MaxSign);
			psta.setString(6,  MinSign);
			psta.addBatch();
			psta.executeBatch();
			con.commit();
		}


		return Grroup;
	}
	
	/**
	 * 根据炉号、批号自动组号
	 * 
	 * @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(String.valueOf(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;
	}
}
