package com.steering.mes.mcp.Mch;

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.converter.QualityDesignTool;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.mes.mcp.Vrp.FrmMchInStore;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgxMEntity;
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.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 测量实绩
 * 
 * @author SZL
 *
 */
public class FrmMeasureResult extends DBCommon {

	private ArrayList<String>  MeasurePline = new ArrayList<String>(Arrays.asList("C028","C029","C030","C031","C032","C033","C034","C035","C036","C037","C038","C039","C040",
			"C051","C052","C053","C056","C057","C058","C059","C078"));



	private String perStuts = "";

	/**
	 * 查询测量实绩
	 */
	public CoreReturnObject getQueryExpandResult(String heatNo ,String BatchNo) {
		return this.getDataBySqlID("FrmMeasureResult.getQueryExpandResult",
				new Object[] { heatNo,BatchNo});
	}

	/**
	 * 查询测量从表实绩
	 */
	public CoreReturnObject getQueryJumDResult(String heatNo) {
		return this.getDataBySqlID("FrmMeasureResult.getQueryExpandResult1",
				new Object[] { heatNo });
	}


	/**
	 * 查询冲击温度
	 */
	public CoreReturnObject getQueryPhyResult(String JudgeStoveNo,String BatchNo) {
		return this.getDataBySqlID("FrmMeasureResult.getQueryPhyResult",
				new Object[] { JudgeStoveNo,BatchNo });
	}

	/**
	 * 查询交付标准的冲击温度
	 */
	public CoreReturnObject getQueryPhyResult(String ORD_LN_DLY_PK) {
		return this.getDataBySqlID("FrmMeasureResult.getQueryPhyResultStand",
				new Object[] {ORD_LN_DLY_PK });
	}


	public  ArrayList<MchMeasureResultDEntity> getMchMeasureResultDEntity(String ResultNo, Connection connection) throws SQLException {
		String sqlMatNo = CoreXmlSqlParsersFactory
				.getSql("FrmMeasureResult.GetResultD");// 查询新增物料信息
		PreparedStatement psta = connection.prepareStatement(sqlMatNo);
		psta.setObject(1, ResultNo);
		ResultSet rs = psta.executeQuery();
		ArrayList<MchMeasureResultDEntity> PortMchBatchSampleResultEntity = EntityTool.getEntitysByRs(rs, MchMeasureResultDEntity.class);
		rs.close();
		psta.close();
		return PortMchBatchSampleResultEntity;
	}

	/**
	 * 查询相同炉计划ID下测量实绩状
	 */
	public CoreReturnObject getQueryExpandResultStuts(String heatNo,
			String plinCode) {
//		String sql = CoreXmlSqlParsersFactory.getSql("FrmMeasureResult.getQueryExpandResultStuts");
//		sql += "AND T.HEAT_PLAN_NO = '"+heatNo+"'";
//		sql += CoreConverter.toSqlConditionString("T.P

		return this.getDataBySqlID(
				"FrmMeasureResult.getQueryExpandResultStuts", new Object[] {
						heatNo, plinCode });
	}
	
	/**
	 * 查询测量点数据
	 * @param jugeNo
	 * @param startTim
	 * @param endTim
	 * @param orderNo
	 * @return
	 */
	public CoreReturnObject QueryZgJugdeApply(String jugeNo, String startTim,
			String endTim, String orderNo,String[] plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMeasureResult.QueryZgJugdeApply");
		if (!startTim.equals("")) {
			sql += " AND T.CREATE_DATA >=TO_DATE('" + startTim
					+ "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_DATA <=TO_DATE('"
					+ endTim + "', 'YYYY-MM-DD HH24:MI:SS')";
		}
		if (!"".equals(jugeNo)) {
			sql += " AND T.JUDGE_STOVE_NO = '" + jugeNo + "'";
		}
		if (!"".equals(orderNo)) {
			sql += " AND T.PRO_ORDER_NO = '" + orderNo + "' ";
		}
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		sql += " GROUP BY T.PLINE_CODE,T.PLINE_NAME,T.JUDGE_STOVE_NO,T.BATCH_NO,T.PRO_ORDER_NO";
		return this.getData(sql);
	}
	
	/***
	 * 查询测量点数据
	 * @param jugeNo
	 * @param batchNo
	 * @param plinCode
	 * @param orderNo
	 * @return
	 */
	public CoreReturnObject QueryBaleRecord(String jugeNo, String batchNo,
			String plinCode, String orderNo) {
		return this.getDataBySqlID("FrmMeasureResult.QueryBaleRecord",
				new Object[] { jugeNo,batchNo, plinCode,orderNo });
	}
	
	/**
	 * 打捆
	 * @param listAll
	 * @return
	 */
	public CoreReturnObject updateBale(ArrayList<ArrayList> listAll) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sql1 = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateBale");
			PreparedStatement updateMilControlLog = connection
					.prepareStatement(sql1);
			for (ArrayList<String> list : listAll) {
				updateMilControlLog.setString(1, list.get(0));
				updateMilControlLog.setString(2, list.get(1));
				updateMilControlLog.addBatch();
			}
			updateMilControlLog.executeBatch();
			connection.commit();
			cro.setV_errMsg("打捆成功!");
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}



	/**
	 * 查询成品测量点数据
	 */
	public CoreReturnObject geRtdMeasurePosdata(String judgeStoveNo,String BatchNo,
			String HeatNo) {
		return this.getDataBySqlID("FrmMeasureResult.geRtdMeasurePosdata",
				new Object[] { judgeStoveNo,BatchNo, HeatNo,BatchNo });
	}

	/**
	 * 查询成品测量点数据
	 */
	public CoreReturnObject geRtdMeasurePosdataAll(String judgeStoveNo,String BatchNo, String PlineCode,
												String Flag) {

		String sql = CoreXmlSqlParsersFactory.getSql("FrmMeasureResult.geRtdMeasurePosdata2");

		if (!judgeStoveNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO='" + judgeStoveNo + "'";
		}
		if (!BatchNo.equals("")) {
			sql += " AND (T.BATCH_NO ='" + BatchNo + "' OR T.PLINE_CODE='C092') "; //天淮本地没有批号  不判断批号
		}
		if (!PlineCode.equals("")) {
			sql += " AND T.PLINE_CODE='" + PlineCode + "'";
		}

		if(Flag.equals("1")){
			sql += " AND T.FLAG = '00'";
		}

		sql+= "ORDER BY TO_NUMBER (MAT_NO)";


		return this.getData(sql);
	}



	/**
	 * 查询测量实绩合格支数的和
	 * 
	 * @param heatNo
	 * @return
	 */
	public CoreReturnObject getQueryExpandResult1(String heatNo) {
		return this.getDataBySqlID("FrmMeasureResult.getQueryExpandResult2",
				new Object[] { heatNo });
	}

	public static RtdMeasurePosdataEntity getRtdMeasurePosdataEntity(Connection con, String JudgeStoveNo, String BatchNo,String PlineCode, String MatNo) throws SQLException {

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMeasureResult.getRtdMeasurePosdataEntity");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, JudgeStoveNo);
		ps.setObject(2, BatchNo);
		ps.setObject(3, PlineCode);
		ps.setObject(4, JudgeStoveNo);
		ps.setObject(5, MatNo);
		ResultSet rs = ps.executeQuery();
		List<RtdMeasurePosdataEntity> data = EntityTool.getEntitysByRs(rs,
				RtdMeasurePosdataEntity.class);
		rs.close();
		ps.close();
		if (data == null || data.size() == 0) {
			return null;
		} else {
			for (RtdMeasurePosdataEntity sendPlanCEntity : data) {
				QualityDesignTool.nullConverNullString(sendPlanCEntity);
			}
			return data.get(0);
		}

	}


	public  CoreReturnObject getUnmatchRtdMeasurePosdataEntity(String JudgeStoveNo, String BatchNo,String PlineCode) throws SQLException {
	    return getDataBySqlID("FrmMeasureResult.getUnmatchRtdMeasurePosdataEntity",new Object[]{ JudgeStoveNo,BatchNo,PlineCode});
	}


	public static RtdMeasurePosdataEntity getRtdMeasurePosdataEntity1(Connection con, String JudgeStoveNo, String BatchNo,String PlineCode,String CxMatNo) throws SQLException {

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMeasureResult.getRtdMeasurePosdataEntity1");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, JudgeStoveNo);
		ps.setObject(2, BatchNo);
		ps.setObject(3, CxMatNo);
		ps.setObject(4, PlineCode);
		ResultSet rs = ps.executeQuery();
		List<RtdMeasurePosdataEntity> data = EntityTool.getEntitysByRs(rs,
				RtdMeasurePosdataEntity.class);
		rs.close();
		ps.close();
		if (data == null || data.size() == 0) {
			return null;
		} else {
			for (RtdMeasurePosdataEntity sendPlanCEntity : data) {
				QualityDesignTool.nullConverNullString(sendPlanCEntity);
			}
			return data.get(0);
		}

	}


	public static RtdMeasurePosdataEntity getRtdMeasurePosdataEntityByID(Connection con, String ID) throws SQLException {

		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmMeasureResult.getRtdMeasurePosdataEntityByID");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ID);
		ResultSet rs = ps.executeQuery();
		List<RtdMeasurePosdataEntity> data = EntityTool.getEntitysByRs(rs,
				RtdMeasurePosdataEntity.class);
		rs.close();
		ps.close();
		if (data == null || data.size() == 0) {
			return null;
		} else {
			for (RtdMeasurePosdataEntity sendPlanCEntity : data) {
				QualityDesignTool.nullConverNullString(sendPlanCEntity);
			}
			return data.get(0);
		}

	}


	public CoreReturnObject DoAddDB(String loadStr, String gx, ArrayList<String> ScrappedList, ArrayList<String> SuspiciousList, ArrayList<String> MatNos,ArrayList<String> LenWeigtStrs){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false); //



			String sqlupdatepln = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.updatePlnZyDbkM2");// 修改加工计划表
			PreparedStatement patupdatepln = connection
					.prepareStatement(sqlupdatepln);

			String addPlnInfo = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.AddComInfoResultNewDB");// 新增公共信息
			PreparedStatement patsPlan = connection
					.prepareStatement(addPlnInfo);


			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updatePortVrp");// 更新管号队列
			PreparedStatement patsupdatePort = connection
					.prepareStatement(updatePort);

			String updateMeasureData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMeasureData");// 把原匹配的数据更新成剔除
			PreparedStatement patsupdateMeasureData = connection
					.prepareStatement(updateMeasureData);

			String updateRtdData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
			PreparedStatement patsupdateRtdData = connection
					.prepareStatement(updateRtdData);

			String updateMatData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatDatadbk");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatData = connection
					.prepareStatement(updateMatData);

			String updateInListData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateInListDataDB");//
			PreparedStatement patsupdateInListData = connection
					.prepareStatement(updateInListData);


			String AddResultD = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.AddResultDdbk");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patsAddResultD = connection
					.prepareStatement(AddResultD);

