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.comm.tools.StringTool;
import com.steering.pss.qcm.model.ComBaseImpactCEntity;
import com.steering.pss.qcm.model.ComBaseImpactEntity;
import com.steering.pss.qcm.model.ComBaseSplineLengthEntity;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.craftDesign.model.ComBaseImpactTempCEntity;
import com.steering.pss.sale.order.craftDesign.model.ComBasePhyItemEntity;
import com.steering.pss.sale.order.craftDesign.model.ComBaseSplineMinSizeEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdPicEntity;
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.tool.CraftTool;

public class MscPhyDao {
	/**
	 * 查询已审批理化索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String> getExaminePic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.getExaminePic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, modelCode);
		ps.setObject(3, msc);
		ps.setObject(4, modelCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String pic = rs.getString("PIC");
			list.add(pic);
		}
		return list;
	}

	/**
	 * 查询有效理化索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String> getEValidPic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.getEValidPic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, modelCode);
		ps.setObject(3, msc);
		ps.setObject(4, modelCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String pic = rs.getString("PIC");
			list.add(pic);
		}
		return list;
	}

	/**
	 * 通过理化索引号查询理化标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param pic
	 *            理化索引号
	 * @return List<DesignStdPicEntity>
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdPicEntity> getPhyByPic(Connection con,
			String pic) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.getPhyByPic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, pic);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdPicEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignStdPicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignStdPicEntity phy : list) {
				CraftTool.nullConverNullString(phy);
			}
			return list;
		} else {
			return null;
		}
	}
	
	public static ComMscStdPicEntity getMscStdPicByPk(String pic, Connection con){
		SqlBuilder b = new SqlBuilder("MscPhyDao.getMscStdPicByPk");
		b.Fill(new String[]{ pic });
		return EntityTool.getEntityBySqlNoClsCon(b.toString(), con, ComMscStdPicEntity.class);
	}
	
	public static ComBaseImpactEntity getBaseImpact(String id, Connection con){
		SqlBuilder b = new SqlBuilder("MscPhyDao.getBaseImpact");
		b.Fill(new String[]{ id });
		return EntityTool.getEntityBySqlNoClsCon(b.toString(), con, ComBaseImpactEntity.class);
	}
	
	public static List<ComBaseSplineLengthEntity> getSplineLength(String itemCodeF, String id,
			Connection con){
		SqlBuilder b = new SqlBuilder("MscPhyDao.getBaseSplineLength");
		b.Fill(new String[]{ itemCodeF, id });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, ComBaseSplineLengthEntity.class);
	}
	
	public static ComBaseImpactCEntity getBaseImpactC(String itemCodeC, String id,
			Connection con){
		SqlBuilder b = new SqlBuilder("MscPhyDao.getBaseImpactC");
		b.Fill(new String[]{ itemCodeC, id });
		return EntityTool.getEntityBySqlNoClsCon(b.toString(), con, ComBaseImpactCEntity.class);
	}

	/**
	 * 保存理化标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param phy
	 *            理化标准类
	 * @throws SQLException
	 */
	public void savePhy(Connection con, CraftOrdDesignStdPicEntity phy)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.savePhy");
		sql = EntityTool.fillSqlByEntity(sql, phy);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	/**
	 * 查询取样要求
	 * 
	 * @param con
	 *            连接字符串
	 * @param pic
	 *            理化索引号
	 * @param phyCodeMin
	 *            取样码
	 * @return DesignStdPicSmpEntity
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdPicSmpEntity> getPicSample(Connection con, String pic, String phyCodeMin, String itemCodeF,
			String itemCodeC, String itemCodeW, String stdCode, String steelCode, String produccode) throws SQLException {
		SqlBuilder b = new SqlBuilder("MscPhyDao.getPicSample");
		b.Fill(new String[]{pic, phyCodeMin, itemCodeF, itemCodeC, itemCodeW, stdCode, steelCode, produccode});
		PreparedStatement ps = con.prepareStatement(b.toString());
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdPicSmpEntity> list = EntityTool.getEntitysByRs(
				rs, CraftOrdDesignStdPicSmpEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignStdPicSmpEntity phy : list) {
				CraftTool.nullConverNullString(phy);
			}
			return list;
		} else {
			return null;
		}
	}
	
	public String getStdCodeNkByPic(Connection con, String pic) throws SQLException{
		GenericEntity value = EntityTool.getEntityBySqlNoClsCon(new SqlBuilder("MscPhyDao.getStdCodeNkByPic")
				.Fill(new String[]{pic}).toString(), con, GenericEntity.class);
		return value.getValue();
	}
	
	/**
	 * 查询试样要求
	 * 
	 * @param con
	 *            连接字符串
	 * @param pic
	 *            理化索引号
	 * @param phyCodeMin
	 *            取样码
	 * @return DesignStdPicSmpEntity
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdPicSmpEntity> getPicSpline(Connection con, String pic, String phyCodeMin, 
			String stdCode, String producCode, String steelCode, String flag, String itemCodeF,
			String itemCodeC, String itemCodeW) throws SQLException {

		String sql = "";
		if(!flag.equals("2")){
			sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.getPicSpline");
		}else{
			sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.getPicSplineNk");
		}
		sql = StringTool.ReplaceAll(sql, "#PIC#", pic);
		sql = StringTool.ReplaceAll(sql, "#PHY_CODE_MIN#", phyCodeMin);
		sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
		sql = StringTool.ReplaceAll(sql, "#PRODUCCODE#", producCode);
		sql = StringTool.ReplaceAll(sql, "#STEELCODE#", steelCode);
		sql = StringTool.ReplaceAll(sql, "#ITEM_CODE_F#", itemCodeF);
		sql = StringTool.ReplaceAll(sql, "#ITEM_CODE_C#", itemCodeC);
		sql = StringTool.ReplaceAll(sql, "#ITEM_CODE_W#", itemCodeW);
		PreparedStatement ps = con.prepareStatement(sql);
		
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdPicSmpEntity> list = EntityTool.getEntitysByRs(
				rs, CraftOrdDesignStdPicSmpEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignStdPicSmpEntity phy : list) {
				CraftTool.nullConverNullString(phy);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 保存取样要求
	 * 
	 * @param con
	 *            连接字符串
	 * @param comPicSmp
	 *            取样类
	 * @param designKey
	 *            质量主键
	 * @throws SQLException
	 */
	public void savePicSample(Connection con, CraftOrdDesignStdPicSmpEntity comPicSmp) throws SQLException {
		if (comPicSmp == null) {
			return;
		}
		String sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.savePicSample");
		sql = EntityTool.fillSqlByEntity(sql, comPicSmp);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}
	
