package com.steering.mes.zgmil.coup;

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

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

import com.steering.common.EntityTool;
import com.steering.common.FmisTool;
import com.steering.common.StringTool;
import com.steering.mes.signature.MeanValueCalculation;
import com.steering.mes.zgmil.entity.FeedRowPlan;
import com.steering.mes.zgmil.entity.MilPlanEntity5;

/**
 * 上料
 * 
 * @author Administrator
 * 
 */
public class FrmFeedLoad extends DBCommon {

	/**
	 * 查询计划信息
	 * 
	 * @param judgeStoveNo
	 * @param lotNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryData(ArrayList<String> parm,
			String[] plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedLoad.doQueryData2");

		String strsql = "";
		if (!parm.get(0).equals("")) {
			strsql += " AND T.JUDGE_STOVE_NO = '" + parm.get(0) + "'";
		}
		if (!parm.get(1).equals("")) {
			strsql += " AND T.ZY_BATCH_ID = '" + parm.get(1) + "'";
		}
		if (!parm.get(2).equals("")) {
			strsql += " AND T.IS_LOAD = '" + parm.get(2) + "'";
		}
		if (!parm.get(3).equals("")) {
			strsql += " AND T.FEED_HEAT_NO = '" + parm.get(3) + "'";
		}
		if(!parm.get(4).equals("") ){
			strsql+= "  AND T2.START_TIME BETWEEN TO_DATE('" + parm.get(4)
					+ "','YYYY-MM-DD HH24:MI:SS') " + " AND TO_DATE('"
					+ parm.get(5) + "','YYYY-MM-DD HH24:MI:SS') ";
		}
		strsql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		sql = sql.replace("#SomeThing#", strsql);
		return this.getData(sql);
	}

	/***
	 * 查询判定信息
	 * 
	 * @param judgeStoveNo
	 * @param jugdeApplyCode
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryApplyData(String judgeStoveNo,
			String jugdeApplyCode, String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedLoad.doQueryApplyData");
		return this.getData(sql, new Object[] { jugdeApplyCode, judgeStoveNo });
	}

	/**
	 * 查询上料实绩信息
	 * 
	 * @param judgeStoveNo
	 * @param lotNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryResultData(String judgeStoveNo,
			String lotNo, String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedLoad.doQueryResultData");
		return this.getData(sql,
				new Object[] { judgeStoveNo, lotNo, plineCode });
	}

	/***
	 * 查询上料行信息
	 * 
	 * @param judgeStoveNo
	 * @param lotNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryMatData(String heatPlanNo, String plineCode,
			String isLoad, String PRO_PLAN_ID, String GX_PLAN_NO, String[] arr) {
		String sql = "";
		sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.doQueryMatData");
		return this.getData(sql, new Object[] { heatPlanNo, plineCode,
				PRO_PLAN_ID, GX_PLAN_NO });
	}

	/**
	 * 查询库存信息
	 * 
	 * @param judgeStoveNo
	 * @return
	 */
	public CoreReturnObject doQueryMatData2(String judgeStoveNo,
			String PRO_PLAN_ID, String GX_PLAN_NO, String isLoad) {
		String sql = "";
		// if (isLoad.equals("0")) {
		sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.doQueryMatData2"); // 未上料
		/*
		 * } else { sql = CoreXmlSqlParsersFactory
		 * .getSql("FrmFeedLoad.doQueryMatDataAfter2"); // 已上料 }
		 */

		return this.getData(sql, new Object[] { judgeStoveNo, PRO_PLAN_ID,
				GX_PLAN_NO });
	}

	/**
	 * 根据上料行feedRowNo查询些上料行是否已存在
	 * 
	 * @param judgeStoveNo
	 * @param lotNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryResultDataByFeedRowNo(String feedRowNo,
			String judgeStoveNo, String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("FrmFeedLoad.doQueryResultDataByFeedRowNo");
		return this.getData(sql, new Object[] { judgeStoveNo, feedRowNo,
				plineCode });
	}

	/**
	 * 新增
	 * 
	 * @param milPlanEntity
	 * @return
	 */
	public CoreReturnObject add(String milPlanEntityString) {
		MilPlanEntity5 milPlanEntity = EntityTool.getEntityByJSONStr(
				milPlanEntityString, MilPlanEntity5.class);
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.add");
		sql = EntityTool.fillSqlByEntity(sql, milPlanEntity);
		return this.setData(sql);
	}

