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.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.Mch.FrmMchBugD;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.InspectResult;
import com.steering.mes.mcp.entity.PlnZyJgEntity;
import com.steering.mes.mcp.entity.PortJgBatchSampleResultEntity;
import com.steering.mes.mcp.entity.ThreadInspectSelfCheck;
import com.steering.mes.mcp.entity.MchEntity.CouplingComInfoEntity;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgWorkbillEntity;

/**
 * 螺纹检验实绩
 * 
 * @author lzh520
 */
public class ThreadInspectResultService extends DBCommon {
	
	/*
	 * 查询螺纹检验实绩支数
	 */
	public CoreReturnObject getthreadinspResult(String woid) {
		return this.getDataBySqlID("ThreadInspectResultService.getthreadinspResult",
				new Object[] { woid });
	}
	/**
	 * 查询螺纹检验废品原因
	 */
	public CoreReturnObject getQueryComInfo(String score) {
		return this.getDataBySqlID("ThreadInspectResultService.getQueryComInfo",
				new Object[] { score });
	}
	/**
	 * 新增螺纹检验实绩
	 * 
	 * @param result
	 *            螺纹检验实绩
	 * @return
	 */
	public CoreReturnObject saveThreadInspectResult(String lwGx,
			InspectResult result, ArrayList<ThreadInspectSelfCheck> list,
			String AddCount, String woId,ArrayList<Object> list1) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
		Date dt = 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(dt);
		String last = date.format(ca.getTime()) + " 21:00:00";
		SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
		PreparedStatement ps = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqlAdd = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.ProcessPlan.AddThreadInspectResult");// 新增螺纹检验
			PreparedStatement patAdd = connection.prepareStatement(sqlAdd);

			String addphosp = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.ProcessPlan.AddThreadingSelfcheck");// 新增抽检记录
			PreparedStatement patsphosp = connection.prepareStatement(addphosp);
			
			String addFail = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.ProcessPlan.AddFailComInfo");// 新增废品信息
			PreparedStatement patsphospFail = connection.prepareStatement(addFail);
			
		
			String lutime = systime;
			if (PlanService.compareDate(systime, last) == true) {
				if (result.getColShft().equals("3")) {
					ca.add(Calendar.DAY_OF_MONTH, 1);
					lutime = date.format(ca.getTime()) + " 00:00:01";
				}
			}
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
					connection, woId, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				core.setV_errMsg("未查询到工单信息,请确认!");
				return core;
			}
