package com.steering.mes.mcp.common;

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.mes.mcp.Vrp.FrmMchInStore;
import com.steering.mes.mcp.entity.*;
import com.steering.mes.mcp.entity.MchEntity.MchOfflineResultEntity;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgxMEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 朱少波 on 2018/6/20.
 */
public class OfflineHelper extends DBCommon {

	public CoreReturnObject QueryReturnLineCount(String heatPlanID,
			Integer ProcessSeq, String gx) {
		return getDataBySqlID("OfflineHelper.QueryReturnLineCount",
				new Object[] { heatPlanID, ProcessSeq, gx, gx });
	}

	public CoreReturnObject QueryReturnLineCount(String heatPlanID,
			Integer ProcessSeq, String gx, String BatchNo) {
		return getDataBySqlID("OfflineHelper.QueryReturnLineCount2",
				new Object[] { heatPlanID, ProcessSeq, gx, gx, BatchNo });
	}

	public CoreReturnObject QueryHttCanOffLineData(String BatchNo,
			String JudgeStove, String[] PlineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("OfflineHelper.QueryHttCanOffLineData");
		sql += CoreConverter.toSqlConditionString("M.PLINE_CODE", PlineCode);

		if (!"".equals(JudgeStove)) {
			sql += " AND M.JUDGE_STOVE_NO = '" + JudgeStove + "' ";
		}

		if (!"".equals(BatchNo)) {
			sql += " AND T1.PHY_BATCH_NO = '" + BatchNo + "' ";
		}

		return getData(sql);
	}

	public CoreReturnObject QueryMchOffLineData(String HeatPlanID,
			String BatchNo) {
		return getDataBySqlID("OfflineHelper.QueryMchOffLineData",
				new Object[] { HeatPlanID, BatchNo });
	}

	public CoreReturnObject QueryVrpCanOffLineData(String BatchNo,
			String JudgeStove, String[] PlineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("OfflineHelper.QueryVrpCanOffLineData");
		sql += CoreConverter.toSqlConditionString("M.PLINE_CODE", PlineCode);

		if (!"".equals(JudgeStove)) {
			sql += " AND M.JUDGE_STOVE_NO = '" + JudgeStove + "' ";
		}

		if (!"".equals(BatchNo)) {
			sql += " AND T1.PHY_BATCH_NO = '" + BatchNo + "' ";
		}

		return getData(sql);
	}

