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

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.craftDesign.model.ComMscGradeRSpecEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlGzEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

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

	/**
	 * 保存质量设计 适用钢种
	 * 
	 * @param con
	 *            连接字符串
	 * @param mscPlGz
	 *            钢种类
	 * @param designKey
	 *            KEY
	 * @throws SQLException
	 */
	public void saveGrade(Connection con, CraftOrdDesignMscPlGzEntity mscPlGz)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("GradeDao.saveGrade");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, mscPlGz.getDesginKey());
		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.setObject(9, mscPlGz.getPriority());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询钢种与成品规格对应关系
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            全程产线号
	 * @param gradeCode
	 *            钢种
	 * @return List<MscGradeRSpecEntity>
	 * @throws SQLException
	 */
	public List<ComMscGradeRSpecEntity> getGradeAndSpec(Connection con,
			String msc, String mscPline, String gradeCode) throws SQLException {
		List<ComMscGradeRSpecEntity> list = new ArrayList<ComMscGradeRSpecEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("GradeDao.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()) {
			ComMscGradeRSpecEntity gradeRSpec = new ComMscGradeRSpecEntity();
			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"));
			CraftTool.nullConverNullString(gradeRSpec);
			list.add(gradeRSpec);
		}
		rs.close();
		ps.close();
		return list;
	}
}