//			if (!work.getZyBillStatus().equals("2")) {
//				core.setV_errMsg("工单状态不是生产中,不允许此操作!");
//				return core;
//			}

			String resultNo = "";
			String sqlloading = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.queryLoadRESULT");// 查询最大实绩编号
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, result.getPlineCode());
			psta.setObject(2, "06");
			psta.setObject(3, result.getPlineCode()+"06");
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();
			// if (AddCount.equals("1")) {
			for (int i = 0; i < list.size(); i++) {
				ThreadInspectSelfCheck tisc = list.get(i);
				// int couNo = 0;
				String jgCode = "";
				// if (tisc.getCouplingNo().equals("")) {
				jgCode = "LW" + matter1.format(dt) + Integer.parseInt("000")
						+ 1;
				sqlloading = CoreXmlSqlParsersFactory
						.getSql("ThreadInspectResultService.queryJcodeResult");// 查询最大检验编号
				psta = connection.prepareStatement(sqlloading);
				psta.setObject(1, resultNo);
				psta.setObject(2, woId);
				rs1 = psta.executeQuery();
				if (rs1.next()) {
					jgCode = rs1.getString("COUPLING_NO");
				}
				rs1.close();
				psta.close();
				// } else {
				// couNo = Integer.parseInt(tisc.getCouplingNo().substring(
				// tisc.getCouplingNo().length() - 3,
				// tisc.getCouplingNo().length()));
				// jgCode = "LW" + matter1.format(dt)
				// + StringTool.formatDecimal(++couNo, "000");
				// jgCode = tisc.getCouplingNo();
				// }
				patsphosp.setObject(1, tisc.getTaper());
				patsphosp.setObject(2, tisc.getLead());
				patsphosp.setObject(3, tisc.getHeight());
				patsphosp.setObject(4, tisc.getPdiameterOvality());
				patsphosp.setObject(5, tisc.getBrokenthread());
				patsphosp.setObject(6, tisc.getAl());
				patsphosp.setObject(7, tisc.getRunOut());
				patsphosp.setObject(8, tisc.getChamfer());
				patsphosp.setObject(9, tisc.getResult());
				patsphosp.setObject(10, tisc.getThreadform());
				patsphosp.setObject(11, resultNo);
				patsphosp.setObject(12, tisc.getPlineCode());
				patsphosp.setObject(13, tisc.getZyPlanId());
				patsphosp.setObject(14, jgCode);
				patsphosp.setObject(15, tisc.getWoId());
				patsphosp.addBatch();
				patsphosp.executeBatch();
			}
			for(int i = 0;i<list1.size();i++)
			{
				ArrayList<String> list2 = (ArrayList<String>) list1.get(i);
				patsphospFail.setObject(1, resultNo);
				patsphospFail.setObject(2, woId);
				patsphospFail.setObject(3, "");
				patsphospFail.setObject(4, list2.get(0));
				patsphospFail.setObject(5, list2.get(1));
				patsphospFail.setObject(6, String.valueOf(Integer.parseInt(list2.get(1))
						* Double.parseDouble(work.getSingleweight()) / 1000));
				patsphospFail.addBatch();
				patsphospFail.executeBatch();
				
			}
			
			// }
			// else {
			// 新增螺纹检验实绩
			patAdd.setObject(1, resultNo);
			patAdd.setObject(2, result.getPlineCode());
			patAdd.setObject(3, result.getQualiriedPcs());
			patAdd.setObject(4, result.getRepairedPcs());
			patAdd.setObject(5, result.getRepairedReason1());
			patAdd.setObject(6, result.getRepairedPcs1());
			patAdd.setObject(7, result.getRepairedReason2());
			patAdd.setObject(8, result.getRepairedPcs2());
			patAdd.setObject(9, result.getRepairedReason3());
			patAdd.setObject(10, result.getRepairedPcs3());
			patAdd.setObject(11, "1");
			//patAdd.setObject(12, lutime);
			patAdd.setObject(12, result.getColTime());
			patAdd.setObject(13, result.getColUser());
			patAdd.setObject(14, result.getColShft());
			patAdd.setObject(15, result.getColGroup());
			patAdd.setObject(16, result.getInspectPieces());
			patAdd.setObject(17, result.getRepairedReason4());
			patAdd.setObject(18, result.getRepairedPcs4());
			patAdd.setObject(19, result.getRejectedPcs());
			patAdd.setObject(20, result.getRejectedPcs1());
			patAdd.setObject(21, result.getRejectedReason1());
			patAdd.setObject(22, result.getRejectedReason2());
			patAdd.setObject(23, result.getRejectedPcs2());
			patAdd.setObject(24, result.getRejectedReason3());
			patAdd.setObject(25, result.getRejectedPcs3());
			patAdd.setObject(26, result.getRejectedReason4());
			patAdd.setObject(27, result.getRejectedPcs4());
			patAdd.setObject(28, result.getRejectedReason5());
			patAdd.setObject(29, result.getRejectedPcs5());
			patAdd.setObject(30, result.getRejectedReason6());
			patAdd.setObject(31, result.getRejectedPcs6());
			patAdd.setObject(32, result.getRejectedReason7());
			patAdd.setObject(33, result.getRejectedPcs7());
			patAdd.setObject(34, result.getRejectedReason8());
			patAdd.setObject(35, result.getRejectedPcs8());
			patAdd.setObject(36, result.getPlanNO());
			patAdd.setObject(37, result.getColTime());
			patAdd.setObject(38, woId);
			patAdd.setObject(39, "0");
			patAdd.setObject(40, work.getPlineName());
			patAdd.setObject(41, work.getJudgeStoveNo());
			patAdd.setObject(42, work.getBatchNo());
			patAdd.setObject(43, result.getMemo());
			patAdd.addBatch();
			patAdd.executeBatch();
			
			

			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(lwGx);
			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("");
			comInfo.setMachNm("");
			comInfo.setProNum(result.getQualiriedPcs());
			comInfo.setProWt(String.valueOf(Integer.parseInt(result
					.getQualiriedPcs())
					* Double.parseDouble(work.getSingleweight()) / 1000));
			comInfo.setFailNum(result.getRepairedPcs());
			comInfo.setFailWt(String.valueOf(Integer.parseInt(result
					.getRepairedPcs())
					* Double.parseDouble(work.getSingleweight()) / 1000));
			comInfo.setRroTime(lutime);
			comInfo.setProUser(result.getColUser());
			comInfo.setProShift(result.getColShft());
			comInfo.setProGroup(result.getColGroup());
			comInfo.setIfAllOutWork(work.getIfAllOutWork());
			comInfo.setCreateTime(systime);
			comInfo.setColOperator(result.getColUser());
			comInfo.setOutwlId(work.getOutwlId());
			comInfo.setOutwlDesc(work.getOutwlDesc());
			comInfo.setStationStatus("1");
			