	/**
	 * 离线
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject HttOffLine(ArrayList list) {

		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyyMMdd");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		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); // 取消自动提交事务
			String addOff = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.AddOffLine");// 新增离线实绩
			PreparedStatement patAdd = connection.prepareStatement(addOff);
			String addOffD = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.AddOffLineD");// 新增离线实绩从表
			PreparedStatement patAddD = connection.prepareStatement(addOffD);

			String updatePath = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.UpdatePathPortVrp2");// 修改队列表工艺路径,材料，工序状态
			PreparedStatement pastupdatePath = connection
					.prepareStatement(updatePath);

			String HeatPlanNo = String.valueOf(list.get(0));
			String BatchNo = String.valueOf(list.get(1));
			String slgx = String.valueOf(list.get(2));
			Integer ProcessSeq = (Integer) list.get(3);
			String name = String.valueOf(list.get(4));
			String bc = String.valueOf(list.get(5));
			String bz = String.valueOf(list.get(6));
			String count = String.valueOf(list.get(7));
			String PlineCode = String.valueOf(list.get(8));
			String[] slgxs = (String[]) list.get(9);

			int num = 0;
			int num1 = 0;
			int num2 = 0;
			if (slgx.equals("600202")) {
				num = 1;
				num1 = 2;
				num2 = 4;
			} else if (slgx.equals("600203")) {
				num = 1;
				num1 = 4;
				num2 = 6;
			} else if (slgx.equals("600204")) {
				num = 1;
				num1 = 6;
				num2 = 8;
			} else if (slgx.equals("600205")) {
				num = 1;
				num1 = 8;
				num2 = 10;
			} else if (slgx.equals("600206")) {
				num = 1;
				num1 = 10;
				num2 = 12;
			} else if (slgx.equals("600207")) {
				num = 1;
				num1 = 12;
				num2 = 14;
			} else if (slgx.equals("600208")) {
				num = 1;
				num1 = 16;
				num2 = 18;
			} else if (slgx.equals("600209")) {
				num = 1;
				num1 = 18;
				num2 = 20;
			} else if (slgx.equals("600210")) {
				num = 1;
				num1 = 20;
				num2 = 22;
			} else if (slgx.equals("600211")) {
				num = 1;
				num1 = 22;
				num2 = 24;
			}

			// 获取热处理计划实体了
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					HeatPlanNo);

			if (pln == null)
				return core;
			if (pln.getPlanstatus().equals("30")) {
				core.setV_errMsg("该计划已生产完成！");
				return core;
			}

			String cutFalg1 = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("HeatTreatmentService.qureyGxComInfoNew1");// 查询工序是否是结炉状态
			sql += CoreConverter.toSqlConditionString("STATION_CODE", slgxs);
			psta = connection.prepareStatement(sql);
			psta.setString(1, HeatPlanNo);
			psta.setString(2, pln.getRealProcessSeq());
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				cutFalg1 = rs1.getString("STATION_STATUS");
			}

			if (cutFalg1 != null && cutFalg1.equals("2")) {
				core.setV_errMsg("该工序已经结炉,无法修改");
				return core;
			}
			rs1.close();
			psta.close();

			List<PortHttBatchSampleResultEntity> matNo = new ArrayList<PortHttBatchSampleResultEntity>();
			String sqlloading1 = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.queryPortVrpHgByProcess2");// 查询管号队列里合格的材料号
			psta = connection.prepareStatement(sqlloading1);
			psta.setObject(1, HeatPlanNo);
			psta.setObject(2, ProcessSeq);
			psta.setObject(3, BatchNo);
			psta.setObject(4, Integer.parseInt(count) + 1);

			ResultSet rs2 = psta.executeQuery();
			matNo = EntityTool.getEntitysByRs(rs2,
					PortHttBatchSampleResultEntity.class);
			rs2.close();
			psta.close();

			if (matNo == null || matNo.size() < Integer.parseInt(count)) {
				core.setV_errMsg("管号队列材料合格支数不足,请重新输入离线支数");
				return core;
			}

			String resultNo = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.queryOffMaxRESULT");// 查询离线最大实绩编号
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, PlineCode + slgx + "L" + matter1.format(dt));
			psta.setObject(2, matter.format(dt));
			psta.setObject(3, pln.getPlineCode());
			rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();

			int countNum = 0;
			double weightNum = 0;
			for (PortHttBatchSampleResultEntity prot : matNo) {
				countNum += Integer.parseInt(prot.getActCount());
				weightNum += Double.parseDouble(prot.getActWeight());
				// 修改管号队列状态
				pastupdatePath.setObject(1, num);
				pastupdatePath.setObject(2, num1);
				pastupdatePath.setObject(3, "2");
				pastupdatePath.setObject(4, num2);
				pastupdatePath.setObject(5, "20");
				pastupdatePath.setObject(6, "10");
				pastupdatePath.setObject(7, slgx);
				pastupdatePath.setObject(8, resultNo);
				pastupdatePath.setObject(9, HeatPlanNo);
				pastupdatePath.setObject(10, ProcessSeq);
				pastupdatePath.setObject(11, prot.getMatNo());
				pastupdatePath.addBatch();

				PlanService.AddOffLineMat(connection, "F", name, "11",
						prot.getMatNo(), slgx, HeatPlanNo);
			}
			pastupdatePath.executeBatch();

			// 新增离线实绩
			patAdd.setObject(1, resultNo);
			patAdd.setObject(2, pln.getPlineCode());
			patAdd.setObject(3, pln.getPlineName());
			patAdd.setObject(4, slgx);
			patAdd.setObject(5, slgx);
			patAdd.setObject(6, HeatPlanNo);
			patAdd.setObject(7, pln.getZyBatchId());
			patAdd.setObject(8, pln.getIndexSeq());
			patAdd.setObject(9, pln.getProPlanId());
			patAdd.setObject(10, pln.getGxPlanNo());
			patAdd.setObject(11, pln.getJudgeStoveNo());
			patAdd.setObject(12, BatchNo);
			patAdd.setObject(13, "");
			patAdd.setObject(14, pln.getProduccode());
			patAdd.setObject(15, pln.getProducname());
			patAdd.setObject(16, pln.getSteelcode());
			patAdd.setObject(17, pln.getSteelname());
			patAdd.setObject(18, pln.getGradecode());
			patAdd.setObject(19, pln.getGradename());
			patAdd.setObject(20, pln.getModelCode());
			patAdd.setObject(21, pln.getModelDesc());
			patAdd.setObject(22, pln.getStdCode());
			patAdd.setObject(23, pln.getStdName());
			patAdd.setObject(24, pln.getStdStyle());
			patAdd.setObject(25, pln.getStdStyleDesc());
			patAdd.setObject(26, matNo.get(0).getActDimater());
			patAdd.setObject(27, matNo.get(0).getActHeight());
			patAdd.setObject(28, matNo.get(0).getActLen());
			patAdd.setObject(29, matNo.get(0).getActLenMin());
			patAdd.setObject(30, matNo.get(0).getActLenMax());
			patAdd.setObject(31, pln.getOrdLnDlyPk());
			patAdd.setObject(32, pln.getOrderNo());
			patAdd.setObject(33, pln.getOrderSeq());
			patAdd.setObject(34, pln.getDeliveryNo());
			patAdd.setObject(35, countNum);
			patAdd.setObject(36, weightNum);
			patAdd.setObject(37, name);
			patAdd.setObject(38, bc);
			patAdd.setObject(39, bz);
			patAdd.setObject(40, systime);
			patAdd.setObject(41, ProcessSeq);
			patAdd.setObject(42, pln.getFeedHeatNo());

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

	/**
	 * 离线
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject HttOffLine(ArrayList<String> list1Str,
			ArrayList list) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyyMMdd");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		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); // 取消自动提交事务
			String addOff = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.AddOffLine");// 新增离线实绩
			PreparedStatement patAdd = connection.prepareStatement(addOff);
			String addOffD = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.AddOffLineD");// 新增离线实绩从表
			PreparedStatement patAddD = connection.prepareStatement(addOffD);

			String updatePath = CoreXmlSqlParsersFactory
					.getSql("AppearanceService.UpdatePathPortVrp2");// 修改队列表工艺路径,材料，工序状态
			PreparedStatement pastupdatePath = connection
					.prepareStatement(updatePath);

			String HeatPlanNo = String.valueOf(list.get(0));
			// String BatchNo = String.valueOf(list.get(1));
			String slgx = String.valueOf(list.get(2));
			Integer ProcessSeq = (Integer) list.get(3);
			String name = String.valueOf(list.get(4));
			String bc = String.valueOf(list.get(5));
			String bz = String.valueOf(list.get(6));
			// String count = String.valueOf( list.get(7));
			String PlineCode = String.valueOf(list.get(8));
			String[] slgxs = (String[]) list.get(9);

			int num = 0;
			int num1 = 0;
			int num2 = 0;
			if (slgx.equals("600202")) {
				num = 1;
				num1 = 2;
				num2 = 4;
			} else if (slgx.equals("600203")) {
				num = 1;
				num1 = 4;
				num2 = 6;
			} else if (slgx.equals("600204")) {
				num = 1;
				num1 = 6;
				num2 = 8;
			} else if (slgx.equals("600205")) {
				num = 1;
				num1 = 8;
				num2 = 10;
			} else if (slgx.equals("600206")) {
				num = 1;
				num1 = 10;
				num2 = 12;
			} else if (slgx.equals("600207")) {
				num = 1;
				num1 = 12;
				num2 = 14;
			} else if (slgx.equals("600208")) {
				num = 1;
				num1 = 16;
				num2 = 18;
			} else if (slgx.equals("600209")) {
				num = 1;
				num1 = 18;
				num2 = 20;
			} else if (slgx.equals("600210")) {
				num = 1;
				num1 = 20;
				num2 = 22;
			} else if (slgx.equals("600211")) {
				num = 1;
				num1 = 22;
				num2 = 24;
			}

			// 获取热处理计划实体了
			PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
					HeatPlanNo);

			if (pln == null)
				return core;
			if (pln.getPlanstatus().equals("30")) {
				core.setV_errMsg("该计划已生产完成！");
				return core;
			}

			String cutFalg1 = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("HeatTreatmentService.qureyGxComInfoNew1");// 查询工序是否是结炉状态
			sql += CoreConverter.toSqlConditionString("STATION_CODE", slgxs);
			psta = connection.prepareStatement(sql);
			psta.setString(1, HeatPlanNo);
			psta.setString(2, pln.getRealProcessSeq());
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				cutFalg1 = rs1.getString("STATION_STATUS");
			}

			if (cutFalg1 != null && cutFalg1.equals("2")) {
				core.setV_errMsg("该工序已经结炉,无法离线");
				return core;
			}
			rs1.close();
			psta.close();

			/*
			 * int countNum = 0; double weightNum = 0;
			 */

