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.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.model.DesignMscPlGzEntity;
import com.steering.pss.sale.order.model.MscGradeRSpecEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.GradeDaoImp;

/**
 * 钢种BEAN类
 * 
 * @author Administrator
 * 
 */
public class IGradeDao implements GradeDaoImp {

	/**
	 * 查询钢种
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            全程产线号
	 * @return 钢种集合
	 * @throws SQLException
	 */
	public List<DesignMscPlGzEntity> getGrade(Connection con, String msc,
			String mscPline) throws SQLException {
		// List<DesignMscPlGzEntity> list = new
		// ArrayList<DesignMscPlGzEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IGradeDao.getGrade");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlGzEntity> grades = QualityDesignTool.getEntitysByRs(rs,
				DesignMscPlGzEntity.class);
		rs.close();
		ps.close();
		if (grades != null && grades.size() != 0) {
			for (DesignMscPlGzEntity grade : grades) {
				QualityDesignTool.nullConverNullString(grade);
			}
			return grades;
		} else {
			return null;
		}

		/**
		 * while (rs.next()) { DesignMscPlGzEntity mscPlGz = new
		 * DesignMscPlGzEntity();
		 * mscPlGz.setMscPline(rs.getString("MSC_PLINE"));
		 * mscPlGz.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
		 * mscPlGz.setWholeBacklogDesc(rs.getString("WHOLE_BACKLOG_DESC"));
		 * mscPlGz.setGradecode(rs.getString("GRADECODE"));
		 * mscPlGz.setGradename(rs.getString("GRADENAME"));
		 * mscPlGz.setAssortmentCode(rs.getString("ASSORTMENT_CODE"));
		 * mscPlGz.setAssortmentName(rs.getString("ASSORTMENT_NAME"));
		 * QualityDesignTool.nullConverNullString(mscPlGz); list.add(mscPlGz); }
		 * rs.close(); ps.close(); return list;
		 **/
	}
	
	/**
	 * 查询钢种
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            全程产线号
	 * @return 钢种集合
	 * @throws SQLException
	 */
	public List<DesignMscPlGzEntity> getBlSlmGrade(Connection con,
			String designKey, String msc, String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IGradeDao.getBlSlmGrade");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, msc);
		ps.setObject(3, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlGzEntity> grades = EntityTool.getEntitysByRs(
				rs, DesignMscPlGzEntity.class);
		rs.close();
		ps.close();
		if (grades != null && grades.size() != 0) {
			for (DesignMscPlGzEntity grade : grades) {
				CraftTool.nullConverNullString(grade);
			}
			return grades;
		} else {
			return null;
		}
	}

	/**
	 * 保存质量设计 适用钢种
	 * 
	 * @param con
	 *            连接字符串
	 * @param mscPlGz
	 *            钢种类
	 * @param designKey
	 *            KEY
	 * @throws SQLException
	 */
	public void saveGrade(Connection con, DesignMscPlGzEntity mscPlGz,
			String designKey) throws SQLException {
		if (mscPlGz == null) {
			return;
		}
		String sql = CoreXmlSqlParsersFactory.getSql("IGradeDao.saveGrade");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPlGz.getMscPline());
		ps.setObject(3, mscPlGz.getWholeBacklog());
		ps.setObject(4, mscPlGz.getWholeBacklogDesc());
		ps.setObject(5, mscPlGz.getGradecode());
		ps.setObject(6, mscPlGz.getGradename());
		ps.setObject(7, mscPlGz.getAssortmentCode());
		ps.setObject(8, mscPlGz.getAssortmentName());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询钢种与成品规格对应关系
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            全程产线号
	 * @param gradeCode
	 *            钢种
	 * @return List<MscGradeRSpecEntity>
	 * @throws SQLException
	 */
	public List<MscGradeRSpecEntity> getGradeAndSpec(Connection con,
			String msc, String mscPline, String gradeCode) throws SQLException {
		List<MscGradeRSpecEntity> list = new ArrayList<MscGradeRSpecEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IGradeDao.getGradeAndSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscPline);
		ps.setObject(3, gradeCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			MscGradeRSpecEntity gradeRSpec = new MscGradeRSpecEntity();
			gradeRSpec.setMsc(rs.getString("MSC"));
			gradeRSpec.setMscPline(rs.getString("MSC_PLINE"));
			gradeRSpec.setGradecode(rs.getString("GRADECODE"));
			gradeRSpec.setGradename(rs.getString("GRADENAME"));
			gradeRSpec.setHeightMin(rs.getString("HEIGHT_MIN"));
			gradeRSpec.setDimaterMin(rs.getString("DIMATER_MIN"));
			gradeRSpec.setDimaterMax(rs.getString("DIMATER_MAX"));
			gradeRSpec.setHeightMiax(rs.getString("HEIGHT_MIAX"));
			QualityDesignTool.nullConverNullString(gradeRSpec);
			list.add(gradeRSpec);
		}
		rs.close();
		ps.close();
		return list;
	}
}
