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

/**
 * 工艺评审-工序类
 * 
 * @author Administrator
 * 
 */
public class OperationDao {
	/**
	 * 查询工序
	 * 
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            制程产线号
	 * @param processCode
	 *            工序代码
	 * @return
	 * @throws SQLException
	 */
	public CraftOrdDesignMscPlPrcEntity getOperation(Connection con,
			String msc, String mscPline, String processCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("OperationDao.getOperation");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscPline);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscPlPrcEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscPlPrcEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() != 1) {
			return null;
		} else {
			CraftTool.nullConverNullString(list.get(0));
			return list.get(0);
		}
	}
	
	/**
	 * 查询工序
	 * 
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            制程产线号
	 * @param processCode
	 *            工序代码
	 * @return
	 * @throws SQLException
	 */
	public List<CraftOrdDesignMscPlPrcEntity> getAllOperation(Connection con,
			String msc, String mscPline)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("OperationDao.getAllOperation");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscPlPrcEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscPlPrcEntity.class);
		rs.close();
		ps.close();
		
		return list;
	}
	
	/**
	 * 是否是有效的二级工序
	 * @param con
	 * @param processCodec
	 * @return
	 * @throws SQLException
	 */
	public boolean BaseProcessCIsValid(Connection con, String processCodec) throws SQLException{
		String sql = CoreXmlSqlParsersFactory
				.getSql("OperationDao.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 mscPlPrcEntity
	 * @throws SQLException
	 */
	public void addOperation(Connection con,
			CraftOrdDesignMscPlPrcEntity mscPlPrcEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("OperationDao.addOperation");
		sql = EntityTool.fillSqlByEntity(sql, mscPlPrcEntity);
		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("OperationDao.isExistEMeterialCode");
		b.Fill(new String[]{modelCode});
		
		GenericEntity gEntity = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, GenericEntity.class);
		return !gEntity.getValue().equals("");
	}
}
