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.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.model.ComBaseProcessCEntity;
import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.ProDucDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignStdDicEntity;

/**
 * 制程bean类
 * 
 * @author Administrator
 */
public class IProDucDao implements ProDucDaoImp {

	/**
	 * 查询制程
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范
	 * @return 集合
	 * @throws SQLException
	 */
	public List<DesignMscPlEntity> getProcess(Connection con, String msc)
			throws SQLException {
		List<DesignMscPlEntity> list = new ArrayList<DesignMscPlEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IProDucDao.getProDuc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DesignMscPlEntity mscPl = new DesignMscPlEntity();
			mscPl.setMscPline(rs.getString("MSC_PLINE"));
			mscPl.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
			mscPl.setWholeBacklogDesc(rs.getString("WHOLE_BACKLOG_DESC"));
			mscPl.setAutoCraft(rs.getString("AUTO_CRAFT"));
			QualityDesignTool.nullConverNullString(mscPl);
			list.add(mscPl);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 保存制程 至 质量设计-制程表
	 * 
	 * @param con
	 *            连接字符串
	 * @param comBack
	 *            制程实体类
	 * @return 操作行数
	 * @throws SQLException
	 */
	public void saveProcess(Connection con, DesignMscPlEntity comMscPl,
			String designKey) throws SQLException {
		if (comMscPl == null) {
			return;
		}
		String sql = CoreXmlSqlParsersFactory.getSql("IProDucDao.saveProDuc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, comMscPl.getMscPline());
		ps.setObject(3, comMscPl.getWholeBacklog());
		ps.setObject(4, comMscPl.getWholeBacklogDesc());
		ps.setObject(5, "0");
		ps.setObject(6, comMscPl.getWarnDesc());
		ps.setObject(7, comMscPl.getOrdLnPk());
		ps.setObject(8, comMscPl.getAutoCraft());
		ps.executeUpdate();
		ps.close();

	}

	/**
	 * 查询质量设计 成分标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getDesignChem(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.getDesignChem");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 理化标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getDesignPhy(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("IProDucDao.getDesignPhy");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 公差标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getDesignSpec(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.getDesignSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 探伤标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getDesignDete(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.getDesignDete");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 水压标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getDesignWater(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.getDesignWater");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 加工标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getDesignGic(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("IProDucDao.getDesignGic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查詢附加要求工序点类型
	 * 
	 * @param con
	 *            连接字符串
	 * @param addAskNo
	 *            附加要求
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String[]> getAddAskNoStation(Connection con, String addAskNo)
			throws SQLException {
		List<String[]> list = new ArrayList<String[]>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.getAddAskNoStation");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, addAskNo);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String[] strarray = new String[3];
			strarray[0] = rs.getString("STATION_CODE");
			strarray[1] = rs.getString("STATION_DESC");
			strarray[2] = rs.getString("ASK_SUB_ITME_DESC");
			list.add(strarray);
		}
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			return list;
		}

	}

	/**
	 * 查询产品规范类型
	 * 
	 * @param con
	 * @param specCode
	 * @return
	 * @throws SQLException
	 */
	public String getSpecStyle(Connection con, String specCode)
			throws SQLException {
		String specStyle = "";
		String sql = CoreXmlSqlParsersFactory.getSql("IProDucDao.getSpecStyle");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, specCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			specStyle = rs.getString("MSC_STYLE");
		}
		rs.close();
		ps.close();
		return specStyle;
	}

	/**
	 * 查询设定制程
	 * 
	 * @param con
	 * @param ordLnPk
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscPlEntity> getMscPlSetFl(Connection con,
			String ordLnPk, String msc) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.getMscPlSetFl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, ordLnPk);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlEntity> list = EntityTool.getEntitysByRs(rs,
				DesignMscPlEntity.class);
		if (list != null && list.size() > 0) {
			for (DesignMscPlEntity designMscPlEntity : list) {
				QualityDesignTool.nullConverNullString(designMscPlEntity);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询探伤检验项
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<SlmOrdDesignStdDicEntity> getDeteInfo(Connection con,
			String designKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IProDucDao.getDeteInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignStdDicEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignStdDicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (SlmOrdDesignStdDicEntity dete : list) {
				QualityDesignTool.nullConverNullString(dete);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 修改制程
	 * 
	 * @param con
	 * @param plEntity
	 * @throws SQLException
	 */
	public void updateSetPl(Connection con, DesignMscPlEntity plEntity,
			String designKeyOld) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.updateMscPlSetFl");
		sql = EntityTool.fillSqlByEntity(sql, plEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKeyOld);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询扣型分类
	 * 
	 * @param con
	 * @param modelCode
	 * @return
	 * @throws SQLException
	 */
	public String[] getModelStyle(Connection con, String modelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("IProDucDao.getModelStyle");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, modelCode);
		String[] modelStyle = new String[2];
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("MODEL_STYLE") != null) {
				modelStyle[0] = rs.getString("MODEL_STYLE");
				modelStyle[1] = rs.getString("MODEL_STYLE_DESC");
			}
		}
		rs.close();
		ps.close();
		return modelStyle;
	}
	
	/**
	 * 获取二级工序描述
	 * @param con
	 * @param processCodeC
	 * @return
	 */
	public String getProcessDescC(Connection con, String processCodeC){
		SqlBuilder b = new SqlBuilder("ProDucDao.getProcessDescC");
		b.Fill(new String[]{ processCodeC });
		ComBaseProcessCEntity processCDesc = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, ComBaseProcessCEntity.class);
		return processCDesc.getProcessDescC();
	}
}