			List<PortHttBatchSampleResultEntity> matNo = new ArrayList<PortHttBatchSampleResultEntity>();

			// 新增离线从表
			for (int i = 0; i < list1Str.size(); i++) {

				String resultNo = "";
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("AppearanceService.queryOffMaxRESULT1");// 查询离线最大实绩编号
				psta = connection.prepareStatement(sqlloading);
				psta.setObject(1, PlineCode + slgx + "L" + matter1.format(dt));
				psta.setObject(2, PlineCode + slgx + "L" + matter1.format(dt));
				rs1 = psta.executeQuery();
				if (rs1.next()) {
					resultNo = rs1.getString("RESULT_NO");
				}
				rs1.close();
				psta.close();

				String resultsqe = "";
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("AppearanceService.querymaxsqeNew");// 查询从表最大序号
				psta = connection.prepareStatement(sqlloading);
				psta.setObject(1, matter1.format(now));
				psta.setObject(2, resultNo);
				psta.setObject(3, matter1.format(now));
				ResultSet rs3 = psta.executeQuery();
				if (rs3.next()) {
					resultsqe = rs3.getString("RESULT_SQE");
				}

				rs3.close();
				psta.close();
				HttOfflineResultDentity resultD = EntityTool
						.getEntityByJSONStr(list1Str.get(i),
								HttOfflineResultDentity.class);
				patAddD.setObject(1, resultNo);
				patAddD.setObject(2, resultsqe);
				patAddD.setObject(3, HeatPlanNo);
				patAddD.setObject(4, PlineCode);
				patAddD.setObject(5, pln.getPlineName());
				patAddD.setObject(6, Integer.parseInt(resultD.getOfflineNum()));
				patAddD.setObject(7, resultD.getGowhereCode());
				patAddD.setObject(8, resultD.getGowhereName());
				patAddD.setObject(9, systime);
				patAddD.setObject(10, slgx);
				patAddD.setObject(11, slgx);
				patAddD.addBatch();
				patAddD.executeBatch();

				String[] MatNos = resultD.getMatNo().split(",");

				String sqlloading1 = CoreXmlSqlParsersFactory
						.getSql("AppearanceService.queryPortVrpHgByProcess3");// 查询管号队列里合格的材料号

				sqlloading1 = sqlloading1
						+ CoreConverter
								.toSqlConditionString("T.MAT_NO", MatNos);

				psta = connection.prepareStatement(sqlloading1);
				psta.setObject(1, HeatPlanNo);
				psta.setObject(2, ProcessSeq);
				psta.setObject(3, resultD.getBatchNo());

				ResultSet rs2 = psta.executeQuery();
				matNo = EntityTool.getEntitysByRs(rs2,
						PortHttBatchSampleResultEntity.class);
				rs2.close();
				psta.close();

				if (matNo == null
						|| matNo.size() < Integer.parseInt(resultD
								.getOfflineNum())) {
					core.setV_errMsg("管号队列材料合格支数不足,请重新输入离线支数");
					return core;
				}
				int countNum = 0;
				double weightNum = 0;

				for (PortHttBatchSampleResultEntity prot : matNo) {
					countNum += Integer.parseInt(prot.getActCount());
					weightNum += Double.parseDouble(prot.getActWeight());
					// 修改管号队列状态
					pastupdatePath.setObject(1, num);
					pastupdatePath.setObject(2, num1);
					pastupdatePath.setObject(3, "2");
					pastupdatePath.setObject(4, num2);
					pastupdatePath.setObject(5, "20");
					pastupdatePath.setObject(6, "10");
					pastupdatePath.setObject(7, slgx);
					pastupdatePath.setObject(8, resultNo);
					pastupdatePath.setObject(9, HeatPlanNo);
					pastupdatePath.setObject(10, ProcessSeq);
					pastupdatePath.setObject(11, prot.getMatNo());
					pastupdatePath.addBatch();

					PlanService.AddOffLineMat(connection, "F", name, "11",
							prot.getMatNo(), slgx, HeatPlanNo,
							resultD.getGowhereCode(), resultD.getGowhereName(),
							resultD.getMemo(),resultNo);
				}
				pastupdatePath.executeBatch();

				// 新增离线实绩
				patAdd.setObject(1, resultNo);
				patAdd.setObject(2, pln.getPlineCode());
				patAdd.setObject(3, pln.getPlineName());
				patAdd.setObject(4, slgx);
				patAdd.setObject(5, slgx);
				patAdd.setObject(6, HeatPlanNo);
				patAdd.setObject(7, pln.getZyBatchId());
				patAdd.setObject(8, pln.getIndexSeq());
				patAdd.setObject(9, pln.getProPlanId());
				patAdd.setObject(10, pln.getGxPlanNo());
				patAdd.setObject(11, pln.getJudgeStoveNo());
				patAdd.setObject(12, resultD.getBatchNo());
				patAdd.setObject(13, "");
				patAdd.setObject(14, pln.getProduccode());
				patAdd.setObject(15, pln.getProducname());
				patAdd.setObject(16, pln.getSteelcode());
				patAdd.setObject(17, pln.getSteelname());
				patAdd.setObject(18, pln.getGradecode());
				patAdd.setObject(19, pln.getGradename());
				patAdd.setObject(20, pln.getModelCode());
				patAdd.setObject(21, pln.getModelDesc());
				patAdd.setObject(22, pln.getStdCode());
				patAdd.setObject(23, pln.getStdName());
				patAdd.setObject(24, pln.getStdStyle());
				patAdd.setObject(25, pln.getStdStyleDesc());
				patAdd.setObject(26, matNo.get(0).getActDimater());
				patAdd.setObject(27, matNo.get(0).getActHeight());
				patAdd.setObject(28, matNo.get(0).getActLen());
				patAdd.setObject(29, matNo.get(0).getActLenMin());
				patAdd.setObject(30, matNo.get(0).getActLenMax());
				patAdd.setObject(31, pln.getOrdLnDlyPk());
				patAdd.setObject(32, pln.getOrderNo());
				patAdd.setObject(33, pln.getOrderSeq());
				patAdd.setObject(34, pln.getDeliveryNo());
				patAdd.setObject(35, countNum);
				patAdd.setObject(36, weightNum);
				patAdd.setObject(37, name);
				patAdd.setObject(38, bc);
				patAdd.setObject(39, bz);
				patAdd.setObject(40, systime);
				patAdd.setObject(41, ProcessSeq);
				patAdd.setObject(42, matNo.get(0).getFlagStoveNo());
				patAdd.setObject(43, resultD.getMemo());
				patAdd.setObject(44, pln.getFeedHeatNo());
				patAdd.addBatch();
				patAdd.executeBatch();
			}
			// pastupdatePath.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 load
	 * @return
	 */
	public CoreReturnObject VrpOffLine(ArrayList<String> list) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyyMMdd");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		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); // 取消自动提交事务
			String addOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpExpandResult.AddOffLine");// 新增离线实绩
			PreparedStatement patAdd = connection.prepareStatement(addOff);
			String MarkOff = CoreXmlSqlParsersFactory
					.getSql("FrmVrpExpandResult.MarkUnCancelFlag");// 标记之前同炉批组返线的离线实绩为无法撤销数据
			PreparedStatement patMarkOff = connection.prepareStatement(MarkOff);

