package com.steering.pss.sale.order.craftDesign.dao;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.Util.StringUtils;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.craftDesign.model.*;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SpecManageDao {
	/**
	 * 查询轧管规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @param gradeCode
	 *            钢种
	 * @param PlineCode
	 *            二级工序
	 * @return ComMscRollRUpsetEntity
	 * @throws SQLException
	 */
	public List<ComMscRollRUpsetEntity> getRollSpec(Connection con,
			String dimater, String height, String gradeCode, String PlineCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("SpecManageDao.getRollSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, PlineCode);
		//ps.setObject(2, gradeCode);
		ps.setObject(2, dimater);
		ps.setObject(3, height);
		ResultSet rs = ps.executeQuery();
		List<ComMscRollRUpsetEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscRollRUpsetEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscRollRUpsetEntity comMscRollRUpsetEntity : list) {
				CraftTool.nullConverNullString(comMscRollRUpsetEntity);
			}
			return list;
		} else {
			return null;
		}
	}
	
	/**
	 * 获取设定的轧管规格
	 * @param con
	 * @param ordLnPk
	 * @param mscPline
	 * @param gradeCode
	 * @return
	 */
	public List<ComMscRollRUpsetEntity> getRollSpecSet(Connection con,
			String ordLnPk, String mscPline, String gradeCode){
		SqlBuilder b = new SqlBuilder("SpecManageDao.getRollSpecS");
		b.Fill(new String[]{ ordLnPk, mscPline});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, ComMscRollRUpsetEntity.class);
	}

	/**
	 * 保存轧管规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param designMscGzGgEntity
	 *            实体类
	 * @throws SQLException
	 */
	public void saveRollSpec(Connection con,
			CraftOrdDesignMscGzGgEntity mscGzGgEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("SpecManageDao.saveRollSpec");
		sql = EntityTool.fillSqlByEntity(sql, mscGzGgEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询锻造规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return List<String[]>
	 * @throws SQLException
	 */
	public List<String[]> getBiltSpec(Connection con, String dimater,
									  String height, String outsStlPipeFl) throws SQLException {
		return getBiltSpec(con,dimater,height,outsStlPipeFl,null);
	}
	public List<String[]> getBiltSpec(Connection con, String dimater,
			String height, String outsStlPipeFl, String supp) throws SQLException {
		List<String[]> list = new ArrayList<String[]>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("SpecManageDao.getBiltSpec");
		if(!StringUtils.isEmpty(supp)){
			sql += " AND T1.SUPPLY_UNIT_CODE = '" + supp +"'";
		}
		if (outsStlPipeFl.equals("121902")||outsStlPipeFl.equals("121903")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("SpecManageDao.getBiltSpecOut");
		}

		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dimater);
		ps.setObject(2, dimater);
		ps.setObject(3, height);
		ps.setObject(4, height);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String[] strArray = new String[2];
			strArray[0] = rs.getString("SPEC_CODE");
			strArray[1] = rs.getString("SPEC_NAME");
			list.add(strArray);
		}
		rs.close();
		ps.close();
		return list;
	}

	/**
	 * 保存锻造规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param CraftOrdDesignMscBiltEntity
	 *            实体类
	 * @throws SQLException
	 */
	public void saveBiltSpec(Connection con,
			CraftOrdDesignMscBiltEntity mscBiltEntity) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("SpecManageDao.saveBiltSpec");
		sql = EntityTool.fillSqlByEntity(sql, mscBiltEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 查询铸造与锻造规格对应关系
	 * 
	 * @param con
	 *            连接字符串
	 * @param SpecCode
	 *            断面规格
	 * @return ComMscCastRForgEntity
	 * @throws SQLException
	 */
	public List<ComMscCastRForgEntity> getBiltCSpec(Connection con,
			String SpecCode, String flag) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("SpecManageDao.getBiltCSpecA");
		if (!flag.equals("B002")) {
			sql = CoreXmlSqlParsersFactory
					.getSql("SpecManageDao.getBiltCSpecB");
		}
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, SpecCode);
		ResultSet rs = ps.executeQuery();
		List<ComMscCastRForgEntity> list = EntityTool.getEntitysByRs(rs,
				ComMscCastRForgEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (ComMscCastRForgEntity comMscCastRForgEntity : list) {
				CraftTool.nullConverNullString(comMscCastRForgEntity);
			}
			return list;
		} else {
			return null;
		}

	}

	/**
	 * 保存坯料规格
	 * 
	 * @param con
	 *            连接字符串
	 * @param designMscBiltCEntity
	 *            实体类
	 * @throws SQLException
	 */
	public void saveBiltCSpec(Connection con,
			CraftOrdDesignMscBiltCEntity BiltSpecs) throws SQLException {
		if (BiltSpecs == null) {
			return;
		}
		BiltSpecs.setSpecCodeS(BiltSpecs.getSpecCode()
				+ BiltSpecs.getBSpecCode() + BiltSpecs.getCSpecCode());
		String sql = CoreXmlSqlParsersFactory
				.getSql("SpecManageDao.saveBiltCSpec");
		sql = EntityTool.fillSqlByEntity(sql, BiltSpecs);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 根据外径、壁厚查询规格代码
	 * 
	 * @param con
	 *            连接字符串
	 * @param dimater
	 *            外径
	 * @param height
	 *            壁厚
	 * @return ComBaseSpecEntity
	 * @throws SQLException
	 */
	public List<ComBaseSpecEntity> getBaseSpec(Connection con, String dimater,
			String height) throws SQLException {

		String sql = CoreXmlSqlParsersFactory
				.getSql("SpecManageDao.getBaseSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, dimater);
		ps.setObject(2, height);
		ResultSet rs = ps.executeQuery();
		List<ComBaseSpecEntity> list = EntityTool.getEntitysByRs(rs,
				ComBaseSpecEntity.class);
		if (list != null && list.size() > 0) {
			for (ComBaseSpecEntity comBaseSpecEntity : list) {
				CraftTool.nullConverNullString(comBaseSpecEntity);
			}

			return list;
		} else {
			return null;
		}

	}
	
	/**
	 * 获取技术评审规格
	 * @param designKey
	 * @param mscPline
	 * @param con
	 * @return
	 */
	public static SpecEntity getSlmDesignSpec(String designKey, String mscPline, Connection con){
		SqlBuilder b = new SqlBuilder("SpecManageDao.getSlmDesignZgSpec");
		b.Fill(new String[]{ designKey, mscPline });
		List<CraftOrdDesignMscGzGgEntity> zgSpecs = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignMscGzGgEntity.class);
		
		b = new SqlBuilder("SpecManageDao.getSlmDesignDzSpec");
		b.Fill(new String[]{ designKey, mscPline });
		List<CraftOrdDesignMscBiltEntity> dzSpecs = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignMscBiltEntity.class);
		
		b = new SqlBuilder("SpecManageDao.getSlmDesignZzSpec");
		b.Fill(new String[]{ designKey, mscPline });
		List<CraftOrdDesignMscBiltCEntity> zzSpecs = EntityTool.getEntitysBySqlNoClsCon(
				b.toString(), con, CraftOrdDesignMscBiltCEntity.class);
		
		SpecEntity spec = new SpecEntity();
		spec.setRollSpecs(zgSpecs);
		spec.setBillSpecs(dzSpecs);
		spec.setBillSpecCs(zzSpecs);
		
		return spec;
	}
}