	/**
	 * 删除
	 * 
	 * @param feedRowNo
	 * @param judgeStoveNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject delete(String feedRowNo, String judgeStoveNo,
			String plineCode) {
		String sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.delete");
		return this.setData(sql, new Object[] { judgeStoveNo, feedRowNo,
				plineCode });
	}

	/**
	 * 上料确认
	 * 
	 * @param judgeStoveNo
	 * @param plineCode
	 * @param bc
	 * @param bz
	 * @param user
	 * @return
	 */
	public CoreReturnObject node(ArrayList<ArrayList> parm,
			ArrayList<String> parm2, String[] arr, FeedRowPlan FeedRowPlan1) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		PreparedStatement psta = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		String ky = ""; // 是否跨月
		String sql = "";
		int seq = 1;
		int acount = 0;
		double inPutWgt = 0;// 上料量
		int inPutNum = 0;// 总支数
		int m = 0;
		String plineName = "";
		String endTime = "";
		ResultSet rs = null;
		ResultSet rs2 = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			String sql1 = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.node"); // 管坯出库
			sql1 = sql1.replace("#Something#", CoreConverter
					.toSqlConditionString("T2.STORAGE_NO", arr));
			PreparedStatement updateFurnaceReslut = connection
					.prepareStatement(sql1);
			String sql2 = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.updateZGC");// 修改轧管计划表上料支、吨
			PreparedStatement updateZGC = connection.prepareStatement(sql2);

			String sql3 = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.updateState");// 修改轧管计划表状态/删除不要的轧管上料行
			PreparedStatement updateState = connection.prepareStatement(sql3);

			String sql4 = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.deleteProcess");// 进程表多余数据清理
			PreparedStatement deleteProcess = connection.prepareStatement(sql4);

			String insertProcessSql = CoreXmlSqlParsersFactory
					.getSql("FeedResult.insertProcess");// 上料生成进程管号队列
			PreparedStatement insertProcessPsta = connection
					.prepareStatement(insertProcessSql);

			// 等更新使用
			/*
			 * String sql4 = CoreXmlSqlParsersFactory
			 * .getSql("FrmFeedLoad.updateBatchNo");// C的把性能批号默认： 检验委托号 +999
			 * PreparedStatement updateBatchNo =
			 * connection.prepareStatement(sql4);
			 */
			
			yearMoth = StringTool.GetBalYear(now,StringTool.GetSupplyUnit(parm2.get(3)));// 改25号结转
			String yearMoth2 = new SimpleDateFormat("yyyyMM").format(now);
			if (Integer.parseInt(yearMoth) > Integer.parseInt(yearMoth2)) {
				ky = "1";
			}
			// int result = FeedSawResult.comPareTime(systime,
			// new SimpleDateFormat("yyyy-MM-dd").format(now)
			// + " 21:00:00");
			// if (result > 0) {
			// yearMoth = getBalYearMoth(systime);
			// ky = isky(systime);
			// }

			for (ArrayList<String> aList : parm) {
				String judgeStoveNo = aList.get(0);
				String JUGDE_APPLY_CODE = aList.get(1);
				int PLAN_INDOUBLE_NUM = Integer.parseInt(aList.get(2));// 上料支
				int FIX_NUM = Integer.parseInt(aList.get(3));// 倍尺数
				String CUT_TARGET = aList.get(8); // 切损去向
				double CUT_LENGTH = Double.parseDouble(aList.get(9)); // 切损长度
				double CUT_WEIGHT = Double.parseDouble(aList.get(10)); // 切损重量
				int planNum = Integer.parseInt(aList.get(11));// 计划上料支

				sql = CoreXmlSqlParsersFactory
						.getSql("FrmFeedLoad.SelectApplyStatus");// 查询送判状态
				psta = connection.prepareStatement(sql);

				psta.setString(1, JUGDE_APPLY_CODE);
				psta.setString(2, judgeStoveNo);
				psta.setString(3, judgeStoveNo);

				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if (acount == 0) {
					cro.setV_errMsg("炉号'" + parm2.get(0) + "'未判定且没有紧急放行,不能上料!");
					return cro;
				}

				acount = 0;
				sql = CoreXmlSqlParsersFactory
						.getSql("FrmFeedLoad.SelectMilPlanIsHave");// 查询轧管炉号是否已生产
				psta = connection.prepareStatement(sql);
				psta.setString(1, parm2.get(0));
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACOUNT"));
					plineName = rs.getString("PLINE_NAME").toString();
					endTime = rs.getString("END_TIME").toString();
				}
				rs.close();
				psta.close();
				if (acount > 0) {
					cro.setV_errMsg("炉号【" + parm2.get(0) + "】已于" + endTime
							+ "在" + plineName + "生产，请退回计划换个炉号再轧!");
					return cro;
				}

