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.List;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.pss.sale.order.model.ComMscEntity;
import com.steering.pss.sale.order.model.DesignStatusEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscDaoImp;

public class IMscDao implements MscDaoImp {

	/**
	 * 根据产品码找有效的基础冶金规范码
	 * 
	 * @param con
	 *            连接字符串
	 * @param psc
	 *            冶金规范码
	 * @return ComMscEntity
	 * @throws SQLException
	 */
	public ComMscEntity getMsc(Connection con, String msc) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscDao.getMsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ResultSet rs = ps.executeQuery();
		List<ComMscEntity> mscs = QualityDesignTool.getEntitysByRs(rs,
				ComMscEntity.class);
		rs.close();
		ps.close();
		if (mscs != null && mscs.size() > 0) {
			QualityDesignTool.nullConverNullString(mscs.get(0));
			return mscs.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 根据产品码找有效的基础冶金规范码
	 * 
	 * @param con
	 *            连接字符串
	 * @param psc
	 *            产品码
	 * @return ComMscEntity
	 * @throws SQLException
	 */
	public String getMscByPsc(Connection con, String psc) throws SQLException {
		String msc = "";
		String sql = CoreXmlSqlParsersFactory.getSql("IMscDao.getMscByPsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, psc);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			msc = rs.getString("MSC");
		}
		rs.close();
		ps.close();
		return msc;
	}

	/**
	 * 修改冶金規范明细状态
	 * 
	 * @param con
	 *            连接字符串
	 * @param status
	 *            明细状态
	 * @throws SQLException
	 */
	public void updateMscStatus(Connection con, DesignStatusEntity status)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscDao.updateMscStatus");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, status.getDesignSts());
		ps.setObject(2, status.getDesignStsRs());
		ps.setObject(3, status.getDesignStsDesc());
		ps.setObject(4, status.getMsc());
		ps.setObject(5, status.getDesignObjKey());
		ps.setObject(6, status.getUnitCode());
		ps.setObject(7, status.getUnitDesc());
		ps.setObject(8, status.getDesginKey());
		ps.setObject(9, status.getOrdLnPk());
		ps.setObject(10, status.getLstItem());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询产品码管理科室
	 * 
	 * @param con
	 *            连接字符串
	 * @param psc
	 *            产品码
	 * @return String[]
	 * @throws SQLException
	 */
	public String[] getPscUnit(Connection con, String psc) throws SQLException {
		String[] unit = null;
		String sql = CoreXmlSqlParsersFactory.getSql("IMscDao.getPscUnit");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, psc);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			unit = new String[2];
			unit[0] = rs.getString("UNIT_CODE");
			unit[1] = rs.getString("UNIT_DESC");
		}
		rs.close();
		ps.close();
		return unit;
	}

	/**
	 * 修改质量设计主表MSC
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @param msc
	 *            冶金规范码
	 * @param mscDesc
	 *            描述
	 * @param mscStyle
	 *            类型
	 * @throws SQLException
	 */
	public void updateDesignMsc(Connection con, String designKey, String msc,
			String mscDesc, String mscStyle) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscDao.updateDesignMsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscDesc);
		ps.setObject(3, mscStyle);
		ps.setObject(4, designKey);
		ps.executeUpdate();
		ps.close();
	}

}
