package com.steering.mes.mcp.coup;

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

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

import com.steering.comm.converter.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.common.PlanService;
import com.steering.mes.mcp.entity.CouplingLoadingResultEntity;
import com.steering.mes.mcp.entity.MatBcMEntity;
import com.steering.mes.mcp.entity.YdmBcOutlistEntity;
import com.steering.mes.mcp.entity.MchEntity.CouplingComInfoEntity;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgWorkbillEntity;

/**
 * 接箍上料实绩业务类
 * 
 * @author tjg 2015-7-16
 */
public class CoupLoadingService extends DBCommon {
	/**
	 * 每支材料对应的倒库倒库单号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String querySendOut(Connection conn, String strlist)
			throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		// Date dt = new Date();
		// SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		// SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.queryOutList");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("OUTSTOCK_DOC");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 查询上料实绩支数总和
	 * 
	 * @param planNo
	 *            计划号
	 * @return
	 */
	public CoreReturnObject getLoadResult1(String heatNo) {
		return this.getDataBySqlID("CoupLoadingService.GetQuery1",
				new Object[] { heatNo });
	}

	/**
	 * 查询工单的离线支数
	 */
	public CoreReturnObject getQueryOfflinePiece(String heatNo) {
		return this.getDataBySqlID("CoupLoadingService.getQueryOfflinePiece",
				new Object[] { heatNo });
	}

	/**
	 * 查询工单下回库支数
	 */
	public CoreReturnObject getCoupBackPiece(String woId) {
		return this.getDataBySqlID("CoupLoadingService.getCoupBackPiece",
				new Object[] { woId });
	}