				acount = 0;
				sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.SelectKCZS");// 查询库存支数
				sql += CoreConverter.toSqlConditionString("T1.STORAGE_NO", arr);
				psta = connection.prepareStatement(sql);
				psta.setString(1, judgeStoveNo);
				psta.setString(2, aList.get(4));
				psta.setString(3, aList.get(5));
				psta.setString(4, aList.get(6));
				psta.setString(5, JUGDE_APPLY_CODE);
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACT_COUNT"));
				}
				rs.close();
				psta.close();
				if (Integer.parseInt(aList.get(2)) > acount) {
					cro.setV_errMsg("炉号'" + parm2.get(0) + "'库存不够!");
					return cro;
				}

				acount = 0;
				sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.SelectTCZS");// 查询套尺原炉号没上料，套尺不能上料
				psta = connection.prepareStatement(sql);
				psta.setString(1, judgeStoveNo);
				psta.setString(2, aList.get(4));
				psta.setString(3, aList.get(5));
				psta.setString(4, aList.get(6));
				psta.setString(5, JUGDE_APPLY_CODE);
				rs = psta.executeQuery();
				if (rs.next()) {
					acount = Integer.parseInt(rs.getString("ACT_COUNT"));
				}
				rs.close();
				psta.close();
				if (acount > 0) {
					cro.setV_errMsg("套尺原炉号没上料，套尺不能上料!");
					return cro;
				}

				updateFurnaceReslut.setString(1, judgeStoveNo);
				updateFurnaceReslut.setString(2, JUGDE_APPLY_CODE);
				updateFurnaceReslut.setInt(3, PLAN_INDOUBLE_NUM); // 实际上料支
				updateFurnaceReslut.setInt(4, FIX_NUM);
				updateFurnaceReslut.setString(5, parm2.get(3));
				updateFurnaceReslut.setString(6, aList.get(4));
				updateFurnaceReslut.setString(7, aList.get(5));
				updateFurnaceReslut.setString(8, aList.get(6));
				updateFurnaceReslut.setString(9, parm2.get(10));
				updateFurnaceReslut.setString(10, yearMoth);
				updateFurnaceReslut.setString(11, ky);
				updateFurnaceReslut.setString(12, parm2.get(4)); // 出库班次
				updateFurnaceReslut.setString(13, parm2.get(5)); // 出库班组
				updateFurnaceReslut.setString(14, parm2.get(6)); // 操作人
				updateFurnaceReslut.setString(15, systime); // 系统时间
				updateFurnaceReslut.setString(16, CUT_TARGET); // 切损去向
				updateFurnaceReslut.setDouble(17, CUT_LENGTH); // 切损长度
				updateFurnaceReslut.setDouble(18, CUT_WEIGHT); // 切损重量
				updateFurnaceReslut.setInt(19, planNum); // 计划上料支
				updateFurnaceReslut.addBatch();

				updateZGC.setString(1, judgeStoveNo);
				updateZGC.setString(2, JUGDE_APPLY_CODE);
				updateZGC.setInt(3, PLAN_INDOUBLE_NUM);
				updateZGC.setInt(4, FIX_NUM);
				updateZGC.setString(5, parm2.get(1));
				updateZGC.setString(6, parm2.get(2));
				updateZGC.setString(7, aList.get(7));
				updateZGC.setDouble(8, CUT_LENGTH);// 切损长度
				updateZGC.addBatch();
			}

			updateState.setString(1, parm2.get(0));
			updateState.setString(2, parm2.get(1));
			updateState.setString(3, parm2.get(2));
			updateState.setString(4, parm2.get(3));
			updateState.setString(5, parm2.get(4));
			updateState.setString(6, parm2.get(5));
			updateState.setString(7, parm2.get(6));
			updateState.setString(8, parm2.get(7));
			updateState.setString(9, parm2.get(8));
			updateState.setString(10, parm2.get(8));
			updateState.setString(11, parm2.get(9));
			updateState.setString(12, yearMoth);
			updateState.addBatch();

			deleteProcess.setString(1, FeedRowPlan1.getResultNo());
			deleteProcess.setString(2, parm2.get(0));
			deleteProcess.addBatch();

			updateFurnaceReslut.executeBatch();
			updateZGC.executeBatch();
			updateState.executeBatch();
			deleteProcess.executeBatch();

			// 上料生成进程管号队列
			sql = CoreXmlSqlParsersFactory.getSql("FeedResult.SelectFeedNum");
			sql = EntityTool.fillSqlByEntity(sql, FeedRowPlan1);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			List<FeedRowPlan> list = EntityTool.getEntitysByRs(rs,
					FeedRowPlan.class);
			rs.close();
			psta.close();
			sql = CoreXmlSqlParsersFactory
					.getSql("FeedResult.SelectFeedToalWgt");
			sql = EntityTool.fillSqlByEntity(sql, FeedRowPlan1);
			psta = connection.prepareStatement(sql);
			rs2 = psta.executeQuery();
			if (rs2.next()) {
				inPutWgt = rs2.getDouble("MAT_TOTAL_HEIGHT");
			}
			rs2.close();
			psta.close();

			sql = CoreXmlSqlParsersFactory
					.getSql("FeedResult.SelectFeedToalNum");
			sql = EntityTool.fillSqlByEntity(sql, FeedRowPlan1);
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				inPutNum = Integer.parseInt(rs.getString("ACT_COUNT"));
			}
			rs.close();
			psta.close();

			List<double[]> list1 = MeanValueCalculation.MeanCalculation(
					inPutWgt, inPutNum);// 分配上料重量

			ArrayList<String> wgtList = new ArrayList<String>();
			List<double[]> lenList = MeanValueCalculation.MeanCalculation(
					inPutWgt, inPutNum);
			for (int k = 0; k < lenList.size(); k++) {
				double[] conLen = lenList.get(k);
				if ((int) conLen[0] > 0) {
					for (int x = 0; x < (int) conLen[0]; x++) {
						DecimalFormat dt = new DecimalFormat("0.000");
						wgtList.add(dt.format(conLen[1]));
					}
				}
			}

			String mMatNo = queryMatNoM(connection, FeedRowPlan1
					.getJudgeStoveNo(), FeedRowPlan1.getLotNo(), FeedRowPlan1
					.getResultNo()); // 母坯材料号
			int g = Integer.parseInt(mMatNo.substring(mMatNo.length() - 3,
					mMatNo.length()));

			for (int k = 0; k < list.size(); k++) {
				FeedRowPlan feedPlan = list.get(k);
				for (int s = 0; s < Integer.parseInt(feedPlan.getFeedNum()); s++) {
					/*
					 * // arr = feedPlan.getPipeSize().split("[*]"); strWgt =
					 * 0.02466 Double.parseDouble(arr[1])
					 * (Double.parseDouble(arr[0]) - Double
					 * .parseDouble(arr[1]))
					 * Double.parseDouble(feedPlan.getTargetLength()) / 1000;
					 */
					for (int i = 1; i < Integer.parseInt(feedPlan.getDbNum()) + 1; i++) {
						for (int j = 1; j < Integer.parseInt(feedPlan
								.getSawNum()) + 1; j++) {
							// 上料生成进程管号队列
							insertProcessPsta.setString(1, FeedRowPlan1
									.getResultNo());
							insertProcessPsta.setString(2, FeedRowPlan1
									.getLotNo());
							insertProcessPsta.setString(3, FeedRowPlan1
									.getStoveNo());
							insertProcessPsta.setString(4, FeedRowPlan1
									.getJudgeStoveNo());
							insertProcessPsta.setString(5, FeedRowPlan1
									.getPlineCode());
							insertProcessPsta.setString(6, String.valueOf(seq));
							insertProcessPsta.setString(7, (FeedRowPlan1
									.getJudgeStoveNo().replaceFirst("-.*", ""))
									+ StringTool.formatDecimal(g, "000")
									+ i
									+ j);// 子管材料号
							insertProcessPsta.setString(8, "");// 定尺长度
							// new DecimalFormat("0.000").format(strWgt)
							insertProcessPsta.setString(9, "");// 重量
							insertProcessPsta.setString(10, systime);
							insertProcessPsta.setString(11, (FeedRowPlan1
									.getJudgeStoveNo().replaceFirst("-.*", ""))
									+ StringTool.formatDecimal(g, "000") + i);// 母管材料号
							insertProcessPsta.setString(12, (FeedRowPlan1
									.getJudgeStoveNo().replaceFirst("-.*", ""))
									+ StringTool.formatDecimal(g, "000"));// 母坯材料号
							insertProcessPsta.setString(13, String
									.valueOf(wgtList.get(m)));// 投料量
							insertProcessPsta.setString(14, yearMoth);
							insertProcessPsta.setString(15, FeedRowPlan1
									.getProPlanId()); // 轧管订单编号
							insertProcessPsta.setString(16, FeedRowPlan1
									.getGxPlanNo()); // 工序排产序号

							insertProcessPsta.addBatch();
							seq++;
							m++;
						}
					}
					g++;
				}
			}

			// 等待更新使用
			/*
			 * if ("C".equals(FeedRowPlan1.getCutTarget())) {
			 * updateBatchNo.setString(1, FeedRowPlan1.getResultNo());
			 * updateBatchNo.setString(2, FeedRowPlan1.getJudgeStoveNo());
			 * updateBatchNo.setString(3, FeedRowPlan1.getPlineCode());
			 * updateBatchNo.setString(4, FeedRowPlan1.getProPlanId());
			 * updateBatchNo.setString(5, FeedRowPlan1.getGxPlanNo());
			 * updateBatchNo.addBatch(); }
			 */

			insertProcessPsta.executeBatch();
			// updateBatchNo.executeBatch();
			// 更新运输需求上料时间
