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

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdCicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdPicSmpEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignStdWicEntity;

public class CraftOrdFdDao {
	
	public static boolean existFdPicAddAsk(Connection con, String ordLnPk) throws SQLException{
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getFdPicAddAskCount");
		b.Fill(new String[]{ ordLnPk });
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(b.toString());
		rs.next();
		if(rs.getInt("FD_PIC_COUNT") > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean existFdWicAddAsk(Connection con, String ordLnPk) throws SQLException{
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getFdWicAddAskCount");
		b.Fill(new String[]{ ordLnPk });
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(b.toString());
		rs.next();
		if(rs.getInt("FD_WIC_COUNT") > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean existFdCicAddAsk(Connection con, String ordLnPk) throws SQLException{
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getFdCicAddAskCount");
		b.Fill(new String[]{ ordLnPk });
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(b.toString());
		rs.next();
		if(rs.getInt("FD_CIC_COUNT") > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public static ArrayList<CraftOrdDesignStdPicEntity> getCraftOrdFdPics(Connection con, String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getCraftOrdFdPics");
		b.Fill(new String[]{ ordLnPk });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdPicEntity.class);
	}
	
	public static ArrayList<CraftOrdDesignStdPicSmpEntity> getCraftOrdFdPicSmps(Connection con, String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getCraftOrdFdPicSmps");
		b.Fill(new String[]{ ordLnPk });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdPicSmpEntity.class);
	}
	
	public static ArrayList<CraftOrdDesignStdWicEntity> getCraftOrdFdWics(Connection con, String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getCraftOrdFdWics");
		b.Fill(new String[]{ ordLnPk });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdWicEntity.class);
	}
	
	public static ArrayList<CraftOrdDesignStdCicEntity> getCraftOrdFdCics(Connection con, String ordLnPk){
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getCraftOrdFdCics");
		b.Fill(new String[]{ ordLnPk });
		return EntityTool.getEntitysBySqlNoClsCon(b.toString(), con, CraftOrdDesignStdCicEntity.class);
	}
	
	public static void insertCraftOrdStdPic(Connection con, ArrayList<CraftOrdDesignStdPicEntity> craftFdPics,
			String designKey) throws SQLException{
		Statement st = con.createStatement();
		for(CraftOrdDesignStdPicEntity craftFdPic : craftFdPics){
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdFdDao.insertCraftOrdStdPic");
			craftFdPic.setDesginKey(designKey);
			craftFdPic.setPic("PIC");
			craftFdPic.setSpeclFl("2");
			craftFdPic.setCraftSource("2");
			sql = EntityTool.fillSqlByEntity(sql, craftFdPic);
			st.executeUpdate(sql);
		}
		st.close();
	}
	
	public static void insertCraftOrdStdPicSmp(Connection con, ArrayList<CraftOrdDesignStdPicSmpEntity> fdPicSmps,
			String designKey) throws SQLException{
		Statement st = con.createStatement();
		for(CraftOrdDesignStdPicSmpEntity fdPicSmp : fdPicSmps){
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdFdDao.insertCraftOrdStdPicSmp");
			fdPicSmp.setDesginKey(designKey);
			fdPicSmp.setPic("PIC");
			fdPicSmp.setCraftSource("2");
			if(fdPicSmp.getCommon().equals("")){
				fdPicSmp.setCommon("0");
			}
			sql = EntityTool.fillSqlByEntity(sql, fdPicSmp);
			st.executeUpdate(sql);
		}
		st.close();
	}
	
	public static String insertCraftOrdStdWic(Connection con, ArrayList<CraftOrdDesignStdWicEntity> craftFdWics,
			String designKey) throws Exception {
		Statement st = con.createStatement();
		for(CraftOrdDesignStdWicEntity craftFdWic : craftFdWics){
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdFdDao.insertCraftOrdStdWic");
			craftFdWic.setDesginKey(designKey);
			craftFdWic.setWic("WIC");
			craftFdWic.setSpeclFl("2");
			craftFdWic.setCraftSource("2");
			if(craftFdWic.getOutputStyle().equals("")){
				return "请选择工艺文件输出方式";
			}
			if(craftFdWic.getOutputStyle().equals("410701")){
				craftFdWic.setPressureY("");
			}else if(craftFdWic.getOutputStyle().equals("410702")){
				craftFdWic.setPressure("");
			}
			sql = EntityTool.fillSqlByEntity(sql, craftFdWic);
			st.executeUpdate(sql);
		}
		st.close();

		return "";
	}
	
	public static void insertCraftOrdStdCic(Connection con, ArrayList<CraftOrdDesignStdCicEntity> craftFdCics,
			String designKey) throws SQLException{
		Statement st = con.createStatement();
		for(CraftOrdDesignStdCicEntity craftFdCic : craftFdCics){
			String sql = CoreXmlSqlParsersFactory.getSql("CraftOrdFdDao.insertCraftOrdStdCic");
			craftFdCic.setDesginKey(designKey);
			craftFdCic.setCic("CIC");
			craftFdCic.setSpeclFl("2");
			craftFdCic.setCraftSource("2");
			if(craftFdCic.getCommon().equals("")){
				craftFdCic.setCommon("0");
			}
			sql = EntityTool.fillSqlByEntity(sql, craftFdCic);
			st.executeUpdate(sql);
		}
		st.close();
	}
	
	public static String getPicFdCheck(String designKey, Connection con) throws SQLException{
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getPicFdCheck");
		b.Fill(new String[]{ designKey });
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(b.toString());
		rs.next();
		String fdCheck = rs.getString("PIC_FD_CHECK");
		rs.close();
		st.close();
		
		return fdCheck;
	}
	
	public static String getWicFdCheck(String designKey, Connection con) throws SQLException{
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getWicFdCheck");
		b.Fill(new String[]{ designKey });
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(b.toString());
		rs.next();
		String fdCheck = rs.getString("WIC_FD_CHECK");
		rs.close();
		st.close();
		
		return fdCheck;
	}
	
	public static String getCicFdCheck(String designKey, Connection con) throws SQLException{
		SqlBuilder b = new SqlBuilder("CraftOrdFdDao.getCicFdCheck");
		b.Fill(new String[]{ designKey });
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(b.toString());
		rs.next();
		String fdCheck = rs.getString("CIC_FD_CHECK");
		rs.close();
		st.close();
		
		return fdCheck;
	}
}
