package com.steering.mes.mcp.Vrp;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
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.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class FrmScrapStoveApplyDescion extends DBCommon {

	/***
	 * 查询整炉废数据镦拔扩
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryMataE(ArrayList<String> parm) {

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmScrapStoveApplyDescion.doQueryMataE");
		return this.getData(sql, new Object[] { parm.get(0), parm.get(1),
				parm.get(2) });
	}
	
	/***
	 * 查询整炉废数据热处理
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryMataF(ArrayList<String> parm) {

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmScrapStoveApplyDescion.doQueryMataF");
		return this.getData(sql, new Object[] { parm.get(0), parm.get(1),
				parm.get(2) });
	}
	
	/***
	 * 查询整炉废数据加工线
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryMataG(ArrayList<String> parm) {

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmScrapStoveApplyDescion.doQueryMataG");
		return this.getData(sql, new Object[] { parm.get(0), parm.get(1),
				parm.get(2) });
	}

	/***
	 * 查询白条信息镦拔扩
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryBTMataE(ArrayList<String> parm) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmScrapStoveApplyDescion.doQueryBTMataE");
		return this.getData(sql, new Object[] { parm.get(0), parm.get(1),
				parm.get(2) });
	}
	/***
	 * 查询白条信息热处理
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryBTMataF(ArrayList<String> parm) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmScrapStoveApplyDescion.doQueryBTMataF");
		return this.getData(sql, new Object[] { parm.get(0), parm.get(1),
				parm.get(2) });
	}
	/***
	 * 查询白条信息加工线
	 * 
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryBTMataG(ArrayList<String> parm) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmScrapStoveApplyDescion.doQueryBTMataG");
		return this.getData(sql, new Object[] { parm.get(0), parm.get(1),
				parm.get(2) });
	}

	/***
	 * 查询整炉废、白条信息
	 * 
	 * @param jugeNo
	 * @return
	 */
	public CoreReturnObject doQueryQcmMata(ArrayList<String> parm) {
		return this.getDataBySqlID("FrmScrapStoveApplyDescion.doQueryQcmMata",
				new Object[] { parm.get(0),parm.get(1), parm.get(2),parm.get(3) });
	}

	/***
	 * 整炉废申请判定镦拔扩
	 * 
	 * @param milPlanTity
	 * @return
	 */
	public CoreReturnObject doDecisionScrapE(ArrayList<String> parmList,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.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");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String jugdeApplyNo = "";// 判定单号
		int judgeSequenNo = 1;
		String inputCount = "";// 投料支
		String inputWeight = "";// 投料吨

		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for (String str : parmList) {
				PlnZyDbkMEntity milPtity = EntityTool.getEntityByJSONStr(str,
						PlnZyDbkMEntity.class);
				QcmZgJugdeApplyEntity qcmTity = new QcmZgJugdeApplyEntity();
				String insertQcmZcSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.jugdeApplyNo");// 生成判定单号
				sql = EntityTool.fillSqlByEntity(sql, EntityTool
						.getEntityByJSONStr(parmList.get(0), PlnZyDbkMEntity.class));
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					jugdeApplyNo = rs.getString("JUGDE_APPLY_CODE");
				}
				rs.close();
				psta.close();
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.SelectJudegNoSequenNo");// 查询申请判定顺序号
				psta = connection.prepareStatement(sql);
				psta.setString(1, jugdeApplyNo);
				rs = psta.executeQuery();
				if (rs.next()) {
					judgeSequenNo = Integer.parseInt(rs
							.getString("JUGDE_APPLY_SEQUENO"));
				}
				rs.close();
				psta.close();

//				sql = CoreXmlSqlParsersFactory
//						.getSql("FrmScrapStoveApplyDescion.SelectScrapInput");// 查询投料支吨
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo().substring(0, 6));
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					inputCount = rs.getString("INPUT_COUNT");
//					inputWeight = rs.getString("INPUT_WEIGHT");
//				}
//				rs.close();
//				psta.close();
				String belongCode = "";
				String belongName = "";
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapStoveApplyDescion.selectBelong");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getPlineCode());							
				rs = psta.executeQuery();
				if(rs.next())
				{
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				psta.close();
				String[] belong = null;
				belong = getBelongJudge(connection, EntityTool.getEntityByJSONStr(
						parmList.get(0), MatBcMEntity.class));
			
				qcmTity.setJugdeApplyCode(jugdeApplyNo);
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setJudgeStoveNo(milPtity.getJudgeStoveNo());
				qcmTity.setProducname(milPtity.getProducname());
				qcmTity.setSteelname(milPtity.getSteelname());
				qcmTity.setSpecName(milPtity.getSpecName());
				qcmTity.setModelDesc(milPtity.getModelDesc());
				qcmTity.setStdStyleDesc(milPtity.getStdStyleDesc());
				qcmTity.setPlineCode(milPtity.getPlineCode());
				qcmTity.setBelongCode(belongCode);
				qcmTity.setBelongName(belongName);
				qcmTity.setBelongCodeJugde(belong[0]);
				qcmTity.setBelongNameJugde(belong[1]);
				qcmTity.setPlineName(milPtity.getPlineName());
			
				qcmTity.setValidflag("20");
				qcmTity.setActCount("0");
				qcmTity.setActWeight("0");
				qcmTity.setActTheoryWeight("0");
				qcmTity.setInputCount(milPtity.getPlnInsingleNum());
				qcmTity.setInputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setOutputCount(milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setIsAllScrap("1");
				qcmTity.setApplyName(userName);
				qcmTity.setTarget(milPtity.getDecision());
				qcmTity.setCrafNo(milPtity.getCraftFileNo());
				qcmTity.setProOrderId(milPtity.getProPlanId());
				qcmTity.setGxPlanNo(milPtity.getGxPlanNo());
				qcmTity.setBalYearMonth(yearMoth);
				qcmTity.setBatchNo(milPtity.getBatchNo());
				qcmTity.setBatchGroudNo("001");
				qcmTity.setLotNo(milPtity.getZyBatchId());
				qcmTity.setProOrderNo(milPtity.getOrderNo());
				qcmTity.setGradename(milPtity.getGradename());
				qcmTity.setStdName(milPtity.getStdName());
				qcmTity.setApplyFlag("0");
				qcmTity.setProcessCode("E");

				insertQcmZcSql = EntityTool.fillSqlByEntity(insertQcmZcSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcSql);
				psta.executeUpdate();
				psta.close();

//				String updateProcess = CoreXmlSqlParsersFactory
//						.getSql("FrmInOnlineStore.insertMilProcess");// 更新进程表mil_processes数据
//				PreparedStatement updateMilProcess = connection
//						.prepareStatement(updateProcess);

//				// 更新进程表mil_processes数据
//				updateMilProcess.setString(1, milPtity.getJudgeStoveNo());
//				updateMilProcess.setString(2, milPtity.getLotNo());
//				updateMilProcess.setString(3, milPtity.getPlineCode());
//				updateMilProcess.setString(4, milPtity.getProPlanId());
//				updateMilProcess.setString(5, milPtity.getGxPlanNo());
//				updateMilProcess.addBatch();
//				updateMilProcess.executeBatch();

				String updatePlanStatus = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.updatePlanStatusE");// 更新炉计划状态
				PreparedStatement updatePlanStatusPst = connection
						.prepareStatement(updatePlanStatus);
				updatePlanStatusPst.setString(1, "40"); // 关闭炉计划
				updatePlanStatusPst.setString(2, milPtity.getHeatPlanNo());
				updatePlanStatusPst.setString(3, milPtity.getJudgeStoveNo());
				updatePlanStatusPst.addBatch();
				updatePlanStatusPst.executeBatch();
				judgeSequenNo ++;
			}
			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 milPlanTity
	 * @return
	 */
	public CoreReturnObject doDecisionScrapF(ArrayList<String> parmList,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.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");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String jugdeApplyNo = "";// 判定单号
		int judgeSequenNo = 1;
		String inputCount = "";// 投料支
		String inputWeight = "";// 投料吨

		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for (String str : parmList) {
				PlnZyRclMEntity1 milPtity = EntityTool.getEntityByJSONStr(str,
						PlnZyRclMEntity1.class);
				QcmZgJugdeApplyEntity qcmTity = new QcmZgJugdeApplyEntity();
				String insertQcmZcSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.jugdeApplyNo");// 生成判定单号
				sql = EntityTool.fillSqlByEntity(sql, EntityTool
						.getEntityByJSONStr(parmList.get(0), PlnZyRclMEntity1.class));
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					jugdeApplyNo = rs.getString("JUGDE_APPLY_CODE");
				}
				rs.close();
				psta.close();
				
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.SelectJudegNoSequenNo");// 查询申请判定顺序号
				psta = connection.prepareStatement(sql);
				psta.setString(1, jugdeApplyNo);
				rs = psta.executeQuery();
				if (rs.next()) {
					judgeSequenNo = Integer.parseInt(rs
							.getString("JUGDE_APPLY_SEQUENO"));
				}
				rs.close();
				psta.close();

//				sql = CoreXmlSqlParsersFactory
//						.getSql("FrmScrapStoveApplyDescion.SelectScrapInput");// 查询投料支吨
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo().substring(0, 6));
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					inputCount = rs.getString("INPUT_COUNT");
//					inputWeight = rs.getString("INPUT_WEIGHT");
//				}
//				rs.close();
//				psta.close();
				String belongCode = "";
				String belongName = "";
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapStoveApplyDescion.selectBelong");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getPlineCode());							
				rs = psta.executeQuery();
				if(rs.next())
				{
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				psta.close();
				String[] belong = null;
				belong = getBelongJudge(connection, EntityTool.getEntityByJSONStr(
						parmList.get(0), MatBcMEntity.class));
			
				qcmTity.setJugdeApplyCode(jugdeApplyNo);
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setJudgeStoveNo(milPtity.getJudgeStoveNo());
				qcmTity.setProducname(milPtity.getProducname());
				qcmTity.setSteelname(milPtity.getSteelname());
				qcmTity.setSpecName(milPtity.getSpecName());
				qcmTity.setModelDesc(milPtity.getModelDesc());
				qcmTity.setStdStyleDesc(milPtity.getStdStyleDesc());
				qcmTity.setPlineCode(milPtity.getPlineCode());
				qcmTity.setBelongCode(belongCode);
				qcmTity.setBelongName(belongName);
				qcmTity.setBelongCodeJugde(belong[0]);
				qcmTity.setBelongNameJugde(belong[1]);
				qcmTity.setPlineName(milPtity.getPlineName());
			
				qcmTity.setValidflag("20");
				qcmTity.setActCount("0");
				qcmTity.setActWeight("0");
				qcmTity.setActTheoryWeight("0");
				qcmTity.setInputCount(milPtity.getPlnInsingleNum());
				qcmTity.setInputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setOutputCount(milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setIsAllScrap("1");
				qcmTity.setApplyName(userName);
				qcmTity.setTarget(milPtity.getDecision());
				qcmTity.setCrafNo(milPtity.getCraftFileNo());
				qcmTity.setProOrderId(milPtity.getProPlanId());
				qcmTity.setGxPlanNo(milPtity.getGxPlanNo());
				qcmTity.setBalYearMonth(yearMoth);
				qcmTity.setBatchNo(milPtity.getBatchNo());
				qcmTity.setBatchGroudNo("001");
				qcmTity.setLotNo(milPtity.getZyBatchId());
				qcmTity.setProOrderNo(milPtity.getOrderNo());
				qcmTity.setGradename(milPtity.getGradename());
				qcmTity.setStdName(milPtity.getStdName());
				qcmTity.setApplyFlag("0");
				qcmTity.setProcessCode("F");

				insertQcmZcSql = EntityTool.fillSqlByEntity(insertQcmZcSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcSql);
				psta.executeUpdate();
				psta.close();

//				String updateProcess = CoreXmlSqlParsersFactory
//						.getSql("FrmInOnlineStore.insertMilProcess");// 更新进程表mil_processes数据
//				PreparedStatement updateMilProcess = connection
//						.prepareStatement(updateProcess);

//				// 更新进程表mil_processes数据
//				updateMilProcess.setString(1, milPtity.getJudgeStoveNo());
//				updateMilProcess.setString(2, milPtity.getLotNo());
//				updateMilProcess.setString(3, milPtity.getPlineCode());
//				updateMilProcess.setString(4, milPtity.getProPlanId());
//				updateMilProcess.setString(5, milPtity.getGxPlanNo());
//				updateMilProcess.addBatch();
//				updateMilProcess.executeBatch();

				String updatePlanStatus = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.updatePlanStatusF");// 更新炉计划状态
				PreparedStatement updatePlanStatusPst = connection
						.prepareStatement(updatePlanStatus);
				updatePlanStatusPst.setString(1, "40"); // 关闭炉计划
				updatePlanStatusPst.setString(2, milPtity.getHeatPlanNo());
				updatePlanStatusPst.setString(3, milPtity.getJudgeStoveNo());
				updatePlanStatusPst.addBatch();
				updatePlanStatusPst.executeBatch();
				judgeSequenNo ++;
			}
			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 milPlanTity
	 * @return
	 */
	public CoreReturnObject doDecisionScrapG(ArrayList<String> parmList,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.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");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String jugdeApplyNo = "";// 判定单号
		int judgeSequenNo = 1;
		String inputCount = "";// 投料支
		String inputWeight = "";// 投料吨

		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for (String str : parmList) {
				PlnZyJgxMEntity1 milPtity = EntityTool.getEntityByJSONStr(str,
						PlnZyJgxMEntity1.class);
				QcmZgJugdeApplyEntity qcmTity = new QcmZgJugdeApplyEntity();
				String insertQcmZcSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.jugdeApplyNo");// 生成判定单号
				sql = EntityTool.fillSqlByEntity(sql, EntityTool
						.getEntityByJSONStr(parmList.get(0), PlnZyJgxMEntity1.class));
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					jugdeApplyNo = rs.getString("JUGDE_APPLY_CODE");
				}
				rs.close();
				psta.close();
				
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.SelectJudegNoSequenNo");// 查询申请判定顺序号
				psta = connection.prepareStatement(sql);
				psta.setString(1, jugdeApplyNo);
				rs = psta.executeQuery();
				if (rs.next()) {
					judgeSequenNo = Integer.parseInt(rs
							.getString("JUGDE_APPLY_SEQUENO"));
				}
				rs.close();
				psta.close();

//				sql = CoreXmlSqlParsersFactory
//						.getSql("FrmScrapStoveApplyDescion.SelectScrapInput");// 查询投料支吨
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo().substring(0, 6));
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					inputCount = rs.getString("INPUT_COUNT");
//					inputWeight = rs.getString("INPUT_WEIGHT");
//				}
//				rs.close();
//				psta.close();
				String belongCode = "";
				String belongName = "";
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapStoveApplyDescion.selectBelong");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getPlineCode());							
				rs = psta.executeQuery();
				if(rs.next())
				{
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				psta.close();
				String[] belong = null;
				belong = getBelongJudge(connection, EntityTool.getEntityByJSONStr(
						parmList.get(0), MatBcMEntity.class));
			
				qcmTity.setJugdeApplyCode(jugdeApplyNo);
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setJudgeStoveNo(milPtity.getJudgeStoveNo());
				qcmTity.setProducname(milPtity.getProducname());
				qcmTity.setSteelname(milPtity.getSteelname());
				qcmTity.setSpecName(milPtity.getSpecName());
				qcmTity.setModelDesc(milPtity.getModelDesc());
				qcmTity.setStdStyleDesc(milPtity.getStdStyleDesc());
				qcmTity.setPlineCode(milPtity.getPlineCode());
				qcmTity.setBelongCode(belongCode);
				qcmTity.setBelongName(belongName);
				qcmTity.setBelongCodeJugde(belong[0]);
				qcmTity.setBelongNameJugde(belong[1]);
				qcmTity.setPlineName(milPtity.getPlineName());
			
				qcmTity.setValidflag("20");
				qcmTity.setActCount("0");
				qcmTity.setActWeight("0");
				qcmTity.setActTheoryWeight("0");
				qcmTity.setInputCount(milPtity.getPlnInsingleNum());
				qcmTity.setInputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setOutputCount(milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setIsAllScrap("1");
				qcmTity.setApplyName(userName);
				qcmTity.setTarget(milPtity.getDecision());
				qcmTity.setCrafNo(milPtity.getCraftFileNo());
				qcmTity.setProOrderId(milPtity.getProPlanId());
				qcmTity.setGxPlanNo(milPtity.getGxPlanNo());
				qcmTity.setBalYearMonth(yearMoth);
				qcmTity.setBatchNo(milPtity.getBatchNo());
				qcmTity.setBatchGroudNo("001");
				qcmTity.setLotNo(milPtity.getZyBatchId());
				qcmTity.setProOrderNo(milPtity.getOrderNo());
				qcmTity.setGradename(milPtity.getGradename());
				qcmTity.setStdName(milPtity.getStdName());
				qcmTity.setApplyFlag("0");
				qcmTity.setProcessCode("G");

				insertQcmZcSql = EntityTool.fillSqlByEntity(insertQcmZcSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcSql);
				psta.executeUpdate();
				psta.close();

//				String updateProcess = CoreXmlSqlParsersFactory
//						.getSql("FrmInOnlineStore.insertMilProcess");// 更新进程表mil_processes数据
//				PreparedStatement updateMilProcess = connection
//						.prepareStatement(updateProcess);

//				// 更新进程表mil_processes数据
//				updateMilProcess.setString(1, milPtity.getJudgeStoveNo());
//				updateMilProcess.setString(2, milPtity.getLotNo());
//				updateMilProcess.setString(3, milPtity.getPlineCode());
//				updateMilProcess.setString(4, milPtity.getProPlanId());
//				updateMilProcess.setString(5, milPtity.getGxPlanNo());
//				updateMilProcess.addBatch();
//				updateMilProcess.executeBatch();

				String updatePlanStatus = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.updatePlanStatusE");// 更新炉计划状态
				PreparedStatement updatePlanStatusPst = connection
						.prepareStatement(updatePlanStatus);
				updatePlanStatusPst.setString(1, "40"); // 关闭炉计划
				updatePlanStatusPst.setString(2, milPtity.getHeatPlanNo());
				updatePlanStatusPst.setString(3, milPtity.getJudgeStoveNo());
				updatePlanStatusPst.addBatch();
				updatePlanStatusPst.executeBatch();
				judgeSequenNo++;
			}
			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 qcmTity
	 * @return
	 */
	public CoreReturnObject cancelScrapMata(String qcmTity) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			QcmZgJugdeApplyEntity qcmEntity = EntityTool.getEntityByJSONStr(
					qcmTity, QcmZgJugdeApplyEntity.class);
			if (qcmEntity.getIsAllScrap().equals("白条")) {
				qcmEntity.setIsAllScrap("2");
			} else if (qcmEntity.getIsAllScrap().equals("整炉废")) {
				qcmEntity.setIsAllScrap("1");
			}
			
			if(qcmEntity.getJugdeApplyCode().substring(0,1).equals("C"))
			{
			String deleteScrapSql = CoreXmlSqlParsersFactory
					.getSql("FrmScrapStoveApplyDescion.cancelScrapMata");
			deleteScrapSql = EntityTool.fillSqlByEntity(deleteScrapSql,
					qcmEntity);
			psta = connection.prepareStatement(deleteScrapSql);
			psta.executeUpdate();
			psta.close();
			
			if (qcmEntity.getIsAllScrap().equals("2")) {
				String updatePortF1Sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.updatePortF1");
				psta = connection.prepareStatement(updatePortF1Sql);
				psta.setObject(1,qcmEntity.getStoveNo());
				psta.setObject(2,qcmEntity.getJugdeApplyCode());
				psta.executeUpdate();
				psta.close();

				String updatePortG1Sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.updatePortG1");
				psta = connection.prepareStatement(updatePortG1Sql);
				psta.setObject(1,qcmEntity.getStoveNo());
				psta.setObject(2,qcmEntity.getJugdeApplyCode());
				psta.executeUpdate();
				psta.close();



				qcmEntity
						.setJugdeApplyCode("B" + qcmEntity.getJugdeApplyCode());
				String deleteScrapSqlS = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.cancelScrapMata");
				deleteScrapSqlS = EntityTool.fillSqlByEntity(deleteScrapSqlS,
						qcmEntity);
				psta = connection.prepareStatement(deleteScrapSqlS);
				psta.executeUpdate();
				psta.close();

			}
			}else
			{
				String deleteScrapSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.cancelScrapMata");
				deleteScrapSql = EntityTool.fillSqlByEntity(deleteScrapSql,
						qcmEntity);
				psta = connection.prepareStatement(deleteScrapSql);
				psta.executeUpdate();
				psta.close();
				
				if (qcmEntity.getIsAllScrap().equals("2")) {
					qcmEntity
							.setJugdeApplyCode(qcmEntity.getJugdeApplyCode().replace("BC", "C"));
					String deleteScrapSqlS = CoreXmlSqlParsersFactory
							.getSql("FrmScrapStoveApplyDescion.cancelScrapMata");
					deleteScrapSqlS = EntityTool.fillSqlByEntity(deleteScrapSqlS,
							qcmEntity);
					psta = connection.prepareStatement(deleteScrapSqlS);
					psta.executeUpdate();
					psta.close();
			}
			}

			
			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 matBcEntity
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("null")
	private String[] getBelongJudge(Connection con, MatBcMEntity matBcEntity)
			throws SQLException {
		String[] belong = new String[2];
		String departMentCode = "";// 现有产线部门
		String nextDrpartMentCode = "";// 下一工序产线部门
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		sql = CoreXmlSqlParsersFactory
				.getSql("FrmVrpApplyNew.SelectPlineBelong");
		sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
		psta = con.prepareStatement(sql);
		rs = psta.executeQuery();
		if (rs.next()) {
			departMentCode = rs.getString("DEPARTMENT_CODE");
		}
		rs.close();
		psta.close();
		if (matBcEntity.getNextProcessCdoe().equals("")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("doDecision.SelectOrderSlaOrg");// 查询合同所属部门
			sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
			psta = con.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				belong[0] = rs.getString("SALE_ORG");
				belong[1] = rs.getString("SALE_ORG_DESC");
			}
			rs.close();
			psta.close();
			if (belong[0] == null) {
				belong[0] = "002001001";
				belong[1] = "制造部";
			}
		} else {
			belong[0] = "002001001";
			belong[1] = "制造部";
			// sql = CoreXmlSqlParsersFactory
			// .getSql("FrmVrpApplyNew.SelectNextPlineBelong");// 查询下工序产线部门
			// if(matBcEntity.getNextProcessCdoe().equals("F"))
			// {
			// sql = sql.replace("#PLAN#", "PLN_ORDER_RCL_S");
			// }else if(matBcEntity.getNextProcessCdoe().equals("G"))
			// {
			// sql = sql.replace("#PLAN#", "PLN_ORDER_JGX_S");
			// }else
			// {
			// sql = sql.replace("#PLAN#", "PLN_ORDER_DBK_S");
			// }
			// sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
			// psta = con.prepareStatement(sql);
			// rs = psta.executeQuery();
			// if (rs.next()) {
			// nextDrpartMentCode = rs.getString("DEPARTMENT_CODE");
			// }
			// rs.close();
			// psta.close();
			// if(departMentCode.equals(nextDrpartMentCode))
			// {
			// belong[0] = matBcEntity.getBelongCode();
			// belong[1] = matBcEntity.getBelongName();
			// }else
			// {
			// belong[0] = "002001001";
			// belong[1] = "制造部";
			// }
		}
		return belong;
	}
	/***
	 * 打白条判定镦拔扩
	 * 
	 * @param milPlanTity
	 * @return
	 */
	public CoreReturnObject insertWhiteBankE(ArrayList<String> parmList,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.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");
		String systime = dateFormat.format(now);
		// String yearMoth = df.format(now);
		String yearMoth = "";
		String yearMothB = "";
		String jugdeApplyNo = "";// 判定单号
		int judgeSequenNo = 1;
		int scapCount = 0;// 所有废品支吨
		int inputCount = 0;// 投料支
		double inputWeight = 0.000;// 投料吨
		int sawCutNum = 0;// 分切数
		int count = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			QcmZgJugdeApplyEntity qcmTity = new QcmZgJugdeApplyEntity();
		

			sql = CoreXmlSqlParsersFactory
					.getSql("FrmScrapStoveApplyDescion.jugdeApplyNo");// 生成判定单号
			sql = EntityTool.fillSqlByEntity(sql, EntityTool
					.getEntityByJSONStr(parmList.get(0), PlnZyDbkMEntity.class));
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				jugdeApplyNo = rs.getString("JUGDE_APPLY_CODE");
			}
			rs.close();
			psta.close();
			
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmScrapStoveApplyDescion.SelectJudegNoSequenNo");// 查询申请判定顺序号
			psta = connection.prepareStatement(sql);
			psta.setString(1, jugdeApplyNo);
			rs = psta.executeQuery();
			if (rs.next()) {
				judgeSequenNo = Integer.parseInt(rs
						.getString("JUGDE_APPLY_SEQUENO"));
			}
			rs.close();
			psta.close();
			for (String str : parmList) {
				String insertQcmZcSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");// 本月正
				String insertQcmZcBSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");// 下月负
				PlnZyDbkMEntity milPtity = EntityTool.getEntityByJSONStr(str,
						PlnZyDbkMEntity.class);
//				sql = CoreXmlSqlParsersFactory
//						.getSql("insertWhiteBank.selectInstore");// 查询是否下线点结炉
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo());
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					count = Integer.parseInt(rs.getString("ACOUNT"));
//				}
//				rs.close();
//				psta.close();
//				if (count > 0) {
//
//				} else {
//					cro.setV_errMsg("下线点未结炉,不允许打白条!");
//					return cro;
//				}
				//查询是否结炉
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.selectDostoreE");// 查询是否下线点结炉
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getJudgeStoveNo());
				rs = psta.executeQuery();
				if (rs.next()) { 
					count = Integer.parseInt(rs.getString("COUNT"));
				}
				rs.close();
				psta.close();
				if (count > 0) {

				} else {
					cro.setV_errMsg("下线点未结炉,不允许打白条!");
					return cro;
				}

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.selectMaxBalYearMoth");// 查询最大结转年月
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					yearMoth = rs.getString("BAL_YEAR_MONTH");
					yearMothB = rs.getString("B_BAL_YEAR_MONTH");
				}
				rs.close();
				psta.close();


				String belongCode = "";
				String belongName = "";
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapStoveApplyDescion.selectBelong");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getPlineCode());							
				rs = psta.executeQuery();
				if(rs.next())
				{
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				psta.close();
				String[] belong = null;
				belong = getBelongJudge(connection, EntityTool.getEntityByJSONStr(
						parmList.get(0), MatBcMEntity.class));
				
				qcmTity.setJugdeApplyCode(jugdeApplyNo);
				qcmTity.setJudgeStoveNo(milPtity.getJudgeStoveNo().substring(0,
						6));
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setProducname(milPtity.getProducname());
				qcmTity.setSteelname(milPtity.getSteelname());
				qcmTity.setSpecName(milPtity.getSpecName());
				qcmTity.setModelDesc(milPtity.getModelDesc());
				qcmTity.setStdStyleDesc(milPtity.getStdStyleDesc());
				qcmTity.setPlineCode(milPtity.getPlineCode());
				qcmTity.setPlineName(milPtity.getPlineName());
				qcmTity.setBelongCode(belongCode);
				qcmTity.setBelongName(belongName);
				qcmTity.setBelongCodeJugde(belong[0]);
				qcmTity.setBelongNameJugde(belong[1]);
				qcmTity.setValidflag("20");
				qcmTity.setActCount(milPtity.getPlnInsingleNum());
				qcmTity.setActWeight(milPtity.getPlnInsingleWt());
				qcmTity.setActTheoryWeight(milPtity.getPlnInsingleWt());
				qcmTity.setInputCount(milPtity.getPlnOnsingleNum());
				qcmTity.setInputWeight(milPtity.getPlnOnsingleWt());
				qcmTity.setOutputCount(milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setIsAllScrap("2");
				qcmTity.setApplyName(userName);
				qcmTity.setTarget(milPtity.getDecision());
				qcmTity.setCrafNo(milPtity.getCraftFileNo());
				qcmTity.setProOrderId(milPtity.getProPlanId());
				qcmTity.setGxPlanNo(milPtity.getGxPlanNo());
				qcmTity.setBalYearMonth(yearMoth);
				qcmTity.setProcessStatus("");
				qcmTity.setBatchNo("001");
				qcmTity.setBatchGroudNo("001");
				qcmTity.setLotNo(milPtity.getZyBatchId());
				qcmTity.setProOrderNo(milPtity.getOrderNo());
				qcmTity.setGradename(milPtity.getGradename());
				qcmTity.setStdName(milPtity.getStdName());
				qcmTity.setApplyFlag("0");
				qcmTity.setProcessCode("E");
				if(milPtity.getDecision().equals("A"))
				{
					qcmTity.setJudgeTolresultCode("40740701");
					qcmTity.setJudgeTolresult("合格");
					qcmTity.setJudgeTolresultTime(systime);
					qcmTity.setJudgeTolmaker(userName);
				}else{
					qcmTity.setJudgeTolresultCode("");
					qcmTity.setJudgeTolresult("");
					qcmTity.setJudgeTolresultTime("");
					qcmTity.setJudgeTolmaker("");
				}
				qcmTity.setMaterialNo(milPtity.getOutwlId());
				qcmTity.setMaterialName(milPtity.getOutwlDesc());
				qcmTity.setProduccode(milPtity.getProduccode());
				qcmTity.setSteelcode(milPtity.getSteelcode());
				qcmTity.setGradecode(milPtity.getGradecode());
				qcmTity.setSpecCode(milPtity.getSpecCode());
				qcmTity.setModelCode(milPtity.getModelCode());
				qcmTity.setStdCode(milPtity.getStdCode());
				qcmTity.setStdStyle(milPtity.getStdStyle());

				insertQcmZcSql = EntityTool.fillSqlByEntity(insertQcmZcSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcSql);
				psta.executeUpdate();

				psta.close();

				qcmTity.setJugdeApplyCode("B" + jugdeApplyNo);
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setActCount("-" + milPtity.getPlnInsingleNum());
				qcmTity.setActWeight("-" + milPtity.getPlnInsingleWt());
				qcmTity.setActTheoryWeight("-" + milPtity.getPlnInsingleWt());
				qcmTity.setInputCount("-"
						+ milPtity.getPlnOnsingleNum());
				qcmTity.setInputWeight("-"
						+ milPtity.getPlnOnsingleWt());
				qcmTity.setOutputCount("-"
						+ milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight("-"
						+ milPtity.getPlnInsingleWt());
				qcmTity.setBalYearMonth(yearMothB);

				insertQcmZcBSql = EntityTool.fillSqlByEntity(insertQcmZcBSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcBSql);
				psta.executeUpdate();

				psta.close();
				judgeSequenNo++;
			}

			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 milPlanTity
	 * @return
	 */
	public CoreReturnObject insertWhiteBankF(ArrayList<String> parmList,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.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");
		String systime = dateFormat.format(now);
		// String yearMoth = df.format(now);
		String yearMoth = "";
		String yearMothB = "";
		String jugdeApplyNo = "";// 判定单号
		int judgeSequenNo = 1;
		int scapCount = 0;// 所有废品支吨
		int inputCount = 0;// 投料支
		double inputWeight = 0.000;// 投料吨
		int sawCutNum = 0;// 分切数
		int count = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			QcmZgJugdeApplyEntity qcmTity = new QcmZgJugdeApplyEntity();
			

			
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmScrapStoveApplyDescion.jugdeApplyNo");// 生成判定单号
			sql = EntityTool.fillSqlByEntity(sql, EntityTool
					.getEntityByJSONStr(parmList.get(0), PlnZyRclMEntity1.class));
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				jugdeApplyNo = rs.getString("JUGDE_APPLY_CODE");
			}
			rs.close();
			psta.close();
			
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmScrapStoveApplyDescion.SelectJudegNoSequenNo");// 查询申请判定顺序号
			psta = connection.prepareStatement(sql);
			psta.setString(1, jugdeApplyNo);
			rs = psta.executeQuery();
			if (rs.next()) {
				judgeSequenNo = Integer.parseInt(rs
						.getString("JUGDE_APPLY_SEQUENO"));
			}
			rs.close();
			psta.close();
			for (String str : parmList) {
				String insertQcmZcSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");// 本月正
				String insertQcmZcBSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");// 下月负
				PlnZyRclMEntity1 milPtity = EntityTool.getEntityByJSONStr(str,
						PlnZyRclMEntity1.class);


				String updatePortF = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.updatePortF");// 更新管号队列送判短号

//				sql = CoreXmlSqlParsersFactory
//						.getSql("insertWhiteBank.selectInstore");// 查询是否下线点结炉
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo());
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					count = Integer.parseInt(rs.getString("ACOUNT"));
//				}
//				rs.close();
//				psta.close();
//				if (count > 0) {
//
//				} else {
//					cro.setV_errMsg("下线点未结炉,不允许打白条!");
//					return cro;
//				}
				//查询是否结炉
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.selectDostoreF");// 查询是否下线点结炉
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getJudgeStoveNo());
				rs = psta.executeQuery();
				if (rs.next()) { 
					count = Integer.parseInt(rs.getString("COUNT"));
				}
				rs.close();
				psta.close();
				if (count > 0) {

				} else {
					cro.setV_errMsg("下线点未结炉,不允许打白条!");
					return cro;
				}

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.selectMaxBalYearMoth");// 查询最大结转年月
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					yearMoth = rs.getString("BAL_YEAR_MONTH");
					yearMothB = rs.getString("B_BAL_YEAR_MONTH");
				}
				rs.close();
				psta.close();

			

				String belongCode = "";
				String belongName = "";
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapStoveApplyDescion.selectBelong");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getPlineCode());							
				rs = psta.executeQuery();
				if(rs.next())
				{
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				psta.close();
				String[] belong = null;
				belong = getBelongJudge(connection, EntityTool.getEntityByJSONStr(
						parmList.get(0), MatBcMEntity.class));
				
				qcmTity.setJugdeApplyCode(jugdeApplyNo);
				qcmTity.setJudgeStoveNo(milPtity.getJudgeStoveNo().substring(0,
						6));
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setProducname(milPtity.getProducname());
				qcmTity.setSteelname(milPtity.getSteelname());
				qcmTity.setSpecName(milPtity.getSpecName());
				qcmTity.setModelDesc(milPtity.getModelDesc());
				qcmTity.setStdStyleDesc(milPtity.getStdStyleDesc());
				qcmTity.setPlineCode(milPtity.getPlineCode());
				qcmTity.setPlineName(milPtity.getPlineName());
				qcmTity.setBelongCode(belongCode);
				qcmTity.setBelongName(belongName);
				qcmTity.setBelongCodeJugde(belong[0]);
				qcmTity.setBelongNameJugde(belong[1]);
				qcmTity.setValidflag("20");
				qcmTity.setActCount(milPtity.getPlnInsingleNum());
				qcmTity.setActWeight(milPtity.getPlnInsingleWt());
				qcmTity.setActTheoryWeight(milPtity.getPlnInsingleWt());
				qcmTity.setInputCount(milPtity.getPlnOnsingleNum());
				qcmTity.setInputWeight(milPtity.getPlnOnsingleWt());
				qcmTity.setOutputCount(milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setIsAllScrap("2");
				qcmTity.setApplyName(userName);
				qcmTity.setTarget(milPtity.getDecision());
				qcmTity.setCrafNo(milPtity.getCraftFileNo());
				qcmTity.setProOrderId(milPtity.getProPlanId());
				qcmTity.setGxPlanNo(milPtity.getGxPlanNo());
				qcmTity.setBalYearMonth(yearMoth);
				qcmTity.setProcessStatus("");
				qcmTity.setBatchNo("001");
				qcmTity.setBatchGroudNo("001");
				qcmTity.setLotNo(milPtity.getZyBatchId());
				qcmTity.setProOrderNo(milPtity.getOrderNo());
				qcmTity.setGradename(milPtity.getGradename());
				qcmTity.setStdName(milPtity.getStdName());
				qcmTity.setApplyFlag("0");
				qcmTity.setProcessCode("F");
				if(milPtity.getDecision().equals("A"))
				{
					qcmTity.setJudgeTolresultCode("40740701");
					qcmTity.setJudgeTolresult("合格");
					qcmTity.setJudgeTolresultTime(systime);
					qcmTity.setJudgeTolmaker(userName);
				}else{
					qcmTity.setJudgeTolresultCode("");
					qcmTity.setJudgeTolresult("");
					qcmTity.setJudgeTolresultTime("");
					qcmTity.setJudgeTolmaker("");
				}
				qcmTity.setMaterialNo(milPtity.getOutwlId());
				qcmTity.setMaterialName(milPtity.getOutwlDesc());
				qcmTity.setProduccode(milPtity.getProduccode());
				qcmTity.setSteelcode(milPtity.getSteelcode());
				qcmTity.setGradecode(milPtity.getGradecode());
				qcmTity.setSpecCode(milPtity.getSpecCode());
				qcmTity.setModelCode(milPtity.getModelCode());
				qcmTity.setStdCode(milPtity.getStdCode());
				qcmTity.setStdStyle(milPtity.getStdStyle());
				qcmTity.setHeatPlanNo(milPtity.getHeatPlanNo());

				insertQcmZcSql = EntityTool.fillSqlByEntity(insertQcmZcSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcSql);
				psta.executeUpdate();

				psta.close();

				qcmTity.setJugdeApplyCode("B" + jugdeApplyNo);
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setActCount("-" + milPtity.getPlnInsingleNum());
				qcmTity.setActWeight("-" + milPtity.getPlnInsingleWt());
				qcmTity.setActTheoryWeight("-" + milPtity.getPlnInsingleWt());
				qcmTity.setInputCount("-"
						+ milPtity.getPlnOnsingleNum());
				qcmTity.setInputWeight("-"
						+ milPtity.getPlnOnsingleWt());
				qcmTity.setOutputCount("-"
						+ milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight("-"
						+ milPtity.getPlnInsingleWt());
				qcmTity.setBalYearMonth(yearMothB);

				insertQcmZcBSql = EntityTool.fillSqlByEntity(insertQcmZcBSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcBSql);
				psta.executeUpdate();

				psta = connection.prepareStatement(updatePortF);
				psta.setObject(1,jugdeApplyNo);
				psta.setObject(2,qcmTity.getHeatPlanNo());
				psta.addBatch();
				psta.executeBatch();

				psta.close();




				judgeSequenNo++;
			}

			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 milPlanTity
	 * @return
	 */
	public CoreReturnObject insertWhiteBankG(ArrayList<String> parmList,
			String userName) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.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");
		String systime = dateFormat.format(now);
		// String yearMoth = df.format(now);
		String yearMoth = "";
		String yearMothB = "";
		String jugdeApplyNo = "";// 判定单号
		int judgeSequenNo = 1;//顺序号
		int scapCount = 0;// 所有废品支吨
		int inputCount = 0;// 投料支
		double inputWeight = 0.000;// 投料吨
		int sawCutNum = 0;// 分切数
		int count = 0;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			QcmZgJugdeApplyEntity qcmTity = new QcmZgJugdeApplyEntity();
			
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmScrapStoveApplyDescion.jugdeApplyNo");// 生成判定单号
			sql = EntityTool.fillSqlByEntity(sql, EntityTool
					.getEntityByJSONStr(parmList.get(0), PlnZyJgxMEntity1.class));
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				jugdeApplyNo = rs.getString("JUGDE_APPLY_CODE");
			}
			rs.close();
			psta.close();
			
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmScrapStoveApplyDescion.SelectJudegNoSequenNo");// 查询申请判定顺序号
			psta = connection.prepareStatement(sql);
			psta.setString(1, jugdeApplyNo);
			rs = psta.executeQuery();
			if (rs.next()) {
				judgeSequenNo = Integer.parseInt(rs
						.getString("JUGDE_APPLY_SEQUENO"));
			}
			rs.close();
			psta.close();
			for (String str : parmList) {
				String insertQcmZcSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");// 本月正
				String insertQcmZcBSql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.doDecisionScrap");// 下月负


				String updatePortF = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.updatePortG");// 更新管号队列送判短号

				
				PlnZyJgxMEntity1 milPtity = EntityTool.getEntityByJSONStr(str,
						PlnZyJgxMEntity1.class);
//				sql = CoreXmlSqlParsersFactory
//						.getSql("insertWhiteBank.selectInstore");// 查询是否下线点结炉
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo());
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					count = Integer.parseInt(rs.getString("ACOUNT"));
//				}
//				rs.close();
//				psta.close();
//				if (count > 0) {
//
//				} else {
//					cro.setV_errMsg("下线点未结炉,不允许打白条!");
//					return cro;
//				}
				//查询是否结炉
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.selectDostoreG");// 查询是否下线点结炉
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getJudgeStoveNo());
				rs = psta.executeQuery();
				if (rs.next()) { 
					count = Integer.parseInt(rs.getString("COUNT"));
				}
				rs.close();
				psta.close();
				if (count > 0) {
				
				} else {
					cro.setV_errMsg("下线点未结炉,不允许打白条!");
					return cro;
				}

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmScrapStoveApplyDescion.selectMaxBalYearMoth");// 查询最大结转年月
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if (rs.next()) {
					yearMoth = rs.getString("BAL_YEAR_MONTH");
					yearMothB = rs.getString("B_BAL_YEAR_MONTH");
				}
				rs.close();
				psta.close();

				

//				sql = CoreXmlSqlParsersFactory
//						.getSql("doDecision.SelectPlanSaw");// 查询分切数
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo());
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					sawCutNum = Integer.parseInt(rs.getString("SAW_NUM"));
//				}
//				rs.close();
//				psta.close();

