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.pss.sale.order.model.DesignMscPlStiEntity;
import com.steering.pss.sale.order.model.MscStationEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.OperationPointDaoImp;

/**
 * 工序点bean类
 * 
 * @author Administrator
 * 
 */
public class IOperationPointDao implements OperationPointDaoImp {

	/**
	 * 查询工序点
	 * 
	 * @param con
	 *            字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            产线号
	 * @param processCode
	 *            工序代码
	 * @return List<MscStationEntity>
	 * @throws SQLException
	 */
	public List<MscStationEntity> getOperationPoint(java.sql.Connection con,
			String msc, String mscPline, String processCode)
			throws SQLException {
		List<MscStationEntity> list = new ArrayList<MscStationEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IOperationPointDao.getOperationPoint");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscPline);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			MscStationEntity mscStation = new MscStationEntity();
			mscStation.setIndexSeq(rs.getString("index_seq"));
			mscStation.setMsc(rs.getString("msc"));
			mscStation.setMscPline(rs.getString("msc_pline"));
			mscStation.setBacklogSeq(rs.getString("backlog_seq"));
			mscStation.setProcessCode(rs.getString("process_code"));
			mscStation.setProcessDesc(rs.getString("process_desc"));
			mscStation.setStationCode(rs.getString("station_code"));
			mscStation.setStationDesc(rs.getString("station_desc"));
			// 工艺参数类型
			mscStation.setStationType(rs.getString("basename"));
			mscStation.setStationId(rs.getString("station_id"));
			mscStation.setCost(rs.getString("cost"));
			mscStation.setChoose(rs.getString("choose"));
			mscStation.setReturnResult(rs.getString("return_result"));
			mscStation.setSendCheckplan(rs.getString("send_checkplan"));
			mscStation.setGroupSeq(rs.getString("group_seq"));
			mscStation.setMemo(rs.getString("memo"));
			mscStation.setProcessSeq(rs.getString("process_seq"));
			mscStation.setProcessCodeC(rs.getString("process_code_c"));
			mscStation.setProcessDescC(rs.getString("process_desc_c"));
			QualityDesignTool.nullConverNullString(mscStation);
			list.add(mscStation);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 保存工序点
	 * 
	 * @param con
	 *            连接字符串
	 * @param operationPoint
	 *            工序点实体类
	 * @throws SQLException
	 */
	public void saveOperationPoint(java.sql.Connection con,
			DesignMscPlStiEntity point) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("IOperationPointDao.saveOperationPoint");
		sql = sql.replaceAll("#DESGIN_KEY#", point.getDesginKey());
		sql = sql.replaceAll("#MSC_PLINE#", point.getMscPline());
		sql = sql.replaceAll("#WHOLE_BACKLOG#", point.getWholeBacklog());
		sql = sql.replaceAll("#WHOLE_BACKLOG_DESC#",
				point.getWholeBacklogDesc());
		sql = sql.replaceAll("#BACKLOG_SEQ#", point.getBacklogSeq());
		sql = sql.replaceAll("#PROCESS_CDOE#", point.getProcessCode());
		sql = sql.replaceAll("#PROCESS_DESC#", point.getProcessDesc());
		sql = sql.replaceAll("#PROCESS_SEQ#", point.getProcessSeq());
		sql = sql.replaceAll("#PROCESS_CODE_C#", point.getProcessCodeC());
		sql = sql.replaceAll("#PROCESS_DESC_C#", point.getProcessDescC());
		sql = sql.replaceAll("#STATION_ID#", point.getStationId());
		sql = sql.replaceAll("#STATION_CODE#", point.getStationCode());
		sql = sql.replaceAll("#STATION_DESC#", point.getStationDesc());
		sql = sql.replaceAll("#STATION_TYPE#", point.getStationType());
		sql = sql.replaceAll("#CRAFTITEM_CODE#", point.getCraftitemCode());
		sql = sql.replaceAll("#CRAFTITEM_DESC#", point.getCraftitemDesc());

		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询质量工序点
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param mscPline
	 *            全程产线号
	 * @param processCode
	 *            工序
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscPlStiEntity> getDesignPoint(Connection con,
			String msc, String mscPline, String processCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("IOperationPointDao.getDesignPoint");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, mscPline);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlStiEntity> points = QualityDesignTool.getEntitysByRs(
				rs, DesignMscPlStiEntity.class);
		rs.close();
		ps.close();
		if (points != null && points.size() > 0) {
			for (DesignMscPlStiEntity point : points) {
				QualityDesignTool.nullConverNullString(point);
			}
			return points;
		} else {
			return null;
		}
	}

}
