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.List;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

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.DAL.SlmOrdDesignMscDAL;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

/**
 * 工艺评审预处理
 * 
 * @author Administrator
 * 
 */
public class PretreatmentDao {
	/**
	 * 查询合同信息
	 * 
	 * @param con
	 * @param ordLnPk
	 *            合同行主键
	 * @return CraftOrdDesignMscEntity
	 * @throws SQLException
	 */
	public CraftOrdDesignMscEntity getOrderInfo(Connection con, String ordLnPk)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.getOrderInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnPk);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() != 1) {
			return null;
		} else {
			CraftTool.nullConverNullString(list.get(0));
			return list.get(0);
		}
	}

	/**
	 * 新增工艺评审主记录
	 * 
	 * @param con
	 * @param craftOrdDesign
	 * @throws SQLException
	 */
	public void addCraftDesignMsc(Connection con,
			CraftOrdDesignMscEntity craftOrdDesign) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.addCraftDesignMsc");
		sql = EntityTool.fillSqlByEntity(sql, craftOrdDesign);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询产品规格类型
	 * 
	 * @param con
	 * @param specCode
	 * @return
	 * @throws SQLException
	 */
	public String getSpecCodeStyle(Connection con, String specCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.getSpecCodeStyle");
		String specStyle = "";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, specCode);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("MSC_STYLE") != null) {
				specStyle = rs.getString("MSC_STYLE");
			}
		}
		rs.close();
		ps.close();
		return specStyle;
	}

	/**
	 * 查询工艺评审主键
	 * 
	 * @param con
	 * @param specCode
	 * @return
	 * @throws SQLException
	 */
	public String getDesignKey(Connection con, String ordLnPk)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.getDesignKey");
		String designKey = "";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, ordLnPk);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("DESGIN_KEY") != null) {
				designKey = rs.getString("DESGIN_KEY");
			}
		}
		rs.close();
		ps.close();
		return designKey;
	}

	/**
	 * 删除工艺评审主记录
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMsc(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMsc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审标准号
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStd(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStd");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审主记录
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdTklg(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdTklg");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审明细
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdTklgLst(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdTklgLst");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审成分标准
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdCic(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdCic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审理化标准
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdPic(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdPic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审理化标准-取样要求
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdPicSmp(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdPicSmp");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审公差标准
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdSic(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdSic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审探伤标准
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdDic(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdDic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审水压标准
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdWic(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdWic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审加工标准
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignStdGic(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignStdGic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审接箍
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscJG(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscJG");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审接箍备料
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscJGBL(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscJGBL");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审接箍制程
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscJGPl(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscJGPl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审备料
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscBL(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscBL");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审铸造规格
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscBiltC(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscBiltC");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审铸造产线
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscBiltCPl(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscBiltCPl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审锻造规格
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscBilt(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscBilt");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审锻造产线
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscBiltPl(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscBiltPl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审轧制规格
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscGzGg(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscGzGg");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审轧制产线
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscGzGgPl(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscGzGgPl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审产线
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscPlGxPrl(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscPlGxPrl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审钢种
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscPlGz(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscPlGz");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 删除附加要求全项
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignAddAsk(Connection con, String designKey) throws SQLException{
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignAddAsk");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审工序
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscPlPrc(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscPlPrc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审工序点
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftDesignMscPlSti(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftDesignMscPlSti");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 删除工艺评审制程(不是设定制程)
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void deleteCraftOrdDesignMscPl(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.deleteCraftOrdDesignMscPl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 修改工艺评审制程
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void updateCraftOrdDesignMscPl(Connection con, String designKey,
			String designKeyB, String msc) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.updateCraftOrdDesignMscPl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKeyB);
		ps.setObject(2, msc);
		ps.setObject(3, designKey);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 修改工艺评审制程
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void updateCraftOrdDesignMscPl2(Connection con, String designKey,
			String ordLnPk) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.updateCraftOrdDesignMscPl2");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, ordLnPk);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 修改工艺评审设定接箍
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void updateCraftDesignMscJG(Connection con, String designKey,
			String designKeyB) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.updateCraftDesignMscJG");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, designKeyB);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 修改工艺评审设定接箍
	 * 
	 * @param con
	 * @param designKey
	 * @throws SQLException
	 */
	public void updateCraftDesignMscJG2(Connection con, String designKey,
			String ordLnPk) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("PretreatmentDao.updateCraftDesignMscJG2");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2,ordLnPk);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 修改附件图片
	 * @param con
	 * @param craftOrdDesign
	 * @throws SQLException
	 */
	public void updateMscFjImg(Connection con, CraftOrdDesignMscEntity craftOrdDesign)
			throws SQLException{
		SqlBuilder b = new SqlBuilder("PretreatmentDao.updateMscFjImg");
		b.Fill(new String[]{ craftOrdDesign.getFileJpeg(), craftOrdDesign.getDesginKey() });
		PreparedStatement ps = con.prepareStatement(b.toString());
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 获取设定附件图片
	 * @param con
	 * @param ordLnPk
	 * @return
	 */
	public String getSetFjImg(Connection con, String ordLnPk){
		SqlBuilder b = new SqlBuilder("PretreatmentDao.getSetFjImg");
		b.Fill(new String[]{ ordLnPk });
		GenericEntity value = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		if(value == null){
			return "";
		}else{
			return value.getValue();
		}
	}
	
	/**
	 * 获取技术评审DesignKey
	 * @param con
	 * @param ordLnPk
	 * @return
	 */
	public String getSlmDesignkey(Connection con, String ordLnPk){
		SqlBuilder b = new SqlBuilder("PretreatmentDao.getSlmDesignKey");
		b.Fill(new String[]{ ordLnPk });
		GenericEntity value = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		return value.getValue();
	}
}
