package com.steering.pss.sale.order.bomGeneration.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.pss.sale.order.bomGeneration.daoImp.BomQueryDaoImp;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomIEntity;
import com.steering.pss.sale.order.bomGeneration.model.SlmOrdDesignBlEntity;
import com.steering.pss.sale.order.bomGeneration.model.SlmOrdDesignMscJgLineEntity;
import com.steering.pss.sale.order.bomGeneration.model.SlmOrdDesignMscPlPrcEntity;
import com.steering.pss.sale.order.model.BaseProductEntity;
import com.steering.pss.sale.order.model.ComPscEntity;
import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGxPrlEntity;
import com.steering.pss.sale.order.model.DesignMscPlGzEntity;
import com.steering.pss.sale.order.model.DesignMscBiltCEntity;
import com.steering.pss.sale.order.model.DesignMscBiltEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

/**
 * BOM业务清单 操作数据库类
 * 
 * @author Administrator
 * 
 */
public class IBomQueryDao implements BomQueryDaoImp {
	/**
	 * 查询合同行号
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public String[] getOrdLnPk(Connection con, String designKey)
			throws SQLException {
		String[] ordLnPk = new String[3];
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getOrdLnPk");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			ordLnPk[0] = rs.getString("ORD_LN_PK");
			ordLnPk[1] = rs.getString("ORD_PK");
			ordLnPk[2] = rs.getString("AVG_LEN_MIN");
		}
		rs.close();
		ps.close();
		return ordLnPk;
	}

	/**
	 * 查询质量设计 钢种
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @param mscPline
	 *            全程产线号
	 * @return List<DesignMscPlGzEntity>
	 * @throws SQLException
	 */
	public List<DesignMscPlGzEntity> getGrade(Connection con, String designKey,
			String mscPline) throws SQLException {
		// List<DesignMscPlGzEntity> grades = new
		// ArrayList<DesignMscPlGzEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getGrade");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		/**
		 * while (rs.next()) { DesignMscPlGzEntity grade = new
		 * DesignMscPlGzEntity();
		 * grade.setDesginKey(rs.getString("DESGIN_KEY"));
		 * grade.setMscPline(rs.getString("MSC_PLINE"));
		 * grade.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
		 * grade.setWholeBacklogDesc(rs.getString("WHOLE_BACKLOG_DESC"));
		 * grade.setGradecode(rs.getString("GRADECODE"));
		 * grade.setGradename(rs.getString("GRADENAME"));
		 * grade.setAssortmentCode(rs.getString("ASSORTMENT_CODE"));
		 * grade.setAssortmentName(rs.getString("ASSORTMENT_NAME"));
		 * QualityDesignTool.nullConverNullString(grade); grades.add(grade); }
		 **/
		List<DesignMscPlGzEntity> grades = QualityDesignTool.getEntitysByRs(rs,
				DesignMscPlGzEntity.class);
		rs.close();
		ps.close();
		return grades;
	}

