package com.steering.mes.mcp.Vrp;

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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

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

import com.steering.comm.converter.EntityTool;
import com.steering.comm.converter.QualityDesignTool;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.mes.mcp.collarMaterial.MeanValueCalculation;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.MatZcMEntity;
import com.steering.mes.mcp.entity.PlnZyDbkMEntity;
import com.steering.mes.mcp.entity.VrpMilEntity;
import com.steering.mes.mcp.entity.YdmZcInlistEntity;

public class FrmVrpMil extends DBCommon {
	/**
	 * 查询投入产出
	 * 
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @return
	 * 
	 */
	// public CoreReturnObject doQuerySingleJugStove(ArrayList<String> list) {
	// return this.getDataBySqlID("FrmVrpMil.doQuerySingleJugStove",
	// new Object[] { list });
	// }
	public CoreReturnObject doQuerySingleJugStove(ArrayList<String> list,
			String startTim, String endTim) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.doQuerySingleJugStove");
		if (!startTim.equals("")) {
			String str = " AND T.CREATE_TIME >=TO_DATE('"
					+ startTim
					+ "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
					+ endTim + "', 'YYYY-MM-DD HH24:MI:SS') ";
		}
		sql += " AND T.PLINE_CODE LIKE '%'||'" + list.get(0) + "'||'%'";
		sql += " AND T.JUDGE_STOVE_NO LIKE '%'||'" + list.get(2) + "'||'%'";
		sql += " AND T.BATCH_NO  LIKE '%'||'" + list.get(1) + "'||'%'";
		sql += "  GROUP BY T.BATCH_NO, T.JUDGE_STOVE_NO, T.PRO_PLAN_ID, T.ACT_MATERIAL_NAME, T.HEAT_PLAN_NO";
		return this.getData(sql);
	}

	/***
	 * 查询表检的炉批信息
	 * 
	 * @param jugeStove
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryQaStove(String jugeStove, String plineCode) {
		return this.getDataBySqlID("FrmVrpMil.doQueryQaStove", new Object[] {
				jugeStove, plineCode });
	}

	/***
	 * 查询详细表检信息
	 * 
	 * @param p
	 * @param p2
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQuerySurface(ArrayList<String> parm, String goType) {
		if (goType.equals("A")) {
			return this.getDataBySqlID("FrmVrpMil.doQuerySurface",
					parm.toArray());
		} else {
			return this.getDataBySqlID("FrmVrpMil.doQuerySurface1",
					parm.toArray());
		}
	}

	/***
	 * 查询工序合同的去向
	 * 
	 * @param orderNo
	 * @return
	 */
	public CoreReturnObject doQueryGoType(String orderNo) {
		return this.getDataBySqlID("FrmVrpMil.doQueryGoType",
				new Object[] { orderNo });
	}

	/***
	 * 查询材料实绩信息
	 * 
	 * @param list
	 * @param startTim
	 * @param endTim
	 * @return
	 */
	public CoreReturnObject doQueryMat(ArrayList<String> list, String startTim,
			String endTim, String departMentId, String plineCode, String value) {
		String sql = CoreXmlSqlParsersFactory.getSql("FrmVrpMil.doQueryMat");
		if (!startTim.equals("")) {
			sql += " AND T1.INSTOCK_TIME >=TO_DATE('"
					+ startTim
					+ "', 'YYYY-MM-DD HH24:MI:SS')AND T1.INSTOCK_TIME <=TO_DATE('"
					+ endTim + "', 'YYYY-MM-DD HH24:MI:SS')";
		}
		// if (value.equals("0")) {
		// sql += " AND T1.JUGDE_APPLY_CODE IS NULL";
		// } else if (value.equals("1")) {
		// sql += " AND T1.JUGDE_APPLY_CODE IS NOT NULL";
		// }
		sql += " AND T1.BELONG_CODE = '" + departMentId + "'";
		sql += " GROUP BY T1.JUDGE_STOVE_NO,T1.PRODUCCODE,T1.PRODUCNAME,T1.STEELCODE,T1.STEELNAME,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.GRADECODE,T1.GRADENAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.PIPMEMO,T1.OLD_ORDER_NO,T1.MAT_STATUS,T1.Heat_Plan_No,";
		sql += " T1.MODEL_DESC,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.LEN_NO,T1.LEN_DESC,T1.LEN_UNIT,T1.BELONG_CODE,T1.BELONG_NAME,T1.PLINE_CODE,T1.PLINE_NAME,T1.BAL_YEAR_MONTH,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.OUTSTOCK_DOC,T2.STORAGE_NO,T2.STORAGE_NAME,T2.LOCATION_NO,";
		sql += " T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.CREATE_NAME,T1.MEMO,T1.PRODUCT_FLAG,T1.JUDGE_STD_CODE,T1.JUDGE_STD_NAME ORDER BY T1.BAL_YEAR_MONTH DESC";
		return this.getData(sql, list.toArray());
	}

	/***
	 * 查询炉次计划号详细炉批组
	 * 
	 * @param heatPlanNo
	 * @return
	 */
	public CoreReturnObject doQueryMatData(String heatPlanNo, String value) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.doQueryMatPlanNo");

		if (value.equals("0")) {
			sql += " AND T1.JUGDE_APPLY_CODE IS NULL";
		} else if (value.equals("1")) {
			sql += " AND T1.JUGDE_APPLY_CODE IS NOT NULL";
		}
		sql += " GROUP BY T1.JUDGE_STOVE_NO, T1.BATCH_NO, T1.BATCH_GROUD_NO,T1.PRODUCCODE,T1.PRODUCNAME,T1.STEELCODE,T1.STEELNAME,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.GRADECODE,T1.GRADENAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.PIPMEMO,T1.OLD_ORDER_NO,T1.MAT_STATUS,T2.LOCATION_NO,T1.OUTSTOCK_DOC,T1.JUGDE_APPLY_CODE,T1.Heat_Plan_No,T1.QCM_STAUS,";
		sql += " T1.MODEL_DESC,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.LEN_NO,T1.LEN_DESC,T1.LEN_UNIT,T1.BELONG_CODE,T1.BELONG_NAME,T1.PLINE_CODE,T1.PLINE_NAME,T1.BAL_YEAR_MONTH,T1.MATERIAL_NO,T1.MATERIAL_NAME,";
		sql += " T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.CREATE_NAME,T2.STORAGE_NO,T2.STORAGE_NAME,T1.MEMO,T1.PRODUCT_FLAG,T1.JUDGE_STD_CODE,T1.JUDGE_STD_NAME ORDER BY T1.BAL_YEAR_MONTH DESC";

		return this.getData(sql, new Object[] { heatPlanNo });
	}

	/***
	 * 查询炉次计划号转移详细炉批组信息
	 * 
	 * @param heatPlanNo
	 * @return
	 */
	public CoreReturnObject doQueryMatDataOut(String heatPlanNo, String value) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.doQueryMatDataOut");

		if (value.equals("0")) {
			sql += " AND T1.JUGDE_APPLY_CODE IS NULL";
		} else if (value.equals("1")) {
			sql += " AND T1.JUGDE_APPLY_CODE IS NOT NULL";
		}
		sql += " GROUP BY T1.JUDGE_STOVE_NO, T1.BATCH_NO, T1.BATCH_GROUD_NO,T1.PRODUCCODE,T1.PRODUCNAME,T1.STEELCODE,T1.STEELNAME,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.GRADECODE,T1.GRADENAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.PIPMEMO,T1.OLD_ORDER_NO,T1.MAT_STATUS,T2.LOCATION_NO,T1.OUTSTOCK_DOC,T1.JUGDE_APPLY_CODE,T1.Heat_Plan_No,T1.QCM_STAUS,";
		sql += " T1.MODEL_DESC,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.LEN_NO,T1.LEN_DESC,T1.LEN_UNIT,T1.BELONG_CODE,T1.BELONG_NAME,T1.PLINE_CODE,T1.PLINE_NAME,T1.BAL_YEAR_MONTH,T1.MATERIAL_NO,T1.MATERIAL_NAME,";
		sql += " T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.CREATE_NAME,T2.STORAGE_NO,T2.STORAGE_NAME,T1.MEMO,T1.PRODUCT_FLAG,T1.JUDGE_STD_CODE,T1.JUDGE_STD_NAME ORDER BY T1.BAL_YEAR_MONTH DESC,T1.OUTSTOCK_DOC";

		return this.getData(sql, new Object[] { heatPlanNo });
	}

	/***
	 * 查询库存实绩
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryMatButess(ArrayList<String> list,
			String startTim, String endTim, String departMentId) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.doQueryMatButess");

		if (!startTim.equals("")) {
			sql += " AND T1.INSTOCK_TIME >=TO_DATE('"
					+ startTim
					+ "', 'YYYY-MM-DD HH24:MI:SS')AND T1.INSTOCK_TIME <=TO_DATE('"
					+ endTim + "', 'YYYY-MM-DD HH24:MI:SS')";
		}
		sql += " AND T1.BELONG_CODE = '" + departMentId + "'";
		sql += " GROUP BY T1.JUDGE_STOVE_NO, T1.BATCH_NO, T1.BATCH_GROUD_NO,T1.PRODUCCODE,T1.PRODUCNAME,T1.STEELCODE,T1.STEELNAME,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.GRADECODE,T1.GRADENAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.PIPMEMO,T1.OLD_ORDER_NO,T1.MAT_STATUS,T1.HEAT_PLAN_NO,";
		sql += " T1.MODEL_DESC,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.LEN_NO,T1.LEN_DESC,T1.LEN_UNIT,T1.BELONG_CODE,T1.BELONG_NAME,T1.PLINE_CODE,T1.PLINE_NAME,T1.BAL_YEAR_MONTH,T1.MATERIAL_NO,T1.MATERIAL_NAME,";
		sql += " T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.CREATE_NAME,T2.STORAGE_NO,S.STORAGE_NAME,T1.MEMO,T1.PRODUCT_FLAG,T1.JUDGE_STD_CODE,T1.JUDGE_STD_NAME ORDER BY T1.BAL_YEAR_MONTH DESC";

		return this.getData(sql, list.toArray());
	}

	/***
	 * 查询入库实绩
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryInList(ArrayList<String> list,
			String startTim, String endTim) {
		String sql = CoreXmlSqlParsersFactory.getSql("FrmVrpMil.doQueryInList");
		if (!startTim.equals("")) {
			sql += " AND t.IN_TIME_F>=TO_DATE('" + startTim
					+ "','YYYY-MM-DD HH24:MI:SS') AND t.IN_TIME_F<= TO_DATE('"
					+ endTim + "','YYYY-MM-DD HH24:MI:SS')";
		}
		sql += " GROUP BY T.INSTOCK_DOC,T.ORDER_NO,T.ORDER_SEQ,T.ORD_LN_DLY_PK,T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,T.PRODUCNAME,T.STD_STYLE_DESC,T.STEELNAME,T.SPEC_NAME,T.MODEL_DESC,T.JUDGE_STD_CODE,T.STD_NAME,T.GRADENAME,T.ACT_DIMATER,T.ACT_HEIGHT,T.ACT_LEN_MAX,T.ACT_LEN_MIN,T.ACT_LEN,T.ACT_COUNT,T.ACT_WEIGHT,";
		sql += "T.STORAGE_ATTR_NAME,T.PLINE_NAME,T.PRODUCT_FLAG,T.PIPMEMO,T.BAL_YEAR_MONTH,T.CREATE_NAME,T.CREATE_TIME,T.INSTOCK_TIME";
		sql += " ORDER BY T.INSTOCK_DOC DESC";
		return this.getData(sql);
	}
	/***
	 * 查询支数、长度
	 * 
	 * @param list
	 * @param staus
	 * @return
	 */
	public CoreReturnObject doQueryNum(ArrayList<String> list, String staus) {
		String sql = "";
		if (staus.equals("样管")) {
			sql = CoreXmlSqlParsersFactory.getSql("doQueryNum.sample");
		} else {
			sql = CoreXmlSqlParsersFactory.getSql("doQueryNum.Qualified");
		}

		return this.getData(sql, list.toArray());
	}

	/***
	 * 查询取样所分批号
	 * 
	 * @param jugeNo
	 * @param lotNo
	 * @param id
	 * @return
	 */
	public CoreReturnObject doQueryBacthNo(String jugeNo, String lotNo,
			String id) {
		return this.getDataBySqlID("FrmVrpMil.doQueryBacthNo", new Object[] {
				jugeNo, lotNo, id });
	}

	/***
	 * 查询探伤所有结果
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryFlawStaus() {
		return this.getDataBySqlID("FrmVrpMil.doQueryFlawStaus");
	}

	/***
	 * 查询表检所有结果
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryTestingStaus() {
		return this.getDataBySqlID("FrmVrpMil.doQueryTestingStaus");
	}

	/***
	 * 查询产出炉信息
	 * 
	 * @param jugeNo
	 * @param lotNo
	 * @param id
	 * @return
	 */
	public CoreReturnObject doQueryBacthNoInformation(String jugeNo,
			String lotNo, String id, String flag) {
		if (flag.equals("样管")) {
			return this.getDataBySqlID("FrmVrpMil.doQueryBacthNoInformation",
					new Object[] { jugeNo, jugeNo, lotNo, id, "1" });
		} else if (flag.equals("合格")) {
			return this.getDataBySqlID("FrmVrpMil.doQueryBacthNoInformation1",
					new Object[] { jugeNo, jugeNo, lotNo, id, "0" });
		} else if (flag.equals("判废")) {
			return this.getDataBySqlID("FrmVrpMil.doQueryBacthNoInformation2",
					new Object[] { jugeNo, jugeNo, lotNo, id, "0" });
		} else {
			return this.getDataBySqlID("FrmVrpMil.doQueryBacthNoInformation",
					new Object[] { jugeNo, jugeNo, lotNo, id, "2" });
		}
	}

	public static PlnZyDbkMEntity getSendDBKPlan(Connection con, String heatNo)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("FrmVrpMil.doQuery");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, heatNo);
		ResultSet rs = ps.executeQuery();
		List<PlnZyDbkMEntity> sendPlanCEntitys = EntityTool.getEntitysByRs(rs,
				PlnZyDbkMEntity.class);
		rs.close();
		ps.close();
		if (sendPlanCEntitys == null || sendPlanCEntitys.size() == 0) {
			return null;
		} else {
			for (PlnZyDbkMEntity sendPlanCEntity : sendPlanCEntitys) {
				QualityDesignTool.nullConverNullString(sendPlanCEntity);
			}
			return sendPlanCEntitys.get(0);
		}
	}

	/***
	 * 镦拔扩交库
	 * 
	 * @param parm
	 * @return
	 * @throws Exception
	 */
	public CoreReturnObject doPipeIn(ArrayList<String> list, String strInBound,
			String strInMemo) throws Exception {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String groupNo = "";
		String inListDoc = "";
		String inListSeq = "";
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String inStoreCount = "";// 已入库支数
		int n = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			String insertMatSql = CoreXmlSqlParsersFactory
//					.getSql("FrmVrpMil.doPipe");// 插入材料、入库、位置表(成品合格)
//			PreparedStatement insertMatPsta = connection
//					.prepareStatement(insertMatSql);
			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.doPipe1");// 
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			//插入销售库存表
			String sqlAddXs = CoreXmlSqlParsersFactory
					.getSql("xsmat.doPipe1");// 
			PreparedStatement patAddXs = connection.prepareStatement(sqlAddXs);
			
			String sqlAddD = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.doPipe2");//
			PreparedStatement patAddD = connection.prepareStatement(sqlAddD);
			String sqlAddD1 = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.doPipe3");//
			PreparedStatement patAddD1 = connection.prepareStatement(sqlAddD1);
			
			//插入销售库存记录表
			String sqlAddD1Xs = CoreXmlSqlParsersFactory
					.getSql("xsmat.doPipe3");//
			PreparedStatement patAddD1Xs = connection.prepareStatement(sqlAddD1Xs);

			// String insertMatSql2 =
			// CoreXmlSqlParsersFactory.getSql("FrmVrpMil.doPipe2");//插入材料、入库、位置表(半成品合格)
			// PreparedStatement insertMatPsta2 =
			// connection.prepareStatement(insertMatSql2);
			// String insertMatSql3 =
			// CoreXmlSqlParsersFactory.getSql("FrmVrpMil.doPipe3");//插入材料、入库、位置表(半成品判废)
			// PreparedStatement insertMatPsta3 =
			// connection.prepareStatement(insertMatSql3);
			String planNO = list.get(9);
			String locode = list.get(10);
			String batch = list.get(11);
			int  num= Integer.parseInt(list.get(12));
			double wgt = Double.parseDouble(list.get(13));
			PlnZyDbkMEntity pln = FrmVrpMil.getSendDBKPlan(connection, planNO); 
			if (pln == null){
		    cro.setV_errMsg("无镦拔扩在线库基础信息，请维护!");
			return cro;
			}
			inListDoc = querySendSqlDoc(connection, strInBound, strInMemo);// 入库单号
			inListSeq = querySendSql(connection, inListDoc); // 入库流水号
			n = Integer.parseInt(inListSeq.substring(inListSeq.length() - 4,
					inListSeq.length()));
			// for (int i = 0; i < parm.size(); i++){
			// ArrayList<String> parm1 = (ArrayList<String>) parm.get(i);
			// VrpMilEntity milPlan = EntityTool.getEntityByJSONStr(maTtity,
			// VrpMilEntity.class);

			groupNo = queryGroupNo(connection, locode, batch);// 组号
			// groupNo = creatGroup(connection, maTtity, parm1);
			String matNo = queryMatNoM(connection, locode, batch); // 材料号
			int g = Integer.parseInt(matNo.substring(matNo.length() - 4,
					matNo.length()));

			List<double[]> list1 = MeanValueCalculation.MeanCalculation(
					wgt, num);
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.SelectdoPipeInstore");// 查询已入库支数
			psta = connection.prepareStatement(sql);
			psta.setString(1, locode);// 真实炉号
			psta.setString(2, batch);// 批号
			rs = psta.executeQuery();
			if (rs.next()) {
				inStoreCount = rs.getString("ACOUNT");
			}
			rs.close();
			psta.close();
			// if(Integer.parseInt(inStoreCount) +
			// Integer.parseInt(parm.get(i).get(1)) >
			// Integer.parseInt(parm.get(i).get(6)))
			// {
			// cro.setV_errMsg("入库支数加已入库支数超出了该批理论最大支数!");
			// return cro;
			// }
			for (int j = 0; j < list1.size(); j++) {
				double[] conW = list1.get(j);
				if ((int) conW[0] >= 0) {
					for (int z = 0; z < (int) conW[0]; z++) {
						patAdd.setObject(1,(locode + batch)+ StringTool.formatDecimal(g, "0000"));// 炉号// 批号// 流水号// =// 材料号
						patAdd.setObject(2, "801401");
						patAdd.setObject(3, "80150101");
						patAdd.setObject(4, locode);
						patAdd.setObject(5, batch);
						patAdd.setObject(6, groupNo);
						patAdd.setObject(7, pln.getOutwlId());
						patAdd.setObject(8, pln.getOutwlDesc());
						patAdd.setObject(9, pln.getInwlDesc());
						patAdd.setObject(10, pln.getInwlId());
						patAdd.setObject(11, pln.getGradecode());
						patAdd.setObject(12, pln.getGradename());
						patAdd.setObject(13, "");
						patAdd.setObject(14, "");
						patAdd.setObject(15, pln.getOrderNo());
						patAdd.setObject(16, pln.getOrderSeq());
						patAdd.setObject(17, pln.getCraftFileNo());
						patAdd.setObject(18, "");
						patAdd.setObject(19, "");
						patAdd.setObject(20, pln.getPlineCode());
						patAdd.setObject(21, pln.getPlineName());
						patAdd.setObject(22, "");
						patAdd.setObject(23, "");
						patAdd.setObject(24, pln.getAimoutdiameter());
						patAdd.setObject(25, pln.getAimwallthick());
						patAdd.setObject(26, pln.getOutdiameterIn());
						patAdd.setObject(27, pln.getWallthickIn());
						patAdd.setObject(28, pln.getLengthIn());
						patAdd.setObject(29, pln.getLengthminIn());
						patAdd.setObject(30, pln.getLengthmaxIn());
						patAdd.setObject(31, "");
						patAdd.setObject(32, "");
						patAdd.setObject(33, "");
						patAdd.setObject(34, "");
						patAdd.setObject(35, list.get(4));
						patAdd.setObject(36, systime);
//						patAdd.setObject(37, pln.getUpdateName());
//						patAdd.setObject(38, pln.getUpdateTime()); 
						patAdd.setObject(37, pln.getOrdLnDlyPk());
						patAdd.setObject(38, pln.getProPlanId());
						patAdd.setObject(39, "");
						patAdd.setObject(40, pln.getGxPlanNo());
						patAdd.addBatch();

						//

						patAddXs.setObject(1,(locode + batch)+ StringTool.formatDecimal(g, "0000"));// 炉号// 批号// 流水号// =// 材料号
						patAddXs.setObject(2, "801401");
						patAddXs.setObject(3, "80150101");
						patAddXs.setObject(4, locode);
						patAddXs.setObject(5, batch);
						patAddXs.setObject(6, groupNo);
						patAddXs.setObject(7, pln.getOutwlId());
						patAddXs.setObject(8, pln.getOutwlDesc());
						patAddXs.setObject(9, pln.getInwlDesc());
						patAddXs.setObject(10, pln.getInwlId());
						patAddXs.setObject(11, pln.getGradecode());
						patAddXs.setObject(12, pln.getGradename());
						patAddXs.setObject(13, "");
						patAddXs.setObject(14, "");
						patAddXs.setObject(15, pln.getOrderNo());
						patAddXs.setObject(16, pln.getOrderSeq());
						patAddXs.setObject(17, pln.getCraftFileNo());
						patAddXs.setObject(18, "");
						patAddXs.setObject(19, "");
						patAddXs.setObject(20, pln.getPlineCode());
						patAddXs.setObject(21, pln.getPlineName());
						patAddXs.setObject(22, "");
						patAddXs.setObject(23, "");
						patAddXs.setObject(24, pln.getAimoutdiameter());
						patAddXs.setObject(25, pln.getAimwallthick());
						patAddXs.setObject(26, pln.getOutdiameterIn());
						patAddXs.setObject(27, pln.getWallthickIn());
						patAddXs.setObject(28, pln.getLengthIn());
						patAddXs.setObject(29, pln.getLengthminIn());
						patAddXs.setObject(30, pln.getLengthmaxIn());
						patAddXs.setObject(31, "");
						patAddXs.setObject(32, "");
						patAddXs.setObject(33, "");
						patAddXs.setObject(34, "");
						patAddXs.setObject(35, list.get(4));
						patAddXs.setObject(36, systime);
//						patAddXs.setObject(37, pln.getUpdateName());
//						patAddXs.setObject(38, pln.getUpdateTime()); 
						patAddXs.setObject(37, pln.getOrdLnDlyPk());
						patAddXs.setObject(38, pln.getProPlanId());
						patAddXs.setObject(39, "");
						patAddXs.setObject(40, pln.getGxPlanNo());
						patAddXs.addBatch();

						
						patAddD.setObject(1,(locode + batch)+ StringTool.formatDecimal(g, "0000"));
						patAddD.setObject(2, locode);
						patAddD.setObject(3, batch);
						patAddD.setObject(4, groupNo);
						patAddD.setObject(5, "");
						patAddD.setObject(6, "");
						patAddD.setObject(7, "");
						patAddD.setObject(8, "");
						patAddD.setObject(9, "");
						patAddD.setObject(10, "");
						patAddD.setObject(11, "");
						patAddD.setObject(12, "");
						patAddD.setObject(13, "");
						patAddD.setObject(14, "");
						patAddD.setObject(15, "");
						patAddD.setObject(16, list.get(4));
						patAddD.setObject(17, systime);
//						patAddD.setObject(18, pln.getUpdateName());
//						patAddD.setObject(19, pln.getUpdateTime());
						patAddD.setObject(18, "");
						patAddD.setObject(19, "");
						patAddD.addBatch();

						patAddD1.setObject(1, inListDoc+StringTool.formatDecimal(n, "0000"));
						patAddD1.setObject(2, inListDoc);
						patAddD1.setObject(3, "");
						patAddD1.setObject(4, "");
						patAddD1.setObject(5, "");
						patAddD1.setObject(6,(locode + batch)+ StringTool.formatDecimal(g, "0000"));
						patAddD1.setObject(7, "");
						patAddD1.setObject(8, "");
						patAddD1.setObject(9, locode);
						patAddD1.setObject(10, batch);
						patAddD1.setObject(11, groupNo);
						patAddD1.setObject(12, pln.getOutwlId());
						patAddD1.setObject(13, pln.getOutwlDesc());
						patAddD1.setObject(14, pln.getGradecode());
						patAddD1.setObject(15, pln.getGradename());
						patAddD1.setObject(16, "");
						patAddD1.setObject(17, "");
						patAddD1.setObject(18, "");
						patAddD1.setObject(19, "");
						patAddD1.setObject(20, pln.getPlineCode());
						patAddD1.setObject(21, pln.getPlineName());
						patAddD1.setObject(22, pln.getOutdiameterIn());
						patAddD1.setObject(23, pln.getWallthickIn());
						patAddD1.setObject(24, pln.getLengthIn());
						patAddD1.setObject(25, pln.getLengthminIn());
						patAddD1.setObject(26, pln.getLengthmaxIn());
						patAddD1.setObject(27, "");
						patAddD1.setObject(28, "");
						patAddD1.setObject(29, "");
						patAddD1.setObject(30, "");
						patAddD1.setObject(31, "");
						patAddD1.setObject(32, "");
						patAddD1.setObject(33, "");
						patAddD1.setObject(34, "");
						patAddD1.setObject(35, list.get(4));
						patAddD1.setObject(36, systime);
//						patAddD1.setObject(37, pln.getUpdateName());
//						patAddD1.setObject(38, pln.getUpdateTime());
						patAddD1.setObject(37, pln.getOrdLnDlyPk());
						patAddD1.setObject(38, "");
						patAddD1.setObject(39, "");
						patAddD1.setObject(40, pln.getOrderNo());
						patAddD1.setObject(41, pln.getOrderSeq());
						patAddD1.setObject(42, pln.getDeliveryNo());
						patAddD1.setObject(43, "801401");
						patAddD1.setObject(44, "80150101");
						patAddD1.setObject(45, "");
						patAddD1.setObject(46, "");
						patAddD1.setObject(47, "");
						patAddD1.setObject(48, "");
						patAddD1.setObject(49, "");
						patAddD1.addBatch();
						
						patAddD1Xs.setObject(1, inListDoc+StringTool.formatDecimal(n, "0000"));
						patAddD1Xs.setObject(2, inListDoc);
						patAddD1Xs.setObject(3, "");
						patAddD1Xs.setObject(4, "");
						patAddD1Xs.setObject(5, "");
						patAddD1Xs.setObject(6,(locode + batch)+ StringTool.formatDecimal(g, "0000"));
						patAddD1Xs.setObject(7, "");
						patAddD1Xs.setObject(8, "");
						patAddD1Xs.setObject(9, locode);
						patAddD1Xs.setObject(10, batch);
						patAddD1Xs.setObject(11, groupNo);
						patAddD1Xs.setObject(12, pln.getOutwlId());
						patAddD1Xs.setObject(13, pln.getOutwlDesc());
						patAddD1Xs.setObject(14, pln.getGradecode());
						patAddD1Xs.setObject(15, pln.getGradename());
						patAddD1Xs.setObject(16, "");
						patAddD1Xs.setObject(17, "");
						patAddD1Xs.setObject(18, "");
						patAddD1Xs.setObject(19, "");
						patAddD1Xs.setObject(20, pln.getPlineCode());
						patAddD1Xs.setObject(21, pln.getPlineName());
						patAddD1Xs.setObject(22, pln.getOutdiameterIn());
						patAddD1Xs.setObject(23, pln.getWallthickIn());
						patAddD1Xs.setObject(24, pln.getLengthIn());
						patAddD1Xs.setObject(25, pln.getLengthminIn());
						patAddD1Xs.setObject(26, pln.getLengthmaxIn());
						patAddD1Xs.setObject(27, "");
						patAddD1Xs.setObject(28, "");
						patAddD1Xs.setObject(29, "");
						patAddD1Xs.setObject(30, "");
						patAddD1Xs.setObject(31, "");
						patAddD1Xs.setObject(32, "");
						patAddD1Xs.setObject(33, "");
						patAddD1Xs.setObject(34, "");
						patAddD1Xs.setObject(35, list.get(4));
						patAddD1Xs.setObject(36, systime);
//						patAddD1Xs.setObject(37, pln.getUpdateName());
//						patAddD1Xs.setObject(38, pln.getUpdateTime());
						patAddD1Xs.setObject(37, pln.getOrdLnDlyPk());
						patAddD1Xs.setObject(38, "");
						patAddD1Xs.setObject(39, "");
						patAddD1Xs.setObject(40, pln.getOrderNo());
						patAddD1Xs.setObject(41, pln.getOrderSeq());
						patAddD1Xs.setObject(42, pln.getDeliveryNo());
						patAddD1Xs.setObject(43, "801401");
						patAddD1Xs.setObject(44, "80150101");
						patAddD1Xs.setObject(45, "");
						patAddD1Xs.setObject(46, "");
						patAddD1Xs.setObject(47, "");
						patAddD1Xs.setObject(48, "");
						patAddD1Xs.setObject(49, "");
						patAddD1Xs.addBatch();
					//	patAddD1.executeBatch();
						// insertMatPsta.addBatch();
						n++;
						g++;
					}
				}
			}
			patAdd.executeBatch();
			patAddXs.executeBatch();
			//patAddD.executeBatch();
			patAddD1.executeBatch();
			patAddD1Xs.executeBatch();
			cro.setV_errMsg("交库成功!");
			connection.commit();
			// }
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 生成组号
	 * 
	 * @param con
	 * @param matTity
	 * @param parm
	 * @return
	 */
	public String creatGroup(Connection con, String matTity,
			ArrayList<String> parm) 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);
		VrpMilEntity milPlan = EntityTool.getEntityByJSONStr(matTity,
				VrpMilEntity.class);
		sql = CoreXmlSqlParsersFactory.getSql("creatGroup.selectMilInStore");
		psta = con.prepareStatement(sql);
		psta.setString(1, milPlan.getJudgestoveNo());
		psta.setString(2, parm.get(3));
		// if (milPlan.getAstatus().equals("样管")) {
		// psta.setString(3, "1");
		// } else {
		// psta.setString(3, "0");
		// }
		// psta.setString(4, parm.get(4));
		// psta.setString(5, parm.get(5));
		psta.setString(4, parm.get(0));
		psta.setString(5, parm.get(0));
		rs = psta.executeQuery();
		if (rs.next()) {
			strGroup = rs.getString("BATCH_GROUD_NO");
		}
		rs.close();
		psta.close();

		if (strGroup.equals("")) {
			strGroup = queryGroupNo(con, milPlan.getJudgestoveNo(), parm.get(3));// 组号
			// if (Double.parseDouble(milPlan.getLengthmin()) > Double
			// .parseDouble(parm.get(0))) {
			// actLenM = lessThanActMin(parm.get(0),
			// new DecimalFormat("0.000")
			// .format(Double.parseDouble(milPlan
			// .getLengthmin().toString())));// 长度小于下限
			// actLenMin = actLenM[0];
			// actLenMax = actLenM[1];
			// } else if (Double.parseDouble(parm.get(0)) > Double
			// .parseDouble(milPlan.getLengthmax())) {
			// actLenM = moreThanActMax(parm.get(0),
			// 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, parm.get(3));
			// if (milPlan.getAstatus().equals("样管")) {
			// psta.setString(4, "1");
			// } else {
			// psta.setString(4, "0");
			// }
			// psta.setString(5, parm.get(4));
			// psta.setString(6, parm.get(5));
			// psta.setString(7, actLenMin);
			// psta.setString(8, actLenMax);
			// psta.setString(9, strGroup);
			// psta.addBatch();
			// psta.executeBatch();
			con.commit();
		}
		return strGroup;
	}

	/***
	 * 小于长度下限时
	 * 
	 * @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));
			// lengmax = getNumber(String.valueOf(lengthmax));
			if (Integer.parseInt(lengmin[1]) > 0
					&& 800 > Integer.parseInt(lengmin[1])
					&& !lengmin[1].equals("5")) {
				lengthmin = Double.parseDouble(lengmin[0].toString() + ".5");
			} else if (Integer.parseInt(lengmin[1]) > 800) {
				lengthmin = Double.parseDouble(String.valueOf(Integer
						.parseInt(lengmin[0]) + 1) + ".0");
			}

			// if(!lengmax[1].equals("0")&&!lengmax[1].equals("5"))
			// {
			// if(Integer.parseInt(lengmax[1])>= 1 &&
			// 8>Integer.parseInt(lengmax[1]))
			// {
			// lengthmax = Double.parseDouble(lengmax[0].toString()+".5");
			// }else if(Integer.parseInt(lengmax[1]) > 8)
			// {
			// lengthmax =
			// Double.parseDouble(String.valueOf(Integer.parseInt(lengmax[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;
			}
			// lengthmin = lengthmin-1;
			// lengthmax = lengthmax-1;
		}
		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));
			// 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(!lengmax[1].equals("0")&&!lengmax[1].equals("5"))
			// {
			// if(Integer.parseInt(lengmax[1])>= 1 &&
			// 8>Integer.parseInt(lengmax[1]))
			// {
			// lengthmax = Double.parseDouble(lengmax[0].toString()+".5");
			// }else if(Integer.parseInt(lengmax[1]) > 8)
			// {
			// lengthmax =
			// Double.parseDouble(String.valueOf(Integer.parseInt(lengmax[0])+1)+".0");
			// }
			// }
			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;
	}

	/***
	 * 撤销交库
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject cancelPipeIn(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		int acount = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String deleteSql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.cancelPipeIn");
			PreparedStatement deletePsta = connection
					.prepareStatement(deleteSql);
			for (String str : parm) {
				YdmZcInlistEntity inListEntity = EntityTool.getEntityByJSONStr(
						str, YdmZcInlistEntity.class);
				// sql = CoreXmlSqlParsersFactory
				// .getSql("cancelPipeIn.doQueryStaus");//
				// psta = connection.prepareStatement(sql);
				// psta.setString(1, inListEntity.getJudgeStoveNo());
				// psta.setString(2, inListEntity.getBatchNo());
				// psta.setString(3, inListEntity.getBatchGroudNo());
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// acount = Integer.parseInt(rs.getString("ACOUNT"));
				// }
				// rs.close();
				// psta.close();
				// if (acount > 0) {
				// cro.setV_errMsg("炉号" + inListEntity.getJudgeStoveNo()
				// + "已送质量判定,不能撤销!");
				// return cro;
				// }

				// sql = CoreXmlSqlParsersFactory
				// .getSql("cancelPipeIn.doQueryCount");// 查询是否已有转移的
				// psta = connection.prepareStatement(sql);
				// psta.setString(1, inListEntity.getJudgeStoveNo());
				// psta.setString(2, inListEntity.getBatchNo());
				// psta.setString(3, inListEntity.getBatchGroudNo());
				// rs = psta.executeQuery();
				// if (rs.next()) {
				// acount = Integer.parseInt(rs.getString("ACOUNT"));
				// }
				// rs.close();
				// psta.close();
				// if (acount > 0) {
				// cro.setV_errMsg("炉号" + inListEntity.getJudgeStoveNo()
				// + "已有转移,不能撤销!");
				// return cro;
				// }

				deletePsta.setString(1, inListEntity.getInstockDoc());
				deletePsta.setString(2, inListEntity.getJudgeStoveNo());
				deletePsta.setString(3, inListEntity.getBatchNo());
				deletePsta.setString(4, inListEntity.getBatchGroudNo());
				deletePsta.setString(5, inListEntity.getJudgeStoveNo());
				deletePsta.setString(6, inListEntity.getBatchNo());
				deletePsta.setString(7, inListEntity.getBatchGroudNo());
				deletePsta.setString(8, inListEntity.getJudgeStoveNo());
				deletePsta.setString(9, inListEntity.getBatchNo());
				deletePsta.setString(10, inListEntity.getBatchGroudNo());
				deletePsta.addBatch();
			}
			deletePsta.executeBatch();
			cro.setV_errMsg("撤销成功!");
			connection.commit();

		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 申请判定
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doDecision(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String jugdeApplyNo = "";// 判定单号
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.doDecision");

			PreparedStatement updatePsta = connection
					.prepareStatement(updateSql);
			MatZcMEntity matZcTityS = EntityTool.getEntityByJSONStr(
					parm.get(0), MatZcMEntity.class);
			sql = CoreXmlSqlParsersFactory.getSql("doDecision.jugdeApplyNo");// 生成判定单号
			psta = connection.prepareStatement(sql);
			psta.setString(1, matZcTityS.getPlineCode());
			psta.setString(2, matZcTityS.getProductFlag());
			psta.setString(3, matZcTityS.getPlineCode());
			psta.setString(4, matZcTityS.getProductFlag());
			rs = psta.executeQuery();
			if (rs.next()) {
				jugdeApplyNo = rs.getString("JUGDE_APPLY_CODE");
			}
			rs.close();
			psta.close();
			for (String str : parm) {
				MatZcMEntity matZcTity = EntityTool.getEntityByJSONStr(str,
						MatZcMEntity.class);

				updatePsta.setString(1, matZcTity.getJudgeStoveNo());
				updatePsta.setString(2, matZcTity.getBatchNo());
				updatePsta.setString(3, matZcTity.getBatchGroudNo());
				updatePsta.setString(4, matZcTity.getStorageNo());
				updatePsta.setString(5, matZcTity.getLocationNo());
				updatePsta.setString(6, matZcTity.getProductFlag());
				updatePsta.setString(7, matZcTity.getMatStatus());
				updatePsta.setString(8, jugdeApplyNo);
				updatePsta.addBatch();
			}
			updatePsta.executeBatch();
			cro.setV_errMsg("申请判定成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 撤销申请判定
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject cancelDoDecision(ArrayList<String> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sql = "";
			PreparedStatement psta = null;
			ResultSet rs = null;
			String acount = "";
			String updateMatZcSql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.cancelDoDecision");
			PreparedStatement updateMatZcPsta = connection
					.prepareStatement(updateMatZcSql);
			for (String str : parm) {
				MatZcMEntity matZcTity = EntityTool.getEntityByJSONStr(str,
						MatZcMEntity.class);
				// if (matZcTity.getJugdeApplyCode().equals("")) {
				// cro.setV_errMsg("炉号'" + matZcTity.getJudgeStoveNo()
				// + "'未进行申请判定,不能进行撤销!");
				// return cro;
				// }
				sql = CoreXmlSqlParsersFactory
						.getSql("cancelDoDecision.doQuery");// 查询是否已判定
				psta = connection.prepareStatement(sql);
				// psta.setString(1, matZcTity.getJugdeApplyCode());
				psta.setString(2, matZcTity.getJudgeStoveNo());
				psta.setString(3, matZcTity.getBatchNo());
				psta.setString(4, matZcTity.getBatchGroudNo());
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if (Integer.parseInt(acount) > 0) {
					cro.setV_errMsg("炉号'" + matZcTity.getJudgeStoveNo()
							+ "'已有进行质量判定的材料,无法撤销申请!");
					return cro;
				}
				// updateMatZcPsta.setString(1, matZcTity.getJugdeApplyCode());
				updateMatZcPsta.setString(2, matZcTity.getJudgeStoveNo());
				updateMatZcPsta.setString(3, matZcTity.getBatchNo());
				updateMatZcPsta.setString(4, matZcTity.getBatchGroudNo());
				updateMatZcPsta.addBatch();
			}
			updateMatZcPsta.executeBatch();
			cro.setV_errMsg("撤销成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 结炉验证
	 * 
	 * @param jugeNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doBeside(String jugeNo, String lotNo,
			String plineCode) throws Exception {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			int matCount = 0;// 库存支数
			int milCount = 0;// 轧管支数
			String updatePlanSql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpMil.doBeside");
			PreparedStatement updatePlanPsta = connection
					.prepareStatement(updatePlanSql);
			matCount = doQueryMatZcCount(connection, jugeNo, plineCode);
			milCount = doQueryMilCount(connection, jugeNo, plineCode);
			if (matCount != milCount) {
				cro.setV_errMsg("入虚拟库支数与轧管投入支数不一致,不允许进行结炉!");
				return cro;
			}
			updatePlanPsta.setString(1, jugeNo);
			updatePlanPsta.setString(2, lotNo);
			updatePlanPsta.setString(3, plineCode);
			updatePlanPsta.addBatch();
			updatePlanPsta.executeBatch();
			cro.setV_errMsg("结炉成功!");
			connection.commit();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}

	/***
	 * 查询已入库支数
	 * 
	 * @param conn
	 * @param jugeNo
	 * @param plineCode
	 * @return
	 * @throws Exception
	 */
	public int doQueryMatZcCount(Connection conn, String jugeNo,
			String plineCode) throws Exception {
		int count = 0;
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.queryMatZcCount");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, jugeNo);
		psta.setString(2, plineCode);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			count = Integer.parseInt(rs.getString("ACOUNT"));
		}
		rs.close();
		psta.close();
		return count;
	}

	/***
	 * 查询轧管产出支数
	 * 
	 * @param conn
	 * @param jugeNo
	 * @param plineCode
	 * @return
	 * @throws Exception
	 */
	public int doQueryMilCount(Connection conn, String jugeNo, String plineCode)
			throws Exception {
		int count = 0;
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.doQueryMilCount");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, jugeNo);
		psta.setString(2, plineCode);
		// psta.setString(3, jugeNo);
		// psta.setString(4, plineCode);
		// psta.setString(5, jugeNo);
		// psta.setString(6, plineCode);
		// psta.setString(7, jugeNo);
		// psta.setString(8, plineCode);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			count = Integer.parseInt(rs.getString("ACOUNT"));
		}
		rs.close();
		psta.close();
		return count;
	}

	/**
	 * 根据炉号、批号自动组号
	 * 
	 * @param conn
	 *            c
	 * @param strLcode
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryGroupNo(Connection conn, String strLcode, String strBatch)
			throws Exception {
		int count = 0;
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.queryGroupNo");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strLcode);
		psta.setString(2, strBatch);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			String res = rs.getString(1);
			if (res == null) {
				res = "001";
			} else {
				count = Integer.parseInt(res.substring(res.length() - 3,
						res.length()));
			}
		}
		rs.close();
		psta.close();
		return StringTool.formatDecimal(++count, "000");
	}

	/**
	 * 每支材料对应的入库单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSqlDoc(Connection conn, String strlist, String memo)
			throws Exception {
		int seq = 0;
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat matter2 = new SimpleDateFormat("yyMMdd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.getInListDoc");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, matter1.format(dt));
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			String res = rs.getString(1);
			if (res == null) {
				res = memo + matter2.format(dt) + "00001";
			} else {
				seq = Integer.parseInt(res.substring(8, 13));
			}
		}
		rs.close();
		psta.close();
		return memo + matter2.format(dt)
				+ StringTool.formatDecimal(++seq, "00000");
	}

	/**
	 * 每支材料对应的入库流水号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSql(Connection conn, String strlist)
			throws Exception {
		int seq = 0;
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.getInListDoc1");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, matter1.format(dt));
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			String res = rs.getString(1);
			if (res == null) {
				res = strlist + "0001";
			} else {
				seq = Integer.parseInt(res.substring(13, 17));
			}
		}
		rs.close();
		psta.close();
		return strlist + StringTool.formatDecimal(++seq, "0000");
	}

	/**
	 * 根据炉号、批号自动生成材料号
	 * 
	 * @param conn
	 *            c
	 * @param strLcode
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String queryMatNoM(Connection conn, String strLcode, String strBatch)
			throws Exception {
		int count = 0;
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmVrpMil.queryMatNoM");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strLcode);
		psta.setString(2, strBatch);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			String res = rs.getString(1);
			if (res == null) {
				res = strLcode + strBatch + "0001";
			} else {
				count = Integer.parseInt(res.substring(res.length() - 4,
						res.length()));
			}
		}
		rs.close();
		psta.close();
		return (strLcode + strBatch)
				+ StringTool.formatDecimal(++count, "0000");
	}

	/***
	 * 根据产线找在线库信息
	 * 
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryStoreNo(String plineCode) {
		return this.getDataBySqlID("FrmVrpMil.doQueryStoreNo",
				new Object[] { plineCode });
	}
	
	
}