	/***
	 * 根据条件查询工单
	 * 
	 * @param department
	 * @param depart
	 * @return
	 */
	public CoreReturnObject getQueryWoId(String woId, String plnNo,
			String judgeStoveNo, String[] starts, String[] plinCodes) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.getQueryWoId");
		// if (!plinCode.equals("")) {
		// sql += " AND T.PLINE_CODE='" + plinCode + "'";
		// }
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
		if (!woId.equals("")) {
			sql += " AND T.WO_ID ='" + woId + "'";
		}
		if (!plnNo.equals("")) {
			sql += " AND T.HEAT_PLAN_NO ='" + plnNo + "'";
		}
		if (!judgeStoveNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO ='" + judgeStoveNo + "'";
		}
		if (starts.length > 0) {
			sql += CoreConverter.toSqlConditionString("T.ZY_BILL_STATUS",
					starts);
		}
		// if (!starts.equals("")) {
		// sql += " AND T.ZY_BILL_STATUS='" + starts + "'";
		// }
		sql += " ORDER BY t.GX_PRO_SEQ ASC";
		return this.getData(sql);
	}

	/***
	 * 根据条件查询工单(在途,全部)
	 * 
	 * @param department
	 * @param depart
	 * @return
	 */
	public CoreReturnObject getQueryWoId1(String woId, String plnNo,
			String judgeStoveNo, String[] starts, String[] plinCodes) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.getQueryWoId");
		// if (!plinCode.equals("")) {
		// sql += " AND T.PLINE_CODE='" + plinCode + "'";
		// }
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
		if (!woId.equals("")) {
			sql += " AND T.WO_ID ='" + woId + "'";
		}
		if (!plnNo.equals("")) {
			sql += " AND T.HEAT_PLAN_NO ='" + plnNo + "'";
		}
		if (!judgeStoveNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO ='" + judgeStoveNo + "'";
		}
		// if (starts.equals("1")) {
		// sql += " AND T.ZY_BILL_STATUS IN ('2','3')";
		// }
		if (starts.length > 0) {
			sql += CoreConverter.toSqlConditionString("T.ZY_BILL_STATUS",
					starts);
		}
		// if (!starts.equals("")) {
		// sql += " AND T.ZY_BILL_STATUS='" + starts + "'";
		// }
		sql += " ORDER BY T.GX_PRO_SEQ ASC";
		return this.getData(sql);
	}

	/**
	 * 查询所有未封闭的工单信息
	 */

	public CoreReturnObject ulgetQueryWoId(String woId, String plnNo,
			String judgeStoveNo, String[] starts, String[] plinCodes) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.getQueryWoId");
		// if (!plinCode.equals("")) {
		// sql += " AND T.PLINE_CODE='" + plinCode + "'";
		// }
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
		if (!woId.equals("")) {
			sql += " AND T.WO_ID ='" + woId + "'";
		}
		if (!plnNo.equals("")) {
			sql += " AND T.HEAT_PLAN_NO ='" + plnNo + "'";
		}
		if (!judgeStoveNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO ='" + judgeStoveNo + "'";
		}
		if (starts.length > 0) {
			sql += CoreConverter.toSqlConditionString("T.ZY_BILL_STATUS",
					starts);
		}
		return this.getData(sql);
	}

	/***
	 * 根据条件查询工单
	 * 
	 * @param department
	 * @param depart
	 * @return
	 */
	public CoreReturnObject getQueryWoIdDetails(String woId) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.getQueryWoId");
		sql += " AND T.WO_ID ='" + woId + "'";
		return this.getData(sql);
	}

	/**
	 * 每支材料对应的倒库（出库）流水号
	 * 
	 * @param conn
	 *            c
	 * @param strlist
	 *            传递参数
	 * @return obj
	 * @throws Exception
	 *             异常处理
	 */
	public static String querySendOutList(Connection conn, String strlist,
			String strType) throws Exception {
		String seq = "";
		PreparedStatement psta = null;
		// Date dt = new Date();
		// SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
		String sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.queryOut");
		psta = conn.prepareStatement(sqlLovsqe);
		psta.setString(1, strlist);
		psta.setString(2, strlist);
		ResultSet rs = psta.executeQuery();
		if (rs.next()) {
			seq = rs.getString("OUTSTOCK_SQE");
		}
		rs.close();
		psta.close();
		return seq;
	}

	/**
	 * 查询工单信息
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static <T> T getPlanWoId(Connection con, String woId, Class<T> cls)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.QueryWoId");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, woId);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 查询上料信息
	 * 
	 * @param con
	 * @throws SQLException
	 */
	public static <T> T getCoupLoad(Connection con, String resultNo,
			Class<T> cls) throws SQLException {
		String sql = "SELECT T.* FROM COUPLING_LOADING_RESULT T WHERE T.RESULT_NO=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, resultNo);
		ResultSet rs = ps.executeQuery();
		T t = EntityTool.getEntityByRs(rs, cls);
		rs.close();
		ps.close();
		return t;
	}

	/**
	 * 查询坯子库存信息
	 * 
	 * @param con
	 * @param matStatus
	 *            材料状态
	 * @param judgeStoveNo
	 *            判定炉号
	 * @throws SQLException
	 */
	public static List<MatBcMEntity> getQureyMatGpM(Connection con,
			String judgeStoveNo, String batchNo, String batchGroupBy,
			int takeCount, String heatPlanNoNext, String storageNo)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.QueryMatGp");
		sql += " AND T.HEAT_PLAN_NO_NEXT='" + heatPlanNoNext + "'";
		sql += "  ORDER BY DECODE(T1.STORAGE_NO,?,0,1),T.MAT_NO ASC) where rownum<=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		ps.setObject(2, batchNo);
		ps.setObject(3, batchGroupBy);
		// ps.setObject(4, proessCode);
		ps.setObject(4, storageNo);
		ps.setObject(5, takeCount);
		ResultSet rs = ps.executeQuery();
		List<MatBcMEntity> list = EntityTool.getEntitysByRs(rs,
				MatBcMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatBcMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 回库查库存
	 * 
	 * @param con
	 * @param matStatus
	 *            材料状态
	 * @param judgeStoveNo
	 *            判定炉号
	 * @throws SQLException
	 */
	public static List<MatBcMEntity> getQureyMatBcList(Connection con,
			String judgeStoveNo, String batchNo, String batchGroupBy,
			int takeCount, String heatPlanNoNext, String woId)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.QueryMatBcList");
		sql += " AND T.HEAT_PLAN_NO_NEXT='" + heatPlanNoNext + "'";
		sql += " AND T.WO_ID ='" + woId + "'";
		sql += "  ORDER BY T.MAT_NO ASC) where rownum<=?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, judgeStoveNo);
		ps.setObject(2, batchNo);
		ps.setObject(3, batchGroupBy);
		// ps.setObject(4, proessCode);
		ps.setObject(4, takeCount);
		ResultSet rs = ps.executeQuery();
		List<MatBcMEntity> list = EntityTool.getEntitysByRs(rs,
				MatBcMEntity.class);
		ps.close();
		rs.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (MatBcMEntity listL : list) {
				QualityDesignTool.nullConverNullString(listL);
			}
			return list;
		}
	}

	/**
	 * 上料实绩保存
	 * 
	 * @param load
	 *            上料实绩实体类
	 * @return
	 */
	public CoreReturnObject saveLoadResult(ArrayList<String> list, String slGx,
			String woId) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = null;
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		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);
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		String systime = dateFormat.format(now);
		String last = date.format(ca.getTime()) + " 21:00:00";
		String balYearMoth = com.steering.common.StringTool.GetBalYear("120501"); //改25号结转
		try {
			con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			con.setAutoCommit(false); // 取消自动提交事务
			PreparedStatement ps = null;
			ResultSet rs = null;
			// 查询工单信息
			PlnZyJgWorkbillEntity work = getPlanWoId(con, woId,
					PlnZyJgWorkbillEntity.class);
			if (work == null) {
				core.setV_errMsg("未查询到工单信息,请确认!");
				return core;
			}
			if (!work.getZyBillStatus().equals("1")
					&& !work.getZyBillStatus().equals("2")) {
				core.setV_errMsg("选择的工单不是下发和生产中的状态,不允许上料！");
				return core;
			}
			String lutime = systime;
			String userName = list.get(0);// 创建人
			String userTime = list.get(1);// 创建时间
			String bc = list.get(2);
			String bz = list.get(3);
			String proTime = list.get(5);// 上料日期
			int count = Integer.parseInt(list.get(4));// 上料支数
			String loadUser = list.get(6);
			String memo = list.get(7);
			String place = list.get(8);
			String mach = list.get(9);
			String failcount=list.get(10);
			if (PlanService.compareDate(systime, last) == true) {
				// 班次
				if (bz.equals("3")) {
					ca.add(Calendar.DAY_OF_MONTH, 1);
					lutime = date.format(ca.getTime()) + " 00:00:01";
				}
			}
			String deleteYdmBack = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.deleteYdmBack");//
			PreparedStatement patsdeleteYdmBack = con
					.prepareStatement(deleteYdmBack);

			String insertYdmBack = CoreXmlSqlParsersFactory
					.getSql("FrmMcpLoading.insertYdmBack");//
			PreparedStatement patsinsertYdmBack = con
					.prepareStatement(insertYdmBack);
			String storageNo = "";
			String sqlload = CoreXmlSqlParsersFactory
					.getSql("FrmInLibrary.queryStorageType");
			psta = con.prepareStatement(sqlload);
			psta.setObject(1, work.getPlineCode());
			rs = psta.executeQuery();
			if (rs.next()) {
				storageNo = rs.getString("STORAGE_NO");
			}
			rs.close();
			psta.close();
			// 查询物料信息
			List<MatBcMEntity> listMat = getQureyMatGpM(con, work
					.getFeedHeatNo(), work.getBatchNo(),
					work.getBatchGroudNo(), count, work.getHeatPlanNo(),
					storageNo);

			if (listMat == null) {
				core.setV_errMsg("未查询到物料信息,请确认!");
				return core;
			}

			if (listMat.size() != count) {
				core.setV_errMsg("库存不够,无法上足" + count + "支");
				return core;
			}
			String sendOut = querySendOut(con, "800801");
			// 获取出库流水号
			String butessMatNo = querySendOutList(con, sendOut, "800801");
			int outNo = Integer.parseInt(butessMatNo.substring(butessMatNo
					.length() - 4, butessMatNo.length()));
			String sendOutlist = querySendOut(con, "800809");
			// 获取出库流水号
			String butessMatNolist = querySendOutList(con, sendOutlist,
					"800809");
			int outNolist = Integer.parseInt(butessMatNolist.substring(
					butessMatNolist.length() - 4, butessMatNolist.length()));
			double wegth = 0;
			int counNum = 0;
			int Instorage = 0;
			for (MatBcMEntity lvMat : listMat) {
				if (lvMat.getStorageNo().equals("")) {
					core.setV_errMsg("库存正在倒运中,无法上料!");
					return core;
				}
				if (!lvMat.getBelongCode().equals("002001001")) {
					core.setV_errMsg("库存所属权不是生产计划部,无法上料!");
					return core;
				}
				if (!lvMat.getStorageNo().equals(storageNo)) {
					if (work.getIsLoadFlag().equals("1")) {
						patsdeleteYdmBack.setObject(1, work.getPlineCode());
						patsdeleteYdmBack.setObject(2, lvMat.getMatNo());
						patsdeleteYdmBack.addBatch();
						patsdeleteYdmBack.executeBatch();

						patsinsertYdmBack.setObject(1, work.getPlineCode());
						patsinsertYdmBack.setObject(2, lvMat.getMatNo());
						patsinsertYdmBack.addBatch();
						patsinsertYdmBack.executeBatch();

					} else {
						if (Instorage == 0) {
							core.setV_errMsg("库存未在投料库,无法上料");
							return core;
						} else {
							core.setV_errMsg("只有" + Instorage + "支在投料库 ,无法上足"
									+ count + "支");
							return core;
						}
					}
				}
				Instorage++;
				YdmBcOutlistEntity outBc = new YdmBcOutlistEntity();
				wegth += Double.parseDouble(lvMat.getActWeight());
				counNum += Integer.parseInt(lvMat.getActCount());

				if ("1".equals(lvMat.getLockFlag())) {
					core.setV_errMsg("库存存在质量封锁料,无法上足" + count + "支");
					return core;
				}

				if ("1".equals(lvMat.getMngLockFlag())) {
					core.setV_errMsg("库存存在管理封锁料,无法上足" + count + "支");
					return core;
				}
				// 新增出库
				outBc.setOutstockSqe(sendOut
						+ StringTool.formatDecimal(outNo, "0000"));
				outBc.setOutstockPlanNo(work.getWoId());
				outBc.setOutstockDoc(sendOut);
				outBc.setMatNo(lvMat.getMatNo());
				outBc.setStoveNo(lvMat.getStoveNo());
				outBc.setJudgeStoveNo(lvMat.getJudgeStoveNo());
				outBc.setBatchNo(lvMat.getBatchNo());
				outBc.setBatchGroudNo(lvMat.getBatchGroudNo());
				outBc.setMaterialNo(lvMat.getMaterialNo());
				outBc.setMaterialName(lvMat.getMaterialName());
				outBc.setProduccode(lvMat.getProduccode());
				outBc.setProducname(lvMat.getProducname());
				outBc.setStdCode(lvMat.getStdCode());
				outBc.setStdName(lvMat.getStdName());
				outBc.setStdStyle(lvMat.getStdStyle());
				outBc.setStdStyleDesc(lvMat.getStdStyleDesc());
				outBc.setGradecode(lvMat.getGradecode());
				outBc.setGradename(lvMat.getGradename());
				outBc.setSteelcode(lvMat.getSteelcode());
				outBc.setSteelname(lvMat.getSteelname());
				outBc.setSpecCode(lvMat.getSpecCode());
				outBc.setSpecName(lvMat.getSpecName());
				outBc.setModelCode(lvMat.getModelCode());
				outBc.setModelDesc(lvMat.getModelDesc());
				outBc.setProOrderNo(lvMat.getProOrderNo());
				outBc.setPlineCode(lvMat.getPlineCode());
				outBc.setPlineName(lvMat.getPlineName());
				outBc.setActCount(lvMat.getActCount());
				outBc.setActWeight(lvMat.getActWeight());
				outBc.setActTheoryWeight(lvMat.getActTheoryWeight());
				outBc.setActDimater(lvMat.getActDimater());
				outBc.setActHeight(lvMat.getActHeight());
				outBc.setActLen(lvMat.getActLen());
				outBc.setActLenMax(lvMat.getActLenMax());
				outBc.setActLenMin(lvMat.getActLenMin());
				outBc.setBelongCode(lvMat.getBelongCode());
				outBc.setBelongName(lvMat.getBelongName());
				outBc.setBarCode(lvMat.getBarCode());
				outBc.setStorageNo(lvMat.getStorageNo());
				outBc.setLocNo(lvMat.getLocNo());
				outBc.setLaryNo(lvMat.getLaryNo());
				outBc.setLocationNo(lvMat.getLocationNo());
				outBc.setStorageAttr(lvMat.getStorageAttr());
				outBc.setStorageAttrName(lvMat.getStorageAttrName());
				outBc.setStorageTypeNo(lvMat.getStorageTypeNo());
				outBc.setStorageTypeName(lvMat.getStorageTypeName());
				outBc.setManagementNo(lvMat.getManagementNo());
				outBc.setManagementName(lvMat.getManagementName());
				outBc.setRpoTime(lvMat.getInstockTime());
				outBc.setProBc(lvMat.getProBc());
				outBc.setProBz(lvMat.getProBz());
				outBc.setInstockTime(lvMat.getInstockTime());
				outBc.setInBc(lvMat.getProBc());
				outBc.setInBz(lvMat.getProBz());
				outBc.setOutstockTime(userTime);
				outBc.setOutBc(bc);
				outBc.setOutBz(bz);
				outBc.setCreateName(userName);
				outBc.setCreateTime(userTime);
				outBc.setOrdPk(lvMat.getOrdPk());
				outBc.setOrdLnPk(lvMat.getOrdLnPk());
				outBc.setOrdLnDlyPk(lvMat.getOrdLnDlyPk());
				outBc.setDepartmentCode(lvMat.getDepartmentCode());
				outBc.setDepartmentDesc(lvMat.getDepartmentDesc());
				outBc.setOrderNo(lvMat.getOrderNo());
				outBc.setOrderSeq(lvMat.getOrderSeq());
				outBc.setDeliveryNo(lvMat.getOrderSeqNo());
				outBc.setProductFlag("801401");
				outBc.setMatStatus("80150105");
				outBc.setJudgeStdCode(lvMat.getJudgeStdCode());
				outBc.setJudgeStdName(lvMat.getJudgeStdDes());
				outBc.setPonderNo(lvMat.getPonderNo());
				outBc.setPonderGrossWt(lvMat.getPonderGrossWt());
				outBc.setLoadPlineCode(work.getPlineCode());
				outBc.setLoadPlineName(work.getPlineName());
				outBc.setOutstockTypeCode("800801");
				outBc.setOutstockTypeName("投料出库");
				outBc.setProcessCdoe(lvMat.getProcessCdoe());
				outBc.setProcessDesc(lvMat.getProcessDesc());
				outBc.setBalYearMonth(balYearMoth);
				// 新增出库表记录 投料出库
				String addOut = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("CoupLoadingService.AddOutList"), outBc);
				ps = con.prepareStatement(addOut);
				ps.executeUpdate();
				ps.close();
				outNo++;

				// 新增出库表记录 产权出库
				outBc.setOutstockSqe(sendOutlist
						+ StringTool.formatDecimal(outNolist, "0000"));
				outBc.setOutstockDoc(sendOutlist);
				outBc.setOutstockTypeCode("800809");
				outBc.setOutstockTypeName("产权出库");
				String addOutC = EntityTool
						.fillSqlByEntity(CoreXmlSqlParsersFactory
								.getSql("CoupLoadingService.AddOutList"), outBc);
				ps = con.prepareStatement(addOutC);
				ps.executeUpdate();
				ps.close();
				outNolist++;

				// 修改库存状态
				String update = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.updateMatZcPlan");
				ps = con.prepareStatement(update);
				ps.setObject(1, "80150105");
				ps.setObject(2, lvMat.getMatNo());
				ps.executeUpdate();
				ps.close();
				// 删除位置表信息
				String deleteButt = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.deleteButter");
				ps = con.prepareStatement(deleteButt);
				ps.setObject(1, lvMat.getMatNo());
				ps.executeUpdate();
				ps.close();

				// 复制物料主表到MAT_BC_BACKUP表
				String addBact = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.CopyBcMat");
				ps = con.prepareStatement(addBact);
				ps.setObject(1, lvMat.getMatNo());
				ps.executeUpdate();
				ps.close();

				// 删除物料主表
				String deleteMat = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.DeteleBcMat");
				ps = con.prepareStatement(deleteMat);
				ps.setObject(1, lvMat.getMatNo());
				ps.executeUpdate();
				ps.close();

			}
			String resultNo = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.queryLoadRESULT");// 查询最大实绩编号
			psta = con.prepareStatement(sqlloading);
			psta.setObject(1, work.getPlineCode());
			psta.setObject(2, "01");
			psta.setObject(3, work.getPlineCode() + "01");
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();

			CouplingLoadingResultEntity load = new CouplingLoadingResultEntity();
			load.setResultNo(resultNo);
			load.setWoId(woId);
			load.setJudgeStoveNo(work.getJudgeStoveNo());
			load.setBatchNo(work.getBatchNo());
			load.setBatchGroudNo(work.getBatchGroudNo());
			load.setLoadPieces(String.valueOf(count));
			load.setLoadWeight(String.valueOf(wegth));
			load.setLoadShift(bc);
			load.setLoadGroup(bz);
			load.setLoadUser(loadUser);
			load.setLoadTime(proTime);
			load.setPlineCode(work.getPlineCode());
			load.setPlineName(work.getPlineName());
			load.setCreateTime(lutime);
			load.setProTime(proTime);
			load.setGradecode(work.getGradecode());
			load.setGradename(work.getGradename());
			load.setSteelcode(work.getSteelcode());
			load.setSteelname(work.getSteelname());
			load.setSpecJg(work.getSpecJg());
			load.setSpecJgDesc(work.getSpecJgDesc());
			load.setStdCode(work.getStdCode());
			load.setStdName(work.getStdName());
			load.setStyleJg(work.getStyleJg());
			load.setStdStyle(work.getStdStyle());
			load.setStdStyleDesc(work.getStdStyleDesc());
			load.setProduccodeJg(work.getProduccodeJg());
			load.setProduccodeJgDesc(work.getProduccodeJgDesc());
			load.setCodeJg(work.getCodeJg());
			load.setCodeJgBl(work.getCodeJgBl());
			load.setModelCode(work.getModelCode());
			load.setModelDesc(work.getModelDesc());
			load.setIfMonitorJg(work.getIfMonitorJg());
			load.setPrdcrNo(work.getPrdcrNo());
			load.setMemo(memo);
			load.setConfirm("0");
			load.setPlace(place);
			load.setMachineNo(mach);
			load.setFailNum(failcount);

			// 新增上料实绩表记录
			String addLoad = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.AddLoadResult"), load);
			ps = con.prepareStatement(addLoad);
			ps.executeUpdate();
			ps.close();

			// 修改工单状态
			String update = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updatePlanStatus");
			ps = con.prepareStatement(update);
			ps.setObject(1, "2");
			ps.setObject(2, count);
			ps.setObject(3, wegth);
			ps.setObject(4, woId);
			ps.executeUpdate();
			ps.close();

			CouplingComInfoEntity comInfo = new CouplingComInfoEntity();
			// 新增接箍公共信息
			comInfo.setResultNo(resultNo);
			comInfo.setWoId(woId);
			comInfo.setJudgeStoveNo(work.getJudgeStoveNo());
			comInfo.setProPlanId("");
			comInfo.setCodeJgBl(work.getCodeJgBl());
			comInfo.setIfOutRpo("0");
			comInfo.setPlineCode(work.getPlineCode());
			comInfo.setPlineName(work.getPlineName());
			comInfo.setStationCode(slGx);
			comInfo.setStationDesc("上料");
			comInfo.setBatchNo(work.getBatchNo());
			comInfo.setBlWlId(work.getBlWlId());
			comInfo.setBlWlDesc(work.getBlWlDesc());
			comInfo.setMinD(work.getMinD());
			comInfo.setMinH(work.getMinH());
			comInfo.setAimlengthBl(work.getAimlengthBl());
			comInfo.setAimCutLen(work.getAimCutLen());
			comInfo.setLengthMax(work.getLengthMax());
			comInfo.setLengthMin(work.getLengthMin());
			comInfo.setTaper(work.getTaper());
			comInfo.setCodeJg(work.getCodeJg());
			comInfo.setGradecode(work.getGradecode());
			comInfo.setGradename(work.getGradename());
			comInfo.setSteelcode(work.getSteelcode());
			comInfo.setSteelname(work.getSteelname());
			comInfo.setModelCode(work.getModelCode());
			comInfo.setModelDesc(work.getModelDesc());
			comInfo.setOutdiameter(work.getDiameterJg());
			comInfo.setWallthick(work.getHeightJg());
			comInfo.setSpecJg(work.getSpecJg());
			comInfo.setSpecJgDesc(work.getSpecJgDesc());
			comInfo.setProduccodeJg(work.getProduccodeJg());
			comInfo.setProduccodeJgDesc(work.getProduccodeJgDesc());
			comInfo.setStdStyle(work.getStdStyle());
			comInfo.setStdStyleDesc(work.getStdStyleDesc());
			comInfo.setStdCode(work.getStdCode());
			comInfo.setStdName(work.getStdName());
			comInfo.setStyleJg(work.getStyleJg());
			comInfo.setDiameter(work.getDiameter());
			comInfo.setCheckJVal(work.getCheckJVal());
			comInfo.setAimlength(work.getLengthJg());
			comInfo.setSingleweight(work.getSingleweight());
			comInfo.setIfMonitorJg(work.getIfMonitorJg());
			comInfo.setPrdcrNo(work.getPrdcrNo());
			comInfo.setMachNo(work.getLatheGrpNo());
			comInfo.setMachNm(work.getLatheGrpNm());
			comInfo.setProNum(work.getPlanBlNum());
			comInfo.setProWt(work.getPlanBlWt());
			comInfo.setRroTime(userTime);
			comInfo.setProUser(userName);
			comInfo.setProShift(bc);
			comInfo.setProGroup(bz);
			comInfo.setIfAllOutWork(work.getIfAllOutWork());
			comInfo.setCreateTime(userTime);
			comInfo.setColOperator(loadUser);
			comInfo.setOutwlId(work.getOutwlId());
			comInfo.setOutwlDesc(work.getOutwlDesc());
			comInfo.setStationStatus("1");
			// 新增接箍公共记录
			String addInfo = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.AddPlnInfo"), comInfo);
			ps = con.prepareStatement(addInfo);
			ps.executeUpdate();
			ps.close();

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

	/***
	 * 删除上料实绩
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject deleteLoadResult(
			ArrayList<ArrayList<String>> list1, String slGx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs1 = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM",
				Locale.CHINA);
		String systime = dateFormat.format(now);
		String balYearMoth = "";//改25号结转
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String sqlDelete = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.DeleteLoadResult");// 删除上料实绩
			PreparedStatement patDelete = con.prepareStatement(sqlDelete);
			String addBackup = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.CopyBackup");// 复制MAT_BC_BACKUP表到物料主表
			PreparedStatement patsAddBackup = con.prepareStatement(addBackup);
			String updateMat = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updateMatZcPlan");// 修改库存状态
			PreparedStatement patsUpMat = con.prepareStatement(updateMat);
			String AddButt = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.AddButter");// 新增位置表
			PreparedStatement patsButt = con.prepareStatement(AddButt);
			String updatePlan = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updatePlanStatus");// 修改工单状态
			PreparedStatement patsUpPlan = con.prepareStatement(updatePlan);
			String deleteOut = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.deleteOutList");// 删除出库记录投料出库
			PreparedStatement patsDeleteOut = con.prepareStatement(deleteOut);
			String deletePort = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.deletePort");// 删除接箍队列表
			PreparedStatement patsDeletePort = con.prepareStatement(deletePort);
			String addOutList = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.DeleteOutListQuan");// 删除出库记录产权出库
			PreparedStatement patsDeleteOutList = con
					.prepareStatement(addOutList);
			String deteleBackup = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.DeteleBackup");// 删除MAT_BC_BACKUP表
			PreparedStatement deteleBackupPath = con
					.prepareStatement(deteleBackup);
			String deletePlnInfo = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.deletePlnInfo");// 删除接箍公共信息
			PreparedStatement patsPlan = con.prepareStatement(deletePlnInfo);
			String deleteOffline = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.DeleteOffline");// 清空离线实绩
			PreparedStatement patsdeleteOffline = con
					.prepareStatement(deleteOffline);

			for (int i = 0; i < list1.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) list1.get(i);
				String resNo = list.get(0);
				String userName = list.get(1);// 创建人
				String userTime = list.get(2);// 创建时间
				String feedHeatNo = list.get(5);// 上料炉号

				// 查询上料实绩
				CouplingLoadingResultEntity coupLod = getCoupLoad(con, resNo,
						CouplingLoadingResultEntity.class);
				balYearMoth = com.steering.common.StringTool.GetBalYear(com.steering.common.StringTool.GetSupplyUnit(coupLod.getPlineCode()));//改25号结转
				if (coupLod == null) {
					core.setV_errMsg("未查询到上料信息,请确认!");
					return core;
				}
				int count = Integer.parseInt(coupLod.getLoadPieces());

				// 判断是否有锯切实绩
				int countQg = 0;
				String sqlloading = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.QueryCutLoading");
				psta = con.prepareStatement(sqlloading);
				psta.setObject(1, coupLod.getWoId());
				rs1 = psta.executeQuery();
				if (rs1.next()) {
					countQg = Integer.parseInt(rs1.getString("COUNT"));
				}
				rs1.close();
				psta.close();
				if (countQg != 0) {
					core.setV_errMsg("该工单存在后工序点实绩，请先删除后工序点实绩!");
					return core;
				}
				// // 判断删除限制
				// String loadbc = "";
				// sqlloading = CoreXmlSqlParsersFactory
				// .getSql("CoupLoadingService.getCoupCountById");
				// psta = con.prepareStatement(sqlloading);
				// psta.setObject(1, coupLod.getWoId());
				// rs1 = psta.executeQuery();
				// if (rs1.next()) {
				// loadbc = rs1.getString("COUNT");
				// }
				// rs1.close();
				// psta.close();
				// if (loadbc.equals("")) {
				// core.setV_errMsg("上料记录已删除！");
				// return core;
				// }

				List<YdmBcOutlistEntity> listOut = new ArrayList<YdmBcOutlistEntity>();
				String sqlMatNo = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.queryYdmOut");// 查询位置信息
				psta = con.prepareStatement(sqlMatNo);
				psta.setObject(1, feedHeatNo);
				psta.setObject(2, coupLod.getBatchNo());
				psta.setObject(3, coupLod.getWoId());
				psta.setObject(4, coupLod.getWoId());
				psta.setObject(5, count + 1);
				ResultSet rs = psta.executeQuery();
				listOut = EntityTool.getEntitysByRs(rs,
						YdmBcOutlistEntity.class);
				rs.close();
				psta.close();
				double wetigh = 0;

				if (listOut == null) {
					core.setV_errMsg("未找到出库记录!");
					return core;
				}
				for (YdmBcOutlistEntity lvMat : listOut) {

					wetigh += Double.parseDouble(lvMat.getActWeight());

					// 新增位置表
					patsButt.setObject(1, lvMat.getMatNo());
					patsButt.setObject(2, feedHeatNo);
					patsButt.setObject(3, lvMat.getBatchNo());
					patsButt.setObject(4, lvMat.getBatchGroudNo());
					patsButt.setObject(5, lvMat.getStorageNo());
					patsButt.setObject(6, lvMat.getLocationNo());
					patsButt.setObject(7, lvMat.getLaryNo());
					patsButt.setObject(8, lvMat.getLocNo());
					patsButt.setObject(9, lvMat.getStorageAttr());
					patsButt.setObject(10, lvMat.getStorageAttrName());
					patsButt.setObject(11, lvMat.getStorageTypeNo());
					patsButt.setObject(12, lvMat.getStorageTypeName());
					patsButt.setObject(13, lvMat.getManagementNo());
					patsButt.setObject(14, lvMat.getManagementName());
					patsButt.setObject(15, userName);
					patsButt.setObject(16, userTime);
					patsButt.setObject(17, lvMat.getDepartmentCode());
					patsButt.setObject(18, lvMat.getDepartmentDesc());
					patsButt.addBatch();

					String OutlistMonth = "";
					String sqlqueryplan = CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.OutlistMonth");// 查询出库表结算日期
					psta = con.prepareStatement(sqlqueryplan);
					psta.setObject(1, lvMat.getOutstockSqe());
					rs = psta.executeQuery();
					if (rs.next()) {
						OutlistMonth = rs.getString("BAL_YEAR_MONTH");
					}
					rs.close();
					psta.close();
					if (!OutlistMonth.equals(balYearMoth)) {
						core.setV_errMsg("上料结算日期和撤销上料结算日期不在同一个月，不允许撤销上料!");
						return core;
					}

					// 删除出库表投料出库
					patsDeleteOut.setObject(1, lvMat.getOutstockSqe());
					patsDeleteOut.addBatch();

					// 删除接箍队列表
					patsDeletePort.setObject(1, lvMat.getMatNo());
					patsDeletePort.setObject(2, coupLod.getWoId());
					patsDeletePort.addBatch();

					// 删除出库表产权出库
					patsDeleteOutList.setObject(1, lvMat.getMatNo());
					patsDeleteOutList.setObject(2, coupLod.getWoId());
					patsDeleteOutList.addBatch();

					// 复制backup表到物料主表
					patsAddBackup.setObject(1, lvMat.getMatNo());
					patsAddBackup.addBatch();

					// 删除backup表
					deteleBackupPath.setObject(1, lvMat.getMatNo());
					deteleBackupPath.addBatch();

					// 修改库存状态
					patsUpMat.setObject(1, "80150103");
					patsUpMat.setObject(2, lvMat.getMatNo());
					patsUpMat.addBatch();

					// 清空离线实绩
					patsdeleteOffline.setObject(1, coupLod.getWoId());
					patsdeleteOffline.addBatch();

				}
				patsButt.executeBatch();
				patsDeleteOut.executeBatch();
				patsDeleteOutList.executeBatch();
				patsAddBackup.executeBatch();
				deteleBackupPath.executeBatch();
				patsUpMat.executeBatch();
				patsDeletePort.executeBatch();
				patsdeleteOffline.executeBatch();
				// 删除上料实绩
				patDelete.setObject(1, resNo);
				patDelete.addBatch();
				// 删除接箍公共信息
				patsPlan.setObject(1, resNo);
				patsPlan.setObject(2, slGx);
				patsPlan.setObject(3, coupLod.getWoId());
				patsPlan.addBatch();

				patDelete.executeBatch();
				patsPlan.executeBatch();
				// 查询是否上料全部删除
				int cont = 0;
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.getCoupCountById");
				psta = con.prepareStatement(sqlloading);
				psta.setObject(1, coupLod.getWoId());
				rs1 = psta.executeQuery();
				if (rs1.next()) {
					cont = Integer.parseInt(rs1.getString("COUNT"));
				}
				rs1.close();
				psta.close();

				String zyBillStatus = "2";
				if (cont == 0) {
					zyBillStatus = "1";
				}
				// 修改工单状态
				patsUpPlan.setObject(1, zyBillStatus);
				patsUpPlan.setObject(2, "-" + coupLod.getLoadPieces());
				patsUpPlan.setObject(3, "-" + coupLod.getLoadWeight());
				patsUpPlan.setObject(4, coupLod.getWoId());
				patsUpPlan.addBatch();

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

	/***
	 * 确认
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject getConfirm(ArrayList<ArrayList<String>> list1) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs1 = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM",
				Locale.CHINA);
		String systime = dateFormat.format(now);
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String sqlDelete = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.getConfirm");// 修改是否确认字段
			PreparedStatement patDelete = con.prepareStatement(sqlDelete);

			for (int i = 0; i < list1.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) list1.get(i);
				String resNo = list.get(0);
				String user = list.get(1);
				String time = list.get(2);

				// 查询上料实绩
				CouplingLoadingResultEntity coupLod = getCoupLoad(con, resNo,
						CouplingLoadingResultEntity.class);
				if (coupLod == null) {
					core.setV_errMsg("未查询到上料信息,请确认!");
					return core;
				}
				// 修改上料实绩确认按钮
				patDelete.setObject(1, user);
				patDelete.setObject(2, time);
				patDelete.setObject(3, resNo);
				patDelete.addBatch();

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

	/***
	 * 取消确认
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject getCancelConfirm(ArrayList<ArrayList<String>> list1) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs1 = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM",
				Locale.CHINA);
		String systime = dateFormat.format(now);
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String sqlDelete = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.getCancelConfirm");// 修改是否确认字段
			PreparedStatement patDelete = con.prepareStatement(sqlDelete);

			for (int i = 0; i < list1.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) list1.get(i);
				String resNo = list.get(0);
				String user = list.get(1);
				String time = list.get(2);

				// 查询上料实绩
				CouplingLoadingResultEntity coupLod = getCoupLoad(con, resNo,
						CouplingLoadingResultEntity.class);
				if (coupLod == null) {
					core.setV_errMsg("未查询到上料信息,请确认!");
					return core;
				}
				// 修改上料实绩确认按钮
				patDelete.setObject(1, user);
				patDelete.setObject(2, time);
				patDelete.setObject(3, resNo);
				patDelete.addBatch();
				patDelete.executeBatch();
			}
			core.setV_errMsg("取消确认成功!");
			core.setResult(1);
			con.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (con != null) {
				try {
					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/***
	 * 回库
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject doBackResult(ArrayList<ArrayList<String>> list1,
			String slGx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs1 = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM",
				Locale.CHINA);
		String systime = dateFormat.format(now);
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String sqlDelete = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updateLoadResult");// 修改上料实绩支吨
			PreparedStatement patDelete = con.prepareStatement(sqlDelete);
			String addBackup = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.CopyBackup");// 复制MAT_BC_BACKUP表到物料主表
			PreparedStatement patsAddBackup = con.prepareStatement(addBackup);
			String updateMat = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updateMatZcPlan");// 修改库存状态
			PreparedStatement patsUpMat = con.prepareStatement(updateMat);
			String AddButt = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.AddButter");// 新增位置表
			PreparedStatement patsButt = con.prepareStatement(AddButt);
			String updatePlan = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updatePlanStatusBack");// 修改工单状态
			PreparedStatement patsUpPlan = con.prepareStatement(updatePlan);
			// String deleteOut = CoreXmlSqlParsersFactory
			// .getSql("CoupLoadingService.InsertOutList");// 新增负的出库记录投料出库
			// PreparedStatement patsDeleteOut =
			// con.prepareStatement(deleteOut);
			//
			// String addOutList = CoreXmlSqlParsersFactory
			// .getSql("CoupLoadingService.InsertOutListQuan");// 新增负的出库记录产权出库
			// PreparedStatement patsDeleteOutList = con
			// .prepareStatement(addOutList);
			String deteleBackup = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.DeteleBackup");// 删除MAT_BC_BACKUP表
			PreparedStatement deteleBackupPath = con
					.prepareStatement(deteleBackup);
			String deletePlnInfo = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updatePlnInfo");// 修改接箍公共信息
			PreparedStatement patsPlan = con.prepareStatement(deletePlnInfo);

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

			for (int i = 0; i < list1.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) list1.get(i);
				String resNo = list.get(0);
				String backNum = list.get(1);
				String userName = list.get(2);
				String userTime = list.get(3);
				String woId = list.get(4);

				// 查询上料实绩
				CouplingLoadingResultEntity coupLod = getCoupLoad(con, resNo,
						CouplingLoadingResultEntity.class);
				if (coupLod == null) {
					core.setV_errMsg("未查询到上料信息,请确认!");
					return core;
				}
				int count = Integer.parseInt(coupLod.getLoadPieces());

				List<YdmBcOutlistEntity> listOut = new ArrayList<YdmBcOutlistEntity>();
				String sqlMatNo = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.queryYdmOut");// 查询出库信息
				psta = con.prepareStatement(sqlMatNo);
				psta.setObject(1, coupLod.getJudgeStoveNo());
				psta.setObject(2, coupLod.getBatchNo());
				psta.setObject(3, coupLod.getWoId());
				psta.setObject(4, coupLod.getWoId());
				psta.setObject(5, Integer.parseInt(backNum) + 1);
				ResultSet rs = psta.executeQuery();
				listOut = EntityTool.getEntitysByRs(rs,
						YdmBcOutlistEntity.class);
				rs.close();
				psta.close();
				double wetigh = 0;

				if (listOut == null) {
					core.setV_errMsg("未找到出库记录!");
					return core;
				}

				String SYNC_FLAG = "0";
				String sql = CoreXmlSqlParsersFactory
						.getSql("FrmVrpLoading.SelectSyncFlag");// 查询财务系统同步状态
				psta = con.prepareStatement(sql);
				psta.setObject(1, coupLod.getJudgeStoveNo());
				psta.setObject(2, coupLod.getBatchNo());
				psta.setObject(3, coupLod.getWoId());
				psta.setObject(4, Integer.parseInt(backNum));
				rs = psta.executeQuery();
				if (rs.next()) {
					SYNC_FLAG = rs.getString("SYNC_FLAG");
				}
				rs.close();
				psta.close();
				if (SYNC_FLAG.equals("1")) {
					core.setV_errMsg("财务系统同步出库信息，请联系财务撤销同步信息!");
					return core;
				}
				// 删除财务系统接口数据
				deleteFmis.setObject(1, coupLod.getJudgeStoveNo());
				deleteFmis.setObject(2, coupLod.getBatchNo());
				deleteFmis.setObject(3, coupLod.getWoId());
				deleteFmis.setObject(4, Integer.parseInt(backNum));
				deleteFmis.addBatch();
				deleteFmis.executeBatch();

				for (YdmBcOutlistEntity lvMat : listOut) {

					String OutlistMonth = "";
					String sqlqueryplan = CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.OutlistMonth");// 查询出库表结算日期
					psta = con.prepareStatement(sqlqueryplan);
					psta.setObject(1, lvMat.getOutstockSqe());
					rs = psta.executeQuery();
					if (rs.next()) {
						OutlistMonth = rs.getString("BAL_YEAR_MONTH");
					}
					rs.close();
					psta.close();
					if (!OutlistMonth.equals(systime)) {
						core.setV_errMsg("上料结算日期和撤销上料结算日期不在同一个月，不允许撤销上料!");
						return core;
					}

					wetigh += Double.parseDouble(lvMat.getActWeight());

					// 新增位置表
					patsButt.setObject(1, lvMat.getMatNo());
					patsButt.setObject(2, lvMat.getJudgeStoveNo());
					patsButt.setObject(3, lvMat.getBatchNo());
					patsButt.setObject(4, lvMat.getBatchGroudNo());
					patsButt.setObject(5, lvMat.getStorageNo());
					patsButt.setObject(6, lvMat.getLocationNo());
					patsButt.setObject(7, lvMat.getLaryNo());
					patsButt.setObject(8, lvMat.getLocNo());
					patsButt.setObject(9, lvMat.getStorageAttr());
					patsButt.setObject(10, lvMat.getStorageAttrName());
					patsButt.setObject(11, lvMat.getStorageTypeNo());
					patsButt.setObject(12, lvMat.getStorageTypeName());
					patsButt.setObject(13, lvMat.getManagementNo());
					patsButt.setObject(14, lvMat.getManagementName());
					patsButt.setObject(15, userName);
					patsButt.setObject(16, userTime);
					patsButt.setObject(17, lvMat.getDepartmentCode());
					patsButt.setObject(18, lvMat.getDepartmentDesc());
					patsButt.addBatch();

					// // 新增负的出库表投料出库
					// patsDeleteOut.setObject(1, lvMat.getOutstockSqe());
					// patsDeleteOut.addBatch();

					// // 新增负的出库表产权出库
					// patsDeleteOutList.setObject(1, lvMat.getMatNo());
					// patsDeleteOutList.setObject(2, coupLod.getWoId());
					// patsDeleteOutList.addBatch();

					// 复制产权出库数据
					String addBact = CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.CopyBcOutlist");
					psta = con.prepareStatement(addBact);
					psta.setObject(1, "-" + 1);
					psta.setObject(2, "-"
							+ Double.parseDouble(lvMat.getActWeight()));
					psta.setObject(3, "-"
							+ Double.parseDouble(lvMat.getActWeight()));
					psta.setObject(4, lvMat.getMatNo());
					psta.setObject(5, woId);
					psta.executeUpdate();
					psta.close();

					// 复制backup表到物料主表
					patsAddBackup.setObject(1, lvMat.getMatNo());
					patsAddBackup.addBatch();

					// 删除backup表
					deteleBackupPath.setObject(1, lvMat.getMatNo());
					deteleBackupPath.addBatch();

					// 修改库存状态
					patsUpMat.setObject(1, "80150102");
					patsUpMat.setObject(2, lvMat.getMatNo());
					patsUpMat.addBatch();

				}
				patsButt.executeBatch();
				// patsDeleteOut.executeBatch();
				// patsDeleteOutList.executeBatch();
				patsAddBackup.executeBatch();
				deteleBackupPath.executeBatch();
				patsUpMat.executeBatch();
				// 修改上料实绩支、吨
				patDelete.setObject(1, "-" + backNum);
				patDelete.setObject(2, "-" + wetigh);
				patDelete.setObject(3, resNo);
				patDelete.setObject(4, coupLod.getWoId());
				patDelete.addBatch();
				// 删除接箍公共信息
				patsPlan.setObject(1, "-" + backNum);
				patsPlan.setObject(2, "-" + wetigh);
				patsPlan.setObject(3, resNo);
				patsPlan.setObject(4, slGx);
				patsPlan.setObject(5, coupLod.getWoId());
				patsPlan.addBatch();

				patDelete.executeBatch();
				patsPlan.executeBatch();
				// 修改工单状态

				patsUpPlan.setObject(1, "-" + backNum);
				patsUpPlan.setObject(2, "-" + wetigh);
				patsUpPlan.setObject(3, coupLod.getWoId());
				patsUpPlan.addBatch();

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

	/***
	 * 撤销回库
	 * 
	 * @param list
	 * @return
	 */
	public CoreReturnObject doCancelBackResult(
			ArrayList<ArrayList<String>> list1, String slGx) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs1 = null;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM",
				Locale.CHINA);
		String systime = dateFormat.format(now);
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String sqlDelete = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updateLoadResult");// 修改上料实绩支吨
			PreparedStatement patDelete = con.prepareStatement(sqlDelete);
			String addBackup = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.CopyBcMat");// 物料主表到list表
			PreparedStatement patsAddBackup = con.prepareStatement(addBackup);
			String updateMat = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updateMatZcPlan");// 修改库存状态
			PreparedStatement patsUpMat = con.prepareStatement(updateMat);
			String AddButt = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.deleteButter");// 删除位置表
			PreparedStatement patsButt = con.prepareStatement(AddButt);
			String updatePlan = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updatePlanStatusBack");// 修改工单状态
			PreparedStatement patsUpPlan = con.prepareStatement(updatePlan);
			// String deleteOut = CoreXmlSqlParsersFactory
			// .getSql("CoupLoadingService.InsertOutList");// 新增负的出库记录投料出库
			// PreparedStatement patsDeleteOut =
			// con.prepareStatement(deleteOut);
			//
			// String addOutList = CoreXmlSqlParsersFactory
			// .getSql("CoupLoadingService.InsertOutListQuan");// 新增负的出库记录产权出库
			// PreparedStatement patsDeleteOutList = con
			// .prepareStatement(addOutList);
			String deteleBackup = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.DeteleBcMat");// 删除主表
			PreparedStatement deteleBackupPath = con
					.prepareStatement(deteleBackup);
			String deletePlnInfo = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.updatePlnInfo");// 修改接箍公共信息
			PreparedStatement patsPlan = con.prepareStatement(deletePlnInfo);

			for (int i = 0; i < list1.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) list1.get(i);
				String resNo = list.get(0);
				String backNum = list.get(1);
				String userName = list.get(2);
				String userTime = list.get(3);
				String woId = list.get(4);

				// 查询上料实绩
				CouplingLoadingResultEntity coupLod = getCoupLoad(con, resNo,
						CouplingLoadingResultEntity.class);
				if (coupLod == null) {
					core.setV_errMsg("未查询到上料信息,请确认!");
					return core;
				}
				int count = Integer.parseInt(coupLod.getLoadPieces());

				List<YdmBcOutlistEntity> listOut = new ArrayList<YdmBcOutlistEntity>();
				String sqlMatNo = CoreXmlSqlParsersFactory
						.getSql("CoupLoadingService.queryYdmOutList");// 查询出库信息
				psta = con.prepareStatement(sqlMatNo);
				psta.setObject(1, coupLod.getJudgeStoveNo());
				psta.setObject(2, coupLod.getBatchNo());
				psta.setObject(3, coupLod.getWoId());
				psta.setObject(4, Integer.parseInt(backNum) + 1);
				ResultSet rs = psta.executeQuery();
				listOut = EntityTool.getEntitysByRs(rs,
						YdmBcOutlistEntity.class);
				rs.close();
				psta.close();
				double wetigh = 0;

				if (listOut == null) {
					core.setV_errMsg("未找到出库记录!");
					return core;
				}
				for (YdmBcOutlistEntity lvMat : listOut) {

					String OutlistMonth = "";
					String sqlqueryplan = CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.OutlistMonth");// 查询出库表结算日期
					psta = con.prepareStatement(sqlqueryplan);
					psta.setObject(1, lvMat.getOutstockSqe());
					rs = psta.executeQuery();
					if (rs.next()) {
						OutlistMonth = rs.getString("BAL_YEAR_MONTH");
					}
					rs.close();
					psta.close();
					if (!OutlistMonth.equals(systime)) {
						core.setV_errMsg("上料结算日期和撤销上料结算日期不在同一个月，不允许撤销上料!");
						return core;
					}

					wetigh += Math
							.abs(Double.parseDouble(lvMat.getActWeight()));

					// 新增位置表
					patsButt.setObject(1, lvMat.getMatNo());
					patsButt.addBatch();

					// // 新增负的出库表投料出库
					// patsDeleteOut.setObject(1, lvMat.getOutstockSqe());
					// patsDeleteOut.addBatch();

					// // 新增负的出库表产权出库
					// patsDeleteOutList.setObject(1, lvMat.getMatNo());
					// patsDeleteOutList.setObject(2, coupLod.getWoId());
					// patsDeleteOutList.addBatch();

					// // 复制产权出库数据
					// String addBact = CoreXmlSqlParsersFactory
					// .getSql("CoupLoadingService.CopyBcOutlist");
					// psta = con.prepareStatement(addBact);
					// psta.setObject(1, "-" + backNum);
					// psta.setObject(2, "-" + wetigh);
					// psta.setObject(3, "-" + wetigh);
					// psta.setObject(4, lvMat.getMatNo());
					// psta.setObject(5, woId);
					// psta.executeUpdate();
					// psta.close();
					// 删除红冲出库记录
					String addBact = CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.DeteleBcOutlist");
					psta = con.prepareStatement(addBact);
					psta.setObject(1, woId);
					psta.setObject(2, lvMat.getMatNo());
					psta.executeUpdate();
					psta.close();

					// 复制backup表到物料主表
					patsAddBackup.setObject(1, lvMat.getMatNo());
					patsAddBackup.addBatch();

					// 删除backup表
					deteleBackupPath.setObject(1, lvMat.getMatNo());
					deteleBackupPath.addBatch();

					// 修改库存状态
					patsUpMat.setObject(1, "80150105");
					patsUpMat.setObject(2, lvMat.getMatNo());
					patsUpMat.addBatch();

				}
				patsButt.executeBatch();
				// patsDeleteOut.executeBatch();
				// patsDeleteOutList.executeBatch();
				patsAddBackup.executeBatch();
				deteleBackupPath.executeBatch();
				patsUpMat.executeBatch();
				// 修改上料实绩支、吨
				patDelete.setObject(1, backNum);
				patDelete.setObject(2, wetigh);
				patDelete.setObject(3, resNo);
				patDelete.setObject(4, coupLod.getWoId());
				patDelete.addBatch();
				// 删除接箍公共信息
				patsPlan.setObject(1, backNum);
				patsPlan.setObject(2, wetigh);
				patsPlan.setObject(3, resNo);
				patsPlan.setObject(4, slGx);
				patsPlan.setObject(5, coupLod.getWoId());
				patsPlan.addBatch();

				patDelete.executeBatch();
				patsPlan.executeBatch();
				// 修改工单状态

				patsUpPlan.setObject(1, backNum);
				patsUpPlan.setObject(2, wetigh);
				patsUpPlan.setObject(3, coupLod.getWoId());
				patsUpPlan.addBatch();

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

	/**
	 * 查询上料实绩
	 * 
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject getLoadResultById(String planNo, String plinCode,
			String bc, String bz) {
		return this.getDataBySqlID("CoupLoadingService.GetLoadResultByID",
				new Object[] { planNo, plinCode, bc, bz });
	}

	/**
	 * 查询物料信息
	 * 
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject getMatZcMById(String judNo, String batchNo,
			String batchNoGroup) {
		return this.getDataBySqlID("CoupLoadingService.getMatZcMById",
				new Object[] { judNo, batchNo, batchNoGroup });
	}

	/**
	 * 查询同班次是否有上料记录
	 * 
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject getCoupCountById1(String planNo, String woId,
			String bc) {
		return this.getDataBySqlID("CoupLoadingService.getCoupCountById1",
				new Object[] { planNo, woId, bc });
	}

	/**
	 * 查询上料信息
	 * 
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject getCoupLingById(String woId) {
		Object[] params = new Object[] { woId };
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.getCoupLingById");
		sql += " ORDER BY LOAD_TIME DESC";
		return this.getData(sql, params);
	}

	/***
	 * 结炉
	 * 
	 * @param zYgNo
	 * @return
	 */
	public CoreReturnObject doBeside(String slGx, String woid) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
					connection, woid, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				cro.setV_errMsg("未查询到工单信息,请确认!");
				return cro;
			}
			if (!work.getZyBillStatus().equals("2")) {
				cro.setV_errMsg("工单状态不是生产中,不允许此操作!");
				return cro;
			}
			// if (work.getPlanBlNum().equals(work.getRealBlNum())) {
			// cro.setV_errMsg("工单计划支与上料支不符,不允许结炉!");
			// return cro;
			// }

			int count = 0;
			String stationStatus = "1";
			// 查询是否结炉
			sql = "SELECT COUNT(*) COUNT,MAX(T.STATION_STATUS) STATION_STATUS FROM COUPLING_COM_INFO T WHERE T.STATION_CODE = ?  AND T.WO_ID=?";
			psta = connection.prepareStatement(sql);
			psta.setString(1, slGx);
			psta.setString(2, woid);
			rs = psta.executeQuery();
			if (rs.next()) {
				count = rs.getInt("COUNT");
				stationStatus = rs.getString("STATION_STATUS");
			}
			rs.close();
			psta.close();

			if (count == 0) {
				cro.setV_errMsg("无上料实绩,不允许此操作!");
				return cro;
			}
			if (stationStatus.equals("2")) {
				cro.setV_errMsg("该工单上料实绩已结炉,不允许此操作!");
				return cro;
			}
			// 修改公共信息表结炉标志
			String updateSql = "UPDATE COUPLING_COM_INFO T SET T.STATION_STATUS = '2' WHERE T.STATION_CODE = ? AND T.WO_ID = ?";
			psta = connection.prepareStatement(updateSql);
			psta.setObject(1, slGx);
			psta.setObject(2, woid);
			psta.executeUpdate();
			psta.close();

			cro.setV_errMsg("结炉成功!");
			connection.commit();
		} 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;
	}

	/***
	 * 撤销结炉
	 * 
	 * @param zYgNo
	 * @return
	 */
	public CoreReturnObject undoBeside(String slGx, String woid) {
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
					connection, woid, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				cro.setV_errMsg("未查询到工单信息,请确认!");
				return cro;
			}
			if (!work.getZyBillStatus().equals("2")) {
				cro.setV_errMsg("工单状态不是生产中,不允许此操作!");
				return cro;
			}
			// if (work.getPlanBlNum().equals(work.getRealBlNum())) {
			// cro.setV_errMsg("工单计划支与上料支不符,不允许结炉!");
			// return cro;
			// }
			int count = 0;
			String stationStatus = "1";
			// 查询是否结炉
			sql = "SELECT COUNT(*) COUNT,MAX(T.STATION_STATUS) STATION_STATUS FROM COUPLING_COM_INFO T WHERE T.STATION_CODE = ?  AND T.WO_ID=?";
			psta = connection.prepareStatement(sql);
			psta.setString(1, slGx);
			psta.setString(2, woid);
			rs = psta.executeQuery();
			if (rs.next()) {
				count = rs.getInt("COUNT");
				stationStatus = rs.getString("STATION_STATUS");
			}
			rs.close();
			psta.close();

			if (count == 0) {
				cro.setV_errMsg("无上料实绩,不允许此操作!");
				return cro;
			}
			if (stationStatus.equals("1")) {
				cro.setV_errMsg("该工单上料实绩未结炉,不允许此操作!");
				return cro;
			}
			// 修改公共信息表结炉标志
			String updateSql = "UPDATE COUPLING_COM_INFO T SET T.STATION_STATUS = '1' WHERE T.STATION_CODE = ? AND T.WO_ID = ?";
			psta = connection.prepareStatement(updateSql);
			psta.setObject(1, slGx);
			psta.setObject(2, woid);
			psta.executeUpdate();
			psta.close();

			cro.setV_errMsg("撤销结炉成功!");
			connection.commit();
		} 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;
	}

	/**
	 * 离线上料
	 * 
	 * @param result
	 *            上料实绩
	 * @return
	 */
	public CoreReturnObject lixianThreadingResult(ArrayList<String> list,
			String latGx, String GxName) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		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);
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		String systime = dateFormat.format(now);
		String last = date.format(ca.getTime()) + " 21:00:00";
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String insertResultSql = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.lixianThreadingResult");// 修改切割实绩离线个数
			PreparedStatement insertResultPsta = con
					.prepareStatement(insertResultSql);
			String insertComInfoSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.lixianComInfoResult");// 离线ComInfo表
			PreparedStatement insertComInfoPsta = con
					.prepareStatement(insertComInfoSql);
			String insertofflineSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.lixianofflineResult");// 离线表
			PreparedStatement insertofflinePsta = con
					.prepareStatement(insertofflineSql);
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.updatelixianofflineResult");// 修改工单工序的离线状态
			PreparedStatement updatePsta = con.prepareStatement(updateSql);
			String lutime = systime;

			String lixian = list.get(0);
			String WoId = list.get(1);
			String heatNo = list.get(2);
			String JudgeNo = list.get(3);
			String BatchNo = list.get(4);
			String PlineCode = list.get(5);
			String PlanName = list.get(6);
			String UserName = list.get(7);
			String bc = list.get(8);
			String bz = list.get(9);
			String time = list.get(10);
			String resultNo = list.get(11);
			if (PlanService.compareDate(systime, last) == true) {
				if (bc.equals("3")) {
					ca.add(Calendar.DAY_OF_MONTH, 1);
					lutime = date.format(ca.getTime()) + " 00:00:0 31";
				}
			}
			// 修改切割实绩离线个数
			insertResultPsta.setString(1, lixian);
			insertResultPsta.setString(2, resultNo);
			insertResultPsta.addBatch();
			// 离线公共信息并修改计划锯切
			insertComInfoPsta.setString(1, heatNo);
			insertComInfoPsta.setString(2, "");
			insertComInfoPsta.setString(3, "");
			insertComInfoPsta.setString(4, "");
			insertComInfoPsta.setString(5, "");
			insertComInfoPsta.setString(6, "");
			insertComInfoPsta.setString(7, "");
			insertComInfoPsta.setString(8, "");
			insertComInfoPsta.setString(9, lixian);
			insertComInfoPsta.setString(10, lutime);
			insertComInfoPsta.setString(11, UserName);
			insertComInfoPsta.setString(12, bc);
			insertComInfoPsta.setString(13, bz);
			insertComInfoPsta.setString(14, resultNo);
			insertComInfoPsta.setString(15, "1");
			insertComInfoPsta.setString(16, latGx);
			insertComInfoPsta.setString(17, systime);
			insertComInfoPsta.setString(18, "");
			insertComInfoPsta.setString(19, WoId);
			insertComInfoPsta.addBatch();
			// 新增离线表信息
			insertofflinePsta.setString(1, resultNo);
			insertofflinePsta.setString(2, PlineCode);
			insertofflinePsta.setString(3, WoId);
			insertofflinePsta.setString(4, latGx);
			insertofflinePsta.setString(5, GxName);
			insertofflinePsta.setString(6, JudgeNo);
			insertofflinePsta.setString(7, BatchNo);
			insertofflinePsta.setString(8, lixian);
			insertofflinePsta.setString(9, "");
			insertofflinePsta.setString(10, "1");
			insertofflinePsta.setString(11, "");
			insertofflinePsta.setString(12, "");
			insertofflinePsta.setString(13, lutime);
			insertofflinePsta.setString(14, UserName);
			insertofflinePsta.setString(15, bc);
			insertofflinePsta.setString(16, bz);
			insertofflinePsta.setString(17, lutime);
			insertofflinePsta.setString(18, resultNo);
			insertofflinePsta.addBatch();

			updatePsta.setString(1, WoId);
			updatePsta.addBatch();
			updatePsta.executeBatch();

			insertResultPsta.executeBatch();
			insertComInfoPsta.executeBatch();
			insertofflinePsta.executeBatch();
			core.setV_errMsg("离线成功!");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					core.setV_errMsg(e.getMessage());
					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 撤销离线上料实绩
	 * 
	 * @param result
	 *            上料实绩
	 * @return
	 */
	public CoreReturnObject unlixianThreadingResult(
			ArrayList<ArrayList<String>> pram, String woid) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String cutResultsql = CoreXmlSqlParsersFactory
					.getSql("CoupLoadingService.lixianThreadingResult");// 删除切割实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.unlixianComInfoResult");// 删除comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
			String planJgSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.unlixianofflineResult");// 撤销离线表
			PreparedStatement planJgPsta = con.prepareStatement(planJgSql);
			String planJgSql1 = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.unupdatelixianofflineResult");// 修改工单表状态
			PreparedStatement planJgPsta1 = con.prepareStatement(planJgSql1);
			String planJgSql2 = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.unupdatelixianofflineResult2");// 修改工单表状态
			PreparedStatement planJgPsta2 = con.prepareStatement(planJgSql2);
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(con,
					woid, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				cor.setV_errMsg("未查询到工单信息,请确认!");
				return cor;
			}
			if (!work.getZyBillStatus().equals("2")) {
				cor.setV_errMsg("工单状态不是生产中,不允许此操作!");
				return cor;
			}
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String resNo = list.get(0);
				String offNum = list.get(1);
				// 修改离线个数
				cutResultPsta.setObject(1, "-" + offNum);
				cutResultPsta.setObject(2, resNo);
				cutResultPsta.addBatch();
				// 删除离线记录
				planJgPsta.setObject(1, resNo);
				planJgPsta.addBatch();
				// 删除公共信息离线记录
				comInfoPsta.setObject(1, resNo);
				comInfoPsta.setObject(2, woid);
				comInfoPsta.addBatch();
			}
			cutResultPsta.executeBatch();
			planJgPsta.executeBatch();
			comInfoPsta.executeBatch();
			// 查询锯切总数
			int countQual = 0;
			int countPcs = 0;
			int emNum = 0;
			int count = 0;
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.querylixian");
			PreparedStatement psta = con.prepareStatement(sqlloading);
			psta.setObject(1, woid);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				count = Integer.parseInt(rs1.getString("COUNT"));
			}
			rs1.close();
			psta.close();
			if (count == 0) {
				planJgPsta1.setObject(1, woid);
				planJgPsta1.addBatch();
				planJgPsta1.executeBatch();
			} else {
				planJgPsta2.setObject(1, woid);
				planJgPsta2.addBatch();
				planJgPsta2.executeBatch();
			}
			cor.setV_errMsg("撤销离线成功！");
			con.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (con != null) {
				try {
					cor.setV_errMsg(e.getMessage());
					con.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (con != null && !con.isClosed()) {
					con.close();
				}
			} catch (Exception e2) {
			}
		}
		return cor;
	}

}
