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.CouplingSandblastResultEntity;
import com.steering.mes.mcp.entity.PlnZyJgEntity;
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 FrmSandBlast extends DBCommon {
	/**
	 * 查询喷砂实绩
	 * 
	 * @param planNO
	 * @return
	 */
	public CoreReturnObject getCoupSandBlast(String woId) {
		Object[] pamar = new Object[] { woId };
		return this.getDataBySqlID("FrmSandBlast.getCoupSandBlast", pamar);
	}
	/*
	 * 查询喷砂实绩支数
	 */
	public CoreReturnObject getSandblastResult(String woid) {
		return this.getDataBySqlID("FrmSandBlast.getSandblastResult",
				new Object[] { woid });
	}
	/**
	 * 新增喷砂实绩
	 * 
	 * @param result
	 *            喷砂实绩
	 * @return·
	 */
	public CoreReturnObject addSandResult(CouplingSandblastResultEntity result,
			String sandGx, String woId) {
		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("FrmSandBlast.AddSandResult");// 新增喷砂实绩
			PreparedStatement insertResultPsta = con
					.prepareStatement(insertResultsql);
			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";
				}
			}
			// 查询工单信息
			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(),"08",work.getPlineCode()+"08", con);
			insertResultPsta.setString(1, resultNo);
			insertResultPsta.setString(2, work.getPlineCode());
			insertResultPsta.setString(3, work.getHeatPlanNo());
			insertResultPsta.setString(4, result.getSprayMaterial());
			insertResultPsta.setString(5, result.getSprayThick());
			insertResultPsta.setString(6, result.getSprayEquipment());
			insertResultPsta.setString(7, result.getSprayPressure());
			insertResultPsta.setString(8, result.getSprayCount());
			insertResultPsta.setString(9, result.getRejectedPcs());
			insertResultPsta.setString(10, result.getRejectedReason());
			insertResultPsta.setString(11, result.getColUser());
			insertResultPsta.setString(12, result.getColShift());
			insertResultPsta.setString(13, result.getColGroup());
			insertResultPsta.setString(14, result.getColOperator());
			insertResultPsta.setString(15, "0");
			insertResultPsta.setString(16, result.getQualiriedPcs());
			insertResultPsta.setString(17, result.getMemo());
			insertResultPsta.setString(18, result.getCreateTime());
			insertResultPsta.setString(19, lutime);
			insertResultPsta.setString(20, work.getWoId());
			insertResultPsta.setString(21, work.getPlineName());
			insertResultPsta.setString(22, work.getJudgeStoveNo());
			insertResultPsta.setString(23, work.getBatchNo());
			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");
			
//			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("FrmSandBlast.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("FrmSandBlast.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("FrmSandBlast.ProcessPlan.lixianThreadingResult");// 离线喷砂实绩
			PreparedStatement insertResultPsta = con
					.prepareStatement(insertResultSql);
			String insertComInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.lixianComInfoResult");// 离线ComInfo表
			PreparedStatement insertComInfoPsta = con
					.prepareStatement(insertComInfoSql);
			String insertofflineSql = CoreXmlSqlParsersFactory
					.getSql("CoupCutService.lixianofflineResult");// 离线表
			PreparedStatement insertofflinePsta = con
					.prepareStatement(insertofflineSql);
			String updateSql = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.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("FrmSandBlast.ProcessPlan.lixianThreadingResult");// 删除喷砂实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.unlixianComInfoResult");// 删除comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
			String planJgSql = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.unlixianofflineResult");// 撤销离线表
			PreparedStatement planJgPsta = con.prepareStatement(planJgSql);
			String planJgSql1 = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.unupdatelixianofflineResult");// 修改工单表状态
			PreparedStatement planJgPsta1 = con.prepareStatement(planJgSql1);
			String planJgSql2 = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.unupdatelixianofflineResult2");// 修改工单表状态
			PreparedStatement planJgPsta2 = con.prepareStatement(planJgSql2);
			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("FrmSandBlast.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(
			CouplingSandblastResultEntity resul, String sandGx, String woId) {
		CoreReturnObject cor = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		try {
			con.setAutoCommit(false); // 取消自动提交事务
			String cutResultsql = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.UpdateSandResult");// 修改喷砂实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.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;
			}
			// 修改喷砂实绩
			cutResultPsta.setObject(1, resul.getSprayMaterial());
			cutResultPsta.setObject(2, resul.getSprayThick());
			cutResultPsta.setObject(3, resul.getSprayEquipment());
			cutResultPsta.setObject(4, resul.getSprayPressure());
			cutResultPsta.setObject(5, resul.getSprayCount());
			cutResultPsta.setObject(6, resul.getRejectedPcs());
			cutResultPsta.setObject(7, resul.getRejectedReason());
			cutResultPsta.setObject(8, resul.getColShift());
			cutResultPsta.setObject(9, resul.getColGroup());
			cutResultPsta.setObject(10, resul.getColOperator());
			cutResultPsta.setObject(11, resul.getQualiriedPcs());
			cutResultPsta.setObject(12, resul.getMemo());
			cutResultPsta.setObject(13, resul.getUpdateName());
			cutResultPsta.setObject(14, resul.getUpdateTime());
			cutResultPsta.setObject(15, resul.getResultNo());
			cutResultPsta.addBatch();
			cutResultPsta.executeBatch();

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

			int hgCount = Integer.parseInt(resul.getQualiriedPcs());
			double hgWt = hgCount * singlewt / 1000;
			double rejWt = Integer.parseInt(resul.getRejectedPcs()) * singlewt
					/ 1000;
			// 修改comInfo表
			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("FrmSandBlast.DeleteSandResult");// 删除喷砂实绩
			PreparedStatement cutResultPsta = con
					.prepareStatement(cutResultsql);
			String comInfoSql = CoreXmlSqlParsersFactory
					.getSql("FrmSandBlast.DeleteComInfoResult");// 删除comInfo表
			PreparedStatement comInfoPsta = con.prepareStatement(comInfoSql);
			// String planJgSql = CoreXmlSqlParsersFactory
			// .getSql("FrmSandBlast.UpdatePlanResult");// 修改工单表
			// PreparedStatement planJgPsta = con.prepareStatement(planJgSql);
			// 查询工单信息
			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);
			}
			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 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("FrmSandBlast.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 psGx, 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 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, psGx);
			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, psGx);
			psta.setObject(2, woID);
			psta.executeUpdate();
			psta.close();
			// int phosNum = 0;
			// // 查询磷化支数
			// String sqlCut =
			// "SELECT SUM(NVL(T.COL_COUNT,0)) COL_COUNT  FROM COUPLING_CUT_RESULT T WHERE T.WO_ID=?";
			// psta = connection.prepareStatement(sqlCut);
			// psta.setString(1, woID);
			// rs = psta.executeQuery();
			// if (rs.next()) {
			// phosNum = rs.getInt("COL_COUNT");
			// }
			// rs.close();
			// psta.close();
			// String updateSql = CoreXmlSqlParsersFactory
			// .getSql("FrmSandBlast.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 psGx, 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, psGx);
			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, psGx);
			psta.setObject(2, woID);
			psta.executeUpdate();
			psta.close();
			// String updateSql = CoreXmlSqlParsersFactory
			// .getSql("FrmSandBlast.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("BLAST_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;
	}
}
