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.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscChemDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdCicEntity;

/**
 * 成分标准 2015/09/18
 * 
 * @author Administrator
 * 
 */
public class IMscChemDao implements MscChemDaoImp {
	/**
	 * 通过成分索引号查询成分标准
	 * 
	 * @param con
	 * @param cic
	 * @return
	 * @throws SQLException
	 */
	public List<SlmOrdDesignStdCicEntity> getChemByCic(Connection con,
			String cic) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscChemDao.getChemByCic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, cic);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignStdCicEntity> chems = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignStdCicEntity.class);
		rs.close();
		ps.close();
		if (chems != null && chems.size() > 0) {
			for (SlmOrdDesignStdCicEntity chem : chems) {
				QualityDesignTool.nullConverNullString(chem);
			}
			return chems;
		} else {
			return null;
		}
	}

	/**
	 * 新增成分标准
	 * 
	 * @param con
	 * @param chem
	 * @throws SQLException
	 */
	public void addChem(Connection con, SlmOrdDesignStdCicEntity chem)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscChemDao.addChem");
		sql = EntityTool.fillSqlByEntity(sql, chem);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 修改交付标准索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param cic
	 *            成分索引号
	 * @param cic
	 *            质量主键
	 * @throws SQLException
	 */
	public void updateDesignStd(Connection con, String cic, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscChemDao.updateDesignStd");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, cic);
		ps.setObject(2, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询已审批成分索引号 （扣型，冶金规范码）
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String> getExamineCic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscChemDao.getExamineCic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, modelCode);
		ps.setObject(3, msc);
		ps.setObject(4, modelCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String cic = rs.getString("CIC");
			list.add(cic);
		}
		return list;
	}

	/**
	 * 查询有效成分索引号 （扣型，冶金规范码）
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String> getValidCic(Connection con, String msc, String modelCode)
			throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory.getSql("IMscChemDao.getValidCic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, modelCode);
		ps.setObject(3, msc);
		ps.setObject(4, modelCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String cic = rs.getString("CIC");
			list.add(cic);
		}
		return list;
	}
}
