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.CouplingInspectionResultEntity;
import com.steering.mes.mcp.entity.CouplingLoadingResultEntity;
import com.steering.mes.mcp.entity.PortJgBatchSampleResultEntity;
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 FrmInspection extends DBCommon {
	/**
	 * 查询探伤实绩
	 * 
	 * @param planNO
	 * @return
	 */
	public CoreReturnObject getCoupSandBlast(String woId) {
		Object[] pamar = new Object[] { woId };
		return this.getDataBySqlID("FrmInspection.getCoupSandBlast", pamar);
	}
	/**
	 * 根据班组带出操作工
	 */
	public CoreReturnObject getComBaseInfo(String code,String group) {
		Object[] pamar = new Object[] { code,group };
		return this.getDataBySqlID(
				"FrmInspection.getComBaseInfo", pamar);
	}
	/*
	 * 查询探伤实绩支数
	 */
	public CoreReturnObject getInspectResult(String woid) {
		return this.getDataBySqlID("FrmInspection.getInspectResult",
				new Object[] { woid });
	}
	/*
	 * 修改探伤路径
	 */
	 public CoreReturnObject UpdateReportUrl(String ResultNo, String Url) {
	        return this.setDataBySqlID("FrmInspection.UpdateReportUrl",
	                new Object[]{Url, ResultNo});
	    }
	/**
	 * 新增探伤实绩
	 * 
	 * @param result
	 *            探伤实绩
	 * @return·
	 */
	public CoreReturnObject addSandResult(ArrayList<String> jsonstrList,
			String sandGx, String woId,ArrayList<Object> list1,ArrayList<Object> list3) {
		CoreReturnObject cor = 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";
		PreparedStatement ps = null;
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String insertResultsql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.AddSandResult");// 新增探伤实绩
			PreparedStatement insertResultPsta = con
					.prepareStatement(insertResultsql);
			
			String addFail = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.ProcessPlan.AddFailComInfo");// 新增废品信息
			PreparedStatement patsphospFail = con.prepareStatement(addFail);
			String lutime = systime;

			CouplingInspectionResultEntity result = new CouplingInspectionResultEntity();

			for (String jsonStr : jsonstrList) {
				result = BaseHelper.jsonToEntity(
						CouplingInspectionResultEntity.class, jsonStr);
			}
			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";
				}
			}
			// 查询工单信息
			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 resultNo = doAddResultNo(work.getPlineCode(),"07",work.getPlineCode()+"07", con);
			insertResultPsta.setString(1, resultNo);
			insertResultPsta.setString(2, result.getPlineCode());
			insertResultPsta.setString(3, work.getHeatPlanNo());
			insertResultPsta.setString(4, result.getQualiriedPcs());
			insertResultPsta.setString(5, result.getRejectedPcs());
			insertResultPsta.setString(6, result.getInspectPieces());
			insertResultPsta.setString(7, result.getColMode());
			insertResultPsta.setString(8, result.getColTime());
			insertResultPsta.setString(9, result.getColUser());
			insertResultPsta.setString(10, result.getColShift());
			insertResultPsta.setString(11, result.getColGroup());
			insertResultPsta.setString(12, result.getInspStandard());
			insertResultPsta.setString(13, result.getSpecimenType());
			insertResultPsta.setString(14, result.getSpecimenMagnetic());
			insertResultPsta.setString(15, result.getMagneticPowder());
			insertResultPsta.setString(16, result.getConcentration());
			insertResultPsta.setString(17, result.getCoilIntensity());
			insertResultPsta.setString(18, result.getCenterIntensity());
			insertResultPsta.setString(19, result.getVoltage());
			insertResultPsta.setString(20, result.getMemo());
			insertResultPsta.setString(21, result.getColTime());
			insertResultPsta.setString(22, result.getColOperator());
			insertResultPsta.setString(23, result.getUnqualiriedReason());
			insertResultPsta.setString(24, result.getUnqualiriedPcs());
			insertResultPsta.setString(25, result.getAmountPcs());
			insertResultPsta.setString(26, result.getFinalQualified());
			insertResultPsta.setString(27, result.getRejectedReason());
			insertResultPsta.setString(28, "0");
			insertResultPsta.setString(29, woId);
			insertResultPsta.setString(30, work.getPlineName());
			insertResultPsta.setString(31, work.getJudgeStoveNo());
			insertResultPsta.setString(32, work.getBatchNo());
			insertResultPsta.setString(33, result.getPosition());
			insertResultPsta.setString(34, result.getMagtimes());
			insertResultPsta.setString(35, result.getIssued());
			insertResultPsta.addBatch();
			insertResultPsta.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(sandGx);
			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.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");
			
			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.setObject(7, list2.get(2));
				patsphospFail.addBatch();
				patsphospFail.executeBatch();
				
			}
			for(int i = 0;i<list3.size();i++)
			{
				ArrayList<String> list2 = (ArrayList<String>) list3.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.setObject(7, list2.get(2));
				patsphospFail.addBatch();
				patsphospFail.executeBatch();
				
			}
			
