package market.Qms.slmord.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import market.Api.Slm.impl.SlmApiServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.ComBaseDeliverystateRPpcMapper;
import market.Qms.com.service.impl.ComBaseConfigServiceImpl;
import market.Qms.com.vo.ComBaseDeliverystateRPpc;
import market.Qms.common.DataHelper;
import market.Qms.common.DataHelper.Enum_StationType;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComBaseAskRStationMapper;
import market.Qms.metallurgy.mapper.ComMscBacklogMapper;
import market.Qms.metallurgy.mapper.ComMscProcessMapper;
import market.Qms.metallurgy.mapper.ComMscStationMapper;
import market.Qms.metallurgy.vo.ComBaseAskRStation;
import market.Qms.metallurgy.vo.ComMscBacklog;
import market.Qms.metallurgy.vo.ComMscProcess;
import market.Qms.metallurgy.vo.ComMscStation;
import market.Qms.qcm.service.impl.ComMscExemptionServiceImpl;
import market.Qms.qcm.vo.ComMscExemption;
import market.Qms.slmord.mapper.SlmOrdDesignAdditionalMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlPrcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlStiMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscSetProcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.service.QualityDesignProcedureService;
import market.Qms.slmord.vo.SlmOrdDesignAdditional;
import market.Qms.slmord.vo.SlmOrdDesignMscPl;
import market.Qms.slmord.vo.SlmOrdDesignMscPlPrc;
import market.Qms.slmord.vo.SlmOrdDesignMscPlSti;
import market.Qms.slmord.vo.SlmOrdDesignMscSetProc;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Slm.ContractManagement.vo.SlmContractBaseStation;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
@RemoteService(serviceInterface=QualityDesignProcedureService.class)
public class QualityDesignProcedureServiceImpl extends BaseServiceImpl<Object,Object> implements QualityDesignProcedureService {

	/**
	 * 过滤制程适用成品规格
	 * @param backlogList
	 * @param orderDeliveryCode
	 * @return
	 */
	private List<ComMscBacklog> ValidationOrderSpec(List<ComMscBacklog> backlogList,SlmContractProcessPlan design){
		List<ComMscBacklog> list=new ArrayList<>();
		for (ComMscBacklog comMscBacklog : backlogList) {
			//过滤适用成品厚度上限
			boolean pass=DataHelper.checkSignAndValue(comMscBacklog.getThick_max_sign(), comMscBacklog.getThick_max(), design.getThick().toString());
			if(!pass){
				continue;
			}
			//过滤适用成品厚度下限
			pass=DataHelper.checkSignAndValue(comMscBacklog.getThick_min_sign(), comMscBacklog.getThick_min(), design.getThick().toString());
			if(!pass){
				continue;
			}
			//过滤适用成品宽度上限
			pass=DataHelper.checkSignAndValue(comMscBacklog.getWidth_max_sign(), comMscBacklog.getWidth_max(), design.getWidth().toString());
			if(!pass){
				continue;
			}
			//过滤适用成品宽度下限
			pass=DataHelper.checkSignAndValue(comMscBacklog.getWidth_min_sign(), comMscBacklog.getWidth_min(), design.getWidth().toString());
			if(!pass){
				continue;
			}
			//过滤适用成品长度上限
			pass=DataHelper.checkSignAndValue(comMscBacklog.getLenth_max_sign(), comMscBacklog.getLenth_max(), DataHelper.GetString(design.getLength()));
			if(!pass){
				continue;
			}
			//过滤适用成品长度下限
			pass=DataHelper.checkSignAndValue(comMscBacklog.getLenth_min_sign(), comMscBacklog.getLenth_min(), DataHelper.GetString(design.getLength()));
			if(!pass){
				continue;
			}
			list.add(comMscBacklog);
		}
		return list;
	}
	
	/**
	 * 过滤制程适用交货状态
	 * @param backlogList
	 * @param orderDeliveryCode
	 * @return
	 */
	private List<ComMscBacklog> ValidationDelivery(List<ComMscBacklog> backlogList,String orderDeliveryCode){
		List<ComMscBacklog> list=new ArrayList<>();
		for (ComMscBacklog comMscBacklog : backlogList) {
			if(!DataHelper.IsNullOrSpace(comMscBacklog.getDelivery_state_code())){
				if(comMscBacklog.getDelivery_state_code().contains(orderDeliveryCode)){
					list.add(comMscBacklog);
				}
			}else{
				list.add(comMscBacklog);
			}
		}
		
		return list;
	}
	
