package com.steering.pss.sale.order.qualityDesign.server.iserver;

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

import com.steering.pss.sale.order.model.SlmBaseAddAskEntity;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.AddAskDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.ComBaseAdditionalEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignAddAskStsEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrderAddAskEntity;
import com.steering.pss.sale.order.qualityDesign.server.imp.AddAskImp;

/**
 * 附加要求业务类
 * 
 * @author Administrator
 * 
 */
public class IAddAsk implements AddAskImp {
	/**
	 * 验证附加要求(如果返回的信息为空,则表示通过验证)
	 * 
	 * @param con
	 *            连接字符串
	 * @param designEntity
	 *            设计类
	 * @return String
	 * @throws SQLException
	 */
	public String checkAddAsk(Connection con, String addAskNo, String stdCode, String ordLnPk)
			throws SQLException {
		String message = "";
		AddAskDaoImp addAskDaoImp = FactoryDao.getAddAskDaoImp();
		
		if(!addAskNo.equals("")){
			List<String> addAskStatus = addAskDaoImp.getAddAskStatus(con,
					addAskNo);
			if (addAskStatus == null || addAskStatus.size() == 0) {
				message = "合同有附加要求,等人工确认";
				return message;
			}
			for (String Status : addAskStatus) {
				if (!Status.equals("S")) {
					message = "合同有附加要求,等人工确认";
					return message;
				}
			}
		}
		
		//如果销售附加要求与标准附加要求不一致，并且只要有一个标准说明没有评审就需要触发人工评审。
		ArrayList<SlmOrderAddAskEntity> orderAddAsks = addAskDaoImp.getOrderAddAsk(con, addAskNo);
		ArrayList<ComBaseAdditionalEntity> stdAddAsks = addAskDaoImp.getStdAddAsk(con, stdCode);
		for(SlmOrderAddAskEntity orderAddAsk : orderAddAsks){
			boolean contains = false;
			for(ComBaseAdditionalEntity stdAddAsk : stdAddAsks){
				if(orderAddAsk.getAskSubItmeVal().equals(stdAddAsk.getAskSubItmeVal())){
					contains = true;
				}
			}
			if(!contains && stdAddAsks.size() > 0){
				SlmBaseAddAskEntity baseAddAsk = addAskDaoImp.getBaseAddAsk(
						con, orderAddAsk.getAskSubItmeVal());
				if(baseAddAsk == null || baseAddAsk.getStdflag().equals("0")){
					SlmOrdDesignAddAskStsEntity designAddAskSts = addAskDaoImp.getDesignAddAskSts(
							con, ordLnPk);
					if(designAddAskSts == null || designAddAskSts.getState().equals("0")){
						message = "合同附加要求与标准附加要求不一致，并且标准说明没通过审核，需要人工确认";
						return message;
					}
				}
			}
		}
		
		return message;
	}

}