//			String UpdatePort = CoreXmlSqlParsersFactory.getSql("CoupCutService.UpdatePortJg");// 更改管号队列状态
//	        PreparedStatement patsUpdatePort = connection.prepareStatement(UpdatePort);
//			
//			  List<PortJgBatchSampleResultEntity> matNo = new ArrayList<PortJgBatchSampleResultEntity>();
//	            String sqlloading1 = CoreXmlSqlParsersFactory
//	                    .getSql("CoupCutService.queryPortJgPort");// 查询管号队列里合格的材料号
//	            ps = connection.prepareStatement(sqlloading1);
//	            ps.setObject(1, work.getWoId());
//	            ps.setObject(2, Integer.parseInt(result.getRepairedPcs()) + 1);
//	            ResultSet rs2 = ps.executeQuery();
//	            matNo = EntityTool.getEntitysByRs(rs2,
//	            		PortJgBatchSampleResultEntity.class);
//	            rs2.close();
//	            ps.close();
//	            if(matNo==null)
//	            {
//	            	core.setV_errMsg("没有合格管！");
//	                return core;
//	            }
//	            for (PortJgBatchSampleResultEntity prot : matNo) {
//	            	     PlanService.AddJgMatBcM(connection,"H",result.getColUser(),"10",
//		                    		prot.getMatNo(),lwGx, work.getWoId(),"");
//
//		            // 修改管号队列状态
//	            	patsUpdatePort.setObject(1, "10");
//		            patsUpdatePort.setObject(2, lwGx);
//		            patsUpdatePort.setObject(3, resultNo);
//		            patsUpdatePort.setObject(4, work.getWoId());
//		            patsUpdatePort.setObject(5, prot.getMatNo());
//		            patsUpdatePort.addBatch();
//		            }
//	            patsUpdatePort.executeBatch();
//			
//			// 获取材料号
//						String matNo = doAddMatNo(work.getJudgeStoveNo(), resultNo,
//								work.getWoId(), connection);
//						if(Integer.parseInt(result.getRejectedPcs())>0)
//						{
//				            	     PlanService.AddJgMatBcM(connection,"H",result.getColUser(),"10",
//				            	    		 matNo,lwGx, work.getWoId(),"",work,resultNo,result.getRejectedPcs(),
//				            	    		 result.getColTime(),result.getColShft(),result.getColGroup());
//						}
			// 新增接箍公共记录
			String addInfo = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.AddPlnInfo"), comInfo);
			ps = connection.prepareStatement(addInfo);
			ps.executeUpdate();
			ps.close();
			// }
			core.setV_errMsg("保存成功！");
			core.setResult(1);
			connection.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/***
	 * 确认
	 * 
	 * @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("ThreadInspectResultService.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("ThreadInspectResultService.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();
		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("ThreadInspectResultService.ProcessPlan.lixianThreadingResult");// 离线螺纹实绩
			PreparedStatement insertResultPsta = con
					.prepareStatement(insertResultSql);
			String insertComInfoSql = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.lixianComInfoResult");// 离线公共信息表表
			PreparedStatement insertComInfoPsta = con
					.prepareStatement(insertComInfoSql);
			String insertofflineSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.lixianofflineResult");// 离线表
			PreparedStatement insertofflinePsta = con
					.prepareStatement(insertofflineSql);
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.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("ThreadInspectResultService.ProcessPlan.lixianThreadingResult");// 删除螺纹检验实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.unlixianComInfoResult");// 删除comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
			String planJgSql = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.unlixianofflineResult");// 撤销离线表
			PreparedStatement planJgPsta = con.prepareStatement(planJgSql);
			String planJgSql1 = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.unupdatelixianofflineResult");// 修改工单表状态
			PreparedStatement planJgPsta1 = con.prepareStatement(planJgSql1);
			String planJgSql2 = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.unupdatelixianofflineResult2");// 修改工单表状态
			PreparedStatement planJgPsta2 = con.prepareStatement(planJgSql2);
			// // 查询计划状态
			// int falg = 0;
			// int cutfalg = 0;
			// String sqlloading = CoreXmlSqlParsersFactory
			// .getSql("PhosphateResultService.queryPlanJg");
			// PreparedStatement psta = con.prepareStatement(sqlloading);
			// psta.setObject(1, plnNo);
			// ResultSet rs1 = psta.executeQuery();
			// if (rs1.next()) {
			// falg = Integer.parseInt(rs1.getString("ZY_PLAN_STATUS"));
			// cutfalg = Integer.parseInt(rs1.getString("CUT_STATUS"));
			// }
			// rs1.close();
			// psta.close();
			//
			// if (falg == 3) {
			// cor.setV_errMsg("该工单不是已生产完成,不允许此操作！");
			// return cor;
			// }
			// if (falg == 4) {
			// cor.setV_errMsg("该工单已取消,不允许此操作！");
			// return cor;
			// }
			// if (cutfalg == 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("ThreadInspectResultService.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 list
	 *            修改抽检记录
	 * @return
	 */
	public CoreReturnObject doInspUpdate(
			ArrayList<ThreadInspectSelfCheck> list, String woId) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String deletephosp = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.ProcessPlan.UpdateThreadingSelfcheck");// 修改抽检记录
			PreparedStatement patsdelPlan = connection
					.prepareStatement(deletephosp);

			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
					connection, woId, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				core.setV_errMsg("未查询到工单信息,请确认!");
				return core;
			}
