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.List;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.craftDesign.model.ComPscEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

/**
 * 备料码数据初始化
 * 
 * @author Administrator
 * 
 */
public class CraftBlDao {
	/**
	 * 查询备料码信息
	 * 
	 * @param con
	 * @param ordLnPk
	 *            备料主键
	 * @return
	 * @throws SQLException
	 */
	public CraftOrdDesignMscEntity getBl(Connection con, String ordLnPk)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftBlDao.getBl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnPk);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() != 1) {
			return null;
		} else {
			CraftTool.nullConverNullString(list.get(0));
			return list.get(0);
		}
	}

	/**
	 * 验证备料码的规格在基础规格表里是否存在
	 * 
	 * @param con
	 * @param dimater
	 * @param height
	 * @return
	 * @throws SQLException
	 */
	public String[] getBaseSpecByDH(Connection con, String dimater,
			String height) throws SQLException {
		String[] arr = null;
		String sql = CoreXmlSqlParsersFactory.getSql("CraftBlDao.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, String produccode,
			String stdCode, String steelCode, String stdStyle)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftBlDao.getPsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, produccode);
		ps.setObject(2, stdCode);
		ps.setObject(3, steelCode);
		ps.setObject(4, stdStyle);
		ResultSet rs = ps.executeQuery();
		List<ComPscEntity> list = EntityTool.getEntitysByRs(rs,
				ComPscEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() == 1) {
			CraftTool.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("CraftBlDao.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;
	}
}
