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.craftDesign.model.ComBaseWaterRatioEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStdWaterEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdWicEntity;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

public class MscWaterDao {
	/**
	 * 查询水压索引号 （扣型，冶金规范码）
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return
	 * @throws SQLException
	 */
	public List<ComMscStdWaterEntity> getExamineWic(Connection con, String msc,
			String modelCode, String flag) throws SQLException {
		String sql = "";
		if (flag.equals("C")) {
			sql = CoreXmlSqlParsersFactory.getSql("MscWaterDao.getExamineWic");
		} else {
			sql = CoreXmlSqlParsersFactory.getSql("MscWaterDao.getValidWic");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ps.setObject(2, modelCode);
		ps.setObject(3, msc);
		ps.setObject(4, modelCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscStdWaterEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscStdWaterEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (ComMscStdWaterEntity comMscStdWaterEntity : list) {
				CraftTool.nullConverNullString(comMscStdWaterEntity);
			}
			return list;
		}
	}

	/**
	 * 查询引用水压标准主键
	 * 
	 * @param con
	 *            连接字符串
	 * @param stdCode
	 *            标准
	 * @param stdStyle
	 *            标准类别
	 * @return 引用水压标准和引用水压标准类别
	 * @throws SQLException
	 */
	public String[] getQuoteWaterStd(Connection con, String stdCode,
			String stdStyle) throws SQLException {
		String[] str = null;
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscWaterDao.getWaterRatioId");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, stdCode);
		ps.setObject(2, stdStyle);
		ps.setObject(3, stdStyle);
		ps.setObject(4, stdStyle);
		ps.setObject(5, stdStyle);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			str = new String[2];
			str[0] = rs.getString("STD_CODE");
			str[1] = rs.getString("STD_STYLE");
		}
		rs.close();
		ps.close();
		return str;
	}

	/**
	 * 通过水压主键找水压系数和压力
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public String[] getPressByMajor(Connection con, String id)
			throws SQLException {
		String[] str = new String[2];
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscWaterDao.getPressByMajor");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			str[0] = rs.getString("RATIO_STD");
			str[1] = rs.getString("MAX_PRESS");
		} else {
			return null;
		}
		return str;
	}
	
	/**
	 * 通过水压主键找水压系数和压力
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public String getTimeByDesignKey(Connection con, String designKey)
			throws SQLException {
		String str = "";
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscWaterDao.getTimeByDesignKey");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, "0");
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			str = rs.getString("save_time");
		} else {
			return null;
		}
		return str;
	}

	/**
	 * 查询水压压力等
	 * 
	 * @param con
	 * @param modelCode
	 *            扣型
	 * @param stdCode
	 *            标准
	 * @param steelCode
	 *            钢级
	 * @param stdStyle
	 *            类别
	 * @return List<DesignWaterRatioEntity>
	 * @throws SQLException
	 */
	public List<ComBaseWaterRatioEntity> getWaterPress(Connection con,
			String modelCode, String stdCode, String steelCode, 
			String stdStyle, String wic)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscWaterDao.getWaterPress");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, modelCode);
		ps.setObject(2, stdCode);
		ps.setObject(3, steelCode);
		ps.setObject(4, stdStyle);
		ps.setObject(5, wic);
		ResultSet rs = ps.executeQuery();
		List<ComBaseWaterRatioEntity> list = EntityTool.getEntitysByRs(rs,
				ComBaseWaterRatioEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (ComBaseWaterRatioEntity comBaseWaterRatioEntity : list) {
				CraftTool.nullConverNullString(comBaseWaterRatioEntity);
			}
			return list;
		}
	}

	/**
	 * 查询水压压力等(如果第一次没有找到)
	 * 
	 * @param con
	 * @param modelCode
	 *            扣型
	 * @param stdCode
	 *            标准
	 * @param steelStyle
	 *            钢级
	 * @return
	 * @throws SQLException
	 */
	public List<ComBaseWaterRatioEntity> getWaterPressOther(Connection con,
			String modelCode, String stdCode, String steelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscWaterDao.getWaterPressOther");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, modelCode);
		ps.setObject(2, stdCode);
		ps.setObject(3, steelCode);
		ResultSet rs = ps.executeQuery();
		List<ComBaseWaterRatioEntity> list = EntityTool.getEntitysByRs(rs,
				ComBaseWaterRatioEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (ComBaseWaterRatioEntity comBaseWaterRatioEntity : list) {
				CraftTool.nullConverNullString(comBaseWaterRatioEntity);
			}
			return list;
		}
	}
	
	/**
	 * 查询水压标准
	 * 
	 * @param con
	 * @param wic
	 * @return
	 * @throws SQLException
	 */
	public ComMscStdWaterEntity getMscStdWaterByWic(Connection con,
			String wic) throws SQLException {
		SqlBuilder b = new SqlBuilder("MscWaterDao.getMscStdWaterByWic");
		b.Fill(new String[]{ wic });
		return EntityTool.getEntityBySqlNoClsCon(b.toString(), con, ComMscStdWaterEntity.class);
	}

	/**
	 * 查询水压标准
	 * 
	 * @param con
	 * @param wic
	 * @return
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdWicEntity> getWaterByWic(Connection con,
			String wic) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscWaterDao.getWaterByWic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, wic);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdWicEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignStdWicEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (CraftOrdDesignStdWicEntity craftOrdDesignStdWicEntity : list) {
				CraftTool.nullConverNullString(craftOrdDesignStdWicEntity);
			}
			return list;
		}

	}

	/**
	 * 保存水压标准
	 * 
	 * @param con
	 * @param water
	 * @param designKey
	 * @throws SQLException
	 */
	public void saveWater(Connection con, CraftOrdDesignStdWicEntity water)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("MscWaterDao.saveWic");
		sql = EntityTool.fillSqlByEntity(sql, water);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	/**
	 * 修改交付标准索引号
	 * 
	 * @param con
	 *            连接字符串
	 * @param Wic
	 *            成分索引号
	 * @param cic
	 *            质量主键
	 * @throws SQLException
	 */
	public void updateDesignStd(Connection con, String wic, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("MscWaterDao.updateDesignStd");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, wic);
		ps.setObject(2, designKey);
		ps.executeUpdate();
		ps.close();
	}
	
	/**
	 * 获取设定水压系数
	 * @param con
	 * @param ordLnPk
	 */
	public String getSetRatioId(Connection con, String ordLnPk) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscWaterDao.getSetRatioId");
		b.Fill(new String[]{ ordLnPk });
		GenericEntity setRatio = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		if(setRatio != null){
			return setRatio.getValue();
		}else{
			return "";
		}
	}
	
	/**
	 * 获取水压最小屈服强度
	 */
	public String getWaterYS(Connection con, String designKey) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscWaterDao.getWaterYS");
		b.Fill(new String[]{ designKey });
		GenericEntity ys = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		if(ys != null){
			return ys.getValue();
		}else{
			return "";
		}
	}
	
	/**
	 * 获取水压最小拉伸强度
	 */
	public String getWaterTS(Connection con, String designKey) throws SQLException{
		SqlBuilder b = new SqlBuilder("MscWaterDao.getWaterTS");
		b.Fill(new String[]{ designKey });
		GenericEntity ts = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		if(ts != null){
			return ts.getValue();
		}else{
			return "";
		}
	}
}
