package com.steering.mes.mcp.Mch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.MchComSetplineEntity;
import com.steering.mes.mcp.entity.RtdMeasurePosdataEntity;

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

public class FrmZjResult extends DBCommon{

	/**
	 * 查询终检基础信息
	 */
	public CoreReturnObject getQueryZjBaseInfo(String heatNo) {
		return this.getDataBySqlID("FrmZjResult.getQueryZjBaseInfo",
				new Object[] { heatNo});
	}
	/**
	 * 查询终检详情信息
	 */
	public CoreReturnObject getQueryZjDetail(String zjId) {
		return this.getDataBySqlID("FrmZjResult.getQueryZjDetial",
				new Object[] { zjId});
	}

	/**
	 * 查询终检管号信息
	 */
	public CoreReturnObject getQueryZjc(String stoveNo) {
		return this.getDataBySqlID("FrmZjResult.getQueryZjc",
				new Object[] { stoveNo});
	}
	
	/**
	 * 保存
	 */
	public CoreReturnObject doSaveZjRusult(ArrayList<String> plnZyZjm,ArrayList<String> plnZyZjcDtail,ArrayList<ArrayList<String>> plnZyZjc) {
		CoreReturnObject core = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		PreparedStatement pstaM = null;
		PreparedStatement pstaMd = null;
		PreparedStatement pstaC = null;
		ResultSet rs = null;
		try {
			connection.setAutoCommit(false); //
			String sql = "SELECT COUNT(*) ACOUNT FROM PLN_ZY_ZJ_M T WHERE T.JUDGE_STOVE_NO = '"+plnZyZjm.get(0)+"'";
			String InsertM = CoreXmlSqlParsersFactory
					.getSql("FrmZjResult.AddPlaZyZjM");
			String UpdatetM = CoreXmlSqlParsersFactory
					.getSql("FrmZjResult.UpdatePlaZyZjM");
			String InsertMd = CoreXmlSqlParsersFactory
					.getSql("FrmZjResult.AddPlaZyZjMd");
			String UpdateMd = CoreXmlSqlParsersFactory
					.getSql("FrmZjResult.UpdatePlaZyZjMd");
			String InsertC = CoreXmlSqlParsersFactory
					.getSql("FrmZjResult.AddPlaZyZjC");
			String UpdateC = CoreXmlSqlParsersFactory
					.getSql("FrmZjResult.UpdatePlaZyZjC");
			psta = connection.prepareStatement(sql);
			rs = psta.executeQuery();
			int count = 0;
			if(rs.next()) {
				count = Integer.parseInt(rs.getString("ACOUNT"));
			}
			//修改
			if(count > 0) {
				//修改基础表
				pstaM = connection.prepareStatement(UpdatetM);
				pstaM.setString(1, plnZyZjm.get(1));
				pstaM.setString(2, plnZyZjm.get(2));
				pstaM.setString(3, plnZyZjm.get(3));
				pstaM.setString(4, plnZyZjm.get(4));
				pstaM.setString(5, plnZyZjm.get(5));
				pstaM.setString(6, plnZyZjm.get(6));
				pstaM.setString(7, plnZyZjm.get(7));
				pstaM.setString(8, plnZyZjm.get(8));
				pstaM.setString(9, plnZyZjm.get(9));
				pstaM.setString(10, plnZyZjm.get(10));
				pstaM.setString(11, plnZyZjm.get(11));
				pstaM.setString(12, plnZyZjm.get(12));
				pstaM.setString(13, plnZyZjm.get(13));
				pstaM.addBatch();
				pstaM.executeBatch();
				//从表
				pstaMd = connection.prepareStatement(UpdateMd);
				pstaMd.setString(1, plnZyZjcDtail.get(0));
				pstaMd.setString(2, plnZyZjcDtail.get(1));
				pstaMd.setString(3, plnZyZjcDtail.get(2));
				pstaMd.setString(4, plnZyZjcDtail.get(3));
				pstaMd.setString(5, plnZyZjcDtail.get(4));
				pstaMd.setString(6, plnZyZjcDtail.get(5));
				pstaMd.setString(7, plnZyZjcDtail.get(6));
				pstaMd.setString(8, plnZyZjcDtail.get(7));
				pstaMd.setString(9, plnZyZjcDtail.get(8));
				pstaMd.setString(10, plnZyZjcDtail.get(9));
				pstaMd.setString(11, plnZyZjcDtail.get(10));
				pstaMd.setString(12, plnZyZjcDtail.get(11));
				pstaMd.setString(13, plnZyZjcDtail.get(12));
				pstaMd.setString(14, plnZyZjcDtail.get(13));
				pstaMd.setString(15, plnZyZjcDtail.get(14));
				pstaMd.setString(16, plnZyZjcDtail.get(15));
				pstaMd.setString(17, plnZyZjcDtail.get(16));
				pstaMd.addBatch();
				pstaMd.executeBatch();
				//管序号
				pstaC = connection.prepareStatement(UpdateC);
				for (ArrayList<String> arrayList : plnZyZjc) {
					pstaC.setString(1, arrayList.get(2));
					pstaC.setString(2, arrayList.get(3));
					pstaC.setString(3, arrayList.get(4));
					pstaC.setString(4, arrayList.get(5));
					pstaC.setString(5, arrayList.get(6));
					pstaC.setString(6, arrayList.get(7));
					pstaC.setString(7, arrayList.get(8));
					pstaC.setString(8, arrayList.get(9));
					pstaC.setString(9, arrayList.get(10));
					pstaC.setString(10, arrayList.get(11));
					pstaC.setString(11, arrayList.get(12));
					pstaC.setString(12, arrayList.get(13));
					pstaC.setString(13, arrayList.get(14));
					pstaC.setString(14, arrayList.get(15));
					pstaC.setString(15, arrayList.get(16));
					pstaC.setString(16, arrayList.get(17));
					pstaC.setString(17, arrayList.get(18));
					pstaC.setString(18, arrayList.get(19));
					pstaC.setString(19, arrayList.get(20));
					pstaC.setString(20, arrayList.get(21));
					pstaC.setString(21, arrayList.get(22));
					pstaC.setString(22, arrayList.get(23));
					pstaC.setString(23, arrayList.get(24));
					pstaC.setString(24, arrayList.get(25));
					pstaC.setString(25, arrayList.get(26));
					pstaC.setString(26, arrayList.get(27));
					pstaC.setString(27, arrayList.get(28));
					pstaC.setString(28, arrayList.get(29));
					pstaC.setString(29, arrayList.get(30));
					pstaC.setString(30, arrayList.get(31));
					pstaC.setString(31, arrayList.get(32));
					pstaC.setString(32, arrayList.get(33));
					pstaC.setString(33, arrayList.get(34));
					pstaC.setString(34, arrayList.get(35));
					pstaC.setString(35, arrayList.get(36));
					pstaC.setString(36, arrayList.get(37));
					pstaC.setString(37, arrayList.get(0));
					pstaC.setString(38, arrayList.get(1));
					pstaC.addBatch();
				}
				pstaC.executeBatch();
			}
			//新增
			else {
				//基础信息
				pstaM = connection.prepareStatement(InsertM);
				pstaM.setString(1, plnZyZjm.get(0));
				pstaM.setString(2, plnZyZjm.get(1));
				pstaM.setString(3, plnZyZjm.get(2));
				pstaM.setString(4, plnZyZjm.get(3));
				pstaM.setString(5, plnZyZjm.get(4));
				pstaM.setString(6, plnZyZjm.get(5));
				pstaM.setString(7, plnZyZjm.get(6));
				pstaM.setString(8, plnZyZjm.get(7));
				pstaM.setString(9, plnZyZjm.get(8));
				pstaM.setString(10, plnZyZjm.get(9));
				pstaM.setString(11, plnZyZjm.get(10));
				pstaM.setString(12, plnZyZjm.get(11));
				pstaM.setString(13, plnZyZjm.get(12));
				pstaM.addBatch();
				pstaM.executeBatch();
				//从表
				String zjIdSql = CoreXmlSqlParsersFactory
						.getSql("FrmZjResult.queryZjBaseInfoId");
				psta = connection.prepareStatement(zjIdSql);
				psta.setString(1, plnZyZjm.get(0));
				rs = psta.executeQuery();
				String zjId = "";
				if(rs.next()) {
					zjId = rs.getString("ZJID");
				}
				pstaMd = connection.prepareStatement(InsertMd);
				pstaMd.setString(1, zjId);
				pstaMd.setString(2, plnZyZjcDtail.get(0));
				pstaMd.setString(3, plnZyZjcDtail.get(1));
				pstaMd.setString(4, plnZyZjcDtail.get(2));
				pstaMd.setString(5, plnZyZjcDtail.get(3));
				pstaMd.setString(6, plnZyZjcDtail.get(4));
				pstaMd.setString(7, plnZyZjcDtail.get(5));
				pstaMd.setString(8, plnZyZjcDtail.get(6));
				pstaMd.setString(9, plnZyZjcDtail.get(7));
				pstaMd.setString(10, plnZyZjcDtail.get(8));
				pstaMd.setString(11, plnZyZjcDtail.get(9));
				pstaMd.setString(12, plnZyZjcDtail.get(10));
				pstaMd.setString(13, plnZyZjcDtail.get(11));
				pstaMd.setString(14, plnZyZjcDtail.get(12));
				pstaMd.setString(15, plnZyZjcDtail.get(13));
				pstaMd.setString(16, plnZyZjcDtail.get(14));
				pstaMd.setString(17, plnZyZjcDtail.get(15));
				pstaMd.addBatch();
				pstaMd.executeBatch();
				//管序号
				pstaC = connection.prepareStatement(UpdateC);
				for (ArrayList<String> arrayList : plnZyZjc) {
					pstaC.setString(1, arrayList.get(2));
					pstaC.setString(2, arrayList.get(3));
					pstaC.setString(3, arrayList.get(4));
					pstaC.setString(4, arrayList.get(5));
					pstaC.setString(5, arrayList.get(6));
					pstaC.setString(6, arrayList.get(7));
					pstaC.setString(7, arrayList.get(8));
					pstaC.setString(8, arrayList.get(9));
					pstaC.setString(9, arrayList.get(10));
					pstaC.setString(10, arrayList.get(11));
					pstaC.setString(11, arrayList.get(12));
					pstaC.setString(12, arrayList.get(13));
					pstaC.setString(13, arrayList.get(14));
					pstaC.setString(14, arrayList.get(15));
					pstaC.setString(15, arrayList.get(16));
					pstaC.setString(16, arrayList.get(17));
					pstaC.setString(17, arrayList.get(18));
					pstaC.setString(18, arrayList.get(19));
					pstaC.setString(19, arrayList.get(20));
					pstaC.setString(20, arrayList.get(21));
					pstaC.setString(21, arrayList.get(22));
					pstaC.setString(22, arrayList.get(23));
					pstaC.setString(23, arrayList.get(24));
					pstaC.setString(24, arrayList.get(25));
					pstaC.setString(25, arrayList.get(26));
					pstaC.setString(26, arrayList.get(27));
					pstaC.setString(27, arrayList.get(28));
					pstaC.setString(28, arrayList.get(29));
					pstaC.setString(29, arrayList.get(30));
					pstaC.setString(30, arrayList.get(31));
					pstaC.setString(31, arrayList.get(32));
					pstaC.setString(32, arrayList.get(33));
					pstaC.setString(33, arrayList.get(34));
					pstaC.setString(34, arrayList.get(35));
					pstaC.setString(35, arrayList.get(36));
					pstaC.setString(36, arrayList.get(37));
					pstaC.setString(37, arrayList.get(0));
					pstaC.setString(38, arrayList.get(1));
					pstaC.addBatch();
				}
				pstaC.executeBatch();
			}
			connection.commit();
			core.setV_errCode(1);
			core.setV_errMsg("操作成功");
		}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 judgeStoveNo
	 * @param BatchNo
	 * @return
	 */
	public CoreReturnObject geRtdMeasurePosdataAll(String judgeStoveNo,String BatchNo) {
			String sql = CoreXmlSqlParsersFactory.getSql("FrmZjResult.geRtdMeasurePosdataAll");
			if (!judgeStoveNo.equals("")) {
			sql += " AND T.JUDGE_STOVE_NO='" + judgeStoveNo + "'";
			}
			if (!BatchNo.equals("")) {
			sql += " AND (T.BATCH_NO ='" + BatchNo + "' OR T.PLINE_CODE='C092') "; //天淮本地没有批号  不判断批号
			}
			sql += " AND T.PLINE_CODE IN ('C092','C100','C102','C104','C107','C108','C109')";
			sql+= "ORDER BY TO_NUMBER (MAT_NO)";
			return this.getData(sql);
			}
	
    /***
     * 确认管号
     * @param pram
     * @return
     */
	 public CoreReturnObject addZjc(ArrayList<String> pram) {
		   CoreReturnObject core = new CoreReturnObject();
	        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
	                .getConnection();
	        PreparedStatement psta = null;
	        try {
	        	connection.setAutoCommit(false); // 取消自动提交事务
	        	String Addsql = CoreXmlSqlParsersFactory
	                    .getSql("FrmZjResult.CheckZjC");// 管号选择
	        	psta = connection.prepareStatement(Addsql);
	        	for (int i = 0; i < pram.size(); i++) {
	        		RtdMeasurePosdataEntity Load = EntityTool.getEntityByJSONStr(pram.get(i),RtdMeasurePosdataEntity.class);
                    String sql = "select count(*) acount from pln_zy_zj_c where mat_no = '"+Load.getMatNo()+"' and heat_plan_sqe = '"+Load.getMatNoCx()+"' and judge_stove_no = '"+Load.getJudgeStoveNo()+"' ";
                    PreparedStatement ps =  connection.prepareStatement(sql);
                    ResultSet rs =  ps.executeQuery();
                    if(rs.next()) {
                     int count =	Integer.parseInt(rs.getString("acount"));
                     if(count > 0) continue;
                    }
	        		psta.setString(1, Load.getMatNo());
	        		psta.setString(2, Load.getMatNoCx());
	        		psta.setString(3, Load.getJudgeStoveNo());
	        		psta.setString(4, Load.getActLen());
	        		psta.addBatch();
	        	}
	        	psta.executeBatch();
	        	connection.commit();
	        	core.setV_errCode(1);
	        	core.setV_errMsg("确认成功");
	        }
	        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 pram
	     * @return
	     */
		 public CoreReturnObject DZjc(ArrayList<String> pram) {
			   CoreReturnObject core = new CoreReturnObject();
		        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
		                .getConnection();
		        PreparedStatement psta = null;
		        try {
		        	connection.setAutoCommit(false); // 取消自动提交事务
		        	String sql = CoreXmlSqlParsersFactory
		                    .getSql("FrmZjResult.DCheckZjC");// 管号选择
		        	psta = connection.prepareStatement(sql);
		        	for (int i = 0; i < pram.size(); i++) {
		        		RtdMeasurePosdataEntity Load = EntityTool.getEntityByJSONStr(pram.get(i),RtdMeasurePosdataEntity.class);
		        		psta.setString(1, Load.getMatNoCx());
		        		psta.setString(2, Load.getJudgeStoveNo());
		        		psta.setString(3, Load.getMatNo());
		        		psta.addBatch();
		        	}
		        	psta.executeBatch();
		        	connection.commit();
		        	core.setV_errCode(1);
		        	core.setV_errMsg("取消成功");
		        }
		        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 judgeStoNo
		  * @return
		  */
		public CoreReturnObject DoDelete(String judgeStoNo) {
				   CoreReturnObject core = new CoreReturnObject();
			        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
			                .getConnection();
			        PreparedStatement psta = null;
			        try {
			        	connection.setAutoCommit(false); // 取消自动提交事务
			        	String sql = CoreXmlSqlParsersFactory
			                    .getSql("FrmZjResult.DoDelete");// 管号选择
			        	psta = connection.prepareStatement(sql);
			        	psta.setString(1,judgeStoNo);
			        	psta.addBatch();
			        	psta.executeBatch();
			        	connection.commit();
			        	core.setV_errCode(1);
			        	core.setV_errMsg("操作成功");
			        }
			        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;
			 }
		 
}
