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

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

import com.steering.pss.sale.order.craftDesign.dao.CraftOrdFdDao;
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 CraftOrdFd {
	
	/**
	 * 检查是否需要首日检
	 * @param con
	 * @param designKey
	 * @param ordLnPk
	 * @return
	 * @throws SQLException
	 */
	public static String[] checkCraftOrdFd(Connection con, String designKey, String ordLnPk) throws Exception {
		String[] results = new String[]{"", ""};
		boolean existFdPicAddAsk = CraftOrdFdDao.existFdPicAddAsk(con, ordLnPk);
		boolean existFdWicAddAsk = CraftOrdFdDao.existFdWicAddAsk(con, ordLnPk);
		boolean existFdCicAddAsk = CraftOrdFdDao.existFdCicAddAsk(con, ordLnPk);
		
		String picFdCheck = CraftOrdFdDao.getPicFdCheck(designKey, con);
		String wicFdCheck = CraftOrdFdDao.getWicFdCheck(designKey, con);
		String cicFdCheck = CraftOrdFdDao.getCicFdCheck(designKey, con);
		if(!existFdPicAddAsk && !existFdWicAddAsk && !existFdCicAddAsk && picFdCheck.equals("0")
				&& wicFdCheck.equals("0") && cicFdCheck.equals("0")){
			return results;
		}
		if(existFdPicAddAsk || picFdCheck.equals("1")){
			ArrayList<CraftOrdDesignStdPicEntity> craftOrdFdPics = CraftOrdFdDao.getCraftOrdFdPics(con, ordLnPk);
			if(craftOrdFdPics.size() == 0){
				results[0] = "请维护理化首日检！";
				results[1] = "Pic";
				return results;
			}
			
			ArrayList<CraftOrdDesignStdPicSmpEntity> fdPicSmps = CraftOrdFdDao.getCraftOrdFdPicSmps(con, ordLnPk);
			
			CraftOrdFdDao.insertCraftOrdStdPic(con, craftOrdFdPics, designKey);
			CraftOrdFdDao.insertCraftOrdStdPicSmp(con, fdPicSmps, designKey);
		}
		
		if(existFdWicAddAsk || wicFdCheck.equals("1")){
			ArrayList<CraftOrdDesignStdWicEntity> craftOrdFdWics = CraftOrdFdDao.getCraftOrdFdWics(con, ordLnPk);
			if(craftOrdFdWics.size() == 0){
				results[0] = "请维护水压首日检！";
				results[1] = "Wic";
				return results;
			}
			String wicMsg = CraftOrdFdDao.insertCraftOrdStdWic(con, craftOrdFdWics, designKey);
			if(!wicMsg.equals("")){
				results[0] = wicMsg;
				results[1] = "Wic";
				return results;
			}
		}
		
		if(existFdCicAddAsk || cicFdCheck.equals("1")){
			ArrayList<CraftOrdDesignStdCicEntity> craftOrdFdCics = CraftOrdFdDao.getCraftOrdFdCics(con, ordLnPk);
			if(craftOrdFdCics.size() == 0){
				results[0] = "请维护成分首日检！";
				results[1] = "Cic";
				return results;
			}
			CraftOrdFdDao.insertCraftOrdStdCic(con, craftOrdFdCics, designKey);
		}
		
		results[0] = "True";
		return results;
	}
}
