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.ArrayList;
import java.util.List;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.tools.EntityTool;
import com.steering.pss.qcm.model.ComBaseSpecEntity;
import com.steering.pss.sale.order.model.ComMscCastRForgEntity;
import com.steering.pss.sale.order.model.ComMscRollRUpsetEntity;
import com.steering.pss.sale.order.model.DesignMscBiltCEntity;
import com.steering.pss.sale.order.model.DesignMscBiltEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.SpecManageDaoImp;

public class ISpecManageDao implements SpecManageDaoImp {
	/**
	 * 查询轧管规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param gradeCode
	 *            钢种
	 * @param PlineCode
	 *            二级工序
	 * @return ComMscRollRUpsetEntity
	 * @throws SQLException
	 */
	public List<ComMscRollRUpsetEntity> getRollSpec(Connection con, String dimater, String height, String gradeCode,
			String PlineCode) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.getRollSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, PlineCode);
		//ps.setObject(2, gradeCode);
		ps.setObject(2, dimater);
		ps.setObject(3, height);
		ResultSet rs = ps.executeQuery();
		List<ComMscRollRUpsetEntity> list = EntityTool.getEntitysByRs(rs, ComMscRollRUpsetEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscRollRUpsetEntity comMscRollRUpsetEntity : list) {
				QualityDesignTool.nullConverNullString(comMscRollRUpsetEntity);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 保存轧管规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param designMscGzGgEntity
	 *            实体类
	 * @throws SQLException
	 */
	public void saveRollSpec(Connection con, DesignMscGzGgEntity designMscGzGgEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.saveRollSpec");
		sql = EntityTool.fillSqlByEntity(sql, designMscGzGgEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询锻造规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return List<String[]>
	 * @throws SQLException
	 */
	public List<String[]> getBiltSpec(Connection con, String dimater, String height, String outsStlPileFl)
			throws SQLException {
		List<String[]> list = new ArrayList<String[]>();
		String sql = "";
		if (outsStlPileFl.equals("121902") || outsStlPileFl.equals("121903")) {
			sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.getBiltSpecOut");
		} else {
			sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.getBiltSpec");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dimater);
		ps.setObject(2, dimater);
		ps.setObject(3, height);
		ps.setObject(4, height);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String[] strArray = new String[2];
			strArray[0] = rs.getString("SPEC_CODE");
			strArray[1] = rs.getString("SPEC_NAME");
			list.add(strArray);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 保存锻造规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param designMscBiltEntity
	 *            实体类
	 * @throws SQLException
	 */
	public void saveBiltSpec(Connection con, DesignMscBiltEntity designMscBiltEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.saveBiltSpec");
		sql = EntityTool.fillSqlByEntity(sql, designMscBiltEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询铸造与锻造规格对应关系
	 * 
	 * @param con
	 *            连接字符串
	 * @param SpecCode
	 *            断面规格
	 * @return ComMscCastRForgEntity
	 * @throws SQLException
	 */
	public List<ComMscCastRForgEntity> getBiltCSpec(Connection con, String SpecCode, String flag) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.getBiltCSpecA");
		if (!flag.equals("B002")) {
			sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.getBiltCSpecB");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, SpecCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscCastRForgEntity> list = EntityTool.getEntitysByRs(rs, ComMscCastRForgEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscCastRForgEntity comMscCastRForgEntity : list) {
				QualityDesignTool.nullConverNullString(comMscCastRForgEntity);
			}
			return list;
		} else {
			return null;
		}

	}

	/**
	 * 保存坯料规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param designMscBiltCEntity
	 *            实体类
	 * @throws SQLException
	 */
	public void saveBiltCSpec(Connection con, DesignMscBiltCEntity BiltSpecs) throws SQLException {
		if (BiltSpecs == null) {
			return;
		}
		BiltSpecs.setSpecCodeS(BiltSpecs.getSpecCode() + BiltSpecs.getBSpecCode() + BiltSpecs.getCSpecCode());
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.saveBiltCSpec");
		sql = EntityTool.fillSqlByEntity(sql, BiltSpecs);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 根据外径、壁厚查询规格代码
	 * 
	 * @param con
	 *            连接字符串
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return ComBaseSpecEntity
	 * @throws SQLException
	 */
	public List<ComBaseSpecEntity> getBaseSpec(Connection con, String dimater, String height) throws SQLException {

		String sql = CoreXmlSqlParsersFactory.getSql("ISpecManageDao.getBaseSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dimater);
		ps.setObject(2, height);
		ResultSet rs = ps.executeQuery();
		List<ComBaseSpecEntity> list = EntityTool.getEntitysByRs(rs, ComBaseSpecEntity.class);
		if (list != null && list.size() > 0) {
			for (ComBaseSpecEntity comBaseSpecEntity : list) {
				QualityDesignTool.nullConverNullString(comBaseSpecEntity);
			}

			return list;
		} else {
			return null;
		}

	}

}
