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.ibm.ObjectQuery.crud.util.Array;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.craftDesign.model.ComMscJgEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscRJgTorqueEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscJgBlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscJgEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;

/**
 * 工艺评审-接箍
 * 
 * @author Administrator
 * 
 */
public class MscJGDao {
	/**
	 * 查询接箍码(外径/壁厚)
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param steelCode
	 *            钢级代码
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<CraftOrdDesignMscJgEntity> getJGByDAndH(Connection con, String msc, String steelCode,
			String modelDesc, String dimater, String height, String orderLinePk, String keyJg) throws SQLException {
		List<CraftOrdDesignMscJgEntity> jgEntitys = new ArrayList<CraftOrdDesignMscJgEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.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()) {
			CraftOrdDesignMscJgEntity jgEntity = new CraftOrdDesignMscJgEntity();
			jgEntity.setCodeJg(rs.getString("CODE_JG"));
			jgEntity.setCustAlphaNo(rs.getString("ORDER_SPECIAL_DESC"));
			jgEntity.setKeyJg(rs.getString("KEY_JG"));
			QualityDesignTool.nullConverNullString(jgEntity);
			jgEntitys.add(jgEntity);
		}
		rs.close();
		ps.close();
		return jgEntitys;
	}

	/**
	 * 查询接箍码(规格)
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范码
	 * @param steelCode
	 *            钢级代码
	 * @param specCode
	 *            产品规格
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<CraftOrdDesignMscJgEntity> getJGBySpecCode(Connection con, String msc, String steelCode,
			String modelDesc, String specCode, String orderLinePk, String keyJg) throws SQLException {
		List<CraftOrdDesignMscJgEntity> jgEntitys = new ArrayList<CraftOrdDesignMscJgEntity>();
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.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()) {
			CraftOrdDesignMscJgEntity jgEntity = new CraftOrdDesignMscJgEntity();
			jgEntity.setCodeJg(rs.getString("CODE_JG"));
			jgEntity.setCustAlphaNo(rs.getString("ORDER_SPECIAL_DESC"));
			jgEntity.setKeyJg(rs.getString("KEY_JG"));
			QualityDesignTool.nullConverNullString(jgEntity);
			jgEntitys.add(jgEntity);
		}
		rs.close();
		ps.close();
		return jgEntitys;

	}

	/**
	 * 保存接箍码
	 * 
	 * @param con
	 *            连接字符串
	 * @param jgEntity
	 *            接箍实体类
	 * @throws SQLException
	 */
	public void saveJG(Connection con, CraftOrdDesignMscJgEntity jgEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.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<CraftOrdDesignMscJgBlEntity> getJGBL(Connection con, String codeJG, 
			String custAlphaNo, String alphaCode) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.getJGBL");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, codeJG);
		ps.setObject(2, custAlphaNo);
		ps.setObject(3, alphaCode);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscJgBlEntity> jgBlEntitys = EntityTool.getEntitysByRs(rs, CraftOrdDesignMscJgBlEntity.class);
		rs.close();
		ps.close();
		return jgBlEntitys;
	}

	/**
	 * 保存接箍在码对应的备料码
	 */
	public void saveJgBl(Connection con, CraftOrdDesignMscJgBlEntity jgblEntity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.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 msc
	 * @param jgCode
	 * @return
	 * @throws SQLException
	 */
	public CraftOrdDesignMscJgEntity getJGInfo(Connection con, String jgKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.getJGInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, jgKey);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscJgEntity> list = EntityTool.getEntitysByRs(rs, CraftOrdDesignMscJgEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignMscJgEntity slmOrdDesignMscJgEntity : list) {
				CraftTool.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("MscJGDao.getJGBLInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, blCode);
		ps.setObject(2, blCode);
		ResultSet rs = ps.executeQuery();
		String[] str = null;
		if (rs.next()) {
			str = new String[3];
			str[0] = rs.getString("ORD_LN_PK");
			str[1] = rs.getString("DESGIN_KEY");
			str[2] = rs.getString("VALIDFLAG");
		}
		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("MscJGDao.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<CraftOrdDesignMscJgEntity> getSetJG(Connection con, String designKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.getSetJG");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscJgEntity> jgEntitys = EntityTool.getEntitysByRs(rs, CraftOrdDesignMscJgEntity.class);
		rs.close();
		ps.close();
		if (jgEntitys == null || jgEntitys.size() == 0) {
			return null;
		} else {
			return jgEntitys;
		}

	}
	
	/**
	 * 删除接箍
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteJg(Connection con, String designKey) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscJGDao.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, CraftOrdDesignMscJgEntity jgEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscJGDao.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("MscJGDao.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;
	}

	/**
	 * 获取拧接信息
	 * @param con
	 * @param keyJg
	 * @param dimater
	 * @param height
	 * @return
	 */
	public ArrayList<ComMscRJgTorqueEntity> getTorque(Connection con, String keyJg,
		String dimater, String height){
		SqlBuilder b = new SqlBuilder("MscJGDao.getTorque", keyJg, dimater, height );
		ArrayList<ComMscRJgTorqueEntity> torque = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, ComMscRJgTorqueEntity.class);
		return torque;
	}
}
