package com.steering.pss.sale.order.craftDesign.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdCicEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdDetectionEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdPicEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdProcessEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdSpecEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdWaterEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdCicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdDicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdGicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicSmpEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdSicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdWicEntity;
import com.steering.pss.sale.order.craftDesign.model.ProcessEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

/**
 * 内控标准
 */
public class InnerStdDao {
	/**
	 * 查询成分索引号信息
	 * 
	 * @param con
	 * @param cic
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdCicEntity getCicInfo(Connection con, String cic, String modelCode, List<String> grades)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("InnerStdDao.getCicInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		String strGrades = "";
		for(String strGrade : grades){
			if(strGrades.equals("")){
				strGrades = strGrade;
			}else{
				strGrades += "," + strGrade;
			}
		}
		ps.setObject(1, cic);
		ps.setObject(2, modelCode);
		ps.setObject(3, strGrades);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdCicEntity> cicInfos = EntityTool.getEntitysByRs(rs,
				ComMscStdCicEntity.class);
		rs.close();
		ps.close();
		if (cicInfos == null || cicInfos.size() == 0) {
			return null;
		} else {
			CraftTool.nullConverNullString(cicInfos.get(0));
			return cicInfos.get(0);
		}
	}

	/**
	 * 查询理化标准
	 * 
	 * @param con
	 * @param cic
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdPicEntity getPicInfo(Connection con, String pic, String modelCode, List<String> grades)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("InnerStdDao.getPicInfo");
		List<ComMscStdPicEntity> cicInfos = new ArrayList<ComMscStdPicEntity>();
		for(String strGrade : grades){
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setObject(1, pic);
			ps.setObject(2, modelCode);
			ps.setObject(3, strGrade);
			ResultSet rs = ps.executeQuery();
			cicInfos = EntityTool.getEntitysByRs(rs,
					ComMscStdPicEntity.class);
			rs.close();
			ps.close();
			if(cicInfos.size() > 0){
				break;
			}
		}
		if (cicInfos == null || cicInfos.size() == 0) {
			return null;
		} else {
			CraftTool.nullConverNullString(cicInfos.get(0));
			return cicInfos.get(0);
		}
	}

	/**
	 * 查询公差标准
	 * 
	 * @param con
	 * @param sic
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdSpecEntity getSicInfo(Connection con, String sic, String modelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("InnerStdDao.getSicInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, sic);
		ps.setObject(2, modelCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdSpecEntity> cicInfos = EntityTool.getEntitysByRs(rs,
				ComMscStdSpecEntity.class);
		rs.close();
		ps.close();
		if (cicInfos == null || cicInfos.size() == 0) {
			return null;
		} else {
			CraftTool.nullConverNullString(cicInfos.get(0));
			return cicInfos.get(0);
		}
	}

	/**
	 * 探伤
	 * 
	 * @param con
	 * @param dic
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdDetectionEntity getDicInfo(Connection con, String dic, String modelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("InnerStdDao.getDicInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dic);
		ps.setObject(2, modelCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdDetectionEntity> cicInfos = EntityTool.getEntitysByRs(rs,
				ComMscStdDetectionEntity.class);
		rs.close();
		ps.close();
		if (cicInfos == null || cicInfos.size() == 0) {
			return null;
		} else {
			CraftTool.nullConverNullString(cicInfos.get(0));
			return cicInfos.get(0);
		}
	}

	/**
	 * 查询水压
	 * 
	 * @param con
	 * @param wic
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdWaterEntity getWicInfo(Connection con, String wic, String modelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("InnerStdDao.getWicInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, wic);
		ps.setObject(2, modelCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdWaterEntity> cicInfos = EntityTool.getEntitysByRs(rs,
				ComMscStdWaterEntity.class);
		rs.close();
		ps.close();
		if (cicInfos == null || cicInfos.size() == 0) {
			return null;
		} else {
			CraftTool.nullConverNullString(cicInfos.get(0));
			return cicInfos.get(0);
		}
	}

	/**
	 * 加工
	 * 
	 * @param con
	 * @param gic
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdProcessEntity getGicInfo(Connection con, String gic, String modelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("InnerStdDao.getGicInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, gic);
		ps.setObject(2, modelCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdProcessEntity> cicInfos = EntityTool.getEntitysByRs(rs,
				ComMscStdProcessEntity.class);
		rs.close();
		ps.close();
		if (cicInfos == null || cicInfos.size() == 0) {
			return null;
		} else {
			CraftTool.nullConverNullString(cicInfos.get(0));
			return cicInfos.get(0);
		}
	}

	/**
	 * 验证内控取样要求是否重复
	 * 
	 * @param con
	 * @param entity
	 * @return
	 * @throws SQLException
	 */
	public static boolean checkInnerPicSmp(Connection con,
			CraftOrdDesignStdPicSmpEntity entity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("InnerStdDao.checkInnerPicSmp");
		if(entity.getPhyNameMin().contains("夏比")){
			int a=0;
			int b=1;
			int c=a+b;
		}
		sql = EntityTool.fillSqlByEntity(sql, entity);
		PreparedStatement ps = con.prepareStatement(sql);
		int count = 0;
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean checkInnerPic(Connection con,
			CraftOrdDesignStdPicSmpEntity entity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("InnerStdDao.checkInnerPicSmp");
		sql = EntityTool.fillSqlByEntity(sql, entity);
		PreparedStatement ps = con.prepareStatement(sql);
		int count = 0;
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static void updateInnerSeq(Connection con,
			ProcessEntity processList, String designKey, String mscPline)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("InnerStdDao.updateInnerSeq");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, processList.getCic());
		ps.setObject(2, processList.getPic());
		ps.setObject(3, processList.getSic());
		ps.setObject(4, processList.getDic());
		ps.setObject(5, processList.getWic());
		ps.setObject(6, processList.getGic());
		ps.setObject(7, designKey);
		ps.setObject(8, mscPline);
		ps.setObject(9, processList.getProcessCode());
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 获取工艺评审中MSC0的成分
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdCicEntity> getCraftMsc0Chem(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMsc0Chem");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdCicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSCN的成分
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdCicEntity> getCraftMscNChem(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMscNChem");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdCicEntity.class);
	}
	
	/**
	 * 获取工艺评审中交付标准的成分
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdCicEntity> getCraftJfChem(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftJfChem");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdCicEntity.class);
	}
	
	/**
	 * 获取质量设计中内控的成分
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdCicEntity> getCraftNkChem(Connection con, String designKey,
			String mscPline, String processCode, String cic){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftNkChem");
		b.Fill(new String[]{ designKey, mscPline, processCode, cic});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdCicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSC0的理化
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdPicEntity> getCraftMsc0Phy(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMsc0Phy");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdPicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSCN的理化
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdPicEntity> getCraftMscNPhy(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMscNPhy");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdPicEntity.class);
	}
	
	/**
	 * 获取质量设计中内控的理化
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdPicEntity> getCraftNkPhy(Connection con, String designKey,
			String mscPline, String processCode, String pic){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftNkPhy");
		b.Fill(new String[]{ designKey, mscPline, processCode, pic});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdPicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSC0的公差
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdSicEntity> getCraftMsc0Spec(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMsc0Spec");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdSicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSCN的公差
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdSicEntity> getCraftMscNSpec(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMscNSpec");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdSicEntity.class);
	}
	
	/**
	 * 获取质量设计中Nk的公差
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdSicEntity> getCraftNkSpec(Connection con, String designKey,
			String mscPline, String processCode, String sic){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftNkSpec");
		b.Fill(new String[]{ designKey, mscPline, processCode, sic});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdSicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSC0的探伤
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdDicEntity> getCraftMsc0Dete(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMsc0Dete");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdDicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSCN的探伤
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdDicEntity> getCraftMscNDete(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMscNDete");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdDicEntity.class);
	}
	
	/**
	 * 获取质量设计中Nk的探伤
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdDicEntity> getCraftNkDete(Connection con, String designKey,
			String mscPline, String processCode, String dic){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftNkDete");
		b.Fill(new String[]{ designKey, mscPline, processCode, dic});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdDicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSC0的水压
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdWicEntity> getCraftMsc0Water(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMsc0Water");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdWicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSCN的水压
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdWicEntity> getCraftMscNWater(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMscNWater");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdWicEntity.class);
	}
	
	/**
	 * 获取质量设计中Nk的水压
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdWicEntity> getCraftNkWater(Connection con, String designKey,
			String mscPline, String processCode, String Wic){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftNkWater");
		b.Fill(new String[]{ designKey, mscPline, processCode, Wic});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdWicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSC0的加工
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdGicEntity> getCraftMsc0Process(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMsc0Process");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdGicEntity.class);
	}
	
	/**
	 * 获取工艺评审中MSCN的加工
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdGicEntity> getCraftMscNProcess(Connection con, String designKey){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftMscNProcess");
		b.Fill(new String[]{ designKey });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdGicEntity.class);
	}
	
	/**
	 * 获取质量设计中Nk的加工
	 * @param con
	 * @param designKey
	 * @return
	 */
	public static List<CraftOrdDesignStdGicEntity> getCraftNkProcess(Connection con, String designKey,
			String mscPline, String processCode, String Gic){
		SqlBuilder b = new SqlBuilder("InnerStdDao.getCraftNkProcess");
		b.Fill(new String[]{ designKey, mscPline, processCode, Gic});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdGicEntity.class);
	}
}