	@Override
	public List<SlmOrdDesignMscPl> ValidationMscProduc(ReturnMsg msg,String designKey, SlmContractProcessPlan design) {
		List<SlmOrdDesignMscPl> checkedProDucs = new ArrayList<SlmOrdDesignMscPl>();
		ComMscBacklogMapper backlogMapper = this.getMapper(ComMscBacklogMapper.class);

		// 查询制造规范码的制程
		ComMscBacklog _backLog = new ComMscBacklog();
		if (design.getMsc_n() != null) {
			_backLog.setMsc(design.getMsc_n());
		} else {
			_backLog.setMsc(design.getMsc());
		}

		// 验证制程中工序是否存在工序点
		ComMscProcessMapper processMapper = this.getMapper(ComMscProcessMapper.class);
		ComMscStationMapper stationMapper = this.getMapper(ComMscStationMapper.class);
		ComBaseAskRStationMapper rstationMapper = this.getMapper(ComBaseAskRStationMapper.class);
		SlmOrdDesignAdditionalMapper ordAddMapper = this.getMapper(SlmOrdDesignAdditionalMapper.class);
		ComMscProcess _process = new ComMscProcess();
		_process.setMsc(_backLog.getMsc());
		_process.setValidflag("1");
		ComMscStation _station = new ComMscStation();
		_station.setMsc(_backLog.getMsc());
		_station.setValidflag("1");
		// 目标工序点
		List<SlmOrdDesignMscPlSti> listStiTarget = new ArrayList<>();
		List<SlmOrdDesignMscPlPrc> listPrcTarget = new ArrayList<>();
		SlmOrdDesignMscPlPrcMapper prcMapper = this.getMapper(SlmOrdDesignMscPlPrcMapper.class);
		SlmOrdDesignMscPlStiMapper stiMapper = this.getMapper(SlmOrdDesignMscPlStiMapper.class);
		//配置项，判断是需要完全匹配还是包含关系
		String con=this.getBean(ComBaseConfigServiceImpl.class).getConfig("DSK0005");
		
		List<ComMscBacklog> proDucs = new ArrayList<>();
		List<SlmOrdDesignMscSetProc> proc = this.getMapper(SlmOrdDesignMscSetProcMapper.class).findWithCondition(null,
				"CONTRACT_LINE_NO='" + design.getContract_line_no() + "'", null, 0, 0);
		if (proc != null && proc.size() > 0) {
		    for (SlmOrdDesignMscSetProc msc_pline : proc) {
			  List<ComMscBacklog> logs = this.getMapper(ComMscBacklogMapper.class).findWithCondition(null,
						"msc='" + _backLog.getMsc() + "' and msc_pline='" + msc_pline.getMsc_pline() + "' and VALIDFLAG='1'", null, 0,
						0);
			  if (logs == null || logs.size() < 1) {
					msg.setMessage("制造规范码‘" + _backLog.getMsc() + "’不存在或已作废指定的制程：[" + msc_pline.getMsc_pline() + "]");
					checkedProDucs.clear();
					return checkedProDucs;
				} else {
					for (ComMscBacklog comMscBacklog : logs) {
						SlmOrdDesignMscPl _ordMscPl = new SlmOrdDesignMscPl();
						try {
							DataHelper.EntityCopy(comMscBacklog, _ordMscPl);
							_ordMscPl.setDesign_key(designKey);
							this.getMapper(SlmOrdDesignMscPlMapper.class).insert(_ordMscPl);
						} catch (Exception ex) {
							msg.setMessage("制程中添加可指定制程失败：" + ex.getMessage());
							checkedProDucs.clear();
							return checkedProDucs;
						}
					}
				}
			
		    }

			for (SlmOrdDesignMscSetProc msc_pline : proc) {
				List<ComMscBacklog> logs = this.getMapper(ComMscBacklogMapper.class).findWithCondition(null,
						"msc='" + _backLog.getMsc() + "' and msc_pline='" + msc_pline.getMsc_pline() + "' and VALIDFLAG='1'", null, 0,
						0);
				if (logs == null || logs.size() < 1) {
					msg.setMessage("制造规范码‘" + _backLog.getMsc() + "’不存在或已作废指定的制程：[" + msc_pline.getMsc_pline() + "]");
					checkedProDucs.clear();
					return checkedProDucs;
				} else {
					proDucs.addAll(logs);
				}
			}

			// 验证指定的制程，必须保证每条都满足
			for (ComMscBacklog comMscBacklog : proDucs) {
				// 验证交货状态
				if (comMscBacklog.getDelivery_state_code() != null
						&& !comMscBacklog.getDelivery_state_code().contains(design.getDelivery_state_code())) {

					msg.setMessage("制造规范码：[" + _backLog.getMsc() + "]向下指定制程：[" + comMscBacklog.getMsc_pline()
							+ "]不满足订单交货状态：[" + design.getDelivery_state_desc() + "]的制程信息，请确认!");
					checkedProDucs.clear();
					return checkedProDucs;
				}

				// 验证规格

				List<ComMscBacklog> random = new ArrayList<ComMscBacklog>();
				random.add(comMscBacklog);
				random = ValidationOrderSpec(random, design);
				if (random.size() < 1) {
					msg.setMessage(
							"制造规范码[" + _backLog.getMsc() + "]向下指定制程：[" + comMscBacklog.getMsc_pline() + "]不符合订单规格");
					checkedProDucs.clear();
					return checkedProDucs;
				}
				// 验证工序是否存在
				List<ComMscStation> allStation = new ArrayList<>();
				_process.setMsc_pline(comMscBacklog.getMsc_pline());
				_station.setMsc_pline(comMscBacklog.getMsc_pline());
				List<ComMscProcess> listProcess = processMapper.find(_process, 0, 0);
				if (listProcess.size() < 1) {
					msg.setMessage(
							"制造规范码：‘" + comMscBacklog.getMsc() + "’中，指定制程：‘" + comMscBacklog.getMsc_pline() + "’无工序");
					checkedProDucs.clear();
					return checkedProDucs;
				}
				// 验证工序点是否存在
				ComMscStation station = new ComMscStation();
				station.setMsc(comMscBacklog.getMsc());
				station.setMsc_pline(comMscBacklog.getMsc_pline());
				allStation = stationMapper.find(station, 0, 0);
				// 验证工序中是否有工序点123
				boolean pass = checkStation(msg, checkedProDucs, stationMapper, _station, comMscBacklog, listProcess);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;
				}
				// 交互标准的双向验证
				pass = checkStardand(msg, designKey, design, checkedProDucs, comMscBacklog, allStation);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;

				}

				// 根据合同行号获取工艺要求集合

				String _sta = null;
				for (ComMscStation comMscStation : allStation) {
					_sta += comMscStation.getProcess_point_code() + ",";
				}
				pass = checkGongYi(msg, design, checkedProDucs, comMscBacklog, _sta);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;
				}

