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.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.model.DesignStdSicEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscSpecDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.ComBaseProductEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdSicEntity;

public class IMscSpecDao implements MscSpecDaoImp {
	/**
	 * 查询已审批公差索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String> getExamineSic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecDao.getExamineSic");
		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 sic = rs.getString("SIC");
			list.add(sic);
		}
		return list;
	}

	/**
	 * 查询有效公差索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String> getvalidSic(Connection con, String msc, String modelCode)
			throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecDao.getvalidSic");
		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 sic = rs.getString("SIC");
			list.add(sic);
		}
		return list;
	}

	/**
	 * 根据公差索引号查询公差标准(提供给计划用)
	 * 
	 * @param con
	 *            连接字符串
	 * @param sic
	 *            公差索引号
	 * @return List<DesignStdSicEntity>
	 * @throws SQLException
	 */
	public List<DesignStdSicEntity> getSpecBySics(Connection con, String sic)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecDao.getSpecBySic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, sic);
		ResultSet rs = ps.executeQuery();
		List<DesignStdSicEntity> list = EntityTool.getEntitysByRs(rs,
				DesignStdSicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (DesignStdSicEntity spec : list) {
				QualityDesignTool.nullConverNullString(spec);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 根据公差索引号查询公差标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param sic
	 *            公差索引号
	 * @return List<DesignStdSicEntity>
	 * @throws SQLException
	 */
	public List<SlmOrdDesignStdSicEntity> getSpecBySic(Connection con,
			String sic) throws SQLException {

		String sql = CoreXmlSqlParsersFactory.getSql("ISpecDao.getSpecBySic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, sic);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignStdSicEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignStdSicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (SlmOrdDesignStdSicEntity spec : list) {
				QualityDesignTool.nullConverNullString(spec);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 保存公差标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param spec
	 *            公差标准类
	 * @throws SQLException
	 */
	public void saveSpec(Connection con, SlmOrdDesignStdSicEntity spec)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecDao.saveSic");
		sql = EntityTool.fillSqlByEntity(sql, spec);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

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

	public String getSicFlag(Connection con, String ordLnPk)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ISpecDao.getSicFlag");
		String flag = "0";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnPk);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			flag = rs.getString("SIC_FLAG");
		}
		rs.close();
		ps.close();
		return flag;
	}
	
	/**
	 * 获取品名基础数据
	 */
	public ComBaseProductEntity getProduct(Connection con, String productCode)
			throws SQLException {
		SqlBuilder b = new SqlBuilder("ISpecDao.getProduct");
		b.Fill(new String[]{ productCode });
		ComBaseProductEntity product = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, ComBaseProductEntity.class);
		return product;
	}
}
