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.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.craftDesign.model.ComBaseStationRCraftEntity;
import com.steering.pss.sale.order.craftDesign.model.ComMscStationEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscBiltCEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscCftEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscPlGzEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdDicEntity;
import com.steering.pss.sale.order.craftDesign.server.ProDuc;
import com.steering.pss.sale.order.craftDesign.tool.CraftTool;
import com.steering.pss.sale.order.model.ComBaseProcessCEntity;

public class ProDucDao {
	/**
	 * 查询制程
	 * 
	 * @param con
	 *            连接字符串
	 * @param msc
	 *            冶金规范
	 * @return 集合
	 * @throws SQLException
	 */
	public List<CraftOrdDesignMscPlEntity> getProDuc(Connection con, String msc)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getProDuc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, msc);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscPlEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscPlEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (CraftOrdDesignMscPlEntity craftOrdDesignMscPlEntity : list) {
				CraftTool.nullConverNullString(craftOrdDesignMscPlEntity);
			}
			return list;
		}
	}
	
	/**
	 * 查询技术评审制程
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            冶金规范
	 * @return 集合
	 * @throws SQLException
	 */
	public List<CraftOrdDesignMscPlEntity> getTecPline(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getTecPline");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscPlEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscPlEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (CraftOrdDesignMscPlEntity craftOrdDesignMscPlEntity : list) {
				CraftTool.nullConverNullString(craftOrdDesignMscPlEntity);
			}
			return list;
		}
	}
	
	/**
	 * 查询技术评审制程
	 * 
	 * @param con
	 *            连接字符串
	 * @param designKey
	 *            冶金规范
	 * @return 集合
	 * @throws SQLException
	 */
	public List<CraftOrdDesignMscPlEntity> getCraftPline(Connection con, String designKey)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getCraftPline");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscPlEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscPlEntity.class);
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			for (CraftOrdDesignMscPlEntity craftOrdDesignMscPlEntity : list) {
				CraftTool.nullConverNullString(craftOrdDesignMscPlEntity);
			}
			return list;
		}
	}

	/**
	 * 修改制程
	 * 
	 * @param con
	 * @param comMscPl
	 * @throws SQLException
	 */
	public void updateProDuc(Connection con, CraftOrdDesignMscPlEntity comMscPl)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.updateProDuc");
		sql = EntityTool.fillSqlByEntity(sql, comMscPl);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
	}

	/**
	 * 保存制程 至 工艺评审设计-制程表
	 * 
	 * @param con
	 *            连接字符串
	 * @param comBack
	 *            制程实体类
	 * @throws SQLException
	 */
	public void saveProDuc(Connection con, CraftOrdDesignMscPlEntity comMscPl)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.saveProDuc");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, comMscPl.getDesginKey());
		ps.setObject(2, comMscPl.getMscPline());
		ps.setObject(3, comMscPl.getWholeBacklog());
		ps.setObject(4, comMscPl.getWholeBacklogDesc());
		ps.setObject(5, "0");
		ps.setObject(6, comMscPl.getWarnDesc());
		ps.setObject(7, comMscPl.getOrdLnPk());
		ps.setObject(8, comMscPl.getProcessNo());
		ps.setObject(9, comMscPl.getProcessName());
		ps.setObject(10, comMscPl.getAutoCraft());
		ps.executeUpdate();
		ps.close();

	}

	/**
	 * 查询质量设计 成分标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getCraftChem(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getDesignChem");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 理化标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getCraftPhy(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getDesignPhy");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 公差标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getCraftSpec(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getDesignSpec");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 探伤标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getCraftDete(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getDesignDete");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 水压标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getCraftWater(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory
				.getSql("ProDucDao.getDesignWater");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查询质量设计 加工标准条数
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public int getCraftGic(Connection con, String designKey)
			throws SQLException {
		int count = 0;
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getDesignGic");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		return count;
	}

	/**
	 * 查詢附加要求工序点类型
	 * 
	 * @param con
	 *            连接字符串
	 * @param addAskNo
	 *            附加要求
	 * @return List<String>
	 * @throws SQLException
	 */
	public List<String[]> getAddAskNoStation(Connection con, String addAskNo)
			throws SQLException {
		List<String[]> list = new ArrayList<String[]>();
		String sql = CoreXmlSqlParsersFactory
				.getSql("ProDucDao.getAddAskNoStation");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, addAskNo);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			String[] strarray = new String[3];
			strarray[0] = rs.getString("STATION_CODE");
			strarray[1] = rs.getString("STATION_DESC");
			strarray[2] = rs.getString("ASK_SUB_ITME_DESC");
			list.add(strarray);
		}
		rs.close();
		ps.close();
		if (list == null || list.size() == 0) {
			return null;
		} else {
			return list;
		}

	}

	/**
	 * 查询设定制程
	 * 
	 * @param con
	 * @param designKey
	 *            设计designKey
	 * @return
	 * @throws SQLException
	 */
	public List<CraftOrdDesignMscPlEntity> getMscPlSetFl(Connection con,
			String designKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getMscPlSetFl");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscPlEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscPlEntity.class);
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignMscPlEntity designMscPlEntity : list) {
				CraftTool.nullConverNullString(designMscPlEntity);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 验证是否存在该制程
	 * 
	 * @param con
	 * @param designKey
	 * @param mscPline
	 * @return
	 * @throws SQLException
	 */
	public boolean checkProDucIsExist(Connection con, String designKey,
			String mscPline) throws SQLException {
		String sql = CoreXmlSqlParsersFactory
				.getSql("ProDucDao.checkProDucIsExist");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ps.setObject(2, mscPline);
		ResultSet rs = ps.executeQuery();
		int count = 0;
		if (rs.next()) {
			count = rs.getInt("COUNT");
		}
		rs.close();
		ps.close();
		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	

	/**
	 * 查询工艺参数
	 * 
	 * @param con
	 * @param msc
	 * @param mscPline
	 * @return
	 * @throws SQLException
	 */
	public int getCft(Connection con, String msc, String mscPline, String processCode,
			String stationCode, String craftItemCode, List<CraftOrdDesignMscPlGzEntity> grades,
			String dimater, String height, String specCode, String modelCode,
			List<CraftOrdDesignMscCftEntity> allResults) throws SQLException {
		SqlBuilder b;
		if(processCode.equals("A")){
			b = new SqlBuilder("ProDucDao.getCft");
			b.Fill(new String[]{msc, mscPline, processCode, stationCode, craftItemCode});
		}else if(processCode.equals("B") || processCode.equals("C") || processCode.equals("H")){
			b = new SqlBuilder("ProDucDao.getCft2");
			b.Fill(new String[]{msc, mscPline, processCode, stationCode, craftItemCode, specCode});
		}else if(processCode.equals("D") || processCode.equals("E") || processCode.equals("F")){
			b = new SqlBuilder("ProDucDao.getCft3");
			b.Fill(new String[]{msc, mscPline, processCode, stationCode, craftItemCode, dimater, height});
		}else if(processCode.equals("G")){
			if(dimater.equals("")){
				b = new SqlBuilder("ProDucDao.getCft4");
				b.Fill(new String[]{msc, mscPline, processCode, stationCode, craftItemCode, specCode, modelCode});
			}else{
				b = new SqlBuilder("ProDucDao.getCft5");
				b.Fill(new String[]{msc, mscPline, processCode, stationCode, craftItemCode, dimater, height, modelCode});
			}
		}else{
			return 0;
		}
		
		PreparedStatement ps = con.prepareStatement(b.toString());
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignMscCftEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignMscCftEntity.class);
		
		// 钢种过滤
		List<String> gradeNameList = new ArrayList<String>();
		for (CraftOrdDesignMscPlGzEntity grade : grades) {
			gradeNameList.add(grade.getGradename());
		}
		
		int count = 0;
		for (CraftOrdDesignMscCftEntity entity : list) {
			if (!entity.getGradeSeq().equals("")) {
				String[] gradeArray = entity.getGradeSeq().split(";");
				List<String> gradeList = new ArrayList<String>();
				for (String str : gradeArray) {
					gradeList.add(str);
				}
				if (ProDuc.comboList(gradeNameList, gradeList)) {
					boolean isExist = false;
					for(CraftOrdDesignMscCftEntity allResult : allResults){
						if(allResult.getMsc().equals(entity.getMsc())
						   && allResult.getMscPline().equals(entity.getMscPline())
						   && allResult.getStationCode().equals(entity.getStationCode())
						   && allResult.getCraftitemCdoe().equals(entity.getCraftitemCdoe())
						   && allResult.getAllowCount().equals(entity.getAllowCount())){
							isExist = true;
						}
					}
					if(!isExist){
						count++;
						allResults.add(entity);
					}
				}
			}else{
				boolean isExist = false;
				for(CraftOrdDesignMscCftEntity allResult : allResults){
					if(allResult.getMsc().equals(entity.getMsc())
					   && allResult.getMscPline().equals(entity.getMscPline())
					   && allResult.getStationCode().equals(entity.getStationCode())
					   && allResult.getCraftitemCdoe().equals(entity.getCraftitemCdoe())
					   && allResult.getAllowCount().equals(entity.getAllowCount())){
						isExist = true;
					}
				}
				if(!isExist){
					count++;
					allResults.add(entity);
				}
			}
		}
		
		rs.close();
		ps.close();
		
		return count;
	}
	
	public List<ComBaseStationRCraftEntity > getCraftStation(String msc, String mscPline,
			String processCode, Connection con){
		SqlBuilder b = new SqlBuilder("ProDucDao.getCraftStation");
		b.Fill(new String[]{msc, mscPline, processCode});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, ComBaseStationRCraftEntity.class);
	}
	
	public List<CraftOrdDesignMscBiltCEntity> getProcessSpec(String designKey, String mscPline, Connection con){
		SqlBuilder b = new SqlBuilder("ProDucDao.getProcessSpec");
		b.Fill(new String[]{designKey, mscPline});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignMscBiltCEntity.class);
	}

	/**
	 * 保存工艺参数
	 * 
	 * @param con
	 * @param cftEntity
	 * @throws SQLException
	 */
	public void saveCft(Connection con, CraftOrdDesignMscCftEntity cftEntity)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.saveCft");
		sql = EntityTool.fillSqlByEntity(sql, cftEntity);
		PreparedStatement ps = con.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();

	}

	/**
	 * 查询探伤检验项
	 * 
	 * @param con
	 * @param designKey
	 * @return
	 * @throws SQLException
	 */
	public List<CraftOrdDesignStdDicEntity> getDeteInfo(Connection con,
			String designKey) throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getDeteInfo");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, designKey);
		ResultSet rs = ps.executeQuery();
		List<CraftOrdDesignStdDicEntity> list = EntityTool.getEntitysByRs(rs,
				CraftOrdDesignStdDicEntity.class);
		rs.close();
		ps.close();
		if (list != null && list.size() > 0) {
			for (CraftOrdDesignStdDicEntity dete : list) {
				CraftTool.nullConverNullString(dete);
			}
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 查询扣型分类
	 * 
	 * @param con
	 * @param modelCode
	 * @return
	 * @throws SQLException
	 */
	public String[] getModelStyle(Connection con, String modelCode)
			throws SQLException {
		String sql = CoreXmlSqlParsersFactory.getSql("ProDucDao.getModelStyle");
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setObject(1, modelCode);
		String[] modelStyle = new String[2];
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			if (rs.getString("MODEL_STYLE") != null) {
				modelStyle[0] = rs.getString("MODEL_STYLE");
				modelStyle[1] = rs.getString("MODEL_STYLE_DESC");
			}
		}
		rs.close();
		ps.close();
		return modelStyle;
	}
	
	public List<CraftOrdDesignMscPlGzEntity> getSlmDesignGrade(String designKey, String mscPline, Connection con){
		SqlBuilder b = new SqlBuilder("ProDucDao.getSLmDesignGrade");
		b.Fill(new String[]{designKey, mscPline});
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignMscPlGzEntity.class);
	}
	
	/**
	 * 获取二级工序描述
	 * @param con
	 * @param processCodeC
	 * @return
	 */
	public String getProcessDescC(Connection con, String processCodeC){
		SqlBuilder b = new SqlBuilder("ProDucDao.getProcessDescC");
		b.Fill(new String[]{ processCodeC });
		ComBaseProcessCEntity processCDesc = EntityTool.getEntityBySqlNoClsCon(
				b.toString(), con, ComBaseProcessCEntity.class);
		return processCDesc.getProcessDescC();
	}
}
