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.pss.sale.order.bomGeneration.daoImp.BomJGDaoImp;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomGEntity;
import com.steering.pss.sale.order.bomGeneration.model.DesignBomJEntity;
import com.steering.pss.sale.order.bomGeneration.model.MscBlEntity;
import com.steering.pss.sale.order.bomGeneration.model.MscJgEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

/**
 * 接箍物料码类
 * 
 * @author Administrator
 * 
 */
public class IBomJGDao implements BomJGDaoImp {

	/**
	 * 查询接箍码信息
	 * 
	 * @param con
	 *            连接字符串
	 * @param jgCode
	 *            接箍码
	 * @return MscJgEntity
	 * @throws SQLException
	 */
	public MscJgEntity getJGInfo(Connection con, String jgCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.getJGInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jgCode);
		ResultSet rs = ps.executeQuery();
		List<MscJgEntity> jg = QualityDesignTool.getEntitysByRs(rs,
				MscJgEntity.class);
		rs.close();
		ps.close();
		if (jg.size() > 0) {
			QualityDesignTool.nullConverNullString(jg.get(0));
			return jg.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 查询接箍码的备料信息
	 * 
	 * @param con
	 *            连接字符串
	 * @param jgCode
	 *            接箍码
	 * @return List<MscBlEntity>
	 * @throws SQLException
	 */
	public List<MscBlEntity> getBLInfo(Connection con, String jgCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.getJGBLInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jgCode);
		ResultSet rs = ps.executeQuery();
		List<MscBlEntity> blInfos = QualityDesignTool.getEntitysByRs(rs,
				MscBlEntity.class);
		rs.close();
		ps.close();
		if (blInfos == null || blInfos.size() == 0) {
			return null;
		} else {
			for (MscBlEntity blInfo : blInfos) {
				QualityDesignTool.nullConverNullString(blInfo);
			}
			return blInfos;
		}

	}

	/**
	 * 保存接箍物料编码
	 * 
	 * @param con
	 * @param bom
	 * @throws SQLException
	 */
	public void saveJGBom(Connection con, DesignBomEntity bom)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.saveJGBom");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, bom.getMaterialNo());
		ps.setObject(2, bom.getMaterialDesc());
		ps.setObject(3, bom.getBomTyp());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询接箍物料编码
	 * 
	 * @param con
	 *            连接字符串
	 * @param jgCode
	 *            接箍码
	 * @return
	 * @throws SQLException
	 */
	public DesignBomEntity getJGBom(Connection con, String jgCode)
			throws SQLException {
		DesignBomEntity bomJG = null;
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.getJGBom");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jgCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			bomJG = new DesignBomEntity();
			bomJG.setBom(rs.getString("BOM"));
			bomJG.setMaterialNo(rs.getString("MATERIAL_NO"));
			bomJG.setMaterialDesc(rs.getString("MATERIAL_DESC"));
		}
		rs.close();
		ps.close();
		return bomJG;
	}

	/**
	 * 保存接箍
	 * 
	 * @param con
	 *            连接字符串
	 * @param bomJ
	 *            接箍类
	 * @throws SQLException
	 */
	public void saveBomJ(Connection con, DesignBomJEntity bomJ)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.saveBomJ");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, bomJ.getBom());
		ps.setObject(2, bomJ.getBomJg());
		ps.setObject(3, bomJ.getOutMaterialNo());
		ps.setObject(4, bomJ.getOutMaterialDesc());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 保存接箍加工
	 * 
	 * @param con
	 *            连接字符串
	 * @param bomJ
	 *            接箍加工类
	 * @throws SQLException
	 */
	public void saveBomG(Connection con, DesignBomGEntity bomG)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.saveBomG");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, bomG.getBom());
		ps.setObject(2, bomG.getBomJg());
		ps.setObject(3, bomG.getInMaterialNo());
		ps.setObject(4, bomG.getInMaterialDesc());
		ps.setObject(5, bomG.getMidMaterialNo());
		ps.setObject(6, bomG.getMidMaterialDesc());
		ps.setObject(7, bomG.getOutMaterialNo());
		ps.setObject(8, bomG.getOutMaterialDesc());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询质量设计接箍码
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            质量主键
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String> getJGCode(Connection con, String designKey,
			String craftFlag) throws SQLException {
		List<String> list = new ArrayList<String>();
		String sql = "";
		if (craftFlag.equals("0")) {
			sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.getJGCodeQuality");
		} else {
			sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.getJGCodeCraft");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			list.add(rs.getString("CODE_JG"));
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 验证质量设计-BOM-接箍是否重复
	 * 
	 * @param con
	 *            连接字符串
	 * @param bom
	 *            BOM
	 * @param bomJG
	 *            BOMJG
	 * @param outMaterialNo
	 *            产出物料
	 * @return int
	 * @throws SQLException
	 */
	public int checkJGCode(Connection con, String bom, String bomJG,
			String outMaterialNo) throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.checkJGCode");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, bom);
		ps.setObject(2, bomJG);
		ps.setObject(3, outMaterialNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = Integer.parseInt(rs.getString("COUNT"));
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 验证质量设计-BOM-加工是否重复
	 * 
	 * @param con
	 * @param bom
	 * @param inMaterialNo
	 * @param outMaterialNo
	 * @return
	 * @throws SQLException
	 */
	public int checkCodeI(Connection con, String bom, String inMaterialNo,
			String outMaterialNo) throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.checkCodeI");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, bom);
		ps.setObject(2, inMaterialNo);
		ps.setObject(3, outMaterialNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = Integer.parseInt(rs.getString("COUNT"));
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 验证质量设计-BOM-接箍加工是否重复
	 * 
	 * @param con
	 * @param bom
	 * @param inMaterialNo
	 * @param outMaterialNo
	 * @return
	 * @throws SQLException
	 */
	public int checkCodeG(Connection con, String bom, String bomJG,
			String inMaterialNo, String outMaterialNo) throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("IBomJGDao.checkCodeG");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, bom);
		ps.setObject(2, bomJG);
		ps.setObject(3, inMaterialNo);
		ps.setObject(4, outMaterialNo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = Integer.parseInt(rs.getString("COUNT"));
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询接箍向下的备料码
	 * 
	 * @param con
	 * @param designKey
	 * @param craftFlag
	 * @return
	 * @throws SQLException
	 */
	public List<String> getJGBLCode(Connection con, String designKey,
			String craftFlag) throws SQLException {
		String sql = "";
		if (craftFlag.equals("1")) {
			sql = "select t.psc_bl from craft_ord_design_msc_jg_bl t where t.desgin_key=? ";
		} else {
			sql = "select t.psc_bl from slm_ord_design_msc_jg_bl t where t.desgin_key=? ";
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<String> list = new ArrayList<String>();
		if (rs.next()) {
			if (rs.getString("psc_bl") != null
					&& !rs.getString("psc_bl").equals(""))
				list.add(rs.getString("psc_bl"));
		}
		rs.close();
		ps.close();
		return list;
	}
}