//			if (!work.getZyBillStatus().equals("2")) {
//				core.setV_errMsg("工单状态不是生产中,不允许此操作!");
//				return core;
//			}
			for (int i = 0; i < list.size(); i++) {
				ThreadInspectSelfCheck tisc = list.get(i);
				patsdelPlan.setObject(1, tisc.getTaper());
				patsdelPlan.setObject(2, tisc.getLead());
				patsdelPlan.setObject(3, tisc.getHeight());
				patsdelPlan.setObject(4, tisc.getPdiameterOvality());
				patsdelPlan.setObject(5, tisc.getBrokenthread());
				patsdelPlan.setObject(6, tisc.getAl());
				patsdelPlan.setObject(7, tisc.getRunOut());
				patsdelPlan.setObject(8, tisc.getChamfer());
				patsdelPlan.setObject(9, tisc.getResult());
				patsdelPlan.setObject(10, tisc.getThreadform());
				patsdelPlan.setObject(11, tisc.getResultNo());
				patsdelPlan.setObject(12, tisc.getCouplingNo());
				patsdelPlan.addBatch();
			}
			patsdelPlan.executeBatch();
			core.setV_errMsg("修改抽检记录成功！");
			core.setResult(1);
			connection.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 删除抽检记录
	 * 
	 * @param list
	 *            修改抽检记录
	 * @return
	 */
	public CoreReturnObject doInspDelete(
			ArrayList<ThreadInspectSelfCheck> list, String woId) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String deletephosp = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.DeleteThreadingSelfcheck");// 删除抽检记录
			PreparedStatement patsdelPlan = connection
					.prepareStatement(deletephosp);
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
					connection, woId, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				core.setV_errMsg("未查询到工单信息,请确认!");
				return core;
			}
