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

public class MscGProcessDao {
	/**
	 * 查询加工标准
	 * 
	 * @param con
	 * @param gic
	 * @return
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdGicEntity> getProcessByGic(Connection con,
			String gic) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscGProcessDao.getProcessByGic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, gic);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdGicEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignStdGicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignStdGicEntity slmOrdDesignStdGicEntity : list) {
				CraftTool.nullConverNullString(slmOrdDesignStdGicEntity);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 保存质量加工标准
	 * 
	 * @param con
	 * @param process
	 * @throws SQLException
	 */
	public void saveProcess(Connection con, CraftOrdDesignStdGicEntity process)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscGProcessDao.saveGic");
		sql = EntityTool.fillSqlByEntity(sql, process);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	/**
	 * 查询审批加工索引号
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return
	 * @throws SQLException
	 */
	public List<String> getExamineGic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscGProcessDao.getExamineGic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, modelCode);
		ps.setObject(3, msc);
		ps.setObject(4, modelCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String gic = rs.getString("GIC");
			list.add(gic);
		}
		return list;
	}

	/**
	 * 查询有效加工索引号
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return
	 * @throws SQLException
	 */
	public List<String> getValidGic(Connection con, String msc, String modelCode)
			throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscGProcessDao.getValidGic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, modelCode);
		ps.setObject(3, msc);
		ps.setObject(4, modelCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String gic = rs.getString("GIC");
			list.add(gic);
		}
		return list;
	}

	/**
	 * 修改交付标准索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param Gic
	 *            成分索引号
	 * @param cic
	 *            质量主键
	 * @throws SQLException
	 */
	public void updateDesignStd(Connection con, String gic, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscGProcessDao.updateDesignStd");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, gic);
		ps.setObject(2, designKey);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 查询扣型分类
	 * 
	 * @param con
	 * @param modelCode
	 * @return
	 * @throws SQLException
	 */
	public String[] getModelStyle(Connection con, String modelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscGProcessDao.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 getStationDesc(Connection con, String stationCode){
		SqlBuilder b = new SqlBuilder("MscGProcessDao.getStationDesc");
		b.Fill(new String[]{ stationCode });
		GenericEntity value = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		return value.getValue();
	}
}