			String updatePath = CoreXmlSqlParsersFactory
					.getSql("FrmVrpExpandResult.UpdatePathPortVrp");// 修改队列表工艺路径,材料，工序状态
			PreparedStatement pastupdatePath = connection
					.prepareStatement(updatePath);

			String HeatPlanNo = String.valueOf(list.get(0));
			String BatchNo = String.valueOf(list.get(1));
			String slgx = String.valueOf(list.get(2));
			String name = String.valueOf(list.get(3));
			String bc = String.valueOf(list.get(4));
			String bz = String.valueOf(list.get(5));
			String count = String.valueOf(list.get(6));
			String PlineCode = String.valueOf(list.get(7));

			int num = 0;
			int num1 = 0;
			int num2 = 0;
			if (PlineCode.equals("C018")) {
				if (slgx.equals("650101")) {
					num = 0;
					num1 = 0;
					num2 = 2;
				} else if (slgx.equals("650102")) {
					num = 1;
					num1 = 2;
					num2 = 4;
				} else if (slgx.equals("650103")) {
					num = 1;
					num1 = 4;
					num2 = 6;
				} else if (slgx.equals("650104")) {
					num = 1;
					num1 = 6;
					num2 = 8;
				} else if (slgx.equals("650105")) {
					num = 1;
					num1 = 8;
					num2 = 10;
				} else if (slgx.equals("650106")) {
					num = 1;
					num1 = 10;
					num2 = 12;
				} else if (slgx.equals("650107")) {
					num = 1;
					num1 = 12;
					num2 = 14;
				}

			} else if (PlineCode.equals("C021")) {
				if (slgx.equals("650401")) {
					num = 0;
					num1 = 0;
					num2 = 2;
				} else if (slgx.equals("650402")) {
					num = 1;
					num1 = 2;
					num2 = 4;
				} else if (slgx.equals("650403")) {
					num = 1;
					num1 = 4;
					num2 = 6;
				} else if (slgx.equals("650404")) {
					num = 1;
					num1 = 6;
					num2 = 8;
				} else if (slgx.equals("650405")) {
					num = 1;
					num1 = 8;
					num2 = 10;
				}
			} else if (PlineCode.equals("C014")) {
				if (slgx.equals("650501")) {
					num = 0;
					num1 = 0;
					num2 = 2;
				} else if (slgx.equals("650502")) {
					num = 1;
					num1 = 2;
					num2 = 4;
				} else if (slgx.equals("650503")) {
					num = 1;
					num1 = 4;
					num2 = 6;
				} else if (slgx.equals("650504")) {
					num = 1;
					num1 = 6;
					num2 = 8;
				} else if (slgx.equals("650505")) {
					num = 1;
					num1 = 8;
					num2 = 10;
				}
			} else if (PlineCode.equals("C013")) {
				if (slgx.equals("650601")) {
					num = 0;
					num1 = 0;
					num2 = 2;
				} else if (slgx.equals("650602")) {
					num = 1;
					num1 = 2;
					num2 = 4;
				} else if (slgx.equals("650603")) {
					num = 1;
					num1 = 4;
					num2 = 6;
				} else if (slgx.equals("650604")) {
					num = 1;
					num1 = 6;
					num2 = 8;
				} else if (slgx.equals("650605")) {
					num = 1;
					num1 = 8;
					num2 = 10;
				}
			} else if (PlineCode.equals("C070")) {
				if (slgx.equals("650701")) {
					num = 0;
					num1 = 0;
					num2 = 2;
				} else if (slgx.equals("650702")) {
					num = 1;
					num1 = 2;
					num2 = 4;
				} else if (slgx.equals("650703")) {
					num = 1;
					num1 = 4;
					num2 = 6;
				} else if (slgx.equals("650704")) {
					num = 1;
					num1 = 6;
					num2 = 8;
				} else if (slgx.equals("650705")) {
					num = 1;
					num1 = 8;
					num2 = 10;
				}
			}