//			if (!work.getZyBillStatus().equals("2")) {
//				core.setV_errMsg("工单状态不是生产中,不允许此操作!");
//				return core;
//			}
			for (int i = 0; i < list.size(); i++) {
				ThreadInspectSelfCheck tisc = list.get(i);

				patsdelPlan.setObject(1, tisc.getResultNo());
				patsdelPlan.setObject(2, tisc.getPlineCode());
				patsdelPlan.setObject(3, tisc.getZyPlanId());
				patsdelPlan.setObject(4, tisc.getCouplingNo());
				patsdelPlan.addBatch();
				patsdelPlan.executeBatch();

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

	/**
	 * 修改螺纹实绩记录
	 * 
	 * @param list
	 *            修改螺纹实绩记录
	 * @return
	 */
	public CoreReturnObject doThreUpdate(String lwGx, ArrayList<String> list,
			String woId) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqlupdate = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.ProcessPlan.UpdateThreadInspectResult");// 修改螺纹检验
			PreparedStatement patupdate = connection
					.prepareStatement(sqlupdate);
			String addupdateInfo = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.UpdatePlnInfo");// 修改接箍公共信息
			PreparedStatement patsPlanUpdate = connection
					.prepareStatement(addupdateInfo);

			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
					connection, woId, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				core.setV_errMsg("未查询到工单信息,请确认!");
				return core;
			}