				// 验证制程的工序点是否交货状态关联的工序点
				pass = checkDelivery(msg, design, checkedProDucs, comMscBacklog, _sta);
				if (!pass) {

					checkedProDucs.clear();
					return checkedProDucs;

				}

				// 获取合同关联的附加要求项
				pass = checkAddRequire(msg, designKey, checkedProDucs, rstationMapper, comMscBacklog, allStation);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;
				}

				pass = copyProcess(msg, designKey, checkedProDucs, listPrcTarget, comMscBacklog, listProcess);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;

				}
				// 新增质量设计制程全工序点信息
				pass = copyStation(msg, designKey, checkedProDucs, listStiTarget, comMscBacklog, allStation);
				if (!pass) {

					checkedProDucs.clear();
					return checkedProDucs;

				}

			}

		} else {

			proDucs = backlogMapper.find(_backLog, 0, 0);
			for (int i = 0; i < proDucs.size(); i++) {
				if (proDucs.get(i).getValidflag().equals("0")) {// 去除无效制程
					proDucs.remove(i);
					i--;
				}
			}
			// 分析存在制程
			if (proDucs.size() == 0) {
				msg.setMessage("制造规范码‘" + _backLog.getMsc() + "’缺少有效制程");
				return checkedProDucs;
			}
			//过滤未包含客户指定的工艺工序信息
			List<SlmContractBaseStation> stList=null;
			try {
				stList = this.getBean(SlmApiServiceImpl.class)
						.getSlmContractBaseStationByContractLineNo(design.getContract_line_no());
				
			} catch (Exception e) {
				msg.setMessage("调用销售查询工艺要求接口失败");
				checkedProDucs.clear();
				return checkedProDucs;
			}
			if (stList!=null && stList.size()>0) {
				//查询全程产线号向下的工序信息
				for (int i = 0; i <proDucs.size(); i++) {
					//获取制程向下的工序信息
					String process_names="";
					List<ComMscProcess> po=this.getMapper(ComMscProcessMapper.class).findWithCondition(null, "MSC='"+proDucs.get(i).getMsc()+"' and MSC_PLINE='"+proDucs.get(i).getMsc_pline()+"' and VALIDFLAG='1'", "BACKLOG_SEQ", 0, 0);
					for (ComMscProcess comMscProcess : po) {
						process_names+=comMscProcess.getProcess_code()+",";
					}
					if ("1".equals(con)) {
						
						if (stList.size()!=po.size()) {
							proDucs.remove(i);
							i--;
							continue;
						}else{
							String oldString="";
							for (SlmContractBaseStation st : stList) {
								oldString+=st.getProcess_code()+",";	
							}
							if (!process_names.equals(oldString)) {
								proDucs.remove(i);
								i--;
								continue;
							}
							
						}
						
						
						
					}else{
						
						
						for (SlmContractBaseStation st : stList) {
							if (!process_names.contains(st.getProcess_code())) {
								proDucs.remove(i);
								i--;
								break;
							}
						}
					
						
					}
					
				
				}
				
					
			}
			
			// 分析存在制程
			if (proDucs.size() == 0) {
				msg.setMessage("制造规范码‘" + _backLog.getMsc() + "’未查询到符合订单全部工艺要求的制程");
				return checkedProDucs;
			}

			

			// 过滤制程适用交货状态
			proDucs = ValidationDelivery(proDucs, design.getDelivery_state_code());
			if (proDucs.size() < 1) {
				msg.setMessage("制造规范码：[" + _backLog.getMsc() + "]向下未找到适用订单交货状态：[" + design.getDelivery_state_desc()
						+ "]的制程信息，请确认!");
				return checkedProDucs;
			}

			// 过滤适用成品规格
			proDucs = ValidationOrderSpec(proDucs, design);
			if (proDucs.size() < 1) {
				msg.setMessage("制造规范码[" + _backLog.getMsc() + "]下的制程中，根据合同订货产品规格找不到合适的制程");
				return checkedProDucs;
			}

			for (ComMscBacklog comMscBacklog : proDucs) {
				SlmOrdDesignMscPl _ordMscPl = new SlmOrdDesignMscPl();
				try {
					DataHelper.EntityCopy(comMscBacklog, _ordMscPl);
					_ordMscPl.setDesign_key(designKey);
					this.getMapper(SlmOrdDesignMscPlMapper.class).insert(_ordMscPl);
				} catch (Exception ex) {
					msg.setMessage("制程中添加可指定制程失败：" + ex.getMessage());
					checkedProDucs.clear();
					return checkedProDucs;
				}
				
				
			}
			

			for (ComMscBacklog comMscBacklog : proDucs) {

				List<ComMscStation> allStation = new ArrayList<>();
				_process.setMsc_pline(comMscBacklog.getMsc_pline());
				_station.setMsc_pline(comMscBacklog.getMsc_pline());
				List<ComMscProcess> listProcess = processMapper.findWithCondition(_process, null, "backlog_seq", 0, 0);
				if (listProcess.size() < 1) {
					msg.setMessage(
							"制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline() + "’无工序");
					return checkedProDucs;
				}
				ComMscStation station = new ComMscStation();
				station.setMsc(comMscBacklog.getMsc());
				station.setMsc_pline(comMscBacklog.getMsc_pline());
				allStation = stationMapper.find(station, 0, 0);
				// 验证工序中是否有工序点123
				boolean pass = checkStation(msg, checkedProDucs, stationMapper, _station, comMscBacklog, listProcess);
				if (!pass) {
					//continue;
					checkedProDucs.clear();
					return checkedProDucs;
				}
				// 交互标准的双向验证
				pass = checkStardand(msg, designKey, design, checkedProDucs, comMscBacklog, allStation);
				if (!pass) {
					//continue;
					checkedProDucs.clear();
					return checkedProDucs;
				}
				// 根据合同行号获取工艺要求集合

				String _sta = null;
				for (ComMscStation comMscStation : allStation) {
					_sta += comMscStation.getProcess_point_code() + ",";
				}
				
				
				if ("1".equals(con)) {
					pass=checkGongYiProcess(msg, design, checkedProDucs, comMscBacklog, listProcess);
				}else{
					pass = checkGongYi(msg, design, checkedProDucs, comMscBacklog, _sta);
				}
				
				if (!pass) {
					//continue;
					checkedProDucs.clear();
					return checkedProDucs;
				}

				// 验证制程的工序点是否交货状态关联的工序点
				pass = checkDelivery(msg, design, checkedProDucs, comMscBacklog, _sta);
				if (!pass) {
					//continue;
					checkedProDucs.clear();
					return checkedProDucs;
				}

				// 获取合同关联的附加要求项
				pass = checkAddRequire(msg, designKey, checkedProDucs, rstationMapper, comMscBacklog, allStation);
				if (!pass) {
					//continue;
					checkedProDucs.clear();
					return checkedProDucs;
				}

				pass = copyProcess(msg, designKey, checkedProDucs, listPrcTarget, comMscBacklog, listProcess);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;

				}
				// 新增质量设计制程全工序点信息
				pass = copyStation(msg, designKey, checkedProDucs, listStiTarget, comMscBacklog, allStation);
				if (!pass) {

					checkedProDucs.clear();
					return checkedProDucs;

				}

			}

			/*if (checkedProDucs == null || checkedProDucs.size() < 1) {
				msg.setMessage("制造规范码：‘" + _backLog.getMsc() + "’未找到符合订单的制程！");
				checkedProDucs.clear();
				return checkedProDucs;
			}*/
				/*
				// 验证第一条制程缺少的数据
				ComMscBacklog fistOne = proDucs.get(0);
				List<ComMscStation> allStation = new ArrayList<>();
				ComMscStation station = new ComMscStation();
				station.setMsc(fistOne.getMsc());
				station.setMsc_pline(fistOne.getMsc_pline());
				allStation = stationMapper.find(station, 0, 0);
				
				ComMscProcess proces=new ComMscProcess();
				proces.setMsc_pline(fistOne.getMsc_pline());
				List<ComMscProcess> listProcess = processMapper.find(proces, 0, 0);
			
				boolean pass = checkStardand(msg, designKey, design, checkedProDucs, fistOne, allStation);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;
				}
				// 根据合同行号获取工艺要求集合

				String _sta = null;
				for (ComMscStation comMscStation : allStation) {
					_sta += comMscStation.getProcess_point_code() + ",";
				}
				List<String> list_id = new ArrayList<String>();
				list_id.add("DSK0005");
				List<ComBaseConfig> con = (List<ComBaseConfig>) this.getBean(ComBaseConfigServiceImpl.class)
						.getBaseConfigInfo(list_id).getData();

				if (con != null && con.size() > 0) {
					if (con.get(0).getConfig_val() != null && !"".equals(con.get(0).getConfig_val())) {
						
						if ("1".equals(con.get(0).getConfig_val())) {
							pass=checkGongYiProcess(msg, design, checkedProDucs, fistOne, listProcess);

						}else{
							pass = checkGongYi(msg, design, checkedProDucs, fistOne, _sta);
						}
						
					}else{
						pass = checkGongYi(msg, design, checkedProDucs, fistOne, _sta);
					}
					
				}else{
					pass = checkGongYi(msg, design, checkedProDucs, fistOne, _sta);
				}
				
				
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;
				}

				// 验证制程的工序点是否交货状态关联的工序点
				pass = checkDelivery(msg, design, checkedProDucs, fistOne, _sta);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;
				}

				// 获取合同关联的附加要求项
				pass = checkAddRequire(msg, designKey, checkedProDucs, rstationMapper, fistOne, allStation);
				if (!pass) {
					checkedProDucs.clear();
					return checkedProDucs;
				}

			*/}

		

		try {
			if (checkedProDucs != null && checkedProDucs.size() > 0) {
				this.getMapper(SlmOrdDesignMscPlMapper.class).DeleteByDesignKey(designKey);
				this.getMapper(SlmOrdDesignMscPlMapper.class).insertBatch(checkedProDucs);
			}

			if (listPrcTarget != null && listPrcTarget.size() > 0) {
				prcMapper.insertBatch(listPrcTarget);
			}
			if (listStiTarget != null && listStiTarget.size() > 0) {
				for (SlmOrdDesignMscPlSti sti : listStiTarget) {
					stiMapper.insert(sti);
				}
				// stiMapper.insertBatch(listStiTarget);
			}

		} catch (Exception e) {
			checkedProDucs.clear();
			msg.setMessage("插入质量设计制程相关信息表失败：" + e.getMessage());
			return checkedProDucs;
		}

		return checkedProDucs;
	}
	
	/**
	 * 验证工序向下是否存在工序点
	 * 
	 * @param msg
	 * @param checkedProDucs
	 * @param stationMapper
	 * @param _station
	 * @param comMscBacklog
	 * @param listProcess
	 * @return
	 */
	private boolean checkStation(ReturnMsg msg, List<SlmOrdDesignMscPl> checkedProDucs,
			ComMscStationMapper stationMapper, ComMscStation _station, ComMscBacklog comMscBacklog,
			List<ComMscProcess> listProcess) {
		for (ComMscProcess comMscProcess : listProcess) {
			_station.setProcess_code(comMscProcess.getProcess_code());
			_station.setProcess_code_c(comMscProcess.getProcess_code_c());
			List<ComMscStation> listStation = stationMapper.find(_station, 0, 0);
			if (listStation.size() < 1) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’，制程：‘" + comMscBacklog.getMsc_pline() + "’，工序：‘"
						+ comMscProcess.getProcess_desc() + "’，二级工序：‘" + comMscBacklog.getProcess_desc_c()
						+ "’中，找不到有效工序点与之关联");
				return false;
			}
		}

		return true;
	}
	
	/**
	 * 制程与交互便准双向验证
	 * 
	 * @param msg
	 * @param designKey
	 * @param design
	 * @param checkedProDucs
	 * @param comMscBacklog
	 * @param allStation
	 * @return
	 */
	private boolean checkStardand(ReturnMsg msg, String designKey, SlmContractProcessPlan design,
			List<SlmOrdDesignMscPl> checkedProDucs, ComMscBacklog comMscBacklog, List<ComMscStation> allStation) {
		Set<String> listPointType = new HashSet<String>();

		for (ComMscStation stations : allStation) {
			listPointType.add(stations.getProcess_point_type());
		}

		// 免检验证逻辑
		CommonPage<SlmContractProcessPlan> slm = new CommonPage<SlmContractProcessPlan>();
		slm.setObject(design);
		ResultModel rm = this.getBean(ComMscExemptionServiceImpl.class).judgeExemptedInfo(slm);
		ComMscExemption cp = (ComMscExemption) rm.getData();

		// 制程工序点验证交互标准
		SlmOrdDesignStd designStd = this.getMapper(SlmOrdDesignStdMapper.class).findById(designKey, design.getPsc());
		for (String typeCode : listPointType) {
			if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Group) && cp != null
					&& "1".equals(cp.getIs_cic_exmt())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了成分类型的工序点，但成分标准已免检");
				// checkedProDucs.clear();
				return false;
			} else if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Detection) && cp != null
					&& "1".equals(cp.getIs_dic_exmt())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了探伤类型的工序点，但探伤标准已免检");
				// checkedProDucs.clear();
				return false;
			} else if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Material) && cp != null
					&& "1".equals(cp.getIs_pic_exmt())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了材质类型的工序点，但材质标准已免检");
				// checkedProDucs.clear();
				return false;
			} else if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Size) && cp != null
					&& "1".equals(cp.getIs_sic_exmt())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了公差类型的工序点，但公差标准已免检");
				// checkedProDucs.clear();
				return false;
			} else if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Group)
					&& DataHelper.IsNullOrSpace(designStd.getCic())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了成分类型的工序点，但却找不到成分标准");
				// checkedProDucs.clear();
				return false;
			} else if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Detection)
					&& DataHelper.IsNullOrSpace(designStd.getDic())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了探伤类型的工序点，但却找不到探伤标准");
				// checkedProDucs.clear();
				return false;
			} else if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Material)
					&& DataHelper.IsNullOrSpace(designStd.getPic())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了材质类型的工序点，但却找不到材质标准");
				// checkedProDucs.clear();
				return false;
			} else if (DataHelper.getStationType(typeCode).equals(Enum_StationType.Size)
					&& DataHelper.IsNullOrSpace(designStd.getSic())) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "包含了公差类型的工序点，但却找不到公差标准");
				// checkedProDucs.clear();
				return false;
			}
		}

		// 验证交互标准，判断是否有制程
		if (!DataHelper.IsNullOrSpace(designStd.getCic())) {
			if (!listPointType.contains("403201")) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "不包含成分类型的工序点，但找到了成分标准");
				// checkedProDucs.clear();
				return false;
			}
		}

		if (!DataHelper.IsNullOrSpace(designStd.getDic())) {
			if (!listPointType.contains("403204")) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "不包含探伤类型的工序点，但找到了探伤标准");
				// checkedProDucs.clear();
				return false;
			}

		}

		if (!DataHelper.IsNullOrSpace(designStd.getPic())) {
			if (!listPointType.contains("403202")) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "不包含材质类型的工序点，但找到了材质标准");
				// checkedProDucs.clear();
				return false;
			}

		}

		if (!DataHelper.IsNullOrSpace(designStd.getSic())) {
			if (!listPointType.contains("403203")) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
						+ "不包含公差类型的工序点，但找到了公差标准");
				// checkedProDucs.clear();
				return false;
			}

		}
		return true;
	}
	
	/**
	 * 判断制程包含的工序点是否与工艺要求指定工序点匹配
	 * 
	 * @param msg
	 * @param design
	 * @param checkedProDucs
	 * @param comMscBacklog
	 * @param _sta
	 * @return
	 */
	private boolean checkGongYi(ReturnMsg msg, SlmContractProcessPlan design, List<SlmOrdDesignMscPl> checkedProDucs,
			ComMscBacklog comMscBacklog, String _sta) {
		List<SlmContractBaseStation> stList=null;
		try {
			stList = this.getBean(SlmApiServiceImpl.class)
					.getSlmContractBaseStationByContractLineNo(design.getContract_line_no());
		} catch (Exception e) {
			msg.setMessage("调用销售查询工艺要求接口失败");
			return false;
		}
		if (stList.size() > 0) {

			for (SlmContractBaseStation slmContractBaseStation : stList) {

				if (!_sta.contains(slmContractBaseStation.getProcess_point_code())) {
					msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
							+ "’未包含订单要求的[" + slmContractBaseStation.getProcess_point_desc() + "]工艺,请确认！");
					// checkedProDucs.clear();
					return false;
				}
			}

		}

		return true;
	}
	
	/**
	 * 验证交货状态关联的工序点
	 * 
	 * @param msg
	 * @param design
	 * @param checkedProDucs
	 * @param comMscBacklog
	 * @param _sta
	 * @return
	 */
	private boolean checkDelivery(ReturnMsg msg, SlmContractProcessPlan design, List<SlmOrdDesignMscPl> checkedProDucs,
			ComMscBacklog comMscBacklog, String _sta) {
		List<ComBaseDeliverystateRPpc> ppcs = this.getMapper(ComBaseDeliverystateRPpcMapper.class)
				.findWithCondition(null, "DELIVERY_STATE_CODE='" + design.getDelivery_state_code() + "'", null, 0, 0);
		if (ppcs != null && ppcs.size() > 0) {
			for (ComBaseDeliverystateRPpc pc : ppcs) {
				if (!_sta.contains(pc.getProcess_point_code())) {
					msg.setMessage("制造规范码：'" + comMscBacklog.getMsc() + "'中，制程：'" + comMscBacklog.getMsc_pline()
							+ "未包含订单交货状态：'" + design.getDelivery_state_desc() + "'关联的工序点：'" + pc.getProcess_point_desc()
							+ "',请确认！");
					// checkedProDucs.clear();
					return false;
				}

			}

		}
		return true;
	}
	
	
	private boolean checkAddRequire(ReturnMsg msg, String designKey, List<SlmOrdDesignMscPl> checkedProDucs,
			ComBaseAskRStationMapper rstationMapper, ComMscBacklog comMscBacklog, List<ComMscStation> allStation) {

		SlmOrdDesignAdditional _ordAdd = new SlmOrdDesignAdditional();
		_ordAdd.setDesign_key(designKey);
		List<SlmOrdDesignAdditional> listOrdAdd = this.getMapper(SlmOrdDesignAdditionalMapper.class).find(_ordAdd, 0,
				0);
		StringBuilder _strBui = new StringBuilder();
		for (ComMscStation comMscStation : allStation) {
			_strBui.append("'" + comMscStation.getProcess_point_code() + "',");
		}
		List<ComBaseAskRStation> listRStation = rstationMapper
				.GetAllByStation(_strBui.toString().substring(0, _strBui.toString().length() - 1));

		// 验证工序点是否包含了必备的附加要求项
		for (SlmOrdDesignAdditional addAsk : listOrdAdd) {
			boolean find = false;
			for (ComBaseAskRStation comBaseAskRStation : listRStation) {
				if (addAsk.getAsk_sub_itme().equals(comBaseAskRStation.getAsk_sub_itme())) {
					find = true;
					break;
				}
			}
			if (!find) {
				msg.setMessage(
						"制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline() + "’没有包含与附加要求项：‘"
								+ addAsk.getAsk_itme_desc() + "’，附加要求分项：‘" + addAsk.getAsk_sub_itme_desc() + "’关联的工序点");
				// checkedProDucs.clear();
				return false;
			}
		}
		SlmOrdDesignMscPl _ordMscPl = new SlmOrdDesignMscPl();
		try {
			DataHelper.EntityCopy(comMscBacklog, _ordMscPl);
			_ordMscPl.setDesign_key(designKey);
		} catch (Exception ex) {
			msg.setMessage("制程中附加要求项与工序点验证节点出错：" + ex.getMessage());
			checkedProDucs.clear();
			return false;
		}
		checkedProDucs.add(_ordMscPl);
		return true;
	}
	
	/**
	 * 复制制程工序信息
	 * 
	 * @param msg
	 * @param designKey
	 * @param checkedProDucs
	 * @param listPrcTarget
	 * @param comMscBacklog
	 * @param listProcess
	 * @return
	 */
	private boolean copyProcess(ReturnMsg msg, String designKey, List<SlmOrdDesignMscPl> checkedProDucs,
			List<SlmOrdDesignMscPlPrc> listPrcTarget, ComMscBacklog comMscBacklog, List<ComMscProcess> listProcess) {
		for (ComMscProcess _mscProcess : listProcess) {
			SlmOrdDesignMscPlPrc _prc = new SlmOrdDesignMscPlPrc();
			try {
				DataHelper.CopyEntity(_mscProcess, _prc);
				_prc.setDesign_key(designKey);
				_prc.setWhole_backlog(comMscBacklog.getWhole_backlog());
				_prc.setWhole_backlog_desc(comMscBacklog.getWhole_backlog_desc());
				listPrcTarget.add(_prc);
			} catch (Exception ex) {
				checkedProDucs.clear();
				msg.setMessage("复制质量设计全制程工序信息失败：" + ex.getMessage());
				return false;
			}
		}
		return true;

	}
	
	/**
	 * 复制工序点信息
	 * 
	 * @param msg
	 * @param designKey
	 * @param checkedProDucs
	 * @param listStiTarget
	 * @param comMscBacklog
	 * @param allStation
	 * @return
	 */
	private boolean copyStation(ReturnMsg msg, String designKey, List<SlmOrdDesignMscPl> checkedProDucs,
			List<SlmOrdDesignMscPlSti> listStiTarget, ComMscBacklog comMscBacklog, List<ComMscStation> allStation) {
		for (ComMscStation _mscStation : allStation) {
			SlmOrdDesignMscPlSti _sti = new SlmOrdDesignMscPlSti();
			try {
				DataHelper.CopyEntity(_mscStation, _sti);
				_sti.setDesign_key(designKey);
				_sti.setWhole_backlog(comMscBacklog.getWhole_backlog());
				_sti.setWhole_backlog_desc(comMscBacklog.getWhole_backlog_desc());
				listStiTarget.add(_sti);
			} catch (Exception ex) {
				checkedProDucs.clear();
				msg.setMessage("复制质量设计全制程工序点信息失败：" + ex.getMessage());
				return false;
			}
		}
		return true;
	}
	
	
	private boolean checkGongYiProcess(ReturnMsg msg, SlmContractProcessPlan design,
			List<SlmOrdDesignMscPl> checkedProDucs, ComMscBacklog comMscBacklog, List<ComMscProcess> listProcess) {
		List<SlmContractBaseStation> stList=null;
		try {
			stList = this.getBean(SlmApiServiceImpl.class)
					.getSlmContractBaseStationByContractLineNo(design.getContract_line_no());
		} catch (Exception e) {
			msg.setMessage("调用销售查询工艺要求接口失败");
			return false;
		}
		if (stList!=null && stList.size() > 0) {
			if (stList.get(0).getRnum()!=null && !"".equals(stList.get(0).getRnum())) {
				for (int j = 0; j < stList.size(); j++) {		
					for (int i = 0; i < listProcess.size(); i++) {
						if (stList.get(j).getProcess_code().equals(listProcess.get(i).getProcess_code())) {
							if (listProcess.get(i).getBacklog_seq().toString().equals(stList.get(j).getRnum())) {
								break;
							}
						}
						
						if (i==listProcess.size()-1) {
							
							msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
							+ "’未包含订单要求顺序的[" + stList.get(j).getProcess_desc() + "]工艺,请确认！");
					         // checkedProDucs.clear();
					      return false;
						}
					}
				}
				
			}else{
				for (int j = 0; j < stList.size(); j++) {		
					for (int i = 0; i < listProcess.size(); i++) {
						if (stList.get(j).getProcess_code().equals(listProcess.get(i).getProcess_code())) {
							break;
						}
						
						if (i==listProcess.size()-1) {
							
							msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
							+ "’未包含订单要求的[" + stList.get(j).getProcess_desc() + "]工艺,请确认！");
					         // checkedProDucs.clear();
					      return false;
						}
					}
				}
			}
				
			
			if (stList.size()!=listProcess.size()) {
				msg.setMessage("制造规范码：‘" + comMscBacklog.getMsc() + "’中，制程：‘" + comMscBacklog.getMsc_pline()
				+ "’不完全符合订单工艺,请确认！");

				return false;
			}
			

		}

		return true;
	}



}