//			TZH 24.4.24 注销物流接口
//			updateTmsDemandMLoadTime(connection, connection2, cro, FeedRowPlan1
//					.getJudgeStoveNo(), FeedRowPlan1.getResultNo());
			if (cro.getV_errMsg().equals("")) {
				cro.setV_errMsg("上料确认成功！");
			}
			// 财务系统接口数据处理
			// FmisTool.addGpOutList(connection,cro,FeedRowPlan1.getResultNo());

			connection.commit();
//			TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
//					TZH 24.4.24 注销物流接口
//					connection2.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
//			TZH 24.4.24 注销物流接口
//			if (connection2 != null) {
//				try {
//					connection.rollback(); // 回滚事务
//					connection2.rollback(); // 回滚事务
//				} catch (Exception e2) {
//				}
//			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
//				TZH 24.4.24 注销物流接口
//				if (connection2 != null && !connection2.isClosed()) {
//					connection2.close();
//				}
			} catch (Exception e2) {
			}
		}

		return cro;
	}

	/**
	 * 上料回退
	 * 
	 * @param judgeStoveNo
	 * @param plineCode
	 * @param bc
	 * @param bz
	 * @param user
	 * @return
	 */
	public CoreReturnObject nodeBack(ArrayList<ArrayList> parm,
			ArrayList<String> parm2) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
