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.List;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.ComMscBlEntity;
import com.steering.pss.sale.order.model.ComPscEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.BLQualityPreDaoImp;

/**
 * 备料码预处理
 * 
 * @author Administrator
 * 
 */
public class BLIQualityPreDao implements BLQualityPreDaoImp {

	/**
	 * 查询备料码
	 * 
	 * @param con
	 *            连接字符串
	 * @param blCode
	 *            备料码
	 * @return ComMscBlEntity
	 * @throws SQLException
	 */
	public ComMscBlEntity getMscBlEntity(Connection con, String blCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("BLIQualityPreDao.getMscBlEntity");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscBlEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscBlEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscBlEntity comMscBlEntity : list) {
				QualityDesignTool.nullConverNullString(comMscBlEntity);
			}
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 通过备料码查询质量主键
	 * 
	 * @param con
	 * @param blCode
	 *            备料码
	 * @return String
	 * @throws SQLException
	 */
	public String getDesignKeyByblCode(Connection con, String blCode)
			throws SQLException {
		String designKey = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("BLIQualityPreDao.getDesignKeyByblCode");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if( rs.getString("DESGIN_KEY")!=null){
				designKey = rs.getString("DESGIN_KEY");
			}else{
				designKey="";
			}
			
		}
		rs.close();
		ps.close();
		return designKey;
	}

	/**
	 * 新增备料码技术评审主记录
	 * 
	 * @param con
	 * @param blEntity
	 * @throws SQLException
	 */
	public void addSlmOrdDesignBl(Connection con, ComMscBlEntity blEntity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("BLIQualityPreDao.addSlmOrdDesignBl");
		sql = EntityTool.fillSqlByEntity(sql, blEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 修改备料码技术评审主记录
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void updateSlmOrdDesignBl(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("BLIQualityPreDao.updateSlmOrdDesignBl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 验证备料码的规格在基础规格表里是否存在
	 * 
	 * @param con
	 * @param dimater
	 * @param height
	 * @return
	 * @throws SQLException
	 */
	public String[] getBaseSpec(Connection con, String dimater, String height)
			throws SQLException {
		String[] arr = null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("BLIQualityPreDao.getBaseSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dimater);
		ps.setObject(2, height);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			arr = new String[2];
			arr[0] = rs.getString("SPEC_CODE");
			arr[1] = rs.getString("SPEC_NAME");
		}
		rs.close();
		ps.close();
		return arr;
	}

	/**
	 * 备料码的四项标准查询产品码
	 * 
	 * @param con
	 * @param blEntity
	 * @return
	 * @throws SQLException
	 */
	public ComPscEntity getPsc(Connection con, ComMscBlEntity blEntity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("BLIQualityPreDao.getPsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blEntity.getProduccode());
		ps.setObject(2, blEntity.getStdCode());
		ps.setObject(3, blEntity.getSteelcode());
		ps.setObject(4, blEntity.getStdStyle());
		ResultSet rs = ps.executeQuery();
		List<ComPscEntity> list = EntityTool.getEntitysByRs(rs,
				ComPscEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() == 1) {
			QualityDesignTool.nullConverNullString(list.get(0));
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 通过产品码与用途码查询冶金规范码
	 * 
	 * @param con
	 * @param psc
	 * @param useCode
	 * @return
	 * @throws SQLException
	 */
	public String getMscByPscAndUseCode(Connection con, String psc,
			String useCode) throws SQLException {
		String msc = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("BLIQualityPreDao.getMscByPscAndUseCode");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, psc);
		ps.setObject(2, useCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			msc = rs.getString("MSC");
		}
		rs.close();
		ps.close();
		return msc;
	}

}