			// 获取镦拔扩计划实体类
			PlnZyDbkMEntity pln = PlanService.getSendDBKPlanCInfo(connection,
					HeatPlanNo);

			if (pln == null)
				return core;
			if (pln.getPlanstatus().equals("30")) {
				core.setV_errMsg("该计划已生产完成！");
				return core;
			}

			String cutFalg = "";
			String sql = CoreXmlSqlParsersFactory
					.getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序结炉状态
			PreparedStatement psta1 = connection.prepareStatement(sql);
			psta1.setString(1, HeatPlanNo);
			psta1.setString(2, PlineCode);
			psta1.setString(3, slgx);
			ResultSet rs = psta1.executeQuery();
			if (rs.next()) {
				cutFalg = rs.getString("STATION_STATUS");
			}
			if (cutFalg.equals("2")) {
				core.setV_errMsg("已结炉,不允许此操作！");
				return core;
			}
			rs.close();
			psta1.close();
			List<PortVrpBatchSampleResultEntity> matNo = new ArrayList<PortVrpBatchSampleResultEntity>();
			String sqlloading1 = CoreXmlSqlParsersFactory
					.getSql("FrmVrpExpandResult.queryPortVrpHg2");// 查询管号队列里合格的材料号
			psta = connection.prepareStatement(sqlloading1);
			psta.setObject(1, HeatPlanNo);
			psta.setObject(2, pln.getJudgeStoveNo());
			psta.setObject(3, BatchNo);
			psta.setObject(4, Integer.parseInt(count) + 1);
			ResultSet rs2 = psta.executeQuery();
			matNo = EntityTool.getEntitysByRs(rs2,
					PortVrpBatchSampleResultEntity.class);
			rs2.close();
			psta.close();

