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 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.model.DesignStdWicEntity;
import com.steering.pss.sale.order.model.DesignWaterRatioEntity;
import com.steering.pss.sale.order.model.MscStdWaterEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.MscWaterDaoImp;

/**
 * 水压标准类
 * 
 * @author Administrator
 * 
 */
public class IMscWaterDao implements MscWaterDaoImp {
	/**
	 * 查询审批水压索引号 （扣型，冶金规范码）
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return
	 * @throws SQLException
	 */
	public List<MscStdWaterEntity> getExamineWic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<MscStdWaterEntity> list = new ArrayList<MscStdWaterEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscWaterDao.getExamineWic");
		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();
		while (rs.next()) {
			MscStdWaterEntity water = new MscStdWaterEntity();
			water.setWic(rs.getString("WIC"));
			water.setStdstyle(rs.getString("STDSTYLE"));
			water.setProcessCdoe(rs.getString("PROCESS_CDOE"));
			water.setProcessDesc(rs.getString("PROCESS_DESC"));
			water.setStdCodeNk(rs.getString("STD_CODE_NK"));
			water.setStdNameNk(rs.getString("STD_NAME_NK"));
			water.setMinD(rs.getString("MIN_D"));
			water.setMaxD(rs.getString("MAX_D"));
			water.setMinH(rs.getString("MIN_H"));
			water.setMaxH(rs.getString("MAX_H"));
			water.setSpecJg(rs.getString("SPEC_JG"));
			water.setSpecJgDesc(rs.getString("SPEC_JG_DESC"));
			water.setModelCode(rs.getString("MODEL_CODE"));
			water.setModelDesc(rs.getString("MODEL_DESC"));
			water.setMsc(rs.getString("MSC"));
			water.setProducHead(rs.getString("PRODUC_HEAD"));
			water.setCodeJg(rs.getString("CODE_JG"));
			QualityDesignTool.nullConverNullString(water);
			list.add(water);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 查询审批水压索引号 （扣型，冶金规范码）
	 * 
	 * @param con
	 * @param msc
	 *            冶金规范码
	 * @param modelCode
	 *            扣型
	 * @return
	 * @throws SQLException
	 */
	public List<MscStdWaterEntity> getValidWic(Connection con, String msc,
			String modelCode) throws SQLException {
		List<MscStdWaterEntity> list = new ArrayList<MscStdWaterEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscWaterDao.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();
		while (rs.next()) {
			MscStdWaterEntity water = new MscStdWaterEntity();
			water.setWic(rs.getString("WIC"));
			water.setStdstyle(rs.getString("STDSTYLE"));
			water.setProcessCdoe(rs.getString("PROCESS_CDOE"));
			water.setProcessDesc(rs.getString("PROCESS_DESC"));
			water.setStdCodeNk(rs.getString("STD_CODE_NK"));
			water.setStdNameNk(rs.getString("STD_NAME_NK"));
			water.setMinD(rs.getString("MIN_D"));
			water.setMaxD(rs.getString("MAX_D"));
			water.setMinH(rs.getString("MIN_H"));
			water.setMaxH(rs.getString("MAX_H"));
			water.setSpecJg(rs.getString("SPEC_JG"));
			water.setSpecJgDesc(rs.getString("SPEC_JG_DESC"));
			water.setModelCode(rs.getString("MODEL_CODE"));
			water.setModelDesc(rs.getString("MODEL_DESC"));
			water.setMsc(rs.getString("MSC"));
			water.setProducHead(rs.getString("PRODUC_HEAD"));
			water.setCodeJg(rs.getString("CODE_JG"));
			QualityDesignTool.nullConverNullString(water);
			list.add(water);
		}
		rs.close();
		ps.close();
		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("IMscWaterDao.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("IMscWaterDao.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 modelCode
	 *            扣型
	 * @param stdCode
	 *            标准
	 * @param steelCode
	 *            钢级
	 * @param stdStyle
	 *            类别
	 * @return List<DesignWaterRatioEntity>
	 * @throws SQLException
	 */
	public List<DesignWaterRatioEntity> getWaterPress(Connection con,
			String modelCode, String stdCode, String steelCode, String stdStyle)
			throws SQLException {
		List<DesignWaterRatioEntity> list = new ArrayList<DesignWaterRatioEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscWaterDao.getWaterPress");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, modelCode);
		ps.setObject(2, stdCode);
		ps.setObject(3, steelCode);
		ps.setObject(4, stdStyle);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DesignWaterRatioEntity entity = new DesignWaterRatioEntity();
			entity.setRatioId(rs.getString("RATIO_ID"));
			entity.setStdCode(rs.getString("STD_CODE"));
			entity.setStdName(rs.getString("STD_NAME"));
			entity.setSizeMinSign(rs.getString("SIZE_MIN_SIGN"));
			entity.setSizeMin(rs.getString("SIZE_MIN"));
			entity.setSizeMaxSign(rs.getString("SIZE_MAX_SIGN"));
			entity.setSizeMax(rs.getString("SIZE_MAX"));
			entity.setRatioStd(rs.getString("RATIO_STD"));
			entity.setRatioReplace(rs.getString("RATIO_REPLACE"));
			entity.setMaxPress(rs.getString("MAX_PRESS"));
			entity.setStdStyle(rs.getString("STD_STYLE"));
			entity.setStdStyleDesc(rs.getString("STD_STYLE_DESC"));
			entity.setModelCode(rs.getString("MODEL_CODE"));
			entity.setModelDesc(rs.getString("MODEL_DESC"));
			entity.setSteelCode(rs.getString("STEELCODE"));
			QualityDesignTool.nullConverNullString(entity);
			list.add(entity);
		}
		return list;
	}

	/**
	 * 查询水压压力等(如果第一次没有找到)
	 * 
	 * @param con
	 * @param modelCode
	 *            扣型
	 * @param stdCode
	 *            标准
	 * @param steelStyle
	 *            钢级
	 * @return
	 * @throws SQLException
	 */
	public List<DesignWaterRatioEntity> getWaterPressOther(Connection con,
			String modelCode, String stdCode, String steelCode)
			throws SQLException {
		List<DesignWaterRatioEntity> list = new ArrayList<DesignWaterRatioEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscWaterDao.getWaterPressOther");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, modelCode);
		ps.setObject(2, stdCode);
		ps.setObject(3, steelCode);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DesignWaterRatioEntity entity = new DesignWaterRatioEntity();
			entity.setRatioId(rs.getString("RATIO_ID"));
			entity.setStdCode(rs.getString("STD_CODE"));
			entity.setStdName(rs.getString("STD_NAME"));
			entity.setSizeMinSign(rs.getString("SIZE_MIN_SIGN"));
			entity.setSizeMin(rs.getString("SIZE_MIN"));
			entity.setSizeMaxSign(rs.getString("SIZE_MAX_SIGN"));
			entity.setSizeMax(rs.getString("SIZE_MAX"));
			entity.setRatioStd(rs.getString("RATIO_STD"));
			entity.setRatioReplace(rs.getString("RATIO_REPLACE"));
			entity.setMaxPress(rs.getString("MAX_PRESS"));
			entity.setStdStyle(rs.getString("STD_STYLE"));
			entity.setStdStyleDesc(rs.getString("STD_STYLE_DESC"));
			entity.setModelCode(rs.getString("MODEL_CODE"));
			entity.setModelDesc(rs.getString("MODEL_DESC"));
			entity.setSteelCode(rs.getString("STEELCODE"));
			QualityDesignTool.nullConverNullString(entity);
			list.add(entity);
		}
		return list;
	}

	/**
	 * 查询水压标准
	 * 
	 * @param con
	 * @param wic
	 * @return
	 * @throws SQLException
	 */
	public List<DesignStdWicEntity> getWaterByWic(Connection con, String wic)
			throws SQLException {
		List<DesignStdWicEntity> list = new ArrayList<DesignStdWicEntity>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("IMscWaterDao.getWaterByWic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, wic);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DesignStdWicEntity water = new DesignStdWicEntity();
			water.setWic(rs.getString("WIC"));
			water.setIndexSeq(rs.getString("XH"));
			water.setPressureY(rs.getString("PRESSURE_Y"));
			water.setPressure(rs.getString("PRESSURE"));
			water.setFormula(rs.getString("FORMULA"));
			water.setSaveTime(rs.getString("SAVE_TIME"));
			water.setGetMaxvalue(rs.getString("GET_MAXVALUE"));
			water.setGetMinvalue(rs.getString("GET_MINVALUE"));
			water.setRoundRule(rs.getString("ROUND_RULE"));
			QualityDesignTool.nullConverNullString(water);
			list.add(water);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 保存水压标准
	 * 
	 * @param con
	 * @param water
	 * @param designKey
	 * @throws SQLException
	 */
	public void saveWater(Connection con, DesignStdWicEntity water,
			String designKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("IMscWaterDao.saveWic");
		sql = sql.replaceAll("#DESGIN_KEY#", designKey);
		sql = sql.replaceAll("#WIC#", water.getWic());
		sql = sql.replaceAll("#USE_CODE#", water.getUseCode());
		sql = sql.replaceAll("#USE_DESC#", water.getUseDesc());
		sql = sql.replaceAll("#SPECL_FL#", water.getSpeclFl());
		sql = sql.replaceAll("#INDEX_SEQ#", water.getIndexSeq());
		sql = sql.replaceAll("#PRESSURE_Y#", water.getPressureY());
		sql = sql.replaceAll("#PRESSURE#", water.getPressure());
		sql = sql.replaceAll("#FORMULA#", water.getFormula());
		sql = sql.replaceAll("#SAVE_TIME#", water.getSaveTime());
		sql = sql.replaceAll("#GET_MAXVALUE#", water.getGetMaxvalue());
		sql = sql.replaceAll("#GET_MINVALUE#", water.getGetMinvalue());
		sql = sql.replaceAll("#RATIO_STD#", water.getRatioStd());
		sql = sql.replaceAll("#RATIO_REPLACE#", water.getRatioReplace());
		sql = sql.replaceAll("#MAX_PRESS#", water.getMaxPress());
		sql = sql.replaceAll("#ROUND_RULE#", water.getRoundRule());
		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("IMscWaterDao.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("IMscWaterDao.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("IMscWaterDao.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("IMscWaterDao.getWaterTS");
		b.Fill(new String[]{ designKey });
		GenericEntity ts = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, GenericEntity.class);
		if(ts != null){
			return ts.getValue();
		}else{
			return "";
		}
	}
}
