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.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.model.MscProcessEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.OperationDaoImp;

/**
 * 工序bean实现类
 * 
 * @author Administrator
 * 
 */
public class IOperationDao implements OperationDaoImp {

	/**
	 * 查询工序
	 * 
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            制程产线号
	 * @param processCode
	 *            工序代码
	 * @return
	 * @throws SQLException
	 */
	public MscProcessEntity getOperation(Connection con, String msc,
			String mscPline, String processCode) throws SQLException {
		// MscProcessEntity mscProcess = new MscProcessEntity();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IOperationDao.getOperation");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscPline);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		/**
		 * if (rs.next()) { mscProcess.setIndexSeq(rs.getString("INDEX_SEQ"));
		 * mscProcess.setMsc(rs.getString("MSC"));
		 * mscProcess.setMscPline(rs.getString("MSC_PLINE"));
		 * mscProcess.setBacklogSeq(rs.getString("BACKLOG_SEQ"));
		 * mscProcess.setProcessCode(rs.getString("PROCESS_CODE"));
		 * mscProcess.setProcessDesc(rs.getString("PROCESS_DESC"));
		 * mscProcess.setMinuteProduct(rs.getString("MINUTE_PRODUCT"));
		 * mscProcess.setMinuteWaite(rs.getString("MINUTE_WAITE"));
		 * mscProcess.setProcessSeq(rs.getString("PROCESS_SEQ"));
		 * mscProcess.setCic(rs.getString("CIC"));
		 * mscProcess.setPic(rs.getString("PIC"));
		 * mscProcess.setSic(rs.getString("SIC"));
		 * mscProcess.setDic(rs.getString("DIC"));
		 * mscProcess.setWic(rs.getString("WIC"));
		 * mscProcess.setGic(rs.getString("GIC"));
		 * mscProcess.setProcessCodeC(rs.getString("PROCESS_CODE_C"));
		 * mscProcess.setProcessDescC(rs.getString("PROCESS_DESC_C"));
		 * QualityDesignTool.nullConverNullString(mscProcess); }
		 **/
		List<MscProcessEntity> list = QualityDesignTool.getEntitysByRs(rs,
				MscProcessEntity.class);
		rs.close();
		ps.close();
		if (list.size() == 0) {
			return null;
		} else {
			QualityDesignTool.nullConverNullString(list.get(0));
			return list.get(0);
		}

	}
	
	/**
	 * 是否是有效的二级工序
	 * @param con
	 * @param processCodec
	 * @return
	 * @throws SQLException
	 */
	public boolean BaseProcessCIsValid(Connection con, String processCodec) throws SQLException{
		String sql = CoreXmlSqlParsersFactory
				.getSql("IOperationDao.BaseProcessCIsValid");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, processCodec);
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			rs.close();
			ps.close();
			return true;
		}else{
			rs.close();
			ps.close();
			return false;
		}
	}

	/**
	 * 保存工序
	 * 
	 * @param con
	 *            连接字符串
	 * @param mscProcess
	 *            工序
	 * @param designKey
	 *            KEY
	 * @param wholeBacklog
	 *            全程途径码
	 * @param wholeBacklogDesc
	 *            全程途径描述
	 * @throws SQLException
	 */
	public void saveOperation(java.sql.Connection con,
			MscProcessEntity mscProcess, String designKey, String wholeBacklog,
			String wholeBacklogDesc) throws SQLException {
		if (mscProcess == null) {
			return;
		}
		String sql = CoreXmlSqlParsersFactory
				.getSql("IOperationDao.saveOperation");
		sql = sql.replaceAll("#DESGIN_KEY#", designKey);
		sql = sql.replaceAll("#MSC_PLINE#", mscProcess.getMscPline());
		sql = sql.replaceAll("#WHOLE_BACKLOG#", wholeBacklog);
		sql = sql.replaceAll("#WHOLE_BACKLOG_DESC#", wholeBacklogDesc);
		sql = sql.replaceAll("#BACKLOG_SEQ#", mscProcess.getBacklogSeq());
		sql = sql.replaceAll("#PROCESS_CODE#", mscProcess.getProcessCode());
		sql = sql.replaceAll("#PROCESS_DESC#", mscProcess.getProcessDesc());
		sql = sql.replaceAll("#PROCESS_SEQ#", mscProcess.getProcessSeq());
		sql = sql.replaceAll("#PROCESS_CODE_C#", mscProcess.getProcessCodeC());
		sql = sql.replaceAll("#PROCESS_DESC_C#", mscProcess.getProcessDescC());
		sql = sql.replaceAll("#MINUTE_PRODUCT#", mscProcess.getMinuteProduct());
		sql = sql.replaceAll("#MINUTE_WAITE#", mscProcess.getMinuteWaite());
		sql = sql.replaceAll("#CIC#", mscProcess.getCic());
		sql = sql.replaceAll("#PIC#", mscProcess.getPic());
		sql = sql.replaceAll("#SIC#", mscProcess.getSic());
		sql = sql.replaceAll("#DIC#", mscProcess.getDic());
		sql = sql.replaceAll("#WIC#", mscProcess.getWic());
		sql = sql.replaceAll("#MEMO#", mscProcess.getMemo());
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 是否存在镦拔扩字母
	 * @param modelCode
	 * @param con
	 * @return
	 */
	public boolean isExistEMeterialCode(String modelCode, Connection con){
		SqlBuilder b = new SqlBuilder("IOperationDao.isExistEMeterialCode");
		b.Fill(new String[]{modelCode});
		
		GenericEntity gEntity = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, GenericEntity.class);
		return !gEntity.getValue().equals("");
	}
}