	/**
	 * 查询轧管规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @param mscPline
	 *            全程产线号
	 * @return List<OrdDesignMscGzGgEntity>
	 * @throws SQLException
	 */
	public List<DesignMscGzGgEntity> getSpec(Connection con, String designKey,
			String mscPline) throws SQLException {
		// List<OrdDesignMscGzGgEntity> specs = new
		// ArrayList<OrdDesignMscGzGgEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscGzGgEntity> specs = QualityDesignTool.getEntitysByRs(rs,
				DesignMscGzGgEntity.class);
		/**
		 * while (rs.next()) { OrdDesignMscGzGgEntity spec = new
		 * OrdDesignMscGzGgEntity();
		 * spec.setDesginKey(rs.getString("DESGIN_KEY"));
		 * spec.setMscPline(rs.getString("MSC_PLINE"));
		 * spec.setGradecode(rs.getString("GRADECODE"));
		 * spec.setGradename(rs.getString("GRADENAME"));
		 * spec.setSpecCode(rs.getString("SPEC_CODE"));
		 * spec.setSpecName(rs.getString("SPEC_NAME"));
		 * spec.setDimater(rs.getString("DIMATER"));
		 * spec.setHeight(rs.getString("HEIGHT"));
		 * QualityDesignTool.nullConverNullString(spec); specs.add(spec); }
		 **/
		rs.close();
		ps.close();
		return specs;
	}

	/**
	 * 查询断面
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @param mscPline
	 *            全程产线号
	 * @return List<OrdDesignMscBiltEntity>
	 * @throws SQLException
	 */
	public List<DesignMscBiltEntity> getBlank(Connection con, String designKey,
			String mscPline) throws SQLException {
		List<DesignMscBiltEntity> blanks = new ArrayList<DesignMscBiltEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getBlank");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DesignMscBiltEntity blank = new DesignMscBiltEntity();
			blank.setDesginKey(rs.getString("DESGIN_KEY"));
			blank.setMscPline(rs.getString("MSC_PLINE"));
			blank.setSpecCode(rs.getString("SPEC_CODE"));
			blank.setbSpecCode(rs.getString("B_SPEC_CODE"));
			blank.setbSpecName(rs.getString("B_SPEC_NAME"));
			QualityDesignTool.nullConverNullString(blank);
			blanks.add(blank);
		}
		rs.close();
		ps.close();
		return blanks;
	}

	/**
	 * 查询坯料规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @param mscPline
	 *            全程产线号
	 * @return List<OrdDesignMscBiltCEntity>
	 * @throws SQLException
	 */
	public List<DesignMscBiltCEntity> getbilt(Connection con, String designKey,
			String mscPline) throws SQLException {
		List<DesignMscBiltCEntity> bilts = new ArrayList<DesignMscBiltCEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getBilt");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DesignMscBiltCEntity bilt = new DesignMscBiltCEntity();
			bilt.setDesginKey(rs.getString("DESGIN_KEY"));
			bilt.setMscPline(rs.getString("MSC_PLINE"));
			bilt.setSpecCode(rs.getString("SPEC_CODE"));
			bilt.setBSpecCode(rs.getString("B_SPEC_CODE"));
			bilt.setCSpecCode(rs.getString("C_SPEC_CODE"));
			bilt.setCSpecName(rs.getString("C_SPEC_NAME"));
			bilts.add(bilt);
		}
		rs.close();
		ps.close();
		return bilts;
	}

	/**
	 * 查询质量设计制程
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @return List<DesignMscPlEntity>
	 * @throws SQLException
	 */
	public List<DesignMscPlEntity> getProcess(Connection con, String designKey)
			throws SQLException {
		List<DesignMscPlEntity> processes = new ArrayList<DesignMscPlEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getProcess");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DesignMscPlEntity process = new DesignMscPlEntity();
			process.setDesginKey(rs.getString("DESGIN_KEY"));
			process.setMscPline(rs.getString("MSC_PLINE"));
			process.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
			process.setWholeBacklogDesc(rs.getString("WHOLE_BACKLOG_DESC"));
			process.setSetFl(rs.getString("SET_FL"));
			QualityDesignTool.nullConverNullString(process);
			processes.add(process);
		}
		rs.close();
		ps.close();
		return processes;
	}

	/**
	 * 查询品名
	 * 
	 * @param con
	 *            连接字符串
	 * @param producCode
	 *            品名编码
	 * @return BaseProductEntity
	 * @throws SQLException
	 */
	public BaseProductEntity getProductCode(Connection con, String producCode)
			throws SQLException {
		BaseProductEntity basePro = null;
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getProductCode");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, producCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			basePro = new BaseProductEntity();
			basePro.setProduccode(rs.getString("PRODUCCODE"));
			basePro.setProducname(rs.getString("PRODUCNAME"));
			basePro.setProducStyle(rs.getString("PRODUC_STYLE"));
			basePro.setProducDesc(rs.getString("PRODUC_DESC"));
			basePro.setHavejg(rs.getString("HAVEJG"));
			basePro.setMemo(rs.getString("MEMO"));
			basePro.setProducJx(rs.getString("PRODUC_JX"));
			basePro.setProducEng(rs.getString("PRODUC_ENG"));
			basePro.setProduccodeOld(rs.getString("PRODUCCODE_OLD"));
			basePro.setProducType(rs.getString("PRODUC_TYPE"));
			basePro.setProducHead(rs.getString("PRODUC_HEAD"));
			basePro.setBaseName(rs.getString("BASENAME"));
			QualityDesignTool.nullConverNullString(basePro);
		}
		rs.close();
		ps.close();
		return basePro;
	}

	/**
	 * 查询是否新物料
	 * 
	 * @param con
	 *            连接字符串
	 * @param materialNo
	 *            物料编码
	 * @return String
	 * @throws SQLException
	 */
	public String getFirstPrdc(Connection con, String materialNo)
			throws SQLException {
		String firstPrdc = "";
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getFirstPrdc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, materialNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			firstPrdc = rs.getString("FIRST_PRDC");
		}
		rs.close();
		ps.close();
		return firstPrdc;
	}

	/**
	 * 查询加工物料
	 * 
	 * @param con
	 *            连接字符串
	 * @param bom
	 *            产品BOM
	 * @return List<DesignBomIEntity>
	 * @throws SQLException
	 */
	public List<DesignBomIEntity> getBomI(Connection con, String bom)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getBomI");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, bom);
		ResultSet rs = ps.executeQuery();
		List<DesignBomIEntity> bomIs = QualityDesignTool.getEntitysByRs(rs,
				DesignBomIEntity.class);
		rs.close();
		ps.close();
		if (bomIs != null && bomIs.size() > 0) {
			return bomIs;
		} else {
			return null;
		}
	}

	/**
	 * 查询产线
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            主键
	 * @param mscPline
	 *            全程产线号
	 * @param processCode
	 *            工序
	 * @return
	 * @throws SQLException
	 */
	public List<DesignMscPlGxPrlEntity> getProline(Connection con,
			String designKey, String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getProline");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		List<DesignMscPlGxPrlEntity> proLines = QualityDesignTool
				.getEntitysByRs(rs, DesignMscPlGxPrlEntity.class);
		rs.close();
		ps.close();
		if (proLines != null && proLines.size() > 0) {
			return proLines;
		} else {
			return null;
		}

	}

	public SlmOrdDesignMscPlPrcEntity getOperation(Connection con,
			String designKey, String mscPline, String processCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getOperation");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ps.setObject(3, processCode);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignMscPlPrcEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignMscPlPrcEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * 查询备料码信息(designKey)
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public SlmOrdDesignBlEntity getBLEntity(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getBLEntity");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignBlEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignBlEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 查询备料码信息(blCode)
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public SlmOrdDesignBlEntity getBLEntityByBl(Connection con, String blCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getBLEntityByBl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blCode);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignBlEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignBlEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 备料码的四项标准查询产品码
	 * 
	 * @param con
	 * @param blEntity
	 * @return
	 * @throws SQLException
	 */
	public ComPscEntity getPsc(Connection con, String producCode,
			String stdCode, String steelCode, String stdStyle)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getPsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, producCode);
		ps.setObject(2, stdCode);
		ps.setObject(3, steelCode);
		ps.setObject(4, stdStyle);
		ResultSet rs = ps.executeQuery();
		List<ComPscEntity> list = EntityTool.getEntitysByRs(rs,
				ComPscEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() == 1) {
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 通过产品码与用途码查询冶金规范码
	 * 
	 * @param con
	 * @param psc
	 * @param useCode
	 * @return
	 * @throws SQLException
	 */
	public String[] getMscByPscAndUseCode(Connection con, String psc,
			String useCode) throws SQLException {
		String[] msc = null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("IBomDao.getMscByPscAndUseCode");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, psc);
		ps.setObject(2, useCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			msc = new String[3];
			msc[0] = rs.getString("MSC");
			msc[1] = rs.getString("MSC_DESC");
			msc[2] = rs.getString("MSC_STYLE");
		}
		rs.close();
		ps.close();
		return msc;
	}

	/**
	 * 验证备料码的规格在基础规格表里是否存在
	 * 
	 * @param con
	 * @param dimater
	 * @param height
	 * @return
	 * @throws SQLException
	 */
	public String[] getBaseSpec(Connection con, String dimater, String height)
			throws SQLException {
		String[] arr = null;
		String sql = CoreXmlSqlParsersFactory.getSql("IBomDao.getBaseSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dimater);
		ps.setObject(2, height);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			arr = new String[2];
			arr[0] = rs.getString("SPEC_CODE");
			arr[1] = rs.getString("SPEC_NAME");
		}
		rs.close();
		ps.close();
		return arr;
	}

	/**
	 * 查询接箍码的产线
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<SlmOrdDesignMscJgLineEntity> getJGLine(Connection con,
			String designKey, String craftFlag) throws SQLException {
		String sql = "";
		if (craftFlag.equals("0")) {
			sql = "select t.pline_code,t.pline_name from SLM_ORD_DESIGN_MSC_JG_LINE t where t.DESGIN_KEY=?";
		} else {
			sql = "select t.pline_code,t.pline_name from CRAFT_ORD_DESIGN_MSC_JG_LINE t where t.DESGIN_KEY=?";
		}
		
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignMscJgLineEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignMscJgLineEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询规格对应的轧管产线
	 * 
	 * @param con
	 * @param designKey
	 * @param specCode
	 * @param craftFlag
	 * @return
	 * @throws SQLException
	 */
	public List<String[]> getZgPline(Connection con, String designKey,
			String mscPline, String specCode, String craftFlag)
			throws SQLException {
		String sql = "";
		if (craftFlag.equals("1")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("IBomDao.getCraftZgPlineBySpec");
		} else {
			sql = CoreXmlSqlParsersFactory
					.getSql("IBomDao.getQualityZgPlineBySpec");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ps.setObject(3, specCode);
		ResultSet rs = ps.executeQuery();
		List<String[]> list = new ArrayList<String[]>();
		while (rs.next()) {
			String[] strArr = new String[2];
			strArr[0] = rs.getString("PLINE_CODE");
			strArr[1] = rs.getString("PLINE_NAME");
			list.add(strArr);
		}
		rs.close();
		ps.close();
		if (list != null && list.size() >0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询规格对应的锻造产线
	 * 
	 * @param con
	 * @param designKey
	 * @param specCode
	 * @param craftFlag
	 * @return
	 * @throws SQLException
	 */
	public List<String[]> getDzPline(Connection con, String designKey,
			String mscPline, String specCode, String craftFlag)
			throws SQLException {
		String sql = "";
		if (craftFlag.equals("1")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("IBomDao.getCraftDzPlineBySpec");
		} else {
			sql = CoreXmlSqlParsersFactory
					.getSql("IBomDao.getQualityDzPlineBySpec");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ps.setObject(3, specCode);
		ResultSet rs = ps.executeQuery();
		List<String[]> list = new ArrayList<String[]>();
		while (rs.next()) {
			String[] strArr = new String[2];
			strArr[0] = rs.getString("PLINE_CODE");
			strArr[1] = rs.getString("PLINE_NAME");
			list.add(strArr);
		}
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询规格对应的铸造产线
	 * 
	 * @param con
	 * @param designKey
	 * @param specCode
	 * @param craftFlag
	 * @return
	 * @throws SQLException
	 */
	public List<String[]> getZzPline(Connection con, String designKey,
			String mscPline, String specCode, String craftFlag)
			throws SQLException {
		String sql = "";
		if (craftFlag.equals("1")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("IBomDao.getCraftZzPlineBySpec");
		} else {
			sql = CoreXmlSqlParsersFactory
					.getSql("IBomDao.getQualityZzPlineBySpec");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ps.setObject(3, specCode);
		ResultSet rs = ps.executeQuery();
		List<String[]> list = new ArrayList<String[]>();
		while (rs.next()) {
			String[] strArr = new String[2];
			strArr[0] = rs.getString("PLINE_CODE");
			strArr[1] = rs.getString("PLINE_NAME");
			list.add(strArr);
		}
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}
}
