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

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.bomGeneration.daoImp.BomBLDaoImp;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomBlEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomEntity;
import com.steering.pss.sale.order.bomGeneration.model.MscBlEntity;
import com.steering.pss.sale.order.bomGeneration.model.SlmOrdDesignMscBlEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

/**
 * 备料 物料编码
 * 
 * @author Administrator
 * 
 */
public class IBomBLDao implements BomBLDaoImp {

	/**
	 * 查询备料码
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<SlmOrdDesignMscBlEntity> getBLCode(Connection con,
			String designKey, String craftFlag) throws SQLException {
		String sql = "";
		if (craftFlag.equals("0")) {
			sql = CoreXmlSqlParsersFactory.getSql("IBomBLDao.getBLCodeQuality");
		} else {
			sql = CoreXmlSqlParsersFactory.getSql("IBomBLDao.getBLCodeCraft");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignMscBlEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignMscBlEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		}
		return list;
	}

	/**
	 * 查询备料码信息
	 * 
	 * @param con
	 *            连接字符串
	 * @param BLCode
	 *            备料码
	 * @return MscBlEntity
	 * @throws SQLException
	 */
	public MscBlEntity getBLInfo(Connection con, String BLCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomBLDao.getBLInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, BLCode);
		ResultSet rs = ps.executeQuery();
		List<MscBlEntity> bLInfos = QualityDesignTool.getEntitysByRs(rs,
				MscBlEntity.class);
		rs.close();
		ps.close();
		if (bLInfos != null && bLInfos.size() > 0) {
			MscBlEntity BLInfo = bLInfos.get(0);
			QualityDesignTool.nullConverNullString(BLInfo);
			return BLInfo;
		} else {
			return null;
		}
	}

	/**
	 * 保存备料物料与产品物料关系
	 * 
	 * @param con
	 *            连接字符串
	 * @param blInfo
	 * @throws SQLException
	 */
	public void saveBLInfo(Connection con, DesignBomBlEntity blInfo)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomBLDao.saveBLInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blInfo.getBom());
		ps.setObject(2, blInfo.getBomBl());
		ps.setObject(3, blInfo.getBlTyp());
		ps.setObject(4, blInfo.getBlMaterialNo());
		ps.setObject(5, blInfo.getBlMaterialDesc());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 验证备料信息重复
	 * 
	 * @param con
	 * @param blInfo
	 * @return
	 * @throws SQLException
	 */
	public int checkBLInfo(Connection con, DesignBomBlEntity blInfo)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("IBomBLDao.checkBLInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blInfo.getBom());
		ps.setObject(2, blInfo.getBomBl());
		ps.setObject(3, blInfo.getBlMaterialNo());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = Integer.parseInt(rs.getString("COUNT"));
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询备料BOM
	 * 
	 * @param con
	 * @param BLCode
	 * @return
	 * @throws SQLException
	 */
	public List<DesignBomEntity> getBLBom(Connection con, String BLCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomBLDao.getBLBom");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, BLCode);
		ResultSet rs = ps.executeQuery();
		List<DesignBomEntity> BLBoms = QualityDesignTool.getEntitysByRs(rs,
				DesignBomEntity.class);
		rs.close();
		ps.close();
		if (BLBoms != null && BLBoms.size() > 0) {
			for (DesignBomEntity BLBom : BLBoms) {
				QualityDesignTool.nullConverNullString(BLBom);
			}
			return BLBoms;
		} else {
			return null;
		}
	}
}
