package market.Qms.slmord.impl;

import java.util.ArrayList;
import java.util.List;

import market.Com.mapper.ComBasePlineMapper;
import market.Com.vo.ComBasePline;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscPlineRProductMapper;
import market.Qms.metallurgy.mapper.ComMscPlineRSpecMapper;
import market.Qms.metallurgy.mapper.ComMscPlineRSteelMapper;
import market.Qms.metallurgy.mapper.ComMscProcessMapper;
import market.Qms.metallurgy.vo.ComMscPlineRProduct;
import market.Qms.metallurgy.vo.ComMscPlineRSpec;
import market.Qms.metallurgy.vo.ComMscPlineRSteel;
import market.Qms.metallurgy.vo.ComMscProcess;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlGxPrlMapper;
import market.Qms.slmord.service.QualityDesignProLinesService;
import market.Qms.slmord.vo.SlmOrdDesignMscPl;
import market.Qms.slmord.vo.SlmOrdDesignMscPlGxPrl;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(serviceInterface=QualityDesignProLinesService.class)
public class QualityDesignProLinesServiceImpl extends BaseServiceImpl<Object,Object> implements QualityDesignProLinesService{
	
	/**
	 * 筛选适用产线 -- 过滤规格  1、过滤长度上下限，2、过滤厚度上下限，3、过滤宽度上下限
	 * @param listSpec
	 * @param orderInfo
	 * @return
	 */
	private List<ComMscPlineRSpec> FilterSpec(List<ComMscPlineRSpec> listSpec,SlmContractProcessPlan orderInfo){
		List<ComMscPlineRSpec> list = new ArrayList<>();
		for (ComMscPlineRSpec mscPlineRSpec : listSpec) {
			Double lenMax = 0.0;
			boolean result=true;
			if (!DataHelper.DoubleIsNull(orderInfo.getLength())) {
				lenMax = orderInfo.getLength();
				result =this.judgeMent(mscPlineRSpec.getLengthMax(),mscPlineRSpec.getLengthMin(),lenMax);
			}
			// 因为只有上限值，所以这样判断
			/*boolean result = (mscPlineRSpec.getLengthMax() >= lenMax && mscPlineRSpec.getLengthMin() <= lenMax)
					|| lenMax == 0 || mscPlineRSpec.getLengthMax() == 0;*/
		
			if (result) {// 过滤厚度
				Double height = orderInfo.getThick();
				/*result = (mscPlineRSpec.getProdSuitMax() >= height && mscPlineRSpec.getProdSuitMin() <= height)
						|| height == 0;*/
				result =this.judgeMent(mscPlineRSpec.getProdSuitMax(),mscPlineRSpec.getProdSuitMin(),height);
			}
			if (result) {// 过滤宽度
				Double dimater = orderInfo.getWidth();
				/*result = (mscPlineRSpec.getProdWidthMax() >= dimater && mscPlineRSpec.getProdWidthMin() <= dimater)
						|| dimater == 0;*/
				result =this.judgeMent(mscPlineRSpec.getProdWidthMax(),mscPlineRSpec.getProdWidthMin(),dimater);
			}
			if (result) {
				list.add(mscPlineRSpec);
			}
		}

		return list;
	}
	
	private boolean judgeMent(Double max,Double min,Double target) {
		
		if (max==null && min==null) {
			return true;
		}else if(min==null && max!=null){
			
			if (max>=target) {
				
				return true;
				
			}
			return false;
			
			
		}else if (min!=null && max==null) {
			
			if (target>=min) {
				return true;
			}
			return false;
		}else{
			
			if (target>=min && max>=target) {
				return true;
			}
			
			
			
			return false;
		}
		
		
		
	}

	/**
	 * 为了获得产能井与产线名 而进行的循环
	 * @param listSpec
	 * @param listSteel
	 * @return
	 */
	private List<ComMscPlineRSpec> FliterPline(List<ComMscPlineRSpec> listSpec,List<ComMscPlineRSteel> listSteel){
		List<ComMscPlineRSpec> filterSpec=new ArrayList<>();
		for (ComMscPlineRSteel mscPlineRSteel : listSteel){
			for (ComMscPlineRSpec mscPlineRSpec : listSpec) {
				if(mscPlineRSpec.getPline_Code().equals(mscPlineRSteel.getPlineCode())){
					filterSpec.add(mscPlineRSpec);
					break;
				}
			}
		}
		return filterSpec;
	}
	