//			if (!work.getZyBillStatus().equals("2")) {
//				core.setV_errMsg("工单状态不是生产中,不允许此操作!");
//				return core;
//			}
			String woid = list.get(0);
			String resultNo = list.get(1);
			String plinCode = list.get(2);
			String thPcs = list.get(3);
			String hgPcs = list.get(4);
			String rePcs = list.get(5);
			String bc = list.get(6);
			String bz = list.get(7);
			String time = list.get(8);
			String memo = list.get(9);
			// 修改螺纹检验实绩
			patupdate.setObject(1, hgPcs);
			patupdate.setObject(2, rePcs);
			patupdate.setObject(3, bc);
			patupdate.setObject(4, bz);
			patupdate.setObject(5, thPcs);	
			patupdate.setObject(6, time);
			patupdate.setObject(7, memo);
			patupdate.setObject(8, resultNo);
			patupdate.addBatch();

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

			int hgCount = Integer.parseInt(hgPcs);
			double hgWt = hgCount * singlewt / 1000;
			double rejWt = Integer.parseInt(rePcs) * singlewt
					/ 1000;
			// 修改接箍公共信息
			patsPlanUpdate.setObject(1, hgCount);
			patsPlanUpdate.setObject(2, hgWt);
			patsPlanUpdate.setObject(3, rePcs);
			patsPlanUpdate.setObject(4, rejWt);
			patsPlanUpdate.setObject(5, bc);
			patsPlanUpdate.setObject(6, bz);
			patsPlanUpdate.setObject(7, "");
			patsPlanUpdate.setObject(8, lwGx);
			patsPlanUpdate.setObject(9, resultNo);
			patsPlanUpdate.setObject(10, woid);
			patsPlanUpdate.addBatch();

			patupdate.executeBatch();
			patsPlanUpdate.executeBatch();

			// // 查询螺纹检验合格个数，废除个数
			// int quaCount = 0;
			// int rejeCount = 0;
			// String sqlloading = CoreXmlSqlParsersFactory
			// .getSql("ThreadInspectResultService.queryThrePlanNumber");
			// psta = connection.prepareStatement(sqlloading);
			// psta.setObject(1, result.getPlanNO());
			// psta.setObject(2, result.getWoId());
			// ResultSet rs = psta.executeQuery();
			// if (rs.next()) {
			// quaCount = Integer.parseInt(rs.getString("QUALIRIED_PCS"));
			// rejeCount = Integer.parseInt(rs.getString("REJECTED_PCS"));
			// }
			// rs.close();
			// psta.close();
			//
			core.setV_errMsg("修改成功！");
			core.setResult(1);
			connection.commit();
		} catch (Exception e) {
			core.setV_errMsg(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return core;
	}

	/**
	 * 删除螺纹实绩记录
	 * 
	 * @param list
	 *            删除螺纹实绩记录
	 * @return
	 */
	public CoreReturnObject doThreDelete(String lwGx,
			ArrayList<ArrayList<String>> pram, String woId) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String sqlupdate = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.DeleteThreadInspectResult");// 删除螺纹检验
			PreparedStatement patupdate = connection
					.prepareStatement(sqlupdate);
			String addupdateInfo = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.DeletePlnInfo");// 删除接箍公共信息
			PreparedStatement patsPlanUpdate = connection
					.prepareStatement(addupdateInfo);

			String deletephosp = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.DeleteThreadingSelfcheck1");// 删除抽检记录
			PreparedStatement patsdelPlan = connection
					.prepareStatement(deletephosp);
			
			String deleteFail = CoreXmlSqlParsersFactory
					.getSql("ThreadInspectResultService.DeleteFail");// 删除废品
			PreparedStatement patsFail = connection
					.prepareStatement(deleteFail);
			// 查询工单信息
			PlnZyJgWorkbillEntity work = CoupLoadingService.getPlanWoId(
					connection, woId, PlnZyJgWorkbillEntity.class);
			if (work == null) {
				core.setV_errMsg("未查询到工单信息,请确认!");
				return core;
			}
//			if (!work.getZyBillStatus().equals("2")) {
//				core.setV_errMsg("工单状态不是生产中,不允许此操作!");
//				return core;
//			}
			for (int i = 0; i < pram.size(); i++) {
				ArrayList<String> list = (ArrayList<String>) pram.get(i);
				String resultNo = list.get(0);
				String plinNo = list.get(1);
				int detCount = Integer.parseInt(list.get(2)); 
				// 修改螺纹检验实绩
				patupdate.setObject(1, resultNo);
				patupdate.addBatch();

				patsPlanUpdate.setObject(1, resultNo);
				patsPlanUpdate.setObject(2, lwGx);
				patsPlanUpdate.setObject(3, woId);
				patsPlanUpdate.addBatch();

				patsdelPlan.setObject(1, resultNo);
				patsdelPlan.addBatch();
				
				patsFail.setObject(1, resultNo);
				patsFail.setObject(2, woId);
				patsFail.addBatch();
				
				//FrmMchBugD.DeleteMchWasteJg(connection, woId,resultNo, plinNo,lwGx,detCount);
			}
			patsFail.executeBatch();
			patupdate.executeBatch();
			patsPlanUpdate.executeBatch();
			patsdelPlan.executeBatch();

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

	/**
	 * 查询单个螺纹检验实绩
	 * 
	 * @param planNO
	 * @return
	 */
	public CoreReturnObject getThreadInspectResult(String woId) {
		return this
				.getDataBySqlID(
						"ThreadInspectResultService.ProcessPlan.GetThreadInspectResult",
						new Object[] { woId });
	}

	/**
	 * 查询自检记录
	 * 
	 * @param planNO
	 * @return
	 */
	public CoreReturnObject getThreadInspectSelfcheck(String planNO, String woId) {
		return this.getDataBySqlID(
				"ThreadInspectResultService.ProcessPlan.GetThreadingSelfcheck",
				new Object[] { planNO, woId });
	}

	public String doAddResultNo(String plineCode,String gx,String reultno, Connection connection) {
		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("ThreadInspectResultService.queryLoadRESULT");// 查询最大实绩编号
		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, plineCode);
			psta.setObject(2, gx);
			psta.setObject(3, reultno);
			ResultSet rs1 = psta.executeQuery();
			if (rs1.next()) {
				resultNo = rs1.getString("RESULT_NO");
			}
			rs1.close();
			psta.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultNo;
	}

	/***
	 * 结炉
	 * 
	 * @param zYgNo
	 * @return
	 */
	public CoreReturnObject doBeside(String lwGx, 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;
			}
			//查询有委外但是没有委外回报完毕
			 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();