/*
			String updateMatBackData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatBackData");// 更新测量数据 -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatBackData = connection
					.prepareStatement(updateMatBackData);*/

			MchMeasureResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchMeasureResultEntity.class);


			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3Dbk(connection,
					Load.getHeatPlanNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "650108");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			String resultNo = "";
			int count = 0;
			int detCount = 0;


			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}
			if(StringTool.isInteger(Load.getProNum())){
				count = Integer.parseInt(Load.getProNum());
			}


			if(count<0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}

			if(count==0 && detCount<=0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}



			if(MatNos.size()!=count){
				core.setV_errMsg("没有足够的管,无法新增实绩！");
				return core;
			}



			//获取最大实绩编号
			sql =   CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.queryResultNo");
			psta = connection.prepareStatement(sql);
			psta.setObject(1, pln.getPlineCode() + gx );
			psta.setObject(2, pln.getPlineCode() + gx );
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();



			Integer RemainNum =0;

			Double SingleWt= 0D;
			Double SingleLen = 0D;




			MchLenWeightEntity LenWeightEntity = null;


			Double ProWt = 0D;

			Integer CountNum = 1;
			Integer CountWt =1;

			Double Dlen = 0.001;
			Double Dwt  = 0.001;

			DecimalFormat df = new DecimalFormat("0.000");

			for (int i  = 0,index = 0; i < MatNos.size(); i++) {
				PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntitydbk(Load.getHeatPlanNo(), MatNos.get(i), connection);



				if(Load.getFlag().equals("1")) {

					if(RemainNum==0){
						if(index>= LenWeigtStrs.size()){
							core.setV_errMsg("手动维护测量点支数不足！");
							return core;
						}

						LenWeightEntity = EntityTool.getEntityByJSONStr(LenWeigtStrs.get(index++),MchLenWeightEntity.class);
						RemainNum = Integer.parseInt(LenWeightEntity.getActCount());
						Double Len = Double.parseDouble(LenWeightEntity.getActLen());
						Double Wt = Double.parseDouble(LenWeightEntity.getActWeight());


						// 总重量平均    取4位小数   多余量累积  平均到后几支
						SingleLen =Math.round(Len/ RemainNum * 1000)/1000D;
						SingleWt=Math.round(Wt/ RemainNum * 1000)/1000D;

						CountNum = (int) Math.round((Len - SingleLen * RemainNum) * 1000);
						if(CountNum>0){
							Dlen = 0.001d;
						}else  if(CountNum==0)
						{
							Dlen = 0d;
						}else {
							Dlen = - 0.001d;
						}
						CountNum = Math.abs(CountNum);

						CountWt = (int) Math.round((Wt - SingleWt * RemainNum) * 1000);

						if(CountWt>0){
							Dwt = 0.001d;
						}else  if(CountWt==0)
						{
							Dwt = 0d;
						}else {
							Dwt = - 0.001d;
						}
						CountWt = Math.abs(CountWt);
					}

					Double Len = RemainNum<=CountNum? Math.round((SingleLen +Dlen)*1000D)/1000D : SingleLen;
					Double Wt = RemainNum<=CountWt?  Math.round((SingleWt +Dwt)*1000D)/1000D: SingleWt;

					ProWt +=  Wt;

					patsupdatePort.setObject(1, "");
				//	patsupdatePort.setObject(2,Len);
					patsupdatePort.setObject(2, Wt);
					patsupdatePort.setObject(3, Load.getHeatPlanNo());
					patsupdatePort.setObject(4, MatNos.get(i));
					patsupdatePort.addBatch();

					 port.setInstoreFlag("1");    //getInstoreFlag()="1";
					if(port.getInstoreFlag().equals("1")) {

						if(port.getGroupNoReal().equals("")){
							port.setGroupNoReal(FrmMchInStore.creatGroup(connection, pln.getOrderNo() + "/" + pln.getOrderSeq(),
									port.getActLenMinReal(), port.getActLenMaxReal(),
									pln.getOrdLnDlyPk()));
						}

						patsupdateMatData.setObject(1,Len);
						patsupdateMatData.setObject(2,  Wt);
						patsupdateMatData.setObject(3, Wt);
					//	patsupdateMatData.setObject(4, port.getGroupNoReal());
					//	patsupdateMatData.setObject(5, port.getActLenMinReal());
					//	patsupdateMatData.setObject(6, port.getActLenMaxReal());

						patsupdateMatData.setObject(4, MatNos.get(i));
						patsupdateMatData.addBatch();

						patsupdateInListData.setObject(1, Len);
						patsupdateInListData.setObject(2,   Wt);
					//	patsupdateInListData.setObject(3, port.getGroupNoReal());
					//	patsupdateInListData.setObject(4, port.getActLenMinReal());
					//	patsupdateInListData.setObject(5, port.getActLenMaxReal());

						patsupdateInListData.setObject(3, MatNos.get(i));
						patsupdateInListData.addBatch();

					}

                       /*
						patsupdateMatBackData.setObject(1, Len);
						patsupdateMatBackData.setObject(2,  Wt);
						patsupdateMatBackData.setObject(3,  Wt);
						patsupdateMatBackData.setObject(4, MatNos.get(i));
						patsupdateMatBackData.addBatch();*/

					RemainNum--;
				}
				else {
					RtdMeasurePosdataEntity RtdData = getRtdMeasurePosdataEntity(connection, Load.getJudgeStoveNo(), Load.getBatchNo(), Load.getPlineCode(),( i + 1 ) + "");
					if (RtdData == null) {
						core.setV_errMsg("找不到成品测量点数据！");
						return core;
					}


					if(RtdData.getActLen().startsWith(".")){
						RtdData.setActLen("0" +  RtdData.getActLen() );
					}

					if(RtdData.getActWeight().startsWith(".")){
						RtdData.setActWeight("0" +  RtdData.getActWeight());
					}

					if(Double.parseDouble(RtdData.getActWeight())<0.0001){
						core.setV_errMsg("测量点数据存在重量为0的数据,请检查数据！");
						return core;
					}

					if(Double.parseDouble(RtdData.getActLen())<0.0001){
						core.setV_errMsg("测量点数据存在长度为0的数据,请检查数据！");
						return core;
					}

					ProWt +=  Double.parseDouble(RtdData.getActWeight());



					int countPort = 0;
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmMeasureResult.checkPort");// 查询 工序是否是结炉状态
					psta = connection.prepareStatement(sql);
					psta.setString(1, Load.getHeatPlanNo());
					psta.setString(2, Load.getBatchNo());
					psta.setString(3, RtdData.getMatNo());
					rs = psta.executeQuery();
					if (rs.next()) {
						countPort = rs.getInt("COUNT");
					}


					if (countPort>0) {
						core.setV_errMsg("喷印管号"+RtdData.getMatNo() + "已经匹配, 若该管号重新经过测量点, 请离线再反至测量点操作!" );
						return core;
					}
					rs.close();
					psta.close();

					patsupdatePort.setObject(1, RtdData.getMatNo());
				//	patsupdatePort.setObject(2, RtdData.getActLen());
					patsupdatePort.setObject(2, RtdData.getActWeight());
					patsupdatePort.setObject(3, Load.getHeatPlanNo());
					patsupdatePort.setObject(4, MatNos.get(i));
					patsupdatePort.addBatch();

					patsupdateMeasureData.setObject(1,"10");
					patsupdateMeasureData.setObject(2,Load.getJudgeStoveNo());
					patsupdateMeasureData.setObject(3,Load.getBatchNo());
					patsupdateMeasureData.setObject(4,MatNos.get(i));
					patsupdateMeasureData.addBatch();

					patsupdateRtdData.setObject(1, MatNos.get(i));
					patsupdateRtdData.setObject(2, RtdData.getKeyId());
					patsupdateRtdData.addBatch();

                     port.setInstoreFlag("1");
					if(port.getInstoreFlag().equals("1")) {

						if(port.getGroupNoReal().equals("")){
							port.setGroupNoReal(FrmMchInStore.creatGroup(connection, pln.getOrderNo() + "/" + pln.getOrderSeq(),
									port.getActLenMinReal(), port.getActLenMaxReal(),
									pln.getOrdLnDlyPk()));
						}


						patsupdateMatData.setObject(1, RtdData.getActWeight());
						patsupdateMatData.setObject(2, RtdData.getActWeight());
					//	patsupdateMatData.setObject(4, port.getGroupNoReal());
						//patsupdateMatData.setObject(5, port.getActLenMinReal());
					//	patsupdateMatData.setObject(6, port.getActLenMaxReal());

						patsupdateMatData.setObject(3, MatNos.get(i));
						patsupdateMatData.addBatch();

					//	patsupdateInListData.setObject(1, RtdData.getActLen());
						patsupdateInListData.setObject(1,   RtdData.getActWeight());
					//patsupdateInListData.setObject(3, port.getGroupNoReal());
						//patsupdateInListData.setObject(4, port.getActLenMinReal());
						//patsupdateInListData.setObject(5, port.getActLenMaxReal());

						patsupdateInListData.setObject(2, MatNos.get(i));
						patsupdateInListData.addBatch();

					}


					patsAddResultD.setObject(1,resultNo);
					patsAddResultD.setObject(2,MatNos.get(i));
					patsAddResultD.setObject(3,RtdData.getMatNo());
					patsAddResultD.setObject(4,"");
					patsAddResultD.setObject(5,RtdData.getActWeight());
					patsAddResultD.setObject(6,Load.getHeatPlanNo());
					patsAddResultD.setObject(7,MatNos.get(i));
					patsAddResultD.setObject(8,Load.getHeatPlanNo());
					patsAddResultD.addBatch();
						/*
						patsupdateMatBackData.setObject(1, RtdData.getActLen());
						patsupdateMatBackData.setObject(2, RtdData.getActWeight());
						patsupdateMatBackData.setObject(3, RtdData.getActWeight());
						patsupdateMatBackData.setObject(4, MatNos.get(i));
						patsupdateMatBackData.addBatch();*/
				}





			}
			patsupdateMeasureData.executeBatch();
			patsupdateMatData.executeBatch();
			patsupdateInListData.executeBatch();
				/*
				patsupdateMatBackData.executeBatch();*/
			patsupdatePort.executeBatch();
			patsupdateRtdData.executeBatch();

			patsAddResultD.executeBatch();



			Load.setResultNo(resultNo);
			Load.setProWt(ProWt.toString());

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.AddResult");// 新增上料实绩

			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, Load);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();


			// 新增加工公共信息
			patsPlan.setObject(1, resultNo);
			patsPlan.setObject(2, Load.getPlineCode());
			patsPlan.setObject(3, Load.getPlineName());
			patsPlan.setObject(4, gx);
			patsPlan.setObject(5, gx);
			patsPlan.setObject(6, Load.getBatchNo());
			patsPlan.setObject(7, Load.getProNum());
			patsPlan.setObject(8, Load.getProWt());
			patsPlan.setObject(9, Load.getFailNum());
			patsPlan.setObject(10, Load.getFailWt());
			patsPlan.setObject(11, Load.getProTime());
			patsPlan.setObject(12, Load.getProShift());
			patsPlan.setObject(13, Load.getProGroup());
			patsPlan.setObject(14, Load.getProUser());
			patsPlan.setObject(15, Load.getProUser());
			patsPlan.setObject(16, Load.getHeatPlanNo());
			patsPlan.addBatch();

			//00-上料开始,10-探伤开始，20-切管开始，30-车丝开始，40-螺纹检验开始，50拧接开始，60-通径开始,70-水压开始，80-倒凌开始，90-测量点开始，11-交库开始
			patupdatepln.setObject(1,"90");
			patupdatepln.setObject(2,Load.getHeatPlanNo());
			patupdatepln.setObject(3,Load.getBatchNo());
			patupdatepln.addBatch();

			patAdd.executeBatch();
			patsPlan.executeBatch();
			patupdatepln.executeBatch();


			FrmMchBugD.AddMchWaste(connection,Load.getHeatPlanNo(),resultNo,Load.getPlineCode(),Load.getProUser(),gx,SuspiciousList, ScrappedList);


			if(!Load.getProNum().equals("0") && !Load.getProNum().equals("")) {

				String addHttControlLog = CoreXmlSqlParsersFactory.getSql("FrmMeasureResult.addHttControlLog"); //增加调度日志
				PreparedStatement pastaddHttControlLog = connection.prepareStatement(addHttControlLog);


				SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.CHINA);
				SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
				SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
				SimpleDateFormat matter3 = new SimpleDateFormat("yyyyMM");
				SimpleDateFormat matter4 = new SimpleDateFormat("yyyyMMdd");



				Date colDate = GetDateByStr(Load.getProTime());
				Date endDate = GetDateByStr(Load.getProEndTime());
				Date procDate = colDate;
				if (Load.getProShift().equals("3")) {
					Calendar date = Calendar.getInstance();
					date.setTime(colDate);
					date.set(Calendar.HOUR_OF_DAY, date.get(Calendar.HOUR_OF_DAY) + 12);
					procDate = dateFormat.parse(dateFormat.format(date.getTime()));
				}

				if (colDate.after(endDate)) {
					core.setV_errMsg("结束时间不能早于开始时间");
					return core;
				}


				pastaddHttControlLog.setObject(1, matter2.format(colDate));
				pastaddHttControlLog.setObject(2, matter2.format(endDate));
				pastaddHttControlLog.setObject(3, Load.getPlineCode());
				pastaddHttControlLog.setObject(4, Load.getPlineName());
				pastaddHttControlLog.setObject(5, resultNo);
				// patAdd.setObject(3, pln.getProdIndex());
				pastaddHttControlLog.setObject(6, Load.getProShift());
				pastaddHttControlLog.setObject(7, Load.getProGroup());
				pastaddHttControlLog.setObject(8, Load.getProNum());
				pastaddHttControlLog.setObject(9, Load.getProWt());
				pastaddHttControlLog.setObject(10, systime);
				pastaddHttControlLog.setObject(11, Load.getProUser());
				pastaddHttControlLog.setObject(12, com.steering.common.StringTool.GetBalYear(procDate,com.steering.common.StringTool.GetSupplyUnit(Load.getPlineCode()))); //改25号结转
				pastaddHttControlLog.setObject(13, matter4.format(procDate));
				pastaddHttControlLog.setObject(14, "2");
				pastaddHttControlLog.setObject(15, Load.getHeatPlanNo());
				pastaddHttControlLog.addBatch();
				pastaddHttControlLog.executeBatch();
			}

			core.setV_errMsg("新增成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject DoAdd(String loadStr, String gx, ArrayList<String> ScrappedList, ArrayList<String> SuspiciousList, ArrayList<String> MatNos,ArrayList<String> LenWeigtStrs){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false); //



			String sqlupdatepln = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.updatePlnZyRclM2");// 修改加工计划表
			PreparedStatement patupdatepln = connection
					.prepareStatement(sqlupdatepln);

			String addPlnInfo = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.AddComInfoResultNew");// 新增公共信息
			PreparedStatement patsPlan = connection
					.prepareStatement(addPlnInfo);


			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updatePort");// 更新管号队列
			PreparedStatement patsupdatePort = connection
					.prepareStatement(updatePort);

			String updateMeasureData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMeasureData");// 把原匹配的数据更新成剔除
			PreparedStatement patsupdateMeasureData = connection
					.prepareStatement(updateMeasureData);

			String updateRtdData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
			PreparedStatement patsupdateRtdData = connection
					.prepareStatement(updateRtdData);

		String updateMatData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatData");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatData = connection
					.prepareStatement(updateMatData);

			String updateInListData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateInListData");//
			PreparedStatement patsupdateInListData = connection
					.prepareStatement(updateInListData);


			String AddResultD = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.AddResultD");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patsAddResultD = connection
					.prepareStatement(AddResultD);

/*
			String updateMatBackData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatBackData");// 更新测量数据 -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatBackData = connection
					.prepareStatement(updateMatBackData);*/

			MchMeasureResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchMeasureResultEntity.class);


			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			String resultNo = "";
			int count = 0;
			int detCount = 0;


			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}
			if(StringTool.isInteger(Load.getProNum())){
				count = Integer.parseInt(Load.getProNum());
			}


			if(count<0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}

			if(count==0 && detCount<=0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}



			if(MatNos.size()!=count){
				core.setV_errMsg("没有足够的管,无法新增实绩！");
				return core;
			}



			//获取最大实绩编号
			sql =   CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.queryResultNo");
			psta = connection.prepareStatement(sql);
			psta.setObject(1, pln.getPlineCode() + gx );
			psta.setObject(2, pln.getPlineCode() + gx );
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();



			Integer RemainNum =0;

            Double SingleWt= 0D;
            Double SingleLen = 0D;




		       	 MchLenWeightEntity LenWeightEntity = null;


		       	 Double ProWt = 0D;

		       	 Integer CountNum = 1;
		       	 Integer CountWt =1;

		       	 Double Dlen = 0.001;
		       	 Double Dwt  = 0.001;

			DecimalFormat df = new DecimalFormat("0.000");

				for (int i  = 0,index = 0; i < MatNos.size(); i++) {
					PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(Load.getHeatPlanNo(), MatNos.get(i), connection);



					if(Load.getFlag().equals("1")) {

						if(RemainNum==0){
							if(index>= LenWeigtStrs.size()){
								core.setV_errMsg("手动维护测量点支数不足！");
								return core;
							}

							LenWeightEntity = EntityTool.getEntityByJSONStr(LenWeigtStrs.get(index++),MchLenWeightEntity.class);
							RemainNum = Integer.parseInt(LenWeightEntity.getActCount());
                            Double Len = Double.parseDouble(LenWeightEntity.getActLen());
                            Double Wt = Double.parseDouble(LenWeightEntity.getActWeight());


                            // 总重量平均    取4位小数   多余量累积  平均到后几支
							SingleLen =Math.round(Len/ RemainNum * 1000)/1000D;
							SingleWt=Math.round(Wt/ RemainNum * 1000)/1000D;

							CountNum = (int) Math.round((Len - SingleLen * RemainNum) * 1000);
							if(CountNum>0){
								Dlen = 0.001d;
							}else  if(CountNum==0)
							{
								Dlen = 0d;
							}else {
								Dlen = - 0.001d;
							}
							CountNum = Math.abs(CountNum);

							CountWt = (int) Math.round((Wt - SingleWt * RemainNum) * 1000);

							if(CountWt>0){
								Dwt = 0.001d;
							}else  if(CountWt==0)
							{
								Dwt = 0d;
							}else {
								Dwt = - 0.001d;
							}
							CountWt = Math.abs(CountWt);
						}

						Double Len = RemainNum<=CountNum? Math.round((SingleLen +Dlen)*1000D)/1000D : SingleLen;
						Double Wt = RemainNum<=CountWt?  Math.round((SingleWt +Dwt)*1000D)/1000D: SingleWt;

						ProWt +=  Wt;

						patsupdatePort.setObject(1, "");
						patsupdatePort.setObject(2,Len);
						patsupdatePort.setObject(3, Wt);
						patsupdatePort.setObject(4, Load.getHeatPlanNo());
						patsupdatePort.setObject(5, MatNos.get(i));
						patsupdatePort.addBatch();

						if(port.getInstoreFlag().equals("1")) {

							if(port.getGroupNoReal().equals("")){
								port.setGroupNoReal(FrmMchInStore.creatGroup(connection, pln.getOrderNo() + "/" + pln.getOrderSeq(),
										port.getActLenMinReal(), port.getActLenMaxReal(),
										pln.getOrdLnDlyPk()));
							}

							patsupdateMatData.setObject(1,Len);
							patsupdateMatData.setObject(2,  Wt);
							patsupdateMatData.setObject(3, Wt);
							patsupdateMatData.setObject(4, port.getGroupNoReal());
							patsupdateMatData.setObject(5, port.getActLenMinReal());
							patsupdateMatData.setObject(6, port.getActLenMaxReal());

							patsupdateMatData.setObject(7, MatNos.get(i));
							patsupdateMatData.addBatch();

							patsupdateInListData.setObject(1, Len);
							patsupdateInListData.setObject(2,   Wt);
							patsupdateInListData.setObject(3, port.getGroupNoReal());
							patsupdateInListData.setObject(4, port.getActLenMinReal());
							patsupdateInListData.setObject(5, port.getActLenMaxReal());

							patsupdateInListData.setObject(6, MatNos.get(i));
							patsupdateInListData.addBatch();

						}

                       /*
						patsupdateMatBackData.setObject(1, Len);
						patsupdateMatBackData.setObject(2,  Wt);
						patsupdateMatBackData.setObject(3,  Wt);
						patsupdateMatBackData.setObject(4, MatNos.get(i));
						patsupdateMatBackData.addBatch();*/

						RemainNum--;
					}
					else {
						RtdMeasurePosdataEntity RtdData = getRtdMeasurePosdataEntity(connection, Load.getJudgeStoveNo(), Load.getBatchNo(), Load.getPlineCode(),( i + 1 ) + "");
						if (RtdData == null) {
							core.setV_errMsg("找不到成品测量点数据！");
							return core;
						}


						if(RtdData.getActLen().startsWith(".")){
							RtdData.setActLen("0" +  RtdData.getActLen() );
						}

						if(RtdData.getActWeight().startsWith(".")){
							RtdData.setActWeight("0" +  RtdData.getActWeight());
						}

						if(Double.parseDouble(RtdData.getActWeight())<0.0001){
							core.setV_errMsg("测量点数据存在重量为0的数据,请检查数据！");
							return core;
						}

						if(Double.parseDouble(RtdData.getActLen())<0.0001){
							core.setV_errMsg("测量点数据存在长度为0的数据,请检查数据！");
							return core;
						}

						ProWt +=  Double.parseDouble(RtdData.getActWeight());



						int countPort = 0;
						 sql = CoreXmlSqlParsersFactory
								.getSql("FrmMeasureResult.checkPort");// 查询 工序是否是结炉状态
						psta = connection.prepareStatement(sql);
						psta.setString(1, Load.getHeatPlanNo());
						psta.setString(2, Load.getBatchNo());
						psta.setString(3, RtdData.getMatNo());
						 rs = psta.executeQuery();
						if (rs.next()) {
							countPort = rs.getInt("COUNT");
						}


						if (countPort>0) {
							core.setV_errMsg("喷印管号"+RtdData.getMatNo() + "已经匹配, 若该管号重新经过测量点, 请离线再反至测量点操作!" );
							return core;
						}
						rs.close();
						psta.close();

						patsupdatePort.setObject(1, RtdData.getMatNo());
						patsupdatePort.setObject(2, RtdData.getActLen());
						patsupdatePort.setObject(3, RtdData.getActWeight());
						patsupdatePort.setObject(4, Load.getHeatPlanNo());
						patsupdatePort.setObject(5, MatNos.get(i));
						patsupdatePort.addBatch();

						patsupdateMeasureData.setObject(1,"10");
						patsupdateMeasureData.setObject(2,Load.getJudgeStoveNo());
						patsupdateMeasureData.setObject(3,Load.getBatchNo());
						patsupdateMeasureData.setObject(4,MatNos.get(i));
						patsupdateMeasureData.addBatch();

						patsupdateRtdData.setObject(1, MatNos.get(i));
						patsupdateRtdData.setObject(2, RtdData.getKeyId());
						patsupdateRtdData.addBatch();


						if(port.getInstoreFlag().equals("1")) {

							if(port.getGroupNoReal().equals("")){
								port.setGroupNoReal(FrmMchInStore.creatGroup(connection, pln.getOrderNo() + "/" + pln.getOrderSeq(),
										port.getActLenMinReal(), port.getActLenMaxReal(),
										pln.getOrdLnDlyPk()));
							}

							patsupdateMatData.setObject(1, RtdData.getActLen());
							patsupdateMatData.setObject(2, RtdData.getActWeight());
							patsupdateMatData.setObject(3, RtdData.getActWeight());
							patsupdateMatData.setObject(4, port.getGroupNoReal());
							patsupdateMatData.setObject(5, port.getActLenMinReal());
							patsupdateMatData.setObject(6, port.getActLenMaxReal());

							patsupdateMatData.setObject(7, MatNos.get(i));
							patsupdateMatData.addBatch();

							patsupdateInListData.setObject(1, RtdData.getActLen());
							patsupdateInListData.setObject(2,   RtdData.getActWeight());
							patsupdateInListData.setObject(3, port.getGroupNoReal());
							patsupdateInListData.setObject(4, port.getActLenMinReal());
							patsupdateInListData.setObject(5, port.getActLenMaxReal());

							patsupdateInListData.setObject(6, MatNos.get(i));
							patsupdateInListData.addBatch();

						}


						patsAddResultD.setObject(1,resultNo);
						patsAddResultD.setObject(2,RtdData.getActLen());
						patsAddResultD.setObject(3,MatNos.get(i));
						patsAddResultD.setObject(4,RtdData.getMatNo());
						patsAddResultD.setObject(5,"");
						patsAddResultD.setObject(6,RtdData.getActWeight());
						patsAddResultD.setObject(7,Load.getHeatPlanNo());
						patsAddResultD.setObject(8,MatNos.get(i));
						patsAddResultD.setObject(9,Load.getHeatPlanNo());
						patsAddResultD.addBatch();
						/*
						patsupdateMatBackData.setObject(1, RtdData.getActLen());
						patsupdateMatBackData.setObject(2, RtdData.getActWeight());
						patsupdateMatBackData.setObject(3, RtdData.getActWeight());
						patsupdateMatBackData.setObject(4, MatNos.get(i));
						patsupdateMatBackData.addBatch();*/
					}





				}
		    	patsupdateMeasureData.executeBatch();
				patsupdateMatData.executeBatch();
		     	patsupdateInListData.executeBatch();
				/*
				patsupdateMatBackData.executeBatch();*/
				patsupdatePort.executeBatch();
				patsupdateRtdData.executeBatch();

			patsAddResultD.executeBatch();



			Load.setResultNo(resultNo);
			Load.setProWt(ProWt.toString());

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.AddResult");// 新增上料实绩

			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, Load);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();


			// 新增加工公共信息
			patsPlan.setObject(1, resultNo);
			patsPlan.setObject(2, Load.getPlineCode());
			patsPlan.setObject(3, Load.getPlineName());
			patsPlan.setObject(4, gx);
			patsPlan.setObject(5, gx);
			patsPlan.setObject(6, Load.getBatchNo());
			patsPlan.setObject(7, Load.getProNum());
			patsPlan.setObject(8, Load.getProWt());
			patsPlan.setObject(9, Load.getFailNum());
			patsPlan.setObject(10, Load.getFailWt());
			patsPlan.setObject(11, Load.getProTime());
			patsPlan.setObject(12, Load.getProShift());
			patsPlan.setObject(13, Load.getProGroup());
			patsPlan.setObject(14, Load.getProUser());
			patsPlan.setObject(15, Load.getProUser());
			patsPlan.setObject(16, Load.getHeatPlanNo());
			patsPlan.addBatch();

          //00-上料开始,10-探伤开始，20-切管开始，30-车丝开始，40-螺纹检验开始，50拧接开始，60-通径开始,70-水压开始，80-倒凌开始，90-测量点开始，11-交库开始
			patupdatepln.setObject(1,"90");
			patupdatepln.setObject(2,Load.getHeatPlanNo());
			patupdatepln.setObject(3,Load.getBatchNo());
			patupdatepln.addBatch();

			patAdd.executeBatch();
			patsPlan.executeBatch();
			patupdatepln.executeBatch();


			FrmMchBugD.AddMchWaste(connection,Load.getHeatPlanNo(),resultNo,Load.getPlineCode(),Load.getProUser(),gx,SuspiciousList, ScrappedList);


			if(!Load.getProNum().equals("0") && !Load.getProNum().equals("")) {

				String addHttControlLog = CoreXmlSqlParsersFactory.getSql("FrmMeasureResult.addHttControlLog"); //增加调度日志
				PreparedStatement pastaddHttControlLog = connection.prepareStatement(addHttControlLog);


				SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.CHINA);
				SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
				SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
				SimpleDateFormat matter3 = new SimpleDateFormat("yyyyMM");
				SimpleDateFormat matter4 = new SimpleDateFormat("yyyyMMdd");



				Date colDate = GetDateByStr(Load.getProTime());
				Date endDate = GetDateByStr(Load.getProEndTime());
				Date procDate = colDate;
				if (Load.getProShift().equals("3")) {
					Calendar date = Calendar.getInstance();
					date.setTime(colDate);
					date.set(Calendar.HOUR_OF_DAY, date.get(Calendar.HOUR_OF_DAY) + 12);
					procDate = dateFormat.parse(dateFormat.format(date.getTime()));
				}

				if (colDate.after(endDate)) {
					core.setV_errMsg("结束时间不能早于开始时间");
					return core;
				}


				pastaddHttControlLog.setObject(1, matter2.format(colDate));
				pastaddHttControlLog.setObject(2, matter2.format(endDate));
				pastaddHttControlLog.setObject(3, Load.getPlineCode());
				pastaddHttControlLog.setObject(4, Load.getPlineName());
				pastaddHttControlLog.setObject(5, resultNo);
				// patAdd.setObject(3, pln.getProdIndex());
				pastaddHttControlLog.setObject(6, Load.getProShift());
				pastaddHttControlLog.setObject(7, Load.getProGroup());
				pastaddHttControlLog.setObject(8, Load.getProNum());
				pastaddHttControlLog.setObject(9, Load.getProWt());
				pastaddHttControlLog.setObject(10, systime);
				pastaddHttControlLog.setObject(11, Load.getProUser());
				pastaddHttControlLog.setObject(12, com.steering.common.StringTool.GetBalYear(procDate,com.steering.common.StringTool.GetSupplyUnit(Load.getPlineCode()))); //改25号结转
				pastaddHttControlLog.setObject(13, matter4.format(procDate));
				pastaddHttControlLog.setObject(14, "2");
				pastaddHttControlLog.setObject(15, Load.getHeatPlanNo());
				pastaddHttControlLog.addBatch();
				pastaddHttControlLog.executeBatch();
			}

			core.setV_errMsg("新增成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}

	public CoreReturnObject DoAddNew(String loadStr,String gx,ArrayList<String> ScrappedList, ArrayList<String> SuspiciousList){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false); //



			String sqlupdatepln = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.updatePlnZyRclM2");// 修改加工计划表
			PreparedStatement patupdatepln = connection
					.prepareStatement(sqlupdatepln);

			String addPlnInfo = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.AddComInfoResultNew");// 新增公共信息
			PreparedStatement patsPlan = connection
					.prepareStatement(addPlnInfo);


			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updatePort");// 更新管号队列
			PreparedStatement patsupdatePort = connection
					.prepareStatement(updatePort);

			String updateMeasureData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMeasureData");// 把原匹配的数据更新成剔除
			PreparedStatement patsupdateMeasureData = connection
					.prepareStatement(updateMeasureData);

			String updateRtdData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
			PreparedStatement patsupdateRtdData = connection
					.prepareStatement(updateRtdData);


/*
			String updateMatBackData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatBackData");// 更新测量数据 -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatBackData = connection
					.prepareStatement(updateMatBackData);*/

			MchMeasureResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchMeasureResultEntity.class);


			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			String resultNo = "";
			int count = 0;
			int detCount = 0;


			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}
			if(StringTool.isInteger(Load.getProNum())){
				count = Integer.parseInt(Load.getProNum());
			}


			if(count<0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}

			if(count==0 && detCount<=0){
				core.setV_errMsg("请输入正确的作业支数！");
				return core;
			}





			//获取最大实绩编号
			sql =   CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.queryResultNo");
			psta = connection.prepareStatement(sql);
			psta.setObject(1, pln.getPlineCode() + gx );
			psta.setObject(2, pln.getPlineCode() + gx );
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();


			Load.setResultNo(resultNo);
			Load.setFlag("2");

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.AddResult");//

			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, Load);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();


			// 新增加工公共信息
			patsPlan.setObject(1, resultNo);
			patsPlan.setObject(2, Load.getPlineCode());
			patsPlan.setObject(3, Load.getPlineName());
			patsPlan.setObject(4, gx);
			patsPlan.setObject(5, gx);
			patsPlan.setObject(6, Load.getBatchNo());
			patsPlan.setObject(7, Load.getProNum());
			patsPlan.setObject(8, Load.getProWt());
			patsPlan.setObject(9, Load.getFailNum());
			patsPlan.setObject(10, Load.getFailWt());
			patsPlan.setObject(11, Load.getProTime());
			patsPlan.setObject(12, Load.getProShift());
			patsPlan.setObject(13, Load.getProGroup());
			patsPlan.setObject(14, Load.getProUser());
			patsPlan.setObject(15, Load.getProUser());
			patsPlan.setObject(16, Load.getHeatPlanNo());
			patsPlan.addBatch();

			//00-上料开始,10-探伤开始，20-切管开始，30-车丝开始，40-螺纹检验开始，50拧接开始，60-通径开始,70-水压开始，80-倒凌开始，90-测量点开始，11-交库开始
			patupdatepln.setObject(1,"90");
			patupdatepln.setObject(2,Load.getHeatPlanNo());
			patupdatepln.setObject(3,Load.getBatchNo());
			patupdatepln.addBatch();

			patAdd.executeBatch();
			patsPlan.executeBatch();
			patupdatepln.executeBatch();


			FrmMchBugD.AddMchWaste(connection,Load.getHeatPlanNo(),resultNo,Load.getPlineCode(),Load.getProUser(),gx,SuspiciousList, ScrappedList);


			if(!Load.getProNum().equals("0") && !Load.getProNum().equals("")) {

				String addHttControlLog = CoreXmlSqlParsersFactory.getSql("FrmMeasureResult.addHttControlLog"); //增加调度日志
				PreparedStatement pastaddHttControlLog = connection.prepareStatement(addHttControlLog);


				SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
				SimpleDateFormat matter3 = new SimpleDateFormat("yyyyMM");
				SimpleDateFormat matter4 = new SimpleDateFormat("yyyyMMdd");



				Date colDate = GetDateByStr(Load.getProTime());
				Date endDate = GetDateByStr(Load.getProEndTime());
				Date procDate = colDate;
				if (Load.getProShift().equals("3")) {
					Calendar date = Calendar.getInstance();
					date.setTime(colDate);
					date.set(Calendar.HOUR_OF_DAY, date.get(Calendar.HOUR_OF_DAY) + 12);
					procDate = dateFormat.parse(dateFormat.format(date.getTime()));
				}

				if (colDate.after(endDate)) {
					core.setV_errMsg("结束时间不能早于开始时间");
					return core;
				}


				pastaddHttControlLog.setObject(1, matter2.format(colDate));
				pastaddHttControlLog.setObject(2, matter2.format(endDate));
				pastaddHttControlLog.setObject(3, resultNo);
				// patAdd.setObject(3, pln.getProdIndex());
				pastaddHttControlLog.setObject(4, Load.getProShift());
				pastaddHttControlLog.setObject(5, Load.getProGroup());
				pastaddHttControlLog.setObject(6, Load.getProNum());
				pastaddHttControlLog.setObject(7, Load.getProWt());
				pastaddHttControlLog.setObject(8, systime);
				pastaddHttControlLog.setObject(9, Load.getProUser());
				pastaddHttControlLog.setObject(10, matter3.format(procDate));
				pastaddHttControlLog.setObject(11, matter4.format(procDate));
				pastaddHttControlLog.setObject(12, "0");
				pastaddHttControlLog.setObject(13, Load.getHeatPlanNo());
				pastaddHttControlLog.addBatch();
				pastaddHttControlLog.executeBatch();
			}

			core.setV_errMsg("新增成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject DoSynchronous(String loadStr, ArrayList<String> MatNos){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false); //




			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updatePort");// 更新管号队列
			PreparedStatement patsupdatePort = connection
					.prepareStatement(updatePort);

			String updateMeasureData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMeasureData");// 把原匹配的数据更新成剔除
			PreparedStatement patsupdateMeasureData = connection
					.prepareStatement(updateMeasureData);

			String updateRtdData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
			PreparedStatement patsupdateRtdData = connection
					.prepareStatement(updateRtdData);

			String updateMatData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatData");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatData = connection
					.prepareStatement(updateMatData);

			String updateInListData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateInListData");//
			PreparedStatement patsupdateInListData = connection
					.prepareStatement(updateInListData);


			String updateResult = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateResult");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patupdateResult = connection
					.prepareStatement(updateResult);




			MchMeasureResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchMeasureResultEntity.class);

			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion

          double TotalWt = 0d;

			for (int i  = 0; i < MatNos.size(); i++) {


					RtdMeasurePosdataEntity RtdData = getRtdMeasurePosdataEntity(connection, Load.getJudgeStoveNo(), Load.getBatchNo(), Load.getPlineCode(),( i + 1 ) + "");
					if (RtdData == null) {
						core.setV_errMsg("找不到成品测量点数据！");
						return core;
					}

				PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(Load.getHeatPlanNo(), MatNos.get(i), connection);

				if(port.getInstoreFlag().equals("1") && !port.getJugdeApplyCode().equals("")){
					core.setV_errMsg("存在管号已经送判,此操作！");
					return core;
				}


					if(RtdData.getActLen().startsWith(".")){
						RtdData.setActLen("0" +  RtdData.getActLen() );
					}

					if(RtdData.getActWeight().startsWith(".")){
						RtdData.setActWeight("0" +  RtdData.getActWeight());
					}

					if(Double.parseDouble(RtdData.getActWeight())<0.0001){
						core.setV_errMsg("测量点数据存在重量为0的数据,请检查数据！");
						return core;
					}

					if(Double.parseDouble(RtdData.getActLen())<0.0001){
						core.setV_errMsg("测量点数据存在长度为0的数据,请检查数据！");
						return core;
					}

			     	TotalWt += Double.parseDouble(RtdData.getActWeight());




					patsupdatePort.setObject(1, RtdData.getMatNo());
					patsupdatePort.setObject(2, RtdData.getActLen());
					patsupdatePort.setObject(3, RtdData.getActWeight());
					patsupdatePort.setObject(4, Load.getHeatPlanNo());
					patsupdatePort.setObject(5, MatNos.get(i));
					patsupdatePort.addBatch();

					patsupdateMeasureData.setObject(1,"10");
					patsupdateMeasureData.setObject(2,Load.getJudgeStoveNo());
					patsupdateMeasureData.setObject(3,Load.getBatchNo());
					patsupdateMeasureData.setObject(4,MatNos.get(i));
					patsupdateMeasureData.addBatch();

					patsupdateRtdData.setObject(1, MatNos.get(i));
					patsupdateRtdData.setObject(2, RtdData.getKeyId());
					patsupdateRtdData.addBatch();

				if(port.getInstoreFlag().equals("1")) {

					if(port.getGroupNoReal().equals("")){
						port.setGroupNoReal(FrmMchInStore.creatGroup(connection, pln.getOrderNo() + "/" + pln.getOrderSeq(),
								port.getActLenMinReal(), port.getActLenMaxReal(),
								pln.getOrdLnDlyPk()));
					}

					patsupdateMatData.setObject(1, RtdData.getActLen());
					patsupdateMatData.setObject(2,   RtdData.getActWeight());
					patsupdateMatData.setObject(3,  RtdData.getActWeight());
					patsupdateMatData.setObject(4, port.getGroupNoReal());
					patsupdateMatData.setObject(5, port.getActLenMinReal());
					patsupdateMatData.setObject(6, port.getActLenMaxReal());

					patsupdateMatData.setObject(7, MatNos.get(i));
					patsupdateMatData.addBatch();


					patsupdateInListData.setObject(1, RtdData.getActLen());
					patsupdateInListData.setObject(2,   RtdData.getActWeight());
					patsupdateInListData.setObject(3, port.getGroupNoReal());
					patsupdateInListData.setObject(4, port.getActLenMinReal());
					patsupdateInListData.setObject(5, port.getActLenMaxReal());

					patsupdateInListData.setObject(6, MatNos.get(i));
					patsupdateInListData.addBatch();

				}

			}



			patupdateResult.setObject(1,TotalWt);
			patupdateResult.setObject(2,Load.getResultNo());
			patupdateResult.addBatch();
			patupdateResult.executeBatch();


			patsupdateMeasureData.executeBatch();
			patsupdateMatData.executeBatch();
			patsupdateInListData.executeBatch();

			patsupdatePort.executeBatch();
			patsupdateRtdData.executeBatch();



			core.setV_errMsg("同步成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}
	public CoreReturnObject DoDeleteDbk(String loadStr,String gx, ArrayList<String> MatNos) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat1 = new SimpleDateFormat(
				"yyyyMM", Locale.CHINA);
		String systime1 = dateFormat1.format(now);
		PreparedStatement psta = null;

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(new Date());

		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			String DeleteComInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.deleteVrpResult");// 删除公共信息
			PreparedStatement patsDeleteComInfo = connection
					.prepareStatement(DeleteComInfoSql);

			String DeleteResultSql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DeleteResult");// 删除实绩
			PreparedStatement patsDeleteResult = connection
					.prepareStatement(DeleteResultSql);


			String DeleteResultDSql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DeleteResultD");// 删除实绩
			PreparedStatement patsDeleteResultD = connection
					.prepareStatement(DeleteResultDSql);

			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updatePort1Dbk");// 更新管号队列
			PreparedStatement patsupdatePort = connection
					.prepareStatement(updatePort);

			String updateRtdData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
			PreparedStatement patsupdateRtdData = connection
					.prepareStatement(updateRtdData);

		/*	String updateMatData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatData1");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatData = connection
					.prepareStatement(updateMatData);*/

	/*		String updateMatBackData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatBackData1");// 更新测量数据 -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatBackData = connection
					.prepareStatement(updateMatBackData);*/

			String updateMeasureData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMeasureData");// 把原匹配的数据 还原成未剔除
			PreparedStatement patsupdateMeasureData = connection
					.prepareStatement(updateMeasureData);


			MchMeasureResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchMeasureResultEntity.class);

			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3Dbk(connection,
					Load.getHeatPlanNo(),Load.getBatchNo());

			if(!pln.getPlineCode().equals(Load.getPlineCode())){
				core.setV_errMsg("不能删除其他产线生成的实绩！");
				return core;
			}


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "650108");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			String LastRsNo = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.CheckResult");
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			rs = psta.executeQuery();
			if (rs.next()) {
				LastRsNo = rs.getString("RESULT_NO");
			}
			rs.close();
			psta.close();


			if(!LastRsNo.equals(Load.getResultNo())){
				core.setV_errMsg("请先删除最后维护的实绩！");
				return core;
			}


			patsDeleteComInfo.setObject(1,Load.getResultNo());
			patsDeleteComInfo.addBatch();
			patsDeleteComInfo.executeBatch();



			patsDeleteResult.setObject(1,Load.getResultNo());
			patsDeleteResult.addBatch();
			patsDeleteResult.executeBatch();

			int count = 0;

			if(StringTool.isInteger(Load.getProNum())){
				count = Integer.parseInt(Load.getProNum());
			}

			if(!Load.getFlag().equals("2")) {

				ArrayList<MchMeasureResultDEntity> Detail =
						getMchMeasureResultDEntity(Load.getResultNo(),connection);


				if (MatNos.size() != count) {
					core.setV_errMsg("找不到足够已匹配测量点数据的管！");
					return core;
				}


				for (int i = 0; i < count; i++) {

					String MatNo = Detail.size()>0 ?Detail.get(i).getMatNo(): MatNos.get(i);


					PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntitydbk(Load.getHeatPlanNo(), MatNo, connection);

					if(port.getInstoreFlag().equals("1") && !port.getJugdeApplyCode().equals("")){
						core.setV_errMsg("存在管号已经送判,不允许删除测量点数据！");
						return core;
					}

					if (port == null || port.getTrFlag().equals("2")) {
						core.setV_errMsg("材料已经离线返线,不允许删除测量点实绩！");
						return core;
					}

					if (Load.getFlag().equals("0")) {

						RtdMeasurePosdataEntity RtdData = getRtdMeasurePosdataEntity1(connection, Load.getJudgeStoveNo(), Load.getBatchNo(), Load.getPlineCode(), MatNo);
						if (RtdData == null) {
							core.setV_errMsg("找不到已匹配的成品测量点数据,请检查是否已经手动剔除！");
							return core;
						}

						/*patsupdateMeasureData.setObject(1, "00");
						patsupdateMeasureData.setObject(2, Load.getJudgeStoveNo());
						patsupdateMeasureData.setObject(3, Load.getBatchNo());
						patsupdateMeasureData.setObject(4, MatNo);
						patsupdateMeasureData.addBatch();*/

						patsupdateRtdData.setObject(1, "");
						patsupdateRtdData.setObject(2, RtdData.getKeyId());
						patsupdateRtdData.addBatch();
					}


					patsupdateMeasureData.executeBatch();
					patsupdatePort.setObject(1, Load.getHeatPlanNo());
					patsupdatePort.setObject(2, MatNo);
					patsupdatePort.addBatch();




				/*	patsupdateMatData.setObject(1, MatNo);
					patsupdateMatData.addBatch();*/

				/*	patsupdateMatBackData.setObject(1, MatNo);
					patsupdateMatBackData.addBatch();*/
				}


				patsupdateRtdData.executeBatch();
				//	patsupdateMatData.executeBatch();
				//	patsupdateMatBackData.executeBatch();
				patsupdatePort.executeBatch();
			}


			patsDeleteResultD.setObject(1,Load.getResultNo());
			patsDeleteResultD.addBatch();
			patsDeleteResultD.executeBatch();



			int detCount =0;
			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}

			FrmMchBugD.DeleteMchWaste(connection, Load.getHeatPlanNo(),Load.getResultNo(), Load.getPlineCode(),gx,detCount);


			String DeleteHttControl = CoreXmlSqlParsersFactory.getSql("FrmMeasureResult.deleteHttControlLog");
			PreparedStatement pastDeleteHttControl = connection.prepareStatement(DeleteHttControl);

			pastDeleteHttControl.setObject(1, Load.getResultNo());
			pastDeleteHttControl.addBatch();
			pastDeleteHttControl.executeBatch();

			core.setV_errMsg("删除成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}
	public CoreReturnObject DoDelete(String loadStr,String gx, ArrayList<String> MatNos) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat1 = new SimpleDateFormat(
				"yyyyMM", Locale.CHINA);
		String systime1 = dateFormat1.format(now);
		PreparedStatement psta = null;

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(new Date());

		try {
			connection.setAutoCommit(false); // 取消自动提交事务

			String DeleteComInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.deleteVrpResult");// 删除公共信息
			PreparedStatement patsDeleteComInfo = connection
					.prepareStatement(DeleteComInfoSql);

			String DeleteResultSql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DeleteResult");// 删除实绩
			PreparedStatement patsDeleteResult = connection
					.prepareStatement(DeleteResultSql);


			String DeleteResultDSql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DeleteResultD");// 删除实绩
			PreparedStatement patsDeleteResultD = connection
					.prepareStatement(DeleteResultDSql);

			String updatePort = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updatePort1");// 更新管号队列
			PreparedStatement patsupdatePort = connection
					.prepareStatement(updatePort);

			String updateRtdData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
			PreparedStatement patsupdateRtdData = connection
					.prepareStatement(updateRtdData);

		/*	String updateMatData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatData1");// 更新库存数据  -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatData = connection
					.prepareStatement(updateMatData);*/

	/*		String updateMatBackData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMatBackData1");// 更新测量数据 -- 管先下线 再经过测量点
			PreparedStatement patsupdateMatBackData = connection
					.prepareStatement(updateMatBackData);*/

			String updateMeasureData = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.updateMeasureData");// 把原匹配的数据 还原成未剔除
			PreparedStatement patsupdateMeasureData = connection
					.prepareStatement(updateMeasureData);


			MchMeasureResultEntity Load = EntityTool.getEntityByJSONStr(loadStr,MchMeasureResultEntity.class);

			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					Load.getHeatPlanNo(),Load.getBatchNo());

			if(!pln.getPlineCode().equals(Load.getPlineCode())){
				core.setV_errMsg("不能删除其他产线生成的实绩！");
				return core;
			}


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			String LastRsNo = "";
			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.CheckResult");
			psta = connection.prepareStatement(sql);
			psta.setString(1, Load.getHeatPlanNo());
			psta.setString(2, Load.getBatchNo());
			rs = psta.executeQuery();
			if (rs.next()) {
				LastRsNo = rs.getString("RESULT_NO");
			}
			rs.close();
			psta.close();


			if(!LastRsNo.equals(Load.getResultNo())){
				core.setV_errMsg("请先删除最后维护的实绩！");
				return core;
			}


			patsDeleteComInfo.setObject(1,Load.getResultNo());
			patsDeleteComInfo.addBatch();
			patsDeleteComInfo.executeBatch();



			patsDeleteResult.setObject(1,Load.getResultNo());
			patsDeleteResult.addBatch();
			patsDeleteResult.executeBatch();

			int count = 0;

			if(StringTool.isInteger(Load.getProNum())){
				count = Integer.parseInt(Load.getProNum());
			}

			if(!Load.getFlag().equals("2")) {

				ArrayList<MchMeasureResultDEntity> Detail =
			    	getMchMeasureResultDEntity(Load.getResultNo(),connection);


				if (MatNos.size() != count) {
					core.setV_errMsg("找不到足够已匹配测量点数据的管！");
					return core;
				}


				for (int i = 0; i < count; i++) {

					String MatNo = Detail.size()>0 ?Detail.get(i).getMatNo(): MatNos.get(i);


					PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(Load.getHeatPlanNo(), MatNo, connection);

					if(port.getInstoreFlag().equals("1") && !port.getJugdeApplyCode().equals("")){
						core.setV_errMsg("存在管号已经送判,不允许删除测量点数据！");
						return core;
					}

					if (port == null || port.getTrFlag().equals("2")) {
						core.setV_errMsg("材料已经离线返线,不允许删除测量点实绩！");
						return core;
					}

					if (Load.getFlag().equals("0")) {

						RtdMeasurePosdataEntity RtdData = getRtdMeasurePosdataEntity1(connection, Load.getJudgeStoveNo(), Load.getBatchNo(), Load.getPlineCode(), MatNo);
						if (RtdData == null) {
							core.setV_errMsg("找不到已匹配的成品测量点数据,请检查是否已经手动剔除！");
							return core;
						}

						/*patsupdateMeasureData.setObject(1, "00");
						patsupdateMeasureData.setObject(2, Load.getJudgeStoveNo());
						patsupdateMeasureData.setObject(3, Load.getBatchNo());
						patsupdateMeasureData.setObject(4, MatNo);
						patsupdateMeasureData.addBatch();*/

						patsupdateRtdData.setObject(1, "");
						patsupdateRtdData.setObject(2, RtdData.getKeyId());
						patsupdateRtdData.addBatch();
					}


					patsupdateMeasureData.executeBatch();
					patsupdatePort.setObject(1, Load.getHeatPlanNo());
					patsupdatePort.setObject(2, MatNo);
					patsupdatePort.addBatch();




				/*	patsupdateMatData.setObject(1, MatNo);
					patsupdateMatData.addBatch();*/

				/*	patsupdateMatBackData.setObject(1, MatNo);
					patsupdateMatBackData.addBatch();*/
				}


				patsupdateRtdData.executeBatch();
				//	patsupdateMatData.executeBatch();
				//	patsupdateMatBackData.executeBatch();
				patsupdatePort.executeBatch();
			}


			patsDeleteResultD.setObject(1,Load.getResultNo());
			patsDeleteResultD.addBatch();
			patsDeleteResultD.executeBatch();



			int detCount =0;
			if(StringTool.isInteger(Load.getFailNum())){
				detCount = Integer.parseInt(Load.getFailNum());
			}

			FrmMchBugD.DeleteMchWaste(connection, Load.getHeatPlanNo(),Load.getResultNo(), Load.getPlineCode(),gx,detCount);


			String DeleteHttControl = CoreXmlSqlParsersFactory.getSql("FrmMeasureResult.deleteHttControlLog");
			PreparedStatement pastDeleteHttControl = connection.prepareStatement(DeleteHttControl);

			pastDeleteHttControl.setObject(1, Load.getResultNo());
			pastDeleteHttControl.addBatch();
			pastDeleteHttControl.executeBatch();

			core.setV_errMsg("删除成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject DoForceDelete(String HeatPlanNo,String JudgeStove, String BatchNo,String gx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat1 = new SimpleDateFormat(
				"yyyyMM", Locale.CHINA);
		String systime1 = dateFormat1.format(now);
		PreparedStatement psta = null;

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(new Date());

		try {
			connection.setAutoCommit(false); // 取消自动提交事务





			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					HeatPlanNo,BatchNo);

			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("MchComm.queryComInfo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, HeatPlanNo);
			psta.setString(2, BatchNo);
			psta.setString(3, "701311");
			ResultSet rs = psta.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}


			if (cutFalg.equals("2")) {
				core.setV_errMsg("下线已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta.close();
			//endregion


			sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.QueryResultNo");// 查询 工序是否是结炉状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, HeatPlanNo);
			psta.setString(2, BatchNo);
			 rs = psta.executeQuery();
			List<MchMeasureResultEntity> Results = EntityTool.getEntitysByRs(rs,
					MchMeasureResultEntity.class);
			rs.close();
			psta.close();


			for (MchMeasureResultEntity entity  : Results
				 ) {
				FrmMchBugD.DeleteMchWaste(connection, entity.getHeatPlanNo(),entity.getResultNo(), entity.getPlineCode(),gx,Integer.parseInt(entity.getFailNum()));
			}


			String DoForceDelete1Sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DoForceDelete1");
			PreparedStatement patsDoForceDelete1 = connection
					.prepareStatement(DoForceDelete1Sql);

			String DoForceDelete2Sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DoForceDelete2");
			PreparedStatement patsDoForceDelete2 = connection
					.prepareStatement(DoForceDelete2Sql);


			String DoForceDelete3Sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DoForceDelete3");
			PreparedStatement patsDoForceDelete3 = connection
					.prepareStatement(DoForceDelete3Sql);

			String DoForceDelete5Sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DoForceDelete5");
			PreparedStatement patsDoForceDelete5 = connection
					.prepareStatement(DoForceDelete5Sql);


			String DoForceDelete4Sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DoForceDelete4");
			PreparedStatement patsDoForceDelete4 = connection
					.prepareStatement(DoForceDelete4Sql);




			patsDoForceDelete1.setObject(1,HeatPlanNo);
			patsDoForceDelete1.setObject(2,BatchNo);
			patsDoForceDelete1.addBatch();
			patsDoForceDelete1.executeBatch();

			patsDoForceDelete2.setObject(1,JudgeStove);
			patsDoForceDelete2.setObject(2,BatchNo);
			patsDoForceDelete2.setObject(3,HeatPlanNo);
			patsDoForceDelete2.setObject(4,BatchNo);
			patsDoForceDelete2.addBatch();
			patsDoForceDelete2.executeBatch();

			patsDoForceDelete3.setObject(1,HeatPlanNo);
			patsDoForceDelete3.setObject(2,BatchNo);
			patsDoForceDelete3.addBatch();
			patsDoForceDelete3.executeBatch();

			patsDoForceDelete5.setObject(1,HeatPlanNo);
			patsDoForceDelete5.setObject(2,BatchNo);
			patsDoForceDelete5.addBatch();
			patsDoForceDelete5.executeBatch();


			patsDoForceDelete4.setObject(1,HeatPlanNo);
			patsDoForceDelete4.setObject(2,BatchNo);
			patsDoForceDelete4.addBatch();
			patsDoForceDelete4.executeBatch();



			core.setV_errMsg("清空成功！");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 每支材料对应的入库单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSqlDoc(Connection conn, String strlist, String memo)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat matter2 = new SimpleDateFormat("yyMMdd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmThreadingResult.getInListDoc");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, memo);
		psta.setString(2, matter2.format(dt));
		psta.setString(3, matter1.format(dt));
		psta.setString(4, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("INSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 每支材料对应的入库流水号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public String querySendSql(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("FrmThreadingResult.getInListDoc1");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("INSTOCK_SQE");
		}
		rs.close();
		psta.close();
		return seq;
	}


	public CoreReturnObject DoRemoveDbk(String HeatPlanID, String  ID){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false);

			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3Dbk(connection,
					HeatPlanID);


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}

			RtdMeasurePosdataEntity entity =   getRtdMeasurePosdataEntityByID(connection, ID);
			if(entity==null){
				core.setV_errMsg("找不到测量点数据,请刷新数据！");
				return core;
			}

			if(!entity.getFlag().equals("00")){
				core.setV_errMsg("该数据已经被剔除,请刷新数据！");
				return core;
			}




/*

			if(!entity.getMatNoCx().equals("")) {

				String updatePort = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.updatePort");// 更新管号队列
				PreparedStatement patsupdatePort = connection
						.prepareStatement(updatePort);

				String updateRtdData = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
				PreparedStatement patsupdateRtdData = connection
						.prepareStatement(updateRtdData);

				//找通用的材料号  是否有重复数据
				String sql = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.getRtdMeasurePosdataEntityAllMatNo");
				PreparedStatement ps = connection.prepareStatement(sql);
				ps.setObject(1, entity.getJudgeStoveNo());
				ps.setObject(2, entity.getBatchNo());
				ps.setObject(3, entity.getMatNo());
				ps.setObject(4, entity.getPlineCode());
				ps.setObject(5, entity.getKeyId());
				ResultSet rs = ps.executeQuery();
				List<RtdMeasurePosdataEntity> AllMatNo = EntityTool.getEntitysByRs(rs,
						RtdMeasurePosdataEntity.class);
				rs.close();
				ps.close();

				if (AllMatNo.size() > 0) {
					PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(HeatPlanID, entity.getMatNoCx(), connection);

					if (port.getInstoreFlag().equals("1")) {
						core.setV_errMsg("管号已经入库,请再下线界面先撤销交库!");
						return core;
					}

					if (AllMatNo.get(0).getActLen().startsWith(".")) {
						AllMatNo.get(0).setActLen("0" +AllMatNo.get(0).getActLen());
					}

					if (AllMatNo.get(0).getActWeight().startsWith(".")) {
						AllMatNo.get(0).setActWeight("0" + AllMatNo.get(0).getActWeight());
					}

					patsupdatePort.setObject(1, AllMatNo.get(0).getMatNo());
					patsupdatePort.setObject(2, AllMatNo.get(0).getActLen());
					patsupdatePort.setObject(3, AllMatNo.get(0).getActWeight());
					patsupdatePort.setObject(4, HeatPlanID);
					patsupdatePort.setObject(5, entity.getMatNoCx());
					patsupdatePort.addBatch();
					patsupdatePort.executeBatch();

					patsupdateRtdData.setObject(1,entity.getMatNoCx());
					patsupdateRtdData.setObject(2, AllMatNo.get(0).getKeyId());
					patsupdateRtdData.addBatch();
					patsupdateRtdData.executeBatch();


					entity.setMatNoCx("");

				} else {

*/
/*
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmMeasureResult.getRtdMeasurePosdataEntityLastAll");
					ps = connection.prepareStatement(sql);
					ps.setObject(1, entity.getJudgeStoveNo());
					ps.setObject(2, entity.getBatchNo());
					ps.setObject(3, entity.getMatNo());
					ps.setObject(4, entity.getPlineCode());
					rs = ps.executeQuery();
					List<RtdMeasurePosdataEntity> data = EntityTool.getEntitysByRs(rs,
							RtdMeasurePosdataEntity.class);
					rs.close();
					ps.close();



					if (data!=null && data.size()>0 ) {
						data.add(0,entity);

						if (!data.get(data.size() - 1).getMatNoCx().equals("")) {


						}else {
							entity.setMatNoCx("");

							for (int i = data.size() - 1; i > 0; i--) {
								if (!data.get(i - 1).getMatNoCx().equals("")) {
									PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(HeatPlanID, data.get(i - 1).getMatNoCx(), connection);

									if (port.getInstoreFlag().equals("1")) {
										core.setV_errMsg("管号已经入库,请再下线界面先撤销交库!");
										return core;
									}

									// 当前的测量点数据的匹配的产线材料号改成上一个数据的产销材料号
									patsupdateRtdData.setObject(1, data.get(i - 1).getMatNoCx());
									patsupdateRtdData.setObject(2, data.get(i).getKeyId());
									patsupdateRtdData.addBatch();

									if (data.get(i).getActLen().startsWith(".")) {
										data.get(i).setActLen("0" + data.get(i).getActLen());
									}

									if (data.get(i).getActWeight().startsWith(".")) {
										data.get(i).setActWeight("0" + data.get(i).getActWeight());
									}

									patsupdatePort.setObject(1, data.get(i).getMatNo());
									patsupdatePort.setObject(2, data.get(i).getActLen());
									patsupdatePort.setObject(3, data.get(i).getActWeight());
									patsupdatePort.setObject(4, HeatPlanID);
									patsupdatePort.setObject(5, data.get(i - 1).getMatNoCx());
									patsupdatePort.addBatch();

								}
							}
						}
					}else {

					}

					patsupdateRtdData.executeBatch();
					patsupdatePort.executeBatch();*//*

				}
			}
*/


			String UpdateFlagSql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.UpdateFlag");
			PreparedStatement patUpdateFlagSql = connection
					.prepareStatement(UpdateFlagSql);
			patUpdateFlagSql.setObject(1,"10");
			patUpdateFlagSql.setObject(2,entity.getMatNoCx());
			patUpdateFlagSql.setObject(3,ID);
			patUpdateFlagSql.addBatch();
			patUpdateFlagSql.executeBatch();


			core.setV_errMsg("成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}
	public CoreReturnObject DoRemove(String HeatPlanID, String  ID){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false);

			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					HeatPlanID);


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}

			RtdMeasurePosdataEntity entity =   getRtdMeasurePosdataEntityByID(connection, ID);
            if(entity==null){
				core.setV_errMsg("找不到测量点数据,请刷新数据！");
				return core;
			}

			if(!entity.getFlag().equals("00")){
				core.setV_errMsg("该数据已经被剔除,请刷新数据！");
				return core;
			}




