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.CraftOrdDesignStdCicEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

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

	/**
	 * 新增成分标准
	 * 
	 * @param con
	 * @param chem
	 * @throws SQLException
	 */
	public void addChem(Connection con, CraftOrdDesignStdCicEntity chem)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscChemDao.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 smeltCnt,
			String producCnt, String rate, String designKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscChemDao.updateDesignStd");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, cic);
		ps.setObject(2, smeltCnt);
		ps.setObject(3, producCnt);
		ps.setObject(4, rate);
		ps.setObject(5, 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("MscChemDao.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);
		}
		rs.close();
		ps.close();
		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("MscChemDao.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);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 查询熔炼成分套数／成品成分套数
	 * 
	 * @param con
	 * @param cic
	 * @return
	 * @throws SQLException
	 */
	public String[] getCntByCic(Connection con, String cic) throws SQLException {
		String[] strArray = new String[3];
		strArray[0] = "";
		strArray[1] = "";
		strArray[2] = "";
		String sql = CoreXmlSqlParsersFactory.getSql("MscChemDao.getCntByCic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, cic);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			if (rs.getString("SMELTING_CNT") != null
					&& !rs.getString("SMELTING_CNT").equals("")) {
				strArray[0] = rs.getString("SMELTING_CNT");
			}
			if (rs.getString("PRODUCT_CNT") != null
					&& !rs.getString("PRODUCT_CNT").equals("")) {
				strArray[1] = rs.getString("PRODUCT_CNT");
			}
			if (rs.getString("RATE") != null
					&& !rs.getString("RATE").equals("")) {
				strArray[2] = rs.getString("RATE");
			}
		}
		rs.close();
		ps.close();
		return strArray;
	}
	
	/**
	 * 通过成分索引号查询成分标准
	 * 
	 * @param con
	 * @param cic
	 * @return
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdCicEntity> getCraftMsc0Chem(Connection con,
			String designKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscChemDao.getCraftMsc0Chem");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdCicEntity> chems = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignStdCicEntity.class);
		rs.close();
		ps.close();
		
		return chems;
	}
}
