package com.steering.pss.sale.order.craftDesign.dao;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.Util.StringUtils;
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.ComBaseCustomRPlineEntity;
import com.steering.pss.sale.order.craftDesign.model.ComBasePlineEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscPlineRSpecEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStationRPlineEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LineManageDao {
	/**
	 * 适用产线
	 * 
	 * @param con
	 * @param processCode
	 *            工序代码
	 * @return List<ComMscPlineRSpecEntity>
	 * @throws SQLException
	 */
	public List<ComMscPlineRSpecEntity> getProLine(Connection con,
			String processCode,String supp) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("LineManageDao.getProLineByProcessCode");
		/*if (!StringUtils.isEmpty(supp)) {
			sql += " AND SUPPLY_UNIT_CODE = '" + supp +"'";
		}*/
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, processCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscPlineRSpecEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscPlineRSpecEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscPlineRSpecEntity comMscPlineRSpec : list) {
				CraftTool.nullConverNullString(comMscPlineRSpec);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 基础产线
	 * 
	 * @param con
	 * @param processCode
	 *            工序代码
	 * @return List<ComBasePlineEntity>
	 * @throws SQLException
	 */
	public List<ComBasePlineEntity> getBaseLine(Connection con,
			String processCode) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("LineManageDao.getBaseLine");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, processCode);
		ResultSet rs = ps.executeQuery();
		List<ComBasePlineEntity> list = EntityTool.getEntitysByRs(rs,
				ComBasePlineEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComBasePlineEntity comBasePlineEntity : list) {
				CraftTool.nullConverNullString(comBasePlineEntity);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询产线与工序点对应关系
	 * 
	 * @param con
	 *            连接字符串
	 * @param proLine
	 *            产线代码
	 * @return List<MscStationRPlineEntity>
	 * @throws SQLException
	 */
	public List<ComMscStationRPlineEntity> getStationRPline(Connection con,
			String proLine) throws SQLException {
		List<ComMscStationRPlineEntity> list = new ArrayList<ComMscStationRPlineEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IRollUpsetDao.getStationRPline");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, proLine);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			ComMscStationRPlineEntity pline = new ComMscStationRPlineEntity();
			pline.setPlineCode(rs.getString("PLINE_CODE"));
			pline.setPlineName(rs.getString("PLINE_NAME"));
			pline.setStationCode(rs.getString("STATION_CODE"));
			pline.setStationDesc(rs.getString("STATION_DESC"));
			pline.setStationId(rs.getString("STATION_ID"));
			CraftTool.nullConverNullString(pline);
			list.add(pline);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 查询加工、接箍加工适用产线
	 * 
	 * @param con
	 *            连接字符串
	 * @param modelCode
	 *            扣型
	 * @param specCode
	 *            产品规格
	 * @param processCode
	 *            工序
	 * @return
	 * @throws SQLException
	 */
	public List<ComMscPlineRSpecEntity> getLineBySpec(Connection con,
			String modelCode, String specCode, String processCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("LineManageDao.getLineBySpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, processCode);
		ps.setObject(2, modelCode);
		ps.setObject(3, specCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscPlineRSpecEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscPlineRSpecEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscPlineRSpecEntity comMscPlineRSpec : list) {
				CraftTool.nullConverNullString(comMscPlineRSpec);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 通过最终客户、品名找认证产线
	 * 
	 * @param con
	 *            连接字符串
	 * @param customerNo
	 *            客户编码
	 * @param producCode
	 *            品名编码
	 * @return List<ComBaseCustomRPlineEntity>
	 * @throws SQLException
	 */
	public List<ComBaseCustomRPlineEntity> getLineByCustAndProduc(
			Connection con, String customerNo, String producCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("LineManageDao.getLineByCustAndProduc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, customerNo);
		ps.setObject(2, producCode);
		ResultSet rs = ps.executeQuery();
		List<ComBaseCustomRPlineEntity> customerRPlines = EntityTool
				.getEntitysByRs(rs, ComBaseCustomRPlineEntity.class);
		rs.close();
		ps.close();
		if (customerRPlines != null && customerRPlines.size() > 0) {
			for (ComBaseCustomRPlineEntity customerRPline : customerRPlines) {
				CraftTool.nullConverNullString(customerRPline);
			}
			return customerRPlines;
		} else {
			return null;
		}
	}
	
	/**
	 * 获取炼钢工序是否存在自炼索引码
	 * @param con
	 * @param designKey
	 * @param mscPline
	 * @return
	 */
	public String getOwnIndexA(Connection con, String cic){
		SqlBuilder b = new SqlBuilder("LineManageDao.getOwnIndexA");
		b.Fill(new String[]{ cic });
		GenericEntity value = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		return value.getValue();
	}
	
	/**
	 * 获取炼钢工序是否存在外购索引码
	 * @param con
	 * @param cic
	 * @return
	 */
	public String getPurchaseIndexA(Connection con, String cic){
		SqlBuilder b = new SqlBuilder("LineManageDao.getPurchaseIndexA");
		b.Fill(new String[]{ cic });
		GenericEntity value = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		return value.getValue();
	}
}