//			String UpdatePort = CoreXmlSqlParsersFactory.getSql("CoupCutService.UpdatePortJg");// 更改管号队列状态
//	        PreparedStatement patsUpdatePort = con.prepareStatement(UpdatePort);
//			
//			  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;
//	            }
//	            for (PortJgBatchSampleResultEntity prot : matNo) {
//	            	     PlanService.AddJgMatBcM(con,"H",result.getColUser(),"10",
//		                    		prot.getMatNo(),sandGx, work.getWoId(),"");
//
//		            // 修改管号队列状态
//	            	patsUpdatePort.setObject(1, "10");
//		            patsUpdatePort.setObject(2, sandGx);
//		            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(), con);
//						if(Integer.parseInt(result.getRejectedPcs())>0)
//						{
//				            	     PlanService.AddJgMatBcM(con,"H",result.getColUser(),"10",
//				            	    		 matNo,sandGx, work.getWoId(),"",work,resultNo,result.getRejectedPcs(),
//				            	    		 result.getColTime(),result.getColShift(),result.getColGroup());
//						}
			// 新增接箍公共记录
			String addInfo = EntityTool.fillSqlByEntity(
					CoreXmlSqlParsersFactory
							.getSql("CoupLoadingService.AddPlnInfo"), comInfo);
			ps = con.prepareStatement(addInfo);
			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("FrmInspection.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("FrmInspection.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("FrmInspection.ProcessPlan.lixianThreadingResult");// 离线探伤实绩
			PreparedStatement insertResultPsta = con
					.prepareStatement(insertResultSql);
			String insertComInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.lixianComInfoResult");// 离线ComInfo表
			PreparedStatement insertComInfoPsta = con
					.prepareStatement(insertComInfoSql);
			String insertofflineSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.lixianofflineResult");// 离线表
			PreparedStatement insertofflinePsta = con
					.prepareStatement(insertofflineSql);
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.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("FrmInspection.ProcessPlan.lixianThreadingResult");// 探伤实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.unlixianComInfoResult");// 删除comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
			String planJgSql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.unlixianofflineResult");// 撤销离线表
			PreparedStatement planJgPsta = con.prepareStatement(planJgSql);
			String planJgSql1 = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.unupdatelixianofflineResult");// 修改工单表状态
			PreparedStatement planJgPsta1 = con.prepareStatement(planJgSql1);
			String planJgSql2 = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.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("FrmInspection.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 updateSandResult(ArrayList<String> jsonstrList,
			String sandGx, String woId) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String cutResultsql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.UpdateSandResult");// 修改探伤实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.UpdateComInfoResult");// 修改comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);

			CouplingInspectionResultEntity resul = new CouplingInspectionResultEntity();

			for (String jsonStr : jsonstrList) {
				resul = BaseHelper.jsonToEntity(
						CouplingInspectionResultEntity.class, jsonStr);
			}
			// 查询工单信息
			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;