/*

			if(!entity.getMatNoCx().equals("")) {

				String updatePort = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.updatePort");// 更新管号队列
				PreparedStatement patsupdatePort = connection
						.prepareStatement(updatePort);

				String updateRtdData = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
				PreparedStatement patsupdateRtdData = connection
						.prepareStatement(updateRtdData);

				//找通用的材料号  是否有重复数据
				String sql = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.getRtdMeasurePosdataEntityAllMatNo");
				PreparedStatement ps = connection.prepareStatement(sql);
				ps.setObject(1, entity.getJudgeStoveNo());
				ps.setObject(2, entity.getBatchNo());
				ps.setObject(3, entity.getMatNo());
				ps.setObject(4, entity.getPlineCode());
				ps.setObject(5, entity.getKeyId());
				ResultSet rs = ps.executeQuery();
				List<RtdMeasurePosdataEntity> AllMatNo = EntityTool.getEntitysByRs(rs,
						RtdMeasurePosdataEntity.class);
				rs.close();
				ps.close();

				if (AllMatNo.size() > 0) {
					PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(HeatPlanID, entity.getMatNoCx(), connection);

					if (port.getInstoreFlag().equals("1")) {
						core.setV_errMsg("管号已经入库,请再下线界面先撤销交库!");
						return core;
					}

					if (AllMatNo.get(0).getActLen().startsWith(".")) {
						AllMatNo.get(0).setActLen("0" +AllMatNo.get(0).getActLen());
					}

					if (AllMatNo.get(0).getActWeight().startsWith(".")) {
						AllMatNo.get(0).setActWeight("0" + AllMatNo.get(0).getActWeight());
					}

					patsupdatePort.setObject(1, AllMatNo.get(0).getMatNo());
					patsupdatePort.setObject(2, AllMatNo.get(0).getActLen());
					patsupdatePort.setObject(3, AllMatNo.get(0).getActWeight());
					patsupdatePort.setObject(4, HeatPlanID);
					patsupdatePort.setObject(5, entity.getMatNoCx());
					patsupdatePort.addBatch();
					patsupdatePort.executeBatch();

					patsupdateRtdData.setObject(1,entity.getMatNoCx());
					patsupdateRtdData.setObject(2, AllMatNo.get(0).getKeyId());
					patsupdateRtdData.addBatch();
					patsupdateRtdData.executeBatch();


					entity.setMatNoCx("");

				} else {

*/
/*
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmMeasureResult.getRtdMeasurePosdataEntityLastAll");
					ps = connection.prepareStatement(sql);
					ps.setObject(1, entity.getJudgeStoveNo());
					ps.setObject(2, entity.getBatchNo());
					ps.setObject(3, entity.getMatNo());
					ps.setObject(4, entity.getPlineCode());
					rs = ps.executeQuery();
					List<RtdMeasurePosdataEntity> data = EntityTool.getEntitysByRs(rs,
							RtdMeasurePosdataEntity.class);
					rs.close();
					ps.close();



					if (data!=null && data.size()>0 ) {
						data.add(0,entity);

						if (!data.get(data.size() - 1).getMatNoCx().equals("")) {


						}else {
							entity.setMatNoCx("");

							for (int i = data.size() - 1; i > 0; i--) {
								if (!data.get(i - 1).getMatNoCx().equals("")) {
									PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(HeatPlanID, data.get(i - 1).getMatNoCx(), connection);

									if (port.getInstoreFlag().equals("1")) {
										core.setV_errMsg("管号已经入库,请再下线界面先撤销交库!");
										return core;
									}

									// 当前的测量点数据的匹配的产线材料号改成上一个数据的产销材料号
									patsupdateRtdData.setObject(1, data.get(i - 1).getMatNoCx());
									patsupdateRtdData.setObject(2, data.get(i).getKeyId());
									patsupdateRtdData.addBatch();

									if (data.get(i).getActLen().startsWith(".")) {
										data.get(i).setActLen("0" + data.get(i).getActLen());
									}

									if (data.get(i).getActWeight().startsWith(".")) {
										data.get(i).setActWeight("0" + data.get(i).getActWeight());
									}

									patsupdatePort.setObject(1, data.get(i).getMatNo());
									patsupdatePort.setObject(2, data.get(i).getActLen());
									patsupdatePort.setObject(3, data.get(i).getActWeight());
									patsupdatePort.setObject(4, HeatPlanID);
									patsupdatePort.setObject(5, data.get(i - 1).getMatNoCx());
									patsupdatePort.addBatch();

								}
							}
						}
					}else {

					}

					patsupdateRtdData.executeBatch();
					patsupdatePort.executeBatch();*//*

				}
			}
*/


			String UpdateFlagSql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.UpdateFlag");
			PreparedStatement patUpdateFlagSql = connection
					.prepareStatement(UpdateFlagSql);
			patUpdateFlagSql.setObject(1,"10");
			patUpdateFlagSql.setObject(2,entity.getMatNoCx());
			patUpdateFlagSql.setObject(3,ID);
			patUpdateFlagSql.addBatch();
			patUpdateFlagSql.executeBatch();


			core.setV_errMsg("成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject DoRecover(String HeatPlanID, String  ID){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false);

			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					HeatPlanID);


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}

			RtdMeasurePosdataEntity entity =   getRtdMeasurePosdataEntityByID(connection, ID);
			if(entity==null){
				core.setV_errMsg("找不到测量点数据,请刷新数据！");
				return core;
			}

			if(!entity.getFlag().equals("10")){
				core.setV_errMsg("该数据未剔除,请刷新数据！");
				return core;
			}

			if(!entity.getMatNoCx().equals("")) {

				String NewMatNo = "";
				String sql = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.GetExitsRtdData");
				psta = connection.prepareStatement(sql);
				psta.setString(1, entity.getMatNoCx());
				ResultSet rs = psta.executeQuery();
				if (rs.next()) {
					NewMatNo = rs.getString("MAT_NO");
				}

				if(!NewMatNo.equals("")){
					core.setV_errMsg("改管号已经重新匹配新的喷印管号,不允许恢复剔除！");
					return core;
				}

			}


			String UpdateFlagSql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.UpdateFlag");
			PreparedStatement patUpdateFlagSql = connection
					.prepareStatement(UpdateFlagSql);
			patUpdateFlagSql.setObject(1,"00");
			patUpdateFlagSql.setObject(2,entity.getMatNoCx());
			patUpdateFlagSql.setObject(3,ID);
			patUpdateFlagSql.addBatch();
			patUpdateFlagSql.executeBatch();

