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

public class MscDeteDao {
	/**
	 * 查询审批探伤索引号
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return
	 * @throws SQLException
	 */
	public List<String> getExamineDic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscDeteDao.getExamineDic");
		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 dic = rs.getString("DIC");
			list.add(dic);
		}
		return list;
	}

	/**
	 * 查询有效探伤索引号
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return
	 * @throws SQLException
	 */
	public List<String> getValidDic(Connection con, String msc, String modelCode)
			throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory.getSql("MscDeteDao.getValidDic");
		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 dic = rs.getString("DIC");
			list.add(dic);
		}
		return list;
	}

	/**
	 * 通过探伤索引号查询探伤标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param dic
	 *            探伤索引号
	 * @return List<DesignStdDicEntity>
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdDicEntity> getDeteByDic(Connection con,
			String dic) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscDeteDao.getDeteByDic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dic);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdDicEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignStdDicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignStdDicEntity dete : list) {
				CraftTool.nullConverNullString(dete);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 保存探伤标准
	 * 
	 * @param con
	 *            连接字符串
	 * @param dete
	 *            探伤标准
	 * @throws SQLException
	 */
	public void saveDete(Connection con, CraftOrdDesignStdDicEntity dete)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscDeteDao.saveDic");
		sql = EntityTool.fillSqlByEntity(sql, dete);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

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

	/**
	 * 查询探伤检验项的类型(验证无损检验必须有对应的工序点)
	 * 
	 * @param con
	 * @param phyCode
	 * @return
	 * @throws SQLException
	 */
	public String getPhyStyle(Connection con, String phyCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscDeteDao.getPhyStyle");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, phyCode);
		ResultSet rs = ps.executeQuery();
		String style = "";
		if (rs.next()) {
			style = rs.getString("PHY_CODE");
		}
		rs.close();
		ps.close();
		return style;
	}

	/**
	 * 查询工序点
	 */
	public List<ComBasePhyRStationEntity> getStationByPhyCode(Connection con,
			String phyCode) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscDeteDao.getStationByPhyCode");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, phyCode);
		ResultSet rs = ps.executeQuery();
		List<ComBasePhyRStationEntity> list = EntityTool.getEntitysByRs(rs,
				ComBasePhyRStationEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComBasePhyRStationEntity dete : list) {
				CraftTool.nullConverNullString(dete);
			}
			return list;
		} else {
			return null;
		}
	}
}