//			}
			// 修改探伤
			cutResultPsta.setObject(1, resul.getQualiriedPcs());
			cutResultPsta.setObject(2, resul.getRejectedPcs());
			cutResultPsta.setObject(3, resul.getInspectPieces());
			cutResultPsta.setObject(4, resul.getColShift());
			cutResultPsta.setObject(5, resul.getColGroup());
			cutResultPsta.setObject(6, resul.getInspStandard());
			cutResultPsta.setObject(7, resul.getSpecimenType());
			cutResultPsta.setObject(8, resul.getSpecimenMagnetic());
			cutResultPsta.setObject(9, resul.getMagneticPowder());
			cutResultPsta.setObject(10, resul.getConcentration());
			cutResultPsta.setObject(11, resul.getCoilIntensity());
			cutResultPsta.setObject(12, resul.getCenterIntensity());
			cutResultPsta.setObject(13, resul.getVoltage());
			cutResultPsta.setObject(14, resul.getMemo());
			cutResultPsta.setObject(15, resul.getColOperator());
			cutResultPsta.setObject(16, resul.getUpdateName());
			cutResultPsta.setObject(17, resul.getUpdateDate());
			cutResultPsta.setObject(18, resul.getUnqualiriedReason());
			cutResultPsta.setObject(19, resul.getUnqualiriedPcs());
			cutResultPsta.setObject(20, resul.getAmountPcs());
			cutResultPsta.setObject(21, resul.getFinalQualified());
			cutResultPsta.setObject(22, resul.getRejectedReason());
			cutResultPsta.setObject(23, resul.getMagtimes());
			cutResultPsta.setObject(24, resul.getIssued());
			cutResultPsta.setObject(25, resul.getResultNo());
			cutResultPsta.addBatch();
			cutResultPsta.executeBatch();

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

			int hgCount = Integer.parseInt(resul.getFinalQualified());
			double hgWt = hgCount * singlewt / 1000;
			double rejWt = Integer.parseInt(resul.getFinalQualified())
					* singlewt / 1000;
			comInfoPsta.setObject(1, hgCount);
			comInfoPsta.setObject(2, hgWt);
			comInfoPsta.setObject(3, resul.getRejectedPcs());
			comInfoPsta.setObject(4, rejWt);
			comInfoPsta.setObject(5, resul.getColShift());
			comInfoPsta.setObject(6, resul.getColGroup());
			comInfoPsta.setObject(7, resul.getColOperator());
			comInfoPsta.setObject(8, sandGx);
			comInfoPsta.setObject(9, resul.getResultNo());
			comInfoPsta.setObject(10, resul.getWoId());
			comInfoPsta.addBatch();
			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 deleteSandResult(ArrayList<ArrayList<String>> pram,
			String sandGx, String woId,String plinCode) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String cutResultsql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.DeleteSandResult");// 删除探伤实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.DeleteComInfoResult");// 删除comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
			
			String deleteFail = CoreXmlSqlParsersFactory
					.getSql("FrmInspection.DeleteFail");// 删除废品
			PreparedStatement patsFail = con
					.prepareStatement(deleteFail);
			// 查询工单信息
			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);
			int detCount = Integer.parseInt(list.get(1));
			// 删除探伤实绩
			cutResultPsta.setObject(1, resNo);
			cutResultPsta.addBatch();
			// 删除探伤公共实绩
			comInfoPsta.setObject(1, resNo);
			comInfoPsta.setObject(2, sandGx);
			comInfoPsta.setObject(3, woId);
			comInfoPsta.addBatch();
			//FrmMchBugD.DeleteMchWasteJg(con, woId,resNo, plinCode,sandGx,detCount);
			patsFail.setObject(1, resNo);
			patsFail.setObject(2, woId);
			patsFail.addBatch();
			 }
			patsFail.executeBatch();
			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 plineCode
	 * @param zygNo
	 * @param connection
	 * @return
	 */
	public String doAddResultNo(String plineCode,String gx,String resultno, 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("FrmInspection.queryLoadRESULT");// 查询最大实绩编号
		try {
			psta = connection.prepareStatement(sqlloading);
			psta.setObject(1, plineCode);
			psta.setObject(2, gx);
			psta.setObject(3, resultno);
			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 tsGx, 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, tsGx);
			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, tsGx);
			psta.setObject(2, woID);
			psta.executeUpdate();
			psta.close();
			// String updateSql = CoreXmlSqlParsersFactory
			// .getSql("FrmInspection.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 tsGx,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, tsGx);
			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, tsGx);
			psta.setObject(2, woID);
			psta.executeUpdate();
			psta.close();

//			psta = connection.prepareStatement(updateSql);
//			psta.setObject(1, tsGx);
//			psta.setObject(2, woID);
//			psta.executeUpdate();
//			psta.close();
			// String updateSql = CoreXmlSqlParsersFactory
			// .getSql("FrmInspection.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("DETECT_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;
	}
}