//				sql = CoreXmlSqlParsersFactory
//						.getSql("doDecision.SelectScrapCount");// 查询所有废品支
//				sql = EntityTool.fillSqlByEntity(sql, milPtity);
//				psta = connection.prepareStatement(sql);
//				rs = psta.executeQuery();
//				if (rs.next()) {
//					scapCount = Integer.parseInt(rs.getString("ACT_COUNT"));
//				}
//				rs.close();
//				psta.close();

//				ArrayList<String> matNoList = new ArrayList<String>();
//				sql = CoreXmlSqlParsersFactory
//						.getSql("FrmScrapStoveApplyDescion.SelectScrapInput1");// 查询投料支吨
//				psta = connection.prepareStatement(sql);
//				psta.setString(1, milPtity.getJudgeStoveNo());
//				psta.setString(2, String.valueOf(Math.ceil((Integer
//						.parseInt(milPtity.getActCount()) + scapCount)
//						/ sawCutNum)));
//				rs = psta.executeQuery();
//				while (rs.next()) {
//					matNoList.add(rs.getString("M_MAT_NO"));
//				}
//				rs.close();
//				psta.close();
//
//				inputCount = matNoList.size();
//				for (int j = 0; j < matNoList.size(); j++) {
//					sql = CoreXmlSqlParsersFactory
//							.getSql("doDecision.SelectPortWgt");// 查询判定支+废品支
//
//					psta = connection.prepareStatement(sql);
//					psta.setString(1, matNoList.get(j));
//					psta.setString(2, milPtity.getJudgeStoveNo());
//					rs = psta.executeQuery();
//					if (rs.next()) {
//						inputWeight = inputWeight
//								+ Double.parseDouble(rs
//										.getString("INPUT_WEIGHT"));
//					}
//					rs.close();
//					psta.close();
//				}
				String belongCode = "";
				String belongName = "";
				sql = CoreXmlSqlParsersFactory.getSql("FrmScrapStoveApplyDescion.selectBelong");
				psta = connection.prepareStatement(sql);
				psta.setString(1, milPtity.getPlineCode());							
				rs = psta.executeQuery();
				if(rs.next())
				{
					belongCode = rs.getString("DEPARTMENT_CODE");
					belongName = rs.getString("DEPARTMENT_DESC");
				}
				rs.close();
				psta.close();
				String[] belong = null;
				belong = getBelongJudge(connection, EntityTool.getEntityByJSONStr(
						parmList.get(0), MatBcMEntity.class));
				
				qcmTity.setJugdeApplyCode(jugdeApplyNo);
				qcmTity.setJudgeStoveNo(milPtity.getJudgeStoveNo().substring(0,
						6));
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setProducname(milPtity.getProducname());
				qcmTity.setSteelname(milPtity.getSteelname());
				qcmTity.setSpecName(milPtity.getSpecName());
				qcmTity.setModelDesc(milPtity.getModelDesc());
				qcmTity.setStdStyleDesc(milPtity.getStdStyleDesc());
				qcmTity.setPlineCode(milPtity.getPlineCode());
				qcmTity.setPlineName(milPtity.getPlineName());
				qcmTity.setBelongCode(belongCode);
				qcmTity.setBelongName(belongName);
				qcmTity.setBelongCodeJugde(belong[0]);
				qcmTity.setBelongNameJugde(belong[1]);
				qcmTity.setValidflag("20");
				qcmTity.setActCount(milPtity.getPlnInsingleNum());
				qcmTity.setActWeight(milPtity.getPlnInsingleWt());
				qcmTity.setActTheoryWeight(milPtity.getPlnInsingleWt());
				qcmTity.setInputCount(milPtity.getPlnOnsingleNum());
				qcmTity.setInputWeight(milPtity.getPlnOnsingleWt());
				qcmTity.setOutputCount(milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight(milPtity.getPlnInsingleWt());
				qcmTity.setIsAllScrap("2");
				qcmTity.setApplyName(userName);
				qcmTity.setTarget(milPtity.getDecision());
				qcmTity.setCrafNo(milPtity.getCraftFileNo());
				qcmTity.setProOrderId(milPtity.getProPlanId());
				qcmTity.setGxPlanNo(milPtity.getGxPlanNo());
				qcmTity.setBalYearMonth(yearMoth);
				qcmTity.setProcessStatus("");
				qcmTity.setBatchNo(milPtity.getBatchNo());
				qcmTity.setBatchGroudNo("001");
				qcmTity.setLotNo(milPtity.getZyBatchId());
				qcmTity.setProOrderNo(milPtity.getOrderNo());
				qcmTity.setGradename(milPtity.getGradename());
				qcmTity.setStdName(milPtity.getStdName());
				qcmTity.setApplyFlag("0");
				qcmTity.setProcessCode("G");
				qcmTity.setJudgeMaker(userName);
				if(milPtity.getDecision().equals("A"))
				{
					qcmTity.setJudgeTolresultCode("40740701");
					qcmTity.setJudgeTolresult("合格");
					qcmTity.setJudgeTolresultTime(systime);
					qcmTity.setJudgeTolmaker(userName);
				}else{
					qcmTity.setJudgeTolresultCode("");
					qcmTity.setJudgeTolresult("");
					qcmTity.setJudgeTolresultTime("");
					qcmTity.setJudgeTolmaker("");
				}
				qcmTity.setMaterialNo(milPtity.getOutwlId());
				qcmTity.setMaterialName(milPtity.getOutwlDesc());
				qcmTity.setProduccode(milPtity.getProduccode());
				qcmTity.setSteelcode(milPtity.getSteelcode());
				qcmTity.setGradecode(milPtity.getGradecode());
				qcmTity.setSpecCode(milPtity.getSpecCode());
				qcmTity.setModelCode(milPtity.getModelCode());
				qcmTity.setStdCode(milPtity.getStdCode());
				qcmTity.setStdStyle(milPtity.getStdStyle());
				qcmTity.setHeatPlanNo(milPtity.getHeatPlanNo());

				insertQcmZcSql = EntityTool.fillSqlByEntity(insertQcmZcSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcSql);
				psta.executeUpdate();

				psta.close();

				qcmTity.setJugdeApplyCode("B" + jugdeApplyNo);
				qcmTity.setJugdeApplySequeno(String.valueOf(judgeSequenNo));
				qcmTity.setActCount("-" + milPtity.getPlnInsingleNum());
				qcmTity.setActWeight("-" + milPtity.getPlnInsingleWt());
				qcmTity.setActTheoryWeight("-" + milPtity.getPlnInsingleWt());
				qcmTity.setInputCount("-"
						+ milPtity.getPlnOnsingleNum());
				qcmTity.setInputWeight("-"
						+ milPtity.getPlnOnsingleWt());
				qcmTity.setOutputCount("-"
						+ milPtity.getPlnInsingleNum());
				qcmTity.setOutputWeight("-"
						+ milPtity.getPlnInsingleWt());
				qcmTity.setBalYearMonth(yearMothB);

				insertQcmZcBSql = EntityTool.fillSqlByEntity(insertQcmZcBSql,
						qcmTity);
				psta = connection.prepareStatement(insertQcmZcBSql);
				psta.executeUpdate();


				psta = connection.prepareStatement(updatePortF);
				psta.setObject(1,jugdeApplyNo);
				psta.setObject(2,qcmTity.getHeatPlanNo());
				psta.setObject(3,qcmTity.getBatchNo());
				psta.addBatch();
				psta.executeBatch();


				psta.close();
				judgeSequenNo++;
			}

			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 yearMoth
	 * @return
	 */
	private String getBalYearMoth(String yearMoth) {
		String year = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sdfS = new SimpleDateFormat("yyyyMM");
		try {
			Date date = sdf.parse(yearMoth);
			Calendar calender = Calendar.getInstance();
			calender.setTime(date);
			calender.add(Calendar.MONTH, 1);
			year = sdfS.format(calender.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return year;

	}
}

