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 CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.craftDesign.model.ComBaseGradeEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdChemBiasEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdCicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdBiasCEntity;

public class DeviationChemDao {
	/**
	 * 查询成分主记录
	 * 
	 * @param con
	 * @param cic
	 *            成分索引号
	 * @return ComMscStdCicEntity
	 * @throws SQLException
	 */
	public ComMscStdCicEntity getChemMain(Connection con, String cic)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("DeviationChemDao.getChemMain");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, cic);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdCicEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscStdCicEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * 查询成分偏差主记录
	 * 
	 * @param con
	 * @param stdCode
	 * @param gradeCode
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdChemBiasEntity getChemBais(Connection con, String stdCode,
			String gradeType) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("DeviationChemDao.getChemBais");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, stdCode);
		ps.setObject(2, gradeType);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdChemBiasEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscStdChemBiasEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * 查询成分偏差主记录
	 * 
	 * @param con
	 * @param stdCode
	 * @param steelCode
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdChemBiasEntity getChemSteel(Connection con, String stdCode,
			String steelCode) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("DeviationChemDao.getChemSteel");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, stdCode);
		ps.setObject(2, steelCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdChemBiasEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscStdChemBiasEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * 增加成分偏差索引号
	 * 
	 * @param con
	 * @param designKey
	 * @param cic
	 * @throws SQLException
	 */
	public void updateCraftStd(Connection con, String designKey, String cic,
			String flag) throws SQLException {
		String sql = "update CRAFT_ORD_DESIGN_STD t set t.CIC_BAIS=? where t.DESGIN_KEY=?";
		if (flag.equals("N")) {
			sql = "update CRAFT_ORD_DESIGN_STD t set t.CIC_N_BAIS=? where t.DESGIN_KEY=?";
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, cic);
		ps.setObject(2, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询成分偏差从记录
	 * 
	 * @param con
	 * @param stdCode
	 * @param steelCode
	 * @return
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdBiasCEntity> getChemBiasC(Connection con,
			String cic) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("DeviationChemDao.getChemBiasC");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, cic);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdBiasCEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignStdBiasCEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		}
		return list;
	}

	/**
	 * 新增从表记录
	 * 
	 * @param con
	 * @param entity
	 * @throws SQLException
	 */
	public void addChemBiasC(Connection con, CraftOrdDesignStdBiasCEntity entity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("DeviationChemDao.addChemBiasC");
		sql = EntityTool.fillSqlByEntity(sql, entity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 获取钢种类型
	 * 
	 * @param con
	 * @param stdCode
	 * @param steelCode
	 * @return
	 * @throws SQLException
	 */
	public ComBaseGradeEntity getGradeType(Connection con, String gradeCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("DeviationChemDao.getGradeType");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, gradeCode);
		ResultSet rs = ps.executeQuery();
		List<ComBaseGradeEntity> list = EntityTool.getEntitysByRs(rs,
				ComBaseGradeEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
}