//			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, lwGx);
			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, lwGx);
			psta.setObject(2, woID);
			psta.executeUpdate();
			psta.close();
			// String updateSql = CoreXmlSqlParsersFactory
			// .getSql("ThreadInspectResultService.doBeside");
			// PreparedStatement updateSqlPsta = connection
			// .prepareStatement(updateSql);
			// sql = CoreXmlSqlParsersFactory
			// .getSql("CoupCutService.selectComInfo");
			// psta = connection.prepareStatement(sql);
			// psta.setString(1, parm.get(0));
			// psta.setString(2, parm.get(1));
			// psta.setString(3, parm.get(2));
			// rs = psta.executeQuery();
			// if (rs.next()) {
			//
			// } else {
			// cro.setV_errMsg("无当前工序实绩,无法进行结炉!");
			// return cro;
			// }
			// rs.close();
			// psta.close();
			// updateSqlPsta.setString(1, parm.get(0));
			// updateSqlPsta.setString(2, parm.get(1));
			// updateSqlPsta.setString(3, parm.get(2));
			// updateSqlPsta.setString(4, parm.get(0));
			// updateSqlPsta.setString(5, parm.get(2));
			// updateSqlPsta.addBatch();
			// updateSqlPsta.executeBatch();
			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 lwGx,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, lwGx);
			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, lwGx);
			psta.setObject(2, woID);
			psta.executeUpdate();
			psta.close();
			// String updateSql = CoreXmlSqlParsersFactory
			// .getSql("ThreadInspectResultService.unDoBeside");
			// PreparedStatement updateSqlPsta = connection
			// .prepareStatement(updateSql);
			// // String cutFalg = "";
			// // sql = CoreXmlSqlParsersFactory
			// // .getSql("PlanService.CutPlan.GetJGCutPlan");
			// // psta = connection.prepareStatement(sql);
			// // psta.setString(1, parm.get(0));
			// // rs = psta.executeQuery();
			// // if (rs.next()) {
			// // cutFalg = rs.getString("THREAD_CHK_STATUS");
			// // }
			// // if (!cutFalg.equals("2")) {
			// // cro.setV_errMsg("未结炉!");
			// // return cro;
			// // }
			// // rs.close();
			// // psta.close();
			// updateSqlPsta.setString(1, parm.get(0));
			// updateSqlPsta.setString(2, parm.get(1));
			// updateSqlPsta.setString(3, parm.get(2));
			// updateSqlPsta.setString(4, parm.get(0));
			// updateSqlPsta.setString(5, parm.get(2));
			// updateSqlPsta.addBatch();
			// updateSqlPsta.executeBatch();
			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 doAddMatNo(String judNo, String result, String woId,
			Connection connection) {
		String MatNo = "";
		PreparedStatement psta = null;
		Date dt = new Date();
		// SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
		String sqlloading = CoreXmlSqlParsersFactory
				.getSql("FrmInLibrary.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;
	}
}
