package com.steering.pss.sale.order.qualityDesign.Dao.idao;

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.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.PhyDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdPicEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdPicSmpEntity;

public class IMscPhyDao implements PhyDaoImp {
	/**
	 * 查询已审批理化索引号
	 * 
	 * @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("IMscPhyDao.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("IMscPhyDao.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<SlmOrdDesignStdPicEntity> getPhyByPic(Connection con, String pic)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscPhyDao.getPhyByPic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, pic);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignStdPicEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignStdPicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (SlmOrdDesignStdPicEntity phy : list) {
				QualityDesignTool.nullConverNullString(phy);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 保存理化标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param phy
	 *            理化标准类
	 * @throws SQLException
	 */
	public void savePhy(Connection con, SlmOrdDesignStdPicEntity phy)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscPhyDao.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<SlmOrdDesignStdPicSmpEntity> 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("IMscPhyDao.getPicSample");
		b.Fill(new String[]{pic, phyCodeMin, itemCodeF, itemCodeC, itemCodeW, stdCode, steelCode, produccode});
		PreparedStatement ps = con.prepareStatement(b.toString());
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignStdPicSmpEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignStdPicSmpEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (SlmOrdDesignStdPicSmpEntity phy : list) {
				QualityDesignTool.nullConverNullString(phy);
			}
			return list;
		} else {
			return null;
		}
	}
	
	/**
	 * 
	 * @param con
	 * @param pic
	 * @param phyCodeMin
	 * @return
	 * @throws SQLException
	 */
	public List<SlmOrdDesignStdPicSmpEntity> getPicSpline(Connection con,
			String pic, String phyCodeMin, String stdCode, String producCode, String steelCode,
			String itemCodeF, String itemCodeC, String itemCodeW) throws SQLException{
		String sql = CoreXmlSqlParsersFactory.getSql("IMscPhyDao.getPicSpline");
		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<SlmOrdDesignStdPicSmpEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignStdPicSmpEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (SlmOrdDesignStdPicSmpEntity phy : list) {
				QualityDesignTool.nullConverNullString(phy);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 保存取样要求
	 * 
	 * @param con
	 *            连接字符串
	 * @param comPicSmp
	 *            取样类
	 * @param designKey
	 *            质量主键
	 * @throws SQLException
	 */
	public void savePicSample(Connection con,
			SlmOrdDesignStdPicSmpEntity comPicSmp) throws SQLException {
		if (comPicSmp == null) {
			return;
		}
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscPhyDao.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,
			SlmOrdDesignStdPicSmpEntity comPicSpline) throws SQLException {
		if (comPicSpline == null) {
			return;
		}
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscPhyDao.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("IMscPhyDao.updateDesignStd");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, pic);
		ps.setObject(2, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 修改交付标准索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param pic
	 *            索引号
	 * @param cic
	 *            质量主键
	 * @throws SQLException
	 */
	public boolean checkSmp(Connection con,
			SlmOrdDesignStdPicSmpEntity comPicSmp) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscPhyDao.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;
	}
}