	/**
	 * 过滤成品规格、牌号、品名
	 * @param msg 返回信息
	 * @param process 工序信息
	 * @param designKey 
	 * @param mscPline 产线信息
	 * @param orderInfo 合同信息
	 * @param backlogSeq 
	 * @return
	 */
	private boolean CheckPline(ReturnMsg msg,ComMscProcess process,String designKey,String mscPline,SlmContractProcessPlan orderInfo,String backlogSeq){
		// 产线信息
		ComBasePlineMapper plineMapper = this.getMapper(ComBasePlineMapper.class);
		ComBasePline _pline = new ComBasePline();
		_pline.setValidflag("1");
		_pline.setProcess_code(process.getProcess_code());
		List<ComBasePline> listPline = plineMapper.find(_pline, 0, 0);
		if (listPline.size() < 1) {
			msg.setMessage("制程：" + mscPline + "中‘" + process.getProcess_desc() + "’工序找不到关联产线");
			return false;
		}
		String plineCodes = "";
		for (ComBasePline basePline : listPline) {
			plineCodes += "'" + basePline.getPline_code() + "',";
		}
		plineCodes = plineCodes.replaceAll(",$", "");

		// 根据规格过滤产线 1、根据产线编码筛选产线，2、根据合同成品规格筛选出产线
		ComMscPlineRSpecMapper rSpecMapper = this.getMapper(ComMscPlineRSpecMapper.class);
		List<ComMscPlineRSpec> listSpec = rSpecMapper.GetMscPlineRSpecByPlineCode(plineCodes);
		if (listSpec.size() < 1) {
			msg.setMessage("制程：" + mscPline + "中‘" + process.getProcess_desc() + "’工序无产线规格关联信息");
			return false;
		}

		List<ComMscPlineRSpec> filterSpec = FilterSpec(listSpec, orderInfo);// 过滤规格之后的产线
		if (filterSpec.size() < 1) {
			msg.setMessage("制程：" + mscPline + "中‘" + process.getProcess_desc() + "’工序根据成品规格筛选出0条产线信息");
			return false;
		}
		
		// 根据上一步过滤出的产线过滤产品名称
		ComMscPlineRProductMapper rPMapper = this.getMapper(ComMscPlineRProductMapper.class);
		List<ComMscPlineRSpec> listPro = new ArrayList<ComMscPlineRSpec>();
		for (ComMscPlineRSpec comMscPlineRSpec : filterSpec) {

			List<ComMscPlineRProduct> lists = rPMapper.findWithCondition(null,
					"PLINE_CODE='" + comMscPlineRSpec.getPline_Code() + "'", null, 0, 0);
			if (lists.size() < 1) {
				listPro.add(comMscPlineRSpec);
				continue;

			}

			// 遍历集合判断产品名称是否相同
			/*for (ComMscPlineRProduct comMscPlineRProduct : lists) {
				if (!comMscPlineRProduct.getProdCode().equals(orderInfo.getProd_code())) {
					lists.remove(comMscPlineRProduct);
				}

			}*/
			
			for (int i = 0; i < lists.size(); i++) {
				if (!lists.get(i).getProdCode().equals(orderInfo.getProd_code())) {
					lists.remove(lists.get(i));
					i--;
				}
				
			}

			if (lists.size() < 1) {
				continue;

			}

			listPro.add(comMscPlineRSpec);

		}

		if (listPro.size() < 1) {
			msg.setMessage("制程：" + mscPline + "中‘" + process.getProcess_desc() + "’工序根据产品名称筛选出0条产线信息");
			return false;
		}

		// 过滤牌号
		List<ComMscPlineRSpec> listPro2 = new ArrayList<ComMscPlineRSpec>();
		ComMscPlineRSteelMapper rSMapper = this.getMapper(ComMscPlineRSteelMapper.class);

		for (ComMscPlineRSpec comMscPlineRSpec : listPro) {
			List<ComMscPlineRSteel> listSteel = rSMapper.findWithCondition(null,
					"PLINE_CODE='" + comMscPlineRSpec.getPline_Code() + "'", null, 0, 0);
			if (listSteel.size() < 1) {

				listPro2.add(comMscPlineRSpec);
				continue;
			}

		/*	for (ComMscPlineRSteel comMscPlineRSteel : listSteel) {
				if (!comMscPlineRSteel.getSteelCode().equals(orderInfo.getSteel_code())) {
					listSteel.remove(comMscPlineRSteel);

				}

			}*/
			
			for (int i = 0; i < listSteel.size(); i++) {
				if (!listSteel.get(i).getSteelCode().equals(orderInfo.getSteel_code())) {
					listSteel.remove(listSteel.get(i));
					i--;

				}
				
			}

			if (listSteel.size() < 1) {
				continue;

			}

			listPro2.add(comMscPlineRSpec);

		}

		if (listPro2.size() < 1) {
			msg.setMessage("制程：" + mscPline + "中‘" + process.getProcess_desc() + "’工序根据牌号筛选出0条产线信息");
			return false;

		}

		SlmOrdDesignMscPlGxPrlMapper prlMapper = this.getMapper(SlmOrdDesignMscPlGxPrlMapper.class);
		for (ComMscPlineRSpec mscPlineRSpec : listPro2) {
			SlmOrdDesignMscPlGxPrl gxPrl = new SlmOrdDesignMscPlGxPrl();
			gxPrl.setDesign_key(designKey);
			gxPrl.setBacklog_seq(Long.parseLong(backlogSeq));
			gxPrl.setIdent_flag("0");
			gxPrl.setMsc_pline(mscPline);
			gxPrl.setPline_code(mscPlineRSpec.getPline_Code());
			gxPrl.setPline_name(mscPlineRSpec.getPline_Name());
			gxPrl.setProcess_code(process.getProcess_code());
			gxPrl.setValidflag("1");
			gxPrl.setWarn_fl("0");
			gxPrl.setWarn_desc(null);
			gxPrl.setWell_code(mscPlineRSpec.getWellCode());
			gxPrl.setWell_name(mscPlineRSpec.getWellName());
			prlMapper.insert(gxPrl);

		}
		return listPro2.size() > 0;
	}
	
	
	/**
	 * 适用产线评审
	 * @param msg 返回信息
	 * @param listOrdMscPl 制程信息
	 * @param designKey 
	 * @param orderLine 合同信息
	 * @return
	 */
	@Override
	public boolean ValidationProductLine(ReturnMsg msg,List<SlmOrdDesignMscPl> listOrdMscPl,String designKey,SlmContractProcessPlan orderLine){
		for (SlmOrdDesignMscPl proDuc : listOrdMscPl) {
			// 制程所包含的所有工序
			String[] processCodes = proDuc.getWhole_backlog().split("-");
			String[] processNames = proDuc.getWhole_backlog_desc().split("-");
			ComMscProcessMapper processMapper = this.getMapper(ComMscProcessMapper.class);
			ComMscProcess ps = new ComMscProcess();
			ps.setValidflag("1");
			if (orderLine.getMsc_n()!=null) {
				ps.setMsc(orderLine.getMsc_n());
			} else {

				ps.setMsc(orderLine.getMsc());
			}
			
			int index = 0;
			//int seq=0;
			List<ComMscProcess>  process=this.getMapper(ComMscProcessMapper.class).findWithCondition(null, "MSC='"+ps.getMsc()+"' and MSC_PLINE='"+proDuc.getMsc_pline()+"'", "BACKLOG_SEQ", 0, 0);
			//for (String processCode : processCodes) {
			for (ComMscProcess comMscProcess : process) {
					
				
				ComMscProcess _process = new ComMscProcess();
				_process.setValidflag("1");
				if (orderLine.getMsc_n()!=null) {
					_process.setMsc(orderLine.getMsc_n());
				} else {

					_process.setMsc(orderLine.getMsc());
				}
				
				_process.setMsc_pline(proDuc.getMsc_pline());
				List<ComMscProcess> listProcess = processMapper.find(_process, 0, 0);
				ComMscProcess mscPlPrcEntity = listProcess.get(0);
				// 适用产线
				_process.setProcess_desc(processNames[index]);
				_process.setProcess_code(comMscProcess.getProcess_code());
				boolean result = CheckPline(msg, _process, designKey, proDuc.getMsc_pline(), orderLine,
						comMscProcess.getBacklog_seq().toString());
				if (!result) {
					return false;
				}
				index++;
			}
		}
		return true;
	}
	
}