			if (matNo == null || matNo.size() < Integer.parseInt(count)) {
				core.setV_errMsg("管号队列材料合格支数不足,请重新输入离线支数");
				return core;
			}

			int countNum = 0;
			double weightNum = 0;
			for (PortVrpBatchSampleResultEntity prot : matNo) {
				countNum += Integer.parseInt(prot.getActCount());
				weightNum += Double.parseDouble(prot.getActWeight());
				// 修改管号队列状态
				pastupdatePath.setObject(1, num);
				pastupdatePath.setObject(2, num1);
				pastupdatePath.setObject(3, "2");
				pastupdatePath.setObject(4, num2);
				pastupdatePath.setObject(5, "20");
				pastupdatePath.setObject(6, "10");
				pastupdatePath.setObject(7, slgx);
				pastupdatePath.setObject(8, HeatPlanNo);
				pastupdatePath.setObject(9, prot.getMatNo());
				pastupdatePath.addBatch();

				PlanService.AddOffLineMat(connection, "E", name, "11",
						prot.getMatNo(), slgx, HeatPlanNo);
			}
			pastupdatePath.executeBatch();

			String resultNo = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("FrmVrpExpandResult.queryOffMaxRESULT");// 查询离线最大实绩编号
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, PlineCode + slgx + "L" + matter1.format(dt));
			psta.setObject(2, matter.format(dt));
			psta.setObject(3, pln.getPlineCode());
			psta.setObject(4, PlineCode + slgx + "L" + matter1.format(dt));
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();

			// 新增离线实绩
			patAdd.setObject(1, resultNo);
			patAdd.setObject(2, HeatPlanNo);
			patAdd.setObject(3, slgx);
			patAdd.setObject(4, slgx);
			patAdd.setObject(5, pln.getPlineCode());
			patAdd.setObject(6, pln.getPlineName());
			patAdd.setObject(7, pln.getZyBatchId());
			patAdd.setObject(8, pln.getProPlanId());
			patAdd.setObject(9, pln.getGxPlanNo());

			patAdd.setObject(10, pln.getJudgeStoveNo());
			patAdd.setObject(11, BatchNo);
			patAdd.setObject(12, pln.getProduccode());
			patAdd.setObject(13, pln.getProducname());
			patAdd.setObject(14, pln.getSteelcode());
			patAdd.setObject(15, pln.getSteelname());
			patAdd.setObject(16, pln.getGradecode());
			patAdd.setObject(17, pln.getGradename());
			patAdd.setObject(18, pln.getModelCode());
			patAdd.setObject(19, pln.getModelDesc());
			patAdd.setObject(20, pln.getStdCode());
			patAdd.setObject(21, pln.getStdName());
			patAdd.setObject(22, pln.getStdStyle());
			patAdd.setObject(23, pln.getStdStyleDesc());
			patAdd.setObject(24, pln.getAimoutdiameter());
			patAdd.setObject(25, pln.getAimwallthick());
			patAdd.setObject(26, matNo.get(0).getActLen());
			patAdd.setObject(27, matNo.get(0).getActLenMin());
			patAdd.setObject(28, matNo.get(0).getActLenMax());
			patAdd.setObject(29, countNum);
			patAdd.setObject(30, weightNum);
			patAdd.setObject(31, systime);
			patAdd.setObject(32, name);
			patAdd.setObject(33, bc);
			patAdd.setObject(34, bz);
			patAdd.setObject(35, pln.getOrdLnDlyPk());
			patAdd.setObject(36, pln.getOrderNo());
			patAdd.setObject(37, pln.getOrderSeq());
			patAdd.setObject(38, pln.getDeliveryNo());
			patAdd.setObject(39, "");
			patAdd.setObject(40, pln.getFlagStoveNo());
			patAdd.setObject(41, "");
            patAdd.setObject(42, pln.getFeedHeatNo());

			patMarkOff.setObject(1, pln.getHeatPlanNo());
			patMarkOff.setObject(2, pln.getJudgeStoveNo());
			patMarkOff.setObject(3, BatchNo);
			patMarkOff.setObject(4, "");
			patMarkOff.setObject(5, slgx);
			patMarkOff.addBatch();
			patMarkOff.executeBatch();

			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 static MatBcMEntity getMatBcM(Connection con, String MatNo)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("OfflineHelper.getMatBcM");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, MatNo);
		ResultSet rs = ps.executeQuery();
		List<MatBcMEntity> MatBcMEntity = EntityTool.getEntitysByRs(rs,
				MatBcMEntity.class);
		rs.close();
		ps.close();
		if (MatBcMEntity == null || MatBcMEntity.size() == 0) {
			return null;
		} else {
			for (MatBcMEntity sendMatBcMEntity : MatBcMEntity) {
				QualityDesignTool.nullConverNullString(sendMatBcMEntity);
			}
			return MatBcMEntity.get(0);
		}
	}


	public static MatZcMEntity getMatZcM(Connection con, String MatNo)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("OfflineHelper.getMatZcM");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, MatNo);
		ResultSet rs = ps.executeQuery();
		List<MatZcMEntity> MatBcMEntity = EntityTool.getEntitysByRs(rs,
				MatZcMEntity.class);
		rs.close();
		ps.close();
		if (MatBcMEntity == null || MatBcMEntity.size() == 0) {
			return null;
		} else {
			for (MatZcMEntity sendMatBcMEntity : MatBcMEntity) {
				QualityDesignTool.nullConverNullString(sendMatBcMEntity);
			}
			return MatBcMEntity.get(0);
		}
	}

	public static CouplingMatMEntity getMatBcMJg(Connection con, String woid,String resultNo)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("OfflineHelper.getMatBcMJg");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, woid);
		ps.setObject(2, resultNo);
		ResultSet rs = ps.executeQuery();
		List<CouplingMatMEntity> MatBcMEntity = EntityTool.getEntitysByRs(rs,
				CouplingMatMEntity.class);
		rs.close();
		ps.close();
		if (MatBcMEntity == null || MatBcMEntity.size() == 0) {
			return null;
		} else {
			for (CouplingMatMEntity sendMatBcMEntity : MatBcMEntity) {
				QualityDesignTool.nullConverNullString(sendMatBcMEntity);
			}
			return MatBcMEntity.get(0);
		}
	}

	/**
	 * 离线
	 * 
	 * @param
	 * @return
	 */
	public CoreReturnObject MchOffLine(ArrayList<String> list1Str) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		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); // 取消自动提交事务
			String addOff = CoreXmlSqlParsersFactory
					.getSql("OfflineHelper.InsertMchOffline");// 新增离线实绩
			PreparedStatement patAdd = connection.prepareStatement(addOff);

			String updatePath = CoreXmlSqlParsersFactory
					.getSql("OfflineHelper.UpdatePathPortMch");// 修改队列表工艺路径,材料，工序状态
			PreparedStatement pastupdatePath = connection
					.prepareStatement(updatePath);

			if (list1Str.size() <= 0) {
				core.setV_errMsg("请输入离线数据！");
				return core;
			}

			MchOfflineResultEntity resultFirst = EntityTool.getEntityByJSONStr(
					list1Str.get(0), MchOfflineResultEntity.class);

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

			if (cutFalg.equals("2")) {
				core.setV_errMsg("已结炉,不允许此操作！");
				return core;
			}





			List<PortMchBatchSampleResultEntity> matNo = new ArrayList<PortMchBatchSampleResultEntity>();

			// 新增离线从表
			for (int i = 0; i < list1Str.size(); i++) {

				MchOfflineResultEntity result = EntityTool.getEntityByJSONStr(
						list1Str.get(i), MchOfflineResultEntity.class);

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


				///无组号的长度范围    生成组号规则
				String groupList = CoreXmlSqlParsersFactory
						.getSql("FrmMchInStore.doQueryVrpRackNoGroup");// 查询管号队列里合格的材料号


				psta = connection.prepareStatement(groupList);
				psta.setObject(1, result.getHeatPlanNo());
				psta.setObject(2, result.getBatchNo());

				ResultSet rs2 = psta.executeQuery();
				List<PortMchBatchSampleResultEntity> GroupList = EntityTool.getEntitysByRs(rs2,
						PortMchBatchSampleResultEntity.class);
				rs2.close();
				psta.close();

				for (PortMchBatchSampleResultEntity entity :GroupList
					 ) {
					FrmMchInStore.creatGroup(connection, pln.getOrderNo() + "/" + pln.getOrderSeq(),
							entity.getActLenMin(), entity.getActLenMax(),
							pln.getOrdLnDlyPk());
				}

			String resultNo = "";
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("OfflineHelper.queryOffMaxRESULT1");// 查询离线最大实绩编号
				psta = connection.prepareStatement(sqlloading);
				psta.setObject(1,
						result.getPlineCode() + result.getStationCode() + "L"
								+ matter1.format(dt));
				psta.setObject(2,
						result.getPlineCode() + result.getStationCode() + "L"
								+ matter1.format(dt));
				rs = psta.executeQuery();
				if (rs.next()) {
					resultNo = rs.getString("RESULT_NO");
				}
				rs.close();
				psta.close();

				String[] MatNos = result.getMatNo().split(",");

				String sqlloading1 = CoreXmlSqlParsersFactory
						.getSql("OfflineHelper.queryPortMch");// 查询管号队列里合格的材料号

				sqlloading1 = sqlloading1
						+ PlanService
								.toSqlConditionString("T.MAT_NO", MatNos);

				psta = connection.prepareStatement(sqlloading1);
				psta.setObject(1, result.getHeatPlanNo());
				psta.setObject(2, result.getBatchNo());

				 rs2 = psta.executeQuery();
				matNo = EntityTool.getEntitysByRs(rs2,
						PortMchBatchSampleResultEntity.class);
				rs2.close();
				psta.close();

				if (matNo == null
						|| matNo.size() < Integer.parseInt(result
								.getOfflineNum())) {
					core.setV_errMsg("管号队列材料合格支数不足,请重新输入离线支数");
					return core;
				}
				int countNum = 0;
				double weightNum = 0;

				for (PortMchBatchSampleResultEntity prot : matNo) {

					countNum += Integer.parseInt(prot.getActCount());
					weightNum += Double.parseDouble(prot.getActWeight());
					// 修改管号队列状态

					if (prot.getTrFlag().equals("2") &&  result.getStationCode().equals("701311") ) {
						core.setV_errMsg("管号已经离线返线过测量点,请先过测量点或撤销上次离线记录!");
						return core;
					}

					pastupdatePath.setObject(1, "20");
					pastupdatePath.setObject(2, "10");
					pastupdatePath.setObject(3, result.getStationCode());
					pastupdatePath.setObject(4, resultNo);
					pastupdatePath.setObject(5, result.getHeatPlanNo());
					pastupdatePath.setObject(6, prot.getMatNo());
					pastupdatePath.addBatch();

					PlanService.AddOffLineMat(connection, "G",
							result.getProUser(), "11", prot.getMatNo(),
							result.getStationCode(), result.getHeatPlanNo(),
							"", "", result.getMemo(),resultNo);
				}
				pastupdatePath.executeBatch();

				// 新增离线实绩
				patAdd.setObject(1, resultNo);
				patAdd.setObject(2, pln.getPlineCode());
				patAdd.setObject(3, pln.getPlineName());
				patAdd.setObject(4, result.getStationCode());
				patAdd.setObject(5, result.getStationCode());
				patAdd.setObject(6, result.getActLenMin());
				patAdd.setObject(7, result.getActLenMax());
				patAdd.setObject(8, result.getOfflineNum());
				patAdd.setObject(9, result.getOfflineWt());
				patAdd.setObject(10, result.getProUser());
				patAdd.setObject(11, result.getProShift());
				patAdd.setObject(12, result.getProGroup());
				patAdd.setObject(13, result.getMemo());
				patAdd.setObject(14, matNo.get(0).getMatNo());
				patAdd.setObject(15, result.getHeatPlanNo());
				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;
	}
}