/*
			//找通用的材料号  是否有重复数据
			String sql = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.getRtdMeasurePosdataEntityAllMatNo");
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setObject(1, entity.getJudgeStoveNo());
			ps.setObject(2, entity.getBatchNo());
			ps.setObject(3, entity.getMatNo());
			ps.setObject(4, entity.getPlineCode());
			ps.setObject(5, entity.getKeyId());
			ResultSet rs = ps.executeQuery();
			List<RtdMeasurePosdataEntity> AllMatNo = EntityTool.getEntitysByRs(rs,
					RtdMeasurePosdataEntity.class);
			rs.close();
			ps.close();


			if(AllMatNo==null || AllMatNo.size()<=0){
					sql = CoreXmlSqlParsersFactory
							.getSql("FrmMeasureResult.getRtdMeasurePosdataEntityLastAll");
					ps = connection.prepareStatement(sql);
					ps.setObject(1, entity.getJudgeStoveNo());
					ps.setObject(2, entity.getBatchNo());
					ps.setObject(3, entity.getMatNo());
				ps.setObject(4, entity.getPlineCode());
					rs = ps.executeQuery();
					List<RtdMeasurePosdataEntity> data = EntityTool.getEntitysByRs(rs,
							RtdMeasurePosdataEntity.class);
					rs.close();
					ps.close();



					if (data!=null && data.size()>0 && !data.get(0).getMatNoCx().equals("") ) {

						String updatePort = CoreXmlSqlParsersFactory
								.getSql("FrmMeasureResult.updatePort");// 更新管号队列
						PreparedStatement patsupdatePort = connection
								.prepareStatement(updatePort);

						String updateRtdData = CoreXmlSqlParsersFactory
								.getSql("FrmMeasureResult.updateRtdData");// 更新测量数据
						PreparedStatement patsupdateRtdData = connection
								.prepareStatement(updateRtdData);


						data.add(0,entity);



						for (int i = 0; i < data.size() ; i++) {

							   if(i==data.size()-1 || data.get(i+1).getMatNoCx().equals("")){
								   patsupdateRtdData.setObject(1, "");
								   patsupdateRtdData.setObject(2, data.get(i).getKeyId());
								   patsupdateRtdData.addBatch();
								   break;
							   }

							 PortMchBatchSampleResultEntity port = PlanService.getPortMchBatchSampleResultEntity(HeatPlanID, data.get(i + 1).getMatNoCx(), connection);

								if (port.getInstoreFlag().equals("1")) {
									core.setV_errMsg("管号已经入库,请再下线界面先撤销交库!");
									return core;
								}


								patsupdateRtdData.setObject(1, data.get(i + 1).getMatNoCx());
								patsupdateRtdData.setObject(2, data.get(i).getKeyId());
								patsupdateRtdData.addBatch();

								if (data.get(i).getActLen().startsWith(".")) {
									data.get(i).setActLen("0" + data.get(i).getActLen());
								}

								if (data.get(i).getActWeight().startsWith(".")) {
									data.get(i).setActWeight("0" + data.get(i).getActWeight());
								}

								patsupdatePort.setObject(1, data.get(i).getMatNo());
								patsupdatePort.setObject(2, data.get(i).getActLen());
								patsupdatePort.setObject(3, data.get(i).getActWeight());
								patsupdatePort.setObject(4, HeatPlanID);
								patsupdatePort.setObject(5, data.get(i + 1).getMatNoCx());
								patsupdatePort.addBatch();


						}

						patsupdateRtdData.executeBatch();
						patsupdatePort.executeBatch();


				}


				}*/


			core.setV_errMsg("成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}

	public CoreReturnObject DoAddMeasureDataDbk(String EntityStr, String  HeatPlanID){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false);

			RtdMeasurePosdataEntity entity =    EntityTool.getEntityByJSONStr(EntityStr,RtdMeasurePosdataEntity.class);
			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3Dbk(connection,
					HeatPlanID,entity.getBatchNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}



			entity.setProOrderNo(pln.getOrderNo() + "/" + pln.getOrderSeq());
			entity.setPlineCode(pln.getPlineCode());
			entity.setPlineName(pln.getPlineName());

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DoAddMeasureData");

			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, entity);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();
			patAdd.executeBatch();

			core.setV_errMsg("新增成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}
	public CoreReturnObject DoAddMeasureData(String EntityStr, String  HeatPlanID){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false);

			RtdMeasurePosdataEntity entity =    EntityTool.getEntityByJSONStr(EntityStr,RtdMeasurePosdataEntity.class);
			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					HeatPlanID,entity.getBatchNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}



			entity.setProOrderNo(pln.getOrderNo() + "/" + pln.getOrderSeq());
			entity.setPlineCode(pln.getPlineCode());
			entity.setPlineName(pln.getPlineName());

			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.DoAddMeasureData");

			sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, entity);
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.addBatch();
			patAdd.executeBatch();

			core.setV_errMsg("新增成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject DoAddMeasureData(ArrayList<String> EntityStrs, String  HeatPlanID){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		String systime = dateFormat.format(now);

		try {
			connection.setAutoCommit(false);

			RtdMeasurePosdataEntity entityFirst =    EntityTool.getEntityByJSONStr(EntityStrs.get(0),RtdMeasurePosdataEntity.class);
			// 获取加工计划实体了
			PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
					HeatPlanID,entityFirst.getBatchNo());


			//region 检查
			if (pln == null){
				core.setV_errMsg("找不到炉计划数据！");
				return core;
			}

			if (!pln.getPlanstatus().equals("20")) {
				core.setV_errMsg("炉计划不处于生产中状态！");
				return core;
			}


			for (String EntityStr :EntityStrs
				 ) {



				RtdMeasurePosdataEntity entity = EntityTool.getEntityByJSONStr(EntityStr, RtdMeasurePosdataEntity.class);

				entity.setProOrderNo(pln.getOrderNo() + "/" + pln.getOrderSeq());
				entity.setPlineCode(pln.getPlineCode());
				entity.setPlineName(pln.getPlineName());

				String sqlAdd = CoreXmlSqlParsersFactory
						.getSql("FrmMeasureResult.DoAddMeasureData");

				sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, entity);
				PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
				patAdd.addBatch();
				patAdd.executeBatch();
				patAdd.close();
			}

			core.setV_errMsg("新增成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}

	public Date GetDateByStr(String Data) throws ParseException {
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat matter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat matter3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		SimpleDateFormat matter4 = new SimpleDateFormat("yyyy/MM/dd HH:mm");

		if(Data.split(":").length==2){
			if(Data.contains("/")){
				return  matter4.parse(Data);
			}else{
				return  matter2.parse(Data);
			}
		}
		else{
			if(Data.contains("/")){
				return  matter3.parse(Data);
			}else{
				return  matter1.parse(Data);
			}
		}


	}



	public CoreReturnObject Turn(Double PerWt, Double em, String  JudgeStoveNo, String BatchNo, String PlineCode){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();

		try {
			connection.setAutoCommit(false);


			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.Turn");

			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.setObject(1, PerWt);
			patAdd.setObject(2, em);
			patAdd.setObject(3, JudgeStoveNo);
			patAdd.setObject(4, BatchNo);
			patAdd.setObject(5, PlineCode);
			patAdd.addBatch();
			patAdd.executeBatch();

			core.setV_errMsg("操作成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}


	public CoreReturnObject Revert(String  JudgeStoveNo, String BatchNo, String PlineCode){
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();

		try {
			connection.setAutoCommit(false);


			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("FrmMeasureResult.Revert");

			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
			patAdd.setObject(1, JudgeStoveNo);
			patAdd.setObject(2, BatchNo);
			patAdd.setObject(3, PlineCode);
			patAdd.addBatch();
			patAdd.executeBatch();

			core.setV_errMsg("操作成功");
			core.setResult(1);
			connection.commit();
		}
		catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				}
				catch (Exception e2) {
				}
			}
		}
		finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			}
			catch (Exception e2) {
			}
		}
		return core;
	}

}
