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.model.SlmOrdDesignMscPlPrcEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.model.DesignMscBiltCEntity;
import com.steering.pss.sale.order.model.DesignMscBiltEntity;
import com.steering.pss.sale.order.model.DesignMscEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgEntity;
import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGxPrlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGzEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

public class CraftDao {
	/**
	 * 查询铸造规格
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscBiltCEntity> getBiltCSpec(Connection con,
			String designKey, String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getBiltCSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscBiltCEntity> list = EntityTool.getEntitysByRs(rs,
				DesignMscBiltCEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			// for (DesignMscBiltCEntity rollSpecPline : list) {
			// CraftTool.nullConverNullString(rollSpecPline);
			// }
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询锻造规格
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscBiltEntity> getBiltSpec(Connection con,
			String designKey, String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getBiltSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscBiltEntity> list = EntityTool.getEntitysByRs(rs,
				DesignMscBiltEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询轧管规格
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscGzGgEntity> getRollSpec(Connection con,
			String designKey, String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getRollSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscGzGgEntity> list = EntityTool.getEntitysByRs(rs,
				DesignMscGzGgEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询钢种
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscPlGzEntity> getGrade(Connection con, String designKey,
			String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getGrade");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlGzEntity> list = EntityTool.getEntitysByRs(rs,
				DesignMscPlGzEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询制程
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscPlEntity> getProDuc(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getProDuc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlEntity> list = EntityTool.getEntitysByRs(rs,
				DesignMscPlEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;

		}
	}

	/**
	 * 查询工艺评审主记录
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public DesignMscEntity getCraftMsc(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getCraftMsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<DesignMscEntity> list = EntityTool.getEntitysByRs(rs,
				DesignMscEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;

		}
	}

	/**
	 * 查询工序
	 * 
	 * @param con
	 * @param designKey
	 * @param mscPline
	 * @param processCode
	 * @return
	 * @throws SQLException
	 */
	public SlmOrdDesignMscPlPrcEntity getOperation(Connection con,
			String designKey, String mscPline, String processCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getOperation");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignMscPlPrcEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignMscPlPrcEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * 查询产线
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            主键
	 * @param mscPline
	 *            全程产线号
	 * @param processCode
	 *            工序
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscPlGxPrlEntity> getProline(Connection con,
			String designKey, String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("CraftDao.getProline");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlGxPrlEntity> proLines = QualityDesignTool
				.getEntitysByRs(rs, DesignMscPlGxPrlEntity.class);
		rs.close();
		ps.close();
		if (proLines != null && proLines.size() > 0) {
			return proLines;
		} else {
			return null;
		}

	}

	/**
	 * 验证 工艺评审-合同行生产BOM
	 * 
	 * @param con
	 * @param designKey
	 * @param bom
	 * @return
	 * @throws SQLException
	 */
	public boolean checkCraftOrdLineBom(Connection con, String designKey,
			String bom) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CraftDao.checkCraftOrdLineBom");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, bom);
		ResultSet rs = ps.executeQuery();
		boolean flag = false;
		if (rs.next()) {
			flag = true;
		}
		rs.close();
		ps.close();
		return flag;
	}

	/**
	 * 保存 工艺评审-合同行生产BOM
	 * 
	 * @param con
	 * @param designKey
	 * @param bom
	 * @throws SQLException
	 */
	public void saveCraftOrdLineBom(Connection con, String designKey, String bom)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("CraftDao.saveCraftOrdLineBom");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, bom);
		ps.executeUpdate();
		ps.close();
	}

}
