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.Mch.FrmMchBugD;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.CouplingCutResultEntity;
import com.steering.mes.mcp.entity.CouplingLoadingResultEntity;
import com.steering.mes.mcp.entity.MatBcMEntity;
import com.steering.mes.mcp.entity.PortJgBatchSampleResultEntity;
import com.steering.mes.mcp.entity.PortMchBatchSampleResultEntity;
import com.steering.mes.mcp.entity.PortVrpBatchSampleResultEntity;
import com.steering.mes.mcp.entity.MchEntity.CouplingComInfoEntity;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgWorkbillEntity;

/**
 * 接箍切割实绩业务类
 * 
 * @author siy 2015/05/28
 * @version v1.0
 */
public class CoupCutService extends DBCommon {

	/***
	 * 查询切管编号
	 * 
	 * @param gx
	 *            工序
	 * @return
	 */
	public CoreReturnObject getMachine(String plineCode, String[] lathCutNm) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.getMachine");
		// sql += " AND T.PLINE_CODE  = '" + plineCode + "'";
		sql += CoreConverter.toSqlConditionString("T.MACHINE_NAME", lathCutNm);
		sql += " ORDER BY T.MACHINE_NO ASC ";
		return this.getData(sql);
	}

	/***
	 * 查询废品原因
	 * 
	 * @param code
	 * @return
	 */
	public CoreReturnObject getCutResultFp(String code) {
		return this.getDataBySqlID("CoupCutService.getCutResultFp",
				new Object[] { code });
	}

	/***
	 * 查询操作工
	 * 
	 * @param code
	 * @return
	 */
	public CoreReturnObject getMachUser(String machtGm, String[] cutMachGroup) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.getMachUser");
		// sql += " AND T.PLINE_CODE = '" + plineCode + "'";
		if (machtGm.equals("")) {
			sql += CoreConverter.toSqlConditionString("T.MACHINE_NAME",
					cutMachGroup);
		} else {
			sql += " AND T.MACHINE_NAME= '" + machtGm + "'";
		}
		return this.getData(sql);
	}

	/***
	 * 查询操作工班组
	 * 
	 * @param code
	 * @return
	 */
	public CoreReturnObject getMatProBz(String name) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.getMatProBz");
		sql += " AND T.MACHINE_USER1= '" + name + "'";
		return this.getData(sql);
	}

	/***
	 * 查询作业工单计划
	 * 
	 * @return
	 */
	public CoreReturnObject doQueryZygNo(ArrayList<String> list,
			String plineCode) {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.doQueryZygNo");
		sql += " AND T.PLINE_CODE || '&' LIKE ('%' || '" + plineCode
				+ "' || '%')  ORDER BY T.ZY_PLAN_ID ASC";
		return this.getData(sql, list.toArray());
	}

	/***
	 * 查询上料实绩
	 * 
	 * @param zYgNo
	 * @param plineCode
	 * @return
	 */
	public CoreReturnObject doQueryLoadResult(String zYgNo) {
		return this.getDataBySqlID("CoupCutService.doQueryLoadResult",
				new Object[] { zYgNo });
	}

	/*
	 * 查询切割实绩支数
	 */
	public CoreReturnObject getCutResult(String woid) {
		return this.getDataBySqlID("CoupCutService.getCutResult",
				new Object[] { woid });
	}

	/*
	 * 查询切割实绩个数
	 */
	public CoreReturnObject getCutResultPiece(String woid) {
		return this.getDataBySqlID("CoupCutService.getCutResultPiece",
				new Object[] { woid });
	}

	/***
	 * 查询机床编号
	 * 
	 */
	public CoreReturnObject doQuerybianhao(String codeNo) {
		return this.getDataBySqlID("CoupCutService.doQuerybianhao",
				new Object[] { codeNo });
	}

	/**
	 * 查询工单信息
	 * 
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject getCoupLingById(String planNo, String plinCode) {
		Object[] params = new Object[] { planNo, plinCode };
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.getCoupLingById");
		sql += " ORDER BY T.WO_ID DESC";
		return this.getData(sql, params);
	}

	/**
	 * 查询切割实绩
	 * 
	 * @param planNO
	 * @return
	 */
	public CoreReturnObject getCutResultByPlanNo(String woId) {
		Object[] pamar = new Object[] { woId };
		return this.getDataBySqlID(
				"CoupCutService.CutPlan.GetCutResultByPlanNo", pamar);
	}

	
	/**
	 * 新增切割实绩
	 * 
	 * @param result
	 *            切割实绩
	 * @return·
	 */
	public CoreReturnObject addCutResult(ArrayList<String> jsonstrList,
			String cutGx, String emGx, String machineNo) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement ps = null;
		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";
		ResultSet rs = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务

			CouplingCutResultEntity result = new CouplingCutResultEntity();

			for (String jsonStr : jsonstrList) {
				result = BaseHelper.jsonToEntity(CouplingCutResultEntity.class,
						jsonStr);
			}
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(con,
					result.getWoId(), PlnZyJgWorkbillEntity.class);
			if (work == null) {
				cor.setV_errMsg("未查询到工单信息,请确认!");
				return cor;
			}
			if (!work.getZyBillStatus().equals("2")) {
				cor.setV_errMsg("工单状态不是生产中,不允许此操作!");
				return cor;
			}
			
			// 查询切割实绩
			String lutime = systime;
			if (PlanService.compareDate(systime, last) == true) {
				if (result.getColShift().equals("3")) {
					ca.add(Calendar.DAY_OF_MONTH, 1);
					lutime = date.format(ca.getTime()) + " 00:00:01";
				}
			}
			// int isEmb = 0;// 是否压印
			// if (result.getIsEmbossed().equals("1")) {
			// isEmb = 1;
			// }

			// 新增锯切实绩
			String resultNo = doAddResultNo(work.getPlineCode(), "02",
					work.getPlineCode() + "02", con);
			result.setResultNo(resultNo);
			result.setIfAllOutWork(work.getIfAllOutWork());
			result.setIfMonitorJg(work.getIfMonitorJg());
			result.setPrdcrNo(work.getPrdcrNo());
			result.setJudgeStoveNo(work.getJudgeStoveNo());
			result.setBatchNo(work.getBatchNo());
			// result.setColTime(work.getCreateTime());
			result.setIsRoughmach("0");
			// result.setCreateTime(systime);
			// 新增锯切实绩记录
			String addLoad = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("CoupCutService.CutPlan.AddCutResult"),
					result);
			ps = con.prepareStatement(addLoad);
			ps.executeUpdate();
			ps.close();

			CouplingComInfoEntity comInfo = new CouplingComInfoEntity();
			// 新增接箍公共信息
			comInfo.setResultNo(resultNo);
			comInfo.setWoId(work.getWoId());
			comInfo.setJudgeStoveNo(work.getJudgeStoveNo());
			comInfo.setProPlanId("");
			comInfo.setCodeJgBl(work.getCodeJgBl());
			comInfo.setIfOutRpo("0");
			comInfo.setPlineCode(work.getPlineCode());
			comInfo.setPlineName(work.getPlineName());
			comInfo.setStationCode(cutGx);
			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(machineNo);
			comInfo.setMachNm(result.getMachineNo());
			comInfo.setProNum(result.getQualifiedNumber());
			comInfo.setProWt(String.valueOf(Integer.parseInt(result
					.getQualifiedNumber())
					* Double.parseDouble(work.getSingleweight()) / 1000));
			comInfo.setFailNum(result.getRejectedPcs());
			comInfo.setFailWt(String.valueOf(Integer.parseInt(result
					.getRejectedPcs())
					* Double.parseDouble(work.getSingleweight()) / 1000));
			comInfo.setRroTime(lutime);
			comInfo.setProUser(result.getColUser());
			comInfo.setProShift(result.getColShift());
			comInfo.setProGroup(result.getColGroup());
			comInfo.setIfAllOutWork(work.getIfAllOutWork());
			comInfo.setCreateTime(systime);
			comInfo.setColOperator(result.getColOperator());
			comInfo.setOutwlId(work.getOutwlId());
			comInfo.setOutwlDesc(work.getOutwlDesc());
			comInfo.setStationStatus("1");

			// String UpdatePort =
			// CoreXmlSqlParsersFactory.getSql("CoupCutService.UpdatePortJg");//
			// 更改管号队列状态
			// PreparedStatement patsUpdatePort =
			// con.prepareStatement(UpdatePort);
			// String AddPortMch = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.AddPortJg");// 增加子管队列
			// PreparedStatement patsAddPortMch = con
			// .prepareStatement(AddPortMch);

			// String MarkPMatNo = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.MarkPortMch");// 标记无效
			// PreparedStatement patsMarkPMatNo = con
			// .prepareStatement(MarkPMatNo);

			// String sqlMatNo = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.QueryCanCut");// 查询新增物料信息
			// ps = con.prepareStatement(sqlMatNo);
			// ps.setObject(1, work.getWoId());
			// ps.setObject(2, result.getCutPieces());
			// ResultSet rs = ps.executeQuery();
			// List<PortJgBatchSampleResultEntity> listMats =
			// EntityTool.getEntitysByRs(rs,
			// PortJgBatchSampleResultEntity.class);
			// rs.close();
			// ps.close();
			//
			//
			// if(listMats.size()!= Integer.parseInt(result.getCutPieces())){
			// cor.setV_errMsg("无足够的可分切的未入库合格管！");
			// return cor;
			// }

			// String MaxMatNo = "";//最大材料号
			// String sqlloading = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.QueryPortMaxMatNo");// 查询最大实绩编号
			// ps = con.prepareStatement(sqlloading);
			// ps.setObject(1, work.getWoId());
			// ps.setObject(2, Integer.parseInt(result.getCutPieces()));
			// ResultSet rs1 = ps.executeQuery();
			// if (rs1.next()) {
			// MaxMatNo = rs1.getString("MAT_NO");
			// }
			//
			// rs1.close();
			// ps.close();
			// for (PortJgBatchSampleResultEntity MatInfo :listMats
			// ) {
			// patsMarkPMatNo.setObject(1,"0");
			// patsMarkPMatNo.setObject(2,MatInfo.getWoId());
			// patsMarkPMatNo.setObject(3,MatInfo.getMatNo());
			// patsMarkPMatNo.addBatch();
			// patsMarkPMatNo.executeBatch();
			//
			// }
			//
			// for (int i = 0; i < Integer.parseInt(result.getQualifiedNumber())
			// ; i++) {
			//
			// patsAddPortMch.setObject(1,i+1);
			// patsAddPortMch.setObject(2,resultNo);
			// patsAddPortMch.setObject(3,work.getWoId());
			// patsAddPortMch.setObject(4,MaxMatNo);
			// patsAddPortMch.addBatch();
			//
			// }
			// patsAddPortMch.executeBatch();
			// List<PortJgBatchSampleResultEntity> matNo = new
			// ArrayList<PortJgBatchSampleResultEntity>();
			// String sqlloading1 = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.queryPortJgPort");// 查询管号队列里合格的材料号
			// ps = con.prepareStatement(sqlloading1);
			// ps.setObject(1, work.getWoId());
			// ps.setObject(2, Integer.parseInt(result.getRejectedPcs()) + 1);
			// ResultSet rs2 = ps.executeQuery();
			// matNo = EntityTool.getEntitysByRs(rs2,
			// PortJgBatchSampleResultEntity.class);
			// rs2.close();
			// ps.close();
			// if(matNo==null)
			// {
			// cor.setV_errMsg("没有合格管！");
			// return cor;
			// }

			// // 获取材料号
			// String matNo = doAddMatNo(work.getJudgeStoveNo(), resultNo,
			// work.getWoId(), con);
			// if(Integer.parseInt(result.getRejectedPcs())>0)
			// {
			// PlanService.AddJgMatBcM(con,"H",result.getColUser(),"10",
			// matNo,cutGx,
			// work.getWoId(),"",work,resultNo,result.getRejectedPcs(),
			// result.getColTime(),result.getColShift(),result.getColGroup());
			// }
			//
			// // 修改管号队列状态
			// patsUpdatePort.setObject(1, "10");
			// patsUpdatePort.setObject(2, cutGx);
			// patsUpdatePort.setObject(3, resultNo);
			// patsUpdatePort.setObject(4, work.getWoId());
			// patsUpdatePort.setObject(5, prot.getMatNo());
			// patsUpdatePort.addBatch();
			// }
			// patsUpdatePort.executeBatch();
			// 新增接箍公共记录
			String addInfo = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.AddPlnInfo"), comInfo);
			ps = con.prepareStatement(addInfo);
			ps.executeUpdate();
			ps.close();

			double wt=(Double.parseDouble(work.getPlanBlWt())/Integer.parseInt(work.getPlanBlNum()))*Integer.parseInt(result.getCutPieces());
			// 修改炉计划完成支吨
			String update = "UPDATE PLN_ZY_JG_C T SET T.REAL_OUTDOUBLE_NUM=TO_NUMBER(NVL(T.REAL_OUTDOUBLE_NUM,0))+TO_NUMBER(?),"
					+ " T.REAL_OUTDOUBLE_WT=TO_NUMBER(NVL(T.REAL_OUTDOUBLE_WT,0))+TO_NUMBER(?) "
					+ " WHERE T.HEAT_PLAN_NO=? AND T.HEAT_PLAN_SQE=?";
			ps = con.prepareStatement(update);
			ps.setObject(1, result.getCutPieces());
			ps.setObject(2, wt);
			ps.setObject(3, work.getHeatPlanNo());
			ps.setObject(4, work.getHeatPlanSqe());
			ps.executeUpdate();
			ps.close();
			
			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;
	}
	
	/***
	 * 确认
	 * 
	 * @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("CoupCutService.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);
			// 修改确认实绩确认按钮
				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("CoupCutService.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);
					// 修改切管实绩确认按钮
				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 result
	 *            切割实绩
	 * @return
	 */
	public CoreReturnObject lixianThreadingResult(ArrayList<String> list,
			String latGx, String GxName) {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		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 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 insertMatBc = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.ProcessPlan.lixianThreadingResult");// 离线切割实绩
			PreparedStatement insertMatBcPsta = con
					.prepareStatement(insertMatBc);
			String updatePort = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.ProcessPlan.updateJgPortResult");// 修改管号队列状态
			PreparedStatement updatePortPsta = con.prepareStatement(updatePort);

			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 resultNo1 = list.get(11);
			String Memo = list.get(12);
			String groupNo = list.get(13);
			String feedHeatNo = list.get(14);
			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";
				}
			}
			String Gx = latGx.substring(latGx.length() - 2);
			String resultNo = doAddResultNo(PlineCode, Gx, PlineCode + Gx, con);
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(con,
					WoId, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				core.setV_errMsg("未查询到工单信息,请确认!");
				return core;
			}
			if (!work.getZyBillStatus().equals("2")) {
				core.setV_errMsg("工单状态不是生产中,不允许此操作!");
				return core;
			}
			List<MatBcMEntity> listMat1 = getQureyPortJg(con,heatNo, feedHeatNo,
					BatchNo, groupNo, Integer.parseInt(lixian));
			Double actWeiht = 0.0;//上料离线的重量
			for(int i= 0;i<listMat1.size();i++){
				actWeiht += Double.parseDouble(listMat1.get(i).getActWeight());
			}
			// List<PortJgBatchSampleResultEntity> matNo = new
			// ArrayList<PortJgBatchSampleResultEntity>();
			// String sqlloading1 = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.queryPortJgPort");// 查询管号队列里合格的材料号
			// psta = con.prepareStatement(sqlloading1);
			// psta.setObject(1, WoId);
			// psta.setObject(2, Integer.parseInt(lixian) + 1);
			// ResultSet rs2 = psta.executeQuery();
			// matNo = EntityTool.getEntitysByRs(rs2,
			// PortJgBatchSampleResultEntity.class);
			// rs2.close();
			// psta.close();
			// if(matNo==null)
			// {
			// core.setV_errMsg("没有合格管，不能离线！");
			// return core;
			// }
			// int batchCount = 0;
			// sqlloading1 = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.queryPortJgBatchHg");// 查询管号队列里不是样管的合格支
			// psta = con.prepareStatement(sqlloading1);
			// psta.setObject(1, WoId);
			// rs2 = psta.executeQuery();
			// if (rs2.next()) {
			// batchCount=Integer.parseInt(rs2.getString("ACT_COUNT"));
			// }
			// rs2.close();
			// psta.close();
			// if (batchCount<Integer.parseInt(lixian)) {
			// core.setV_errMsg("管号队列材料合格支数不足,请重新输入离线支数！");
			// return core;
			// }
			// //修改切割实绩离线个数
			// insertResultPsta.setString(1, lixian);
			// insertResultPsta.setString(2, resultNo);
			// insertResultPsta.addBatch();
			// for (PortJgBatchSampleResultEntity prot : matNo) {
			// // 修改管号队列状态
			// updatePortPsta.setObject(1, "20");
			// updatePortPsta.setObject(2, "10");
			// updatePortPsta.setObject(3, latGx);
			// updatePortPsta.setObject(4, resultNo);
			// updatePortPsta.setObject(5, WoId);
			// updatePortPsta.setObject(6, prot.getMatNo());
			// updatePortPsta.addBatch();
			// }
			// updatePortPsta.executeBatch();
			// 离线公共信息并修改计划锯切
			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);
			if(latGx.equals("700301"))
			{
				insertofflinePsta.setString(9, String.valueOf(actWeiht));
			}else{ 
				insertofflinePsta.setString(9, String.valueOf(Integer
						.parseInt(lixian)
						* Double.parseDouble(work.getSingleweight()) / 1000));
			}
		
			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.setString(19, Memo);
			insertofflinePsta.setString(20, groupNo);
			insertofflinePsta.setString(21, PlanName);
			insertofflinePsta.addBatch();

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

			// // 查询物料信息
			// List<PortJgBatchSampleResultEntity> listMat = getQureyPortJg(con,
			// JudgeNo, BatchNo, groupNo, Integer.parseInt(lixian));
			// if (listMat == null) {
			// core.setV_errMsg("未查询到管号队列信息,请确认!");
			// return core;
			// }
			// for(PortJgBatchSampleResultEntity lvMat:matNo)
			if (latGx.equals("700301")) {
				List<MatBcMEntity> listMat = getQureyPortJg(con, work.getJudgeStoveNo(),feedHeatNo,
						BatchNo, groupNo, Integer.parseInt(lixian));
				//
				for (MatBcMEntity lvMat : listMat) {
					// 新增材料表
					if (!lixian.equals("") && !lixian.equals("0")) {

						PlanService.AddJgMatBcM(con, "H", UserName, "11",
								lvMat.getMatNo(), latGx, work.getWoId(), "",work.getJudgeStoveNo());
					}
				}

			} else {
				// 新增材料表
				if (!lixian.equals("") && !lixian.equals("0")) {
					// 判断是否存在该工单已经离线
					String matNo = "";
					double SINGLEWEIGHT = 0;
					String sqlloading = CoreXmlSqlParsersFactory
							.getSql("PlanService.selectOfflineCount");
					psta = con.prepareStatement(sqlloading);
					psta.setObject(1, work.getJudgeStoveNo());
					psta.setObject(2, work.getBatchNo());
					psta.setObject(3, work.getWoId());
					ResultSet rs1 = psta.executeQuery();
					if (rs1.next()) {
						matNo = rs1.getString("Mat_No");
						SINGLEWEIGHT = rs1.getDouble("SINGLEWEIGHT");
					}
					rs1.close();
					psta.close();
					if (matNo == null || matNo.equals("")) {
						// 获取材料号
						String matNo1 = doAddMatNo(work.getJudgeStoveNo(),
								work.getWoId(), con);

						PlanService.AddJgCoupMatNo(con, "H", UserName, "11",
								matNo1, latGx, work.getWoId(), "", lixian,
								resultNo, work, time);
					} else {
						// 修改接箍库存表
						String updateMat = "UPDATE COUPLING_MAT_M T SET T.PRO_NUM=TO_NUMBER(T.PRO_NUM)+TO_NUMBER(?),T.PRO_WT=TO_NUMBER(T.PRO_WT)+TO_NUMBER(?) WHERE T.MAT_NO=?";
						psta = con.prepareStatement(updateMat);
						psta.setObject(1, lixian);
						psta.setObject(
								2,
								(SINGLEWEIGHT / 1000)
										* Integer.parseInt(lixian));
						psta.setObject(3, matNo);
						psta.executeUpdate();
						psta.close();
						// 修改接箍位置表
						String updateMatBut = " UPDATE COUPLING_MAT_BUTTRESS T SET T.PIECES=TO_NUMBER(T.PIECES)+TO_NUMBER(?),T.WEIGHT=TO_NUMBER(T.WEIGHT)+TO_NUMBER(?) WHERE T.MAT_NO=?";
						psta = con.prepareStatement(updateMatBut);
						psta.setObject(1, lixian);
						psta.setObject(
								2,
								(SINGLEWEIGHT / 1000)
										* Integer.parseInt(lixian));
						psta.setObject(3, matNo);
						psta.executeUpdate();
						psta.close();
					}

				}

			}

			// 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 con
	 * @param matStatus
	 *            材料状态
	 * @param judgeStoveNo
	 *            判定炉号
	 * @throws SQLException
	 */
	public static List<MatBcMEntity> getQureyPortJg(Connection con,
			String judgeStoveNo,String feedHeatNo, String batchNo, String batchGroupBy,
			int takeCount) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.QueryMatGp");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, feedHeatNo);
		ps.setObject(2, batchNo);
		ps.setObject(3, batchGroupBy);
		// ps.setObject(4, proessCode);
		ps.setObject(4, takeCount);
		ps.setObject(5, judgeStoveNo);
		ps.setObject(6, batchNo);
		ps.setObject(7, batchGroupBy);
		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 result
	 *            切割实绩
	 * @return
	 */
	public CoreReturnObject unlixianThreadingResult(
			ArrayList<ArrayList<String>> pram, String woid, String latGx) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务

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

			// String cutResultsql = "";
			// PreparedStatement cutResultPsta = null;
			// if(latGx.equals("700301"))
			// {
			// cutResultsql = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.ProcessPlan.lixianThreadingResultLoad");//
			// 修改上料实绩离线个数
			// cutResultPsta = con
			// .prepareStatement(cutResultsql);
			// }else if(latGx.equals("700302")){
			// cutResultsql = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.ProcessPlan.lixianThreadingResult");//
			// 修改切割实绩离线个数
			// cutResultPsta = con
			// .prepareStatement(cutResultsql);
			// }else if(latGx.equals("700305")){
			// cutResultsql = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.ProcessPlan.lixianThreadingResultThread");//
			// 修改车丝实绩离线个数
			// cutResultPsta = con
			// .prepareStatement(cutResultsql);
			// }else if(latGx.equals("700306")){
			// cutResultsql = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.ProcessPlan.lixianThreadingResultThinsp");//
			// 修改螺纹检验实绩离线个数
			// cutResultPsta = con
			// .prepareStatement(cutResultsql);
			// }else if(latGx.equals("700308")){
			// cutResultsql = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.ProcessPlan.lixianThreadingResultSand");//
			// 修改喷砂实绩离线个数
			// cutResultPsta = con
			// .prepareStatement(cutResultsql);
			// }else if(latGx.equals("700310")){
			// cutResultsql = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.ProcessPlan.lixianThreadingResultPhosp");//
			// 修改磷化实绩离线个数
			// cutResultPsta = con
			// .prepareStatement(cutResultsql);
			// }
			// 查询工单信息
			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;
	}

	/**
	 * 修改切割实绩
	 * 
	 * @param result
	 *            切割实绩
	 * @return
	 */
	public CoreReturnObject updateCutResult(ArrayList<String> list,
			String cutGx, String emGX, String woid) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String cutResultsql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.CutPlan.UpdateCutResult");// 修改切割实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.UpdateComInfoResult");// 修改comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);

			// 查询工单信息
			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;
			}

			String resNo = list.get(0);
			String quaCount = list.get(1);
			String cutPies = list.get(2);
			String rejCount = list.get(3);
			String reason = list.get(4);
			String bc = list.get(5);
			String bz = list.get(6);
			String jcNo = list.get(7);
			String lvaLen = list.get(8);
			String colName = list.get(9);
			String emContent = list.get(10);
			String emCount = list.get(11);

			// 修改切管实绩
			cutResultPsta.setObject(1, quaCount);
			cutResultPsta.setObject(2, cutPies);
			cutResultPsta.setObject(3, rejCount);
			cutResultPsta.setObject(4, reason);
			cutResultPsta.setObject(5, bc);
			cutResultPsta.setObject(6, bz);
			cutResultPsta.setObject(7, jcNo);
			cutResultPsta.setObject(8, lvaLen);
			cutResultPsta.setObject(9, colName);
			cutResultPsta.setObject(10, emContent);
			cutResultPsta.setObject(11, emCount);
			cutResultPsta.setObject(12, resNo);
			cutResultPsta.addBatch();

			// 修改切割公共信息表
			double singlewt = Double.parseDouble(work.getSingleweight());// 接箍单重

			int hgCount = Integer.parseInt(quaCount)
					- Integer.parseInt(rejCount);
			double hgWt = hgCount * singlewt / 1000;
			double rejWt = Integer.parseInt(rejCount) * singlewt / 1000;
			comInfoPsta.setObject(1, hgCount);
			comInfoPsta.setObject(2, hgWt);
			comInfoPsta.setObject(3, rejCount);
			comInfoPsta.setObject(4, rejWt);
			comInfoPsta.setObject(5, bc);
			comInfoPsta.setObject(6, bz);
			comInfoPsta.setObject(7, colName);
			comInfoPsta.setObject(8, resNo);
			comInfoPsta.setObject(9, cutGx);
			comInfoPsta.setObject(10, woid);
			comInfoPsta.addBatch();

			cutResultPsta.executeBatch();
			comInfoPsta.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;
	}

	/**
	 * 删除切割实绩
	 * 
	 * @param result
	 *            切割实绩
	 * @return
	 */
	public CoreReturnObject deleteCutResult(ArrayList<String> list,
			String woid, String cutGx) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		try {
			// String DeletePortMch = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.DeletePortJg");// 删除子管队列
			// PreparedStatement patsDeletePortMch = con
			// .prepareStatement(DeletePortMch);
			//
			// String MarkPMatNo = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.MarkPortMch");// 标记母管有效
			// PreparedStatement patsMarkPMatNo = con
			// .prepareStatement(MarkPMatNo);
			// 查询工单信息
			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;
			}

			ArrayList<String> sqllist = new ArrayList<String>();
			for (String str : list) {
				CouplingCutResultEntity matBcEntity = EntityTool
						.getEntityByJSONStr(str, CouplingCutResultEntity.class);

				// int detCount =0;
				// if(StringTool.isInteger(matBcEntity.getRejectedPcs())){
				// detCount = Integer.parseInt(matBcEntity.getRejectedPcs());
				// }

				// FrmMchBugD.DeleteMchWasteJg(con,
				// matBcEntity.getWoId(),matBcEntity.getResultNo(),
				// matBcEntity.getPlineCode(),cutGx,detCount);

				//
				//
				// String sqlMatNo = CoreXmlSqlParsersFactory
				// .getSql("CoupCutService.QueryCanCut2");// 查询新增物料信息
				// psta = con.prepareStatement(sqlMatNo);
				// psta.setObject(1, matBcEntity.getWoId());
				// psta.setObject(2, matBcEntity.getCutPieces());
				// ResultSet rs = psta.executeQuery();
				// List<PortJgBatchSampleResultEntity> listMats =
				// EntityTool.getEntitysByRs(rs,
				// PortJgBatchSampleResultEntity.class);
				// rs.close();
				// psta.close();
				//
				//
				// if(listMats.size()!=
				// Integer.parseInt(matBcEntity.getCutPieces())){
				// cor.setV_errMsg("数据异常, 找不到部分原管号的信息！");
				// return cor;
				// }
				// for (PortJgBatchSampleResultEntity MatInfo :listMats
				// ) {
				//
				// patsMarkPMatNo.setObject(1,"1");
				// patsMarkPMatNo.setObject(2,MatInfo.getWoId());
				// patsMarkPMatNo.setObject(3,MatInfo.getMatNo());
				// patsMarkPMatNo.addBatch();
				// patsMarkPMatNo.executeBatch();
				//
				//
				//
				// sqlMatNo = CoreXmlSqlParsersFactory
				// .getSql("CoupCutService.QueryChildPortJg");// 查询新增物料信息
				// psta = con.prepareStatement(sqlMatNo);
				// psta.setObject(1, matBcEntity.getWoId());
				// psta.setObject(2, matBcEntity.getResultNo());
				// rs = psta.executeQuery();
				// List<PortJgBatchSampleResultEntity> listChildMats =
				// EntityTool.getEntitysByRs(rs,
				// PortJgBatchSampleResultEntity.class);
				// rs.close();
				// psta.close();
				//
				// for (PortJgBatchSampleResultEntity ChildInfo :listChildMats
				// ) {
				// if(!ChildInfo.getMatStatus().equals("00")){
				// cor.setV_errMsg("子管不是合格状态,无法撤销分切！");
				// return cor;
				// }
				//
				// if(ChildInfo.getInstoreFlag().equals("1")){
				// cor.setV_errMsg("子管已入库,无法撤销分切！");
				// return cor;
				// }
				// }
				// patsDeletePortMch.setObject(1,MatInfo.getWoId());
				// patsDeletePortMch.setObject(2,matBcEntity.getResultNo());
				// patsDeletePortMch.addBatch();
				// patsDeletePortMch.executeBatch();
				//
				// }
				String deleteSql = CoreXmlSqlParsersFactory
						.getSql("CoupCutService.deleteCutData");
				deleteSql = EntityTool.fillSqlByEntity(deleteSql, matBcEntity);
				sqllist.add(deleteSql);

				String deleteCom = CoreXmlSqlParsersFactory
						.getSql("CoupCutService.deleteComData");
				deleteCom = EntityTool.fillSqlByEntity(deleteCom, matBcEntity);
				sqllist.add(deleteCom);
				
				double wt=(Double.parseDouble(work.getPlanBlWt())/Integer.parseInt(work.getPlanBlNum()))*Integer.parseInt(matBcEntity.getCutPieces());
				// 修改炉计划完成支吨
				String update = "UPDATE PLN_ZY_JG_C T SET T.REAL_OUTDOUBLE_NUM=TO_NUMBER(NVL(T.REAL_OUTDOUBLE_NUM,0))-TO_NUMBER(?),"
						+ " T.REAL_OUTDOUBLE_WT =TO_NUMBER(NVL(T.REAL_OUTDOUBLE_WT,0))-TO_NUMBER(?) "
						+ " WHERE T.HEAT_PLAN_NO=? AND T.HEAT_PLAN_SQE=?";
				psta = con.prepareStatement(update);
				psta.setObject(1, matBcEntity.getCutPieces());
				psta.setObject(2, wt);
				psta.setObject(3, work.getHeatPlanNo());
				psta.setObject(4, work.getHeatPlanSqe());
				psta.executeUpdate();
				psta.close();
			}
			cor = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
					sqllist);
			if (cor.getV_errMsg().equals("")) {
				cor.setV_errMsg("删除锯切实绩成功！");
			}
		} 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;
	}

	/***
	 * 生成实绩编号
	 * 
	 * @param plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 * @throws SQLException
	 */
	public String doAddResultNo(String plineCode, String cutGx,
			String resultno, Connection connection) throws SQLException {
		String resultNo = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.queryLoadRESULT");// 查询最大实绩编号
		psta = connection.prepareStatement(sqlloading);
		psta.setObject(1, plineCode);
		psta.setObject(2, cutGx);
		psta.setObject(3, resultno);
		ResultSet rs1 = psta.executeQuery();
		if (rs1.next()) {
			resultNo = rs1.getString("RESULT_NO");
		}
		rs1.close();
		psta.close();
		return resultNo;
	}

	/***
	 * 查询监制人
	 * 
	 * @param prdcrNo
	 * @return
	 */
	public CoreReturnObject doQueryPrdcr(String prdcrNo) {
		return this.getDataBySqlID("CoupCutService.doQueryPrdcr",
				new Object[] { prdcrNo });
	}

	/***
	 * 结炉
	 * 
	 * @param zYgNo
	 * @return
	 */
	public CoreReturnObject doBeside(String cutGx, String embGx, 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;
			}
			//查询有委外但是没有委外回报完毕
			 String subCount = "";//委外数据
			 String sqlloading = CoreXmlSqlParsersFactory
			 .getSql("CoupCutService.QueryCouplingOff");// 查询委外数据
			 psta = connection.prepareStatement(sqlloading);
			 psta.setObject(1, work.getWoId());
			 ResultSet rs1 = psta.executeQuery();
			 if (rs1.next()) {
				 subCount = rs1.getString("COUNT");
			 }
			if(Integer.parseInt(subCount)>0)
			{
				cro.setV_errMsg("有委外数据但没有委外回报!");
				return cro;
			}
			 rs1.close();
			 psta.close();
			int cutNum = 0;
			// 查询切割支数
			String sqlCut = "SELECT SUM(NVL(T.CUT_PIECES,0)) CUT_PIECES  FROM COUPLING_CUT_RESULT T WHERE T.WO_ID=?";
			psta = connection.prepareStatement(sqlCut);
			psta.setString(1, woid);
			rs = psta.executeQuery();
			if (rs.next()) {
				cutNum = rs.getInt("CUT_PIECES");
			}
			rs.close();
			psta.close();
			// if (cutNum != Integer.parseInt(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, cutGx);
			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, cutGx);
			psta.setObject(2, woid);
			psta.executeUpdate();
			psta.close();

			psta = connection.prepareStatement(updateSql);
			psta.setObject(1, embGx);
			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 cutGx, String embGx, 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;
			}
			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, cutGx);
			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, cutGx);
			psta.setObject(2, woid);
			psta.executeUpdate();
			psta.close();

			psta = connection.prepareStatement(updateSql);
			psta.setObject(1, embGx);
			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 plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 */
	public String doAddDealOrde(String woId, Connection connection) {
		String resultNo = "";
		PreparedStatement psta = null;
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.queryMaxDealOrde");// 查询交易单号
		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, woId);
			psta.setObject(2, woId);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("DEAL_ORDE");
			}
			rs1.close();
			psta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return resultNo;
	}

	/***
	 * 生产材料号
	 * 
	 * @param plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 */
	public String doAddMatNo(String judNo, String woId, Connection connection) {
		String MatNo = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		// SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("CoupCutService.queryMaxMatNo");// 查询最大材料号

		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, judNo);
			psta.setObject(2, judNo);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				MatNo = rs1.getString("MAT_NO");
			}
			rs1.close();
			psta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return MatNo;
	}
}