	/**
	 * 保存试样要求
	 * 
	 * @param con
	 *            连接字符串
	 * @param comPicSmp
	 *            取样类
	 * @param designKey
	 *            质量主键
	 * @throws SQLException
	 */
	public void savePicSpline(Connection con,
			CraftOrdDesignStdPicSmpEntity comPicSpline) throws SQLException {
		if (comPicSpline == null) {
			return;
		}
		String sql = CoreXmlSqlParsersFactory.getSql("MscPhyDao.savePicSpline");
		sql = EntityTool.fillSqlByEntity(sql, comPicSpline);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	/**
	 * 修改交付标准索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param pic
	 *            索引号
	 * @param cic
	 *            质量主键
	 * @throws SQLException
	 */
	public void updateDesignStd(Connection con, String pic, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscPhyDao.updateDesignStd");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, pic);
		ps.setObject(2, designKey);
		ps.executeUpdate();
		ps.close();
	}

	public boolean checkSmp(Connection con,
			CraftOrdDesignStdPicSmpEntity comPicSmp) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscPhyDao.checkSmp");
		sql = EntityTool.fillSqlByEntity(sql, comPicSmp);
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		boolean flag = false;
		if (rs.next()) {
			flag = true;
		}
		rs.close();
		ps.close();
		return flag;
	}
	
	public static ComBaseSplineMinSizeEntity  getSplineMinSize(Connection con, String splineDirection,
			String splineStdCode, String size, String dimater, String height) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscPhyDao.getSplineMinSize");
		b.Fill( new String[]{ splineDirection, splineStdCode, size, dimater, height });
		ComBaseSplineMinSizeEntity splineMinSize = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, ComBaseSplineMinSizeEntity.class);
		
		return splineMinSize;
	}
	
	public static ComBaseSplineMinSizeEntity  getSplineMinSizeByName(Connection con, String splineDirection,
			String StdName, String size, String dimater, String height) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscPhyDao.getSplineMinSizeByName");
		b.Fill( new String[]{ splineDirection, StdName, size, dimater, height });
		ComBaseSplineMinSizeEntity splineMinSize = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, ComBaseSplineMinSizeEntity.class);
		
		return splineMinSize;
	}
	
	public static String  checkStdIsExist(Connection con,String splineDirection,String StdName) throws SQLException{
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscPhyDao.checkStdIsExist");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, splineDirection);
		ps.setObject(2, StdName);
		String flag = "0";
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			flag = rs.getString(1);
		}
		rs.close();
		ps.close();
		return flag;
	}
	
	/**
	 * 获取递减温度
	 * @param con
	 * @param id
	 * @param itemCodeC
	 * @param height
	 * @return
	 * @throws SQLException
	 */
	public static ComBaseImpactTempCEntity  getComBaseImpactTempC(Connection con, String id,
			String itemCodeC, String height) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscPhyDao.getComBaseImpactTempC");
		b.Fill( new String[]{ id, itemCodeC, height });
		ComBaseImpactTempCEntity impactTemp = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, ComBaseImpactTempCEntity.class);
		
		return impactTemp;
	}
	
	/**
	 * 通过温度描述获取温度代码
	 * @param con
	 * @param itemNameW
	 * @return
	 * @throws SQLException
	 */
	public static ComBasePhyItemEntity  getPhyItemW(Connection con, String itemNameW) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscPhyDao.getPhyItemW");
		b.Fill( new String[]{ itemNameW });
		ComBasePhyItemEntity phyItem = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, ComBasePhyItemEntity.class);
		
		return phyItem;
	}
}
