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 com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscJGDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.ComMscJgEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignMscJgBlEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignMscJgEntity;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

/**
 * 接箍码bean类
 * 
 * @author Administrator
 * 
 */
public class IMscJGDao implements MscJGDaoImp {

	/**
	 * 查询接箍码(外径/壁厚)
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param steelCode
	 *            钢级代码
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<SlmOrdDesignMscJgEntity> getJGByDAndH(Connection con,
			String msc, String steelCode, String modelDesc, String dimater, String height,
			String orderLinePk, String keyJg) throws SQLException {
		List<SlmOrdDesignMscJgEntity> c0deJGs = new ArrayList<SlmOrdDesignMscJgEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.getJGByDAndH");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, keyJg);
		ps.setObject(3, keyJg);
		ps.setObject(4, height);
		ps.setObject(5, height);
		ps.setObject(6, dimater);
		ps.setObject(7, dimater);
		ps.setObject(8, steelCode);
		ps.setObject(9, modelDesc);
		ps.setObject(10, orderLinePk);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			SlmOrdDesignMscJgEntity codeJG = new SlmOrdDesignMscJgEntity();
			codeJG.setCodeJg(rs.getString("CODE_JG"));
			codeJG.setCustAlphaNo(rs.getString("ORDER_SPECIAL_DESC"));
			codeJG.setKeyJg(rs.getString("KEY_JG"));
			QualityDesignTool.nullConverNullString(codeJG);
			c0deJGs.add(codeJG);
		}
		rs.close();
		ps.close();
		return c0deJGs;
	}

	/**
	 * 查询接箍码(规格)
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param steelCode
	 *            钢级代码
	 * @param specCode
	 *            产品规格
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<SlmOrdDesignMscJgEntity> getJGBySpecCode(Connection con,
			String msc, String steelCode, String modelDesc, String specCode,
			String orderLinePk, String keyJg)
			throws SQLException {
		List<SlmOrdDesignMscJgEntity> c0deJGs = new ArrayList<SlmOrdDesignMscJgEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscJGDao.getJGBySpecCode");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, specCode);
		ps.setObject(3, keyJg);
		ps.setObject(4, keyJg);
		ps.setObject(5, steelCode);
		ps.setObject(6, modelDesc);
		ps.setObject(7, orderLinePk);
		
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			SlmOrdDesignMscJgEntity codeJG = new SlmOrdDesignMscJgEntity();
			codeJG.setCodeJg(rs.getString("CODE_JG"));
			codeJG.setCustAlphaNo(rs.getString("ORDER_SPECIAL_DESC"));
			codeJG.setKeyJg(rs.getString("KEY_JG"));
			QualityDesignTool.nullConverNullString(codeJG);
			c0deJGs.add(codeJG);
		}
		rs.close();
		ps.close();
		return c0deJGs;

	}

	/**
	 * 保存接箍码
	 * 
	 * @param con
	 *            连接字符串
	 * @param jgEntity
	 *            接箍实体类
	 * @throws SQLException
	 */
	public void saveJG(Connection con, SlmOrdDesignMscJgEntity jgEntity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.saveJG");
		sql = EntityTool.fillSqlByEntity(sql, jgEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询接箍码对应的备料码
	 * 
	 * @param con
	 *            连接字符串
	 * @param codeJG
	 *            接箍码
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<SlmOrdDesignMscJgBlEntity> getJGBL(Connection con, String codeJg, String custAlphaNo, String alphaCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.getJGBL");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, codeJg);
		ps.setObject(2, custAlphaNo);
		ps.setObject(3, alphaCode);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignMscJgBlEntity> jgblEntitys = EntityTool.getEntitysByRs(rs, SlmOrdDesignMscJgBlEntity.class);
		rs.close();
		ps.close();
		return jgblEntitys;
	}

	/**
	 * 保存接箍在码对应的备料码
	 */
	public void saveJgBl(Connection con, SlmOrdDesignMscJgBlEntity jgBlEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.saveMscJGBL");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jgBlEntity.getDesginKey());
		ps.setObject(2, jgBlEntity.getCodeJg());
		ps.setObject(3, jgBlEntity.getPscBl());
		ps.setObject(4, jgBlEntity.getDesginKeyBl());
		ps.setObject(5, jgBlEntity.getCustAlphaNo());
		ps.setObject(6, jgBlEntity.getOrderSpecialDesc());
		ps.setObject(7, jgBlEntity.getAlphaCode());
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 通过冶金规范码和接箍码查询接箍信息
	 * 
	 * @param con
	 * @param jgkey
	 * @return
	 * @throws SQLException
	 */
	public SlmOrdDesignMscJgEntity getJGInfo(Connection con, String jgkey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.getJGInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jgkey);
		ResultSet rs = ps.executeQuery();
		List<SlmOrdDesignMscJgEntity> list = EntityTool.getEntitysByRs(rs,
				SlmOrdDesignMscJgEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (SlmOrdDesignMscJgEntity slmOrdDesignMscJgEntity : list) {
				QualityDesignTool.nullConverNullString(slmOrdDesignMscJgEntity);
			}
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 查询备料码技术评审状态
	 * 
	 * @param con
	 * @param blCode
	 * @return
	 * @throws SQLException
	 */
	public String[] getJGBLInfo(Connection con, String blCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.getJGBLInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blCode);
		ResultSet rs = ps.executeQuery();
		String[] str = null;
		if (rs.next()) {
			str = new String[2];
			str[0] = rs.getString("DESGIN_KEY");
			str[1] = rs.getString("REVIEW_STATUS");
		}
		rs.close();
		ps.close();
		return str;
	}

	/**
	 * 接箍信息
	 * 
	 * @param con
	 * @param msc
	 * @param jgCode
	 * @return
	 * @throws SQLException
	 */
	public ComMscJgEntity getJGBaseInfo(Connection con, String jgCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.getJGBaseInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jgCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscJgEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscJgEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscJgEntity slmOrdDesignMscJgEntity : list) {
				CraftTool.nullConverNullString(slmOrdDesignMscJgEntity);
			}
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 查询设定接箍
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<SlmOrdDesignMscJgEntity> getSetJG(Connection con,
			String designKey) throws SQLException {
		List<SlmOrdDesignMscJgEntity> c0deJGs = new ArrayList<SlmOrdDesignMscJgEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.getSetJG");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			SlmOrdDesignMscJgEntity codeJG = new SlmOrdDesignMscJgEntity();
			codeJG.setCodeJg(rs.getString("CODE_JG"));
			codeJG.setCustAlphaNo(rs.getString("CUST_ALPHA_NO"));
			codeJG.setKeyJg(rs.getString("KEY_JG"));
			QualityDesignTool.nullConverNullString(codeJG);
			c0deJGs.add(codeJG);
		}
		rs.close();
		ps.close();
		return c0deJGs;
	}
	
	/**
	 * 删除接箍
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteJg(Connection con, String designKey) throws SQLException{
		SqlBuilder b = new SqlBuilder("IMscJGDao.DeleteJg");
		b.Fill(new String[]{designKey});
		PreparedStatement ps = con.prepareStatement(b.toString());
		ps.executeUpdate();
		ps.close();
		return;
	}

	/**
	 * 修改设定接箍信息
	 * 
	 * @param con
	 * @param jgEntity
	 * @throws SQLException
	 */
	public void updateSetJG(Connection con, SlmOrdDesignMscJgEntity jgEntity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.updateSetJG");
		sql = EntityTool.fillSqlByEntity(sql, jgEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 获取keyjg数
	 * 
	 * @param con
	 * @param blCode
	 * @return
	 * @throws SQLException
	 */
	public String getCountByKeyJg(Connection con, String keyJg) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscJGDao.getCountByKeyJg");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, keyJg);
		ResultSet rs = ps.executeQuery();
		rs.next();
		String str = rs.getString(1);
		rs.close();
		ps.close();
		return str;
	}
}