//		TZH 24.4.24 注销物流接口
//		Connection connection2 = this.getDao("TmsDao").getConnection();
		PreparedStatement psta = null;
		String sql = "";
		ResultSet rs = null;
		int acount = 0;
		String BAL_YEAR_MONTH1 = "";
		String BAL_YEAR_MONTH2 = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
//			TZH 24.4.24 注销物流接口
//			connection2.setAutoCommit(false); // 取消自动提交事务
			String sql1 = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.nodeBack");
			PreparedStatement updateFurnaceReslut = connection
					.prepareStatement(sql1);

			String sql2 = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.updateStateBack");// 修改轧管计划表状态
			PreparedStatement updateStateBack = connection
					.prepareStatement(sql2);

			String sql3 = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.deletePortMil");// 删除进程表管号队列
			PreparedStatement deletePortMil = connection.prepareStatement(sql3);

			String sql4 = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.deleteFmis");// 删除财务系统接口数据
			PreparedStatement deleteFmis = connection.prepareStatement(sql4);

			// 财务系统接口数据处理
			// FmisTool.deleteGpOutList(connection,cro,parm2.get(4));
			String SYNC_FLAG = "0";
			sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.SelectSyncFlag");// 查询财务系统同步状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm2.get(4));
			rs = psta.executeQuery();
			if (rs.next()) {
				SYNC_FLAG = rs.getString("SYNC_FLAG");
			}
			rs.close();
			psta.close();
			if (SYNC_FLAG.equals("1")) {
				cro.setV_errMsg("财务系统同步坯料出库信息，请联系财务撤销同步信息!");
				return cro;
			}
			//删除财务系统接口数据
			deleteFmis.setString(1, parm.get(0).get(0).toString());
			deleteFmis.setString(2, parm2.get(1));
			deleteFmis.setString(3, parm2.get(2));
			deleteFmis.addBatch();
			deleteFmis.executeBatch();

			sql = CoreXmlSqlParsersFactory.getSql("FrmFeedLoad.SelectStatus");// 查询生产状态
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm2.get(0));
			psta.setString(2, parm2.get(1));
			psta.setString(3, parm2.get(2));
			psta.setString(4, parm2.get(3));
			rs = psta.executeQuery();
			if (rs.next()) {
				acount = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			psta.close();
			if (acount > 0) {
				cro.setV_errMsg("炉号'" + parm2.get(0) + "'已生产，不能上料回退!");
				return cro;
			}
			acount = 0;
			/*sql = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.SelectGetSample");// 查询是否取样
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm2.get(0));
			psta.setString(2, parm2.get(1));
			psta.setString(3, parm2.get(2));
			psta.setString(4, parm2.get(3));
			rs = psta.executeQuery();
			if (rs.next()) {
				acount = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			psta.close();
			if (acount > 0) {
				cro.setV_errMsg("炉号'" + parm2.get(0) + "'已取样委托，不能上料回退!");
				return cro;
			}*/

			sql = CoreXmlSqlParsersFactory
					.getSql("FrmFeedLoad.SelectBalYearMonth");// 查询结转年月，判断是否已结转
			psta = connection.prepareStatement(sql);
			psta.setString(1, parm2.get(4));
			rs = psta.executeQuery();
			if (rs.next()) {
				BAL_YEAR_MONTH1 = rs.getString("BAL_YEAR_MONTH");
				BAL_YEAR_MONTH2 = rs.getString("BAL_YEAR_MONTH2");
			}
			rs.close();
			psta.close();
			if (Integer.parseInt(BAL_YEAR_MONTH1) < Integer
					.parseInt(BAL_YEAR_MONTH2)) {
				cro.setV_errMsg("已结转，不能上料回退!");
				return cro;
			}

			for (ArrayList<String> aList : parm) {
				String judgeStoveNo = aList.get(0);
				String JUGDE_APPLY_CODE = aList.get(1);
				int PLAN_INDOUBLE_NUM = Integer.parseInt(aList.get(2));
				int FIX_NUM = Integer.parseInt(aList.get(3));

				String CUT_TARGET = aList.get(7); // 切损去向
				double CUT_LENGTH = Double.parseDouble(aList.get(8)); // 切损长度
				double CUT_WEIGHT = Double.parseDouble(aList.get(9)); // 切损重量

				// String plineCode = (String) aList.get(4);
				updateFurnaceReslut.setString(1, judgeStoveNo);
				updateFurnaceReslut.setString(2, JUGDE_APPLY_CODE);
				updateFurnaceReslut.setInt(3, PLAN_INDOUBLE_NUM);
				updateFurnaceReslut.setInt(4, FIX_NUM);
				updateFurnaceReslut.setString(5, parm2.get(3));
				updateFurnaceReslut.setString(6, aList.get(4));
				updateFurnaceReslut.setString(7, aList.get(5));
				updateFurnaceReslut.setString(8, aList.get(6));

				updateFurnaceReslut.setString(9, CUT_TARGET); // 切损去向
				updateFurnaceReslut.setDouble(10, CUT_LENGTH); // 切损长度
				updateFurnaceReslut.setDouble(11, CUT_WEIGHT); // 切损重量
				updateFurnaceReslut.addBatch();
			}

			updateStateBack.setString(1, parm2.get(0));
			updateStateBack.setString(2, parm2.get(1));
			updateStateBack.setString(3, parm2.get(2));
			updateStateBack.setString(4, parm2.get(3));
			updateStateBack.addBatch();

			deletePortMil.setString(1, parm2.get(4));
			deletePortMil.setString(2, parm2.get(0) + "-01");
			deletePortMil.setString(3, parm2.get(1));
			deletePortMil.setString(4, parm2.get(2));
			deletePortMil.addBatch();

			updateFurnaceReslut.executeBatch();
			updateStateBack.executeBatch();
			deletePortMil.executeBatch();

			// 更新运输需求上料时间
//			TZH 24.4.24 注销物流接口
//			updateTmsDemandMLoadTime(connection, connection2, cro,
//					parm2.get(0), parm2.get(4));
			if (cro.getV_errMsg().equals("")) {
				cro.setV_errMsg("上料回退成功！");
			}
			connection.commit();
//			TZH 24.4.24 注销物流接口
//			connection2.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
//					TZH 24.4.24 注销物流接口
//					connection2.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
//			TZH 24.4.24 注销物流接口
//			if (connection2 != null) {
//				try {
//					connection.rollback(); // 回滚事务
//					connection2.rollback(); // 回滚事务
//				} catch (Exception e2) {
//				}
//			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
//				TZH 24.4.24 注销物流接口
//				if (connection2 != null && !connection2.isClosed()) {
//					connection2.close();
//				}
			} catch (Exception e2) {
			}
		}

		return cro;
	}

	/**
	 * 日期加一天获取下一月(年月)
	 * 
	 * @param yearMoth
	 * @return
	 */
	private String getBalYearMoth(String systime) {
		String year = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfS = new SimpleDateFormat("yyyyMM");
		try {
			Date date = sdf.parse(systime);
			Calendar calender = Calendar.getInstance();
			calender.setTime(date);
			calender.add(Calendar.DATE, 1);
			year = sdfS.format(calender.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return year;
	}

	/**
	 * 判断是否跨月 ky=1则跨月了。
	 * 
	 * @param systime
	 * @return
	 */
	private String isky(String systime) {
		String ky = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfS = new SimpleDateFormat("yyyyMM");
		try {
			String year1 = "";
			String year2 = "";
			Date date = sdf.parse(systime);
			Calendar calender = Calendar.getInstance();
			calender.setTime(date);
			calender.add(Calendar.DATE, 5);// 改25号结转
			year1 = sdfS.format(calender.getTime());
			year2 = sdfS.format(date);
			if (Integer.parseInt(year1) > Integer.parseInt(year2)) {
				ky = "1";
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ky;
	}

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

	/**
	 * 更新运输需求上料时间
	 * 
	 * @param connection
	 * @param connection2
	 * @param core
	 * @param JudgeStoveNo
	 * @param HeatPlanNo
	 * @return
	 * @throws SQLException
	 */
	public CoreReturnObject updateTmsDemandMLoadTime(Connection connection,
			Connection connection2, CoreReturnObject core, String JudgeStoveNo,
			String HeatPlanNo) throws SQLException {
		PreparedStatement psta = null;
		ResultSet rs = null;
		String sql = "";
		String loadTime = "";// 上料时间
		String yjz = "";// 预计支
		String yjd = "";// 预计吨
		sql = CoreXmlSqlParsersFactory.getSql("FeedResult.selectLoadTime");// 查询上料时间
		psta = connection.prepareStatement(sql);
		psta.setString(1, JudgeStoveNo);
		psta.setString(2, HeatPlanNo);
		rs = psta.executeQuery();
		if (rs.next()) {
			loadTime = rs.getString("loadTime");
			yjz = rs.getString("YJZ");
			yjd = rs.getString("YJD");
		}
		rs.close();
		psta.close();
		String updateOff = CoreXmlSqlParsersFactory
				.getSql("FeedResult.UpdateTmsDemandMLoadTime");// 更新运输需求上料时间
		PreparedStatement pstaupdateOff = connection2
				.prepareStatement(updateOff);
		pstaupdateOff.setString(1, JudgeStoveNo);
		pstaupdateOff.setString(2, HeatPlanNo);
		pstaupdateOff.setString(3, loadTime);
		pstaupdateOff.setString(4, yjz);
		pstaupdateOff.setString(5, yjd);
		pstaupdateOff.addBatch();
		pstaupdateOff.executeBatch();
		return core;
	}
}
