package market.Qms.slmord.impl;

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

import market.Com.mapper.ComBaseProcessMapper;
import market.Com.vo.ComBaseProcess;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscGradeMapper;
import market.Qms.metallurgy.mapper.ComMscGradeRDeliverystateMapper;
import market.Qms.metallurgy.mapper.ComMscGradeRSpecMapper;
import market.Qms.metallurgy.vo.ComMscGrade;
import market.Qms.metallurgy.vo.ComMscGradeRDeliverystate;
import market.Qms.metallurgy.vo.ComMscGradeRSpec;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlGzMapper;
import market.Qms.slmord.service.QualityDesignSteelBrandService;
import market.Qms.slmord.vo.SlmOrdDesignMscPl;
import market.Qms.slmord.vo.SlmOrdDesignMscPlGz;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

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

	@Override
	public boolean ValidationSteelBrand(ReturnMsg msg,List<SlmOrdDesignMscPl> listOrdMscPl,SlmContractProcessPlan orderLine, String designKey) {
		// TODO Auto-generated method stub
		ComMscGradeMapper gradeMapper=this.getMapper(ComMscGradeMapper.class);
		ComMscGradeRDeliverystateMapper stateMapper=this.getMapper(ComMscGradeRDeliverystateMapper.class);
		SlmOrdDesignMscPlGzMapper plGzMapper=this.getMapper(SlmOrdDesignMscPlGzMapper.class);
		
		for (SlmOrdDesignMscPl slmOrdDesignMscPl : listOrdMscPl) {
			
			//根据全程产线号查询工序代码
			String[] processCodes = slmOrdDesignMscPl.getWhole_backlog().split("-");
			String[] processNames = slmOrdDesignMscPl .getWhole_backlog_desc().split("-");
			boolean result=judgeGrade(processCodes);
			if (result) {
				continue;
			}
			
			ComMscGrade _grade=new ComMscGrade();
			if (orderLine.getMsc_n()!=null) {
				_grade.setMsc(orderLine.getMsc_n());
			}else{
				_grade.setMsc(orderLine.getMsc());
			}
			
			_grade.setMsc_pline(slmOrdDesignMscPl.getMsc_pline());
			_grade.setValidflag("1");
			List<ComMscGrade> listList=gradeMapper.find(_grade, 0, 0);
			if(listList.size()<1){
				msg.setMessage("制造规范码"+_grade.getMsc()+"的制程："+slmOrdDesignMscPl.getMsc_pline()+"，未找到关联的炼钢牌号");
				return false;
			}
			
			//验证炼钢牌号适用交货状态
			List<ComMscGrade> listMscGrade=new ArrayList<>();
			for (ComMscGrade comMscGrade : listList) {
				ComMscGradeRDeliverystate _state=new ComMscGradeRDeliverystate();
				_state.setGrade_code(comMscGrade.getGrade_code());
				List<ComMscGradeRDeliverystate> _listDeliveryRState=stateMapper.find(_state, 0, 0);
				
				
				if(_listDeliveryRState.size()<1){//适用交货状态与炼钢牌号均通过验证
					listMscGrade.add(comMscGrade);
					continue;
				}
				
				for(int index=0;index<_listDeliveryRState.size();index++){//交货状态不为null，且交货状态不等于订单的交货状态则不满足要求
					if(_listDeliveryRState.get(index).getDelivery_state_code()!=null
					&&!_listDeliveryRState.get(index).getDelivery_state_code().equals(orderLine.getDelivery_state_code())){
						_listDeliveryRState.remove(index);
						index--;	
					}
				}
				
				if(_listDeliveryRState.size()>0){//适用交货状态与炼钢牌号均通过验证
					listMscGrade.add(comMscGrade);
				}
			}
			if(listMscGrade.size()<1){
				msg.setMessage("制造规范码"+_grade.getMsc()+"制程："+slmOrdDesignMscPl.getMsc_pline()+"根据合同交货状态找不到对应的炼钢牌号");
				return false;
			}

			//验证炼钢牌号适用规格
			ComMscGradeRSpecMapper specMapper=this.getMapper(ComMscGradeRSpecMapper.class);
			for (int i = 0; i < listMscGrade.size(); i++) {
				ComMscGrade _mscGrad=listMscGrade.get(i);
				ComMscGradeRSpec _spec=new ComMscGradeRSpec();
				_spec.setMsc_pline(slmOrdDesignMscPl.getMsc_pline());
				if (orderLine.getMsc_n()!=null) {
					_spec.setMsc(orderLine.getMsc_n());
				} else {
					_spec.setMsc(orderLine.getMsc());
				}
			
				_spec.setGrade_code(_mscGrad.getGrade_code());
				List<ComMscGradeRSpec> _listSpec=specMapper.find(_spec, 0, 0);
				if(_listSpec.size()<1){
					continue;
				}

				//过滤适用规格
				boolean isFind=false;
				for (ComMscGradeRSpec comMscGradeRSpec : _listSpec) {
					boolean pass=DataHelper.checkSignAndValue(comMscGradeRSpec.getThick_max_sign(),comMscGradeRSpec.getThick_max(),DataHelper.GetString(orderLine.getThick()));
					if(!pass){
						continue;
					}
					pass=DataHelper.checkSignAndValue(comMscGradeRSpec.getThick_min_sign(),comMscGradeRSpec.getThick_min(),DataHelper.GetString(orderLine.getThick()));
					if(!pass){
						continue;
					}
					pass=DataHelper.checkSignAndValue(comMscGradeRSpec.getWidth_max_sign(),comMscGradeRSpec.getWidth_max(),DataHelper.GetString(orderLine.getWidth()));
					if(!pass){
						continue;
					}
					pass=DataHelper.checkSignAndValue(comMscGradeRSpec.getWidth_min_sign(),comMscGradeRSpec.getWidth_min(),DataHelper.GetString(orderLine.getWidth()));
					if(!pass){
						continue;
					}
					isFind=true;
					break;
				}
				
				if(!isFind){
					listMscGrade.remove(i);
					i--;
				}
			}
			if(listMscGrade.size()<1){
				msg.setMessage("制造规范码"+_grade.getMsc()+"制程：’"+slmOrdDesignMscPl.getMsc_pline()+"‘，根据合同产品规格筛选不出对应的炼钢牌号");
				return false;
			}
			try{
				for (ComMscGrade comMscGrade : listMscGrade) {
					SlmOrdDesignMscPlGz plGz=new SlmOrdDesignMscPlGz();
					DataHelper.CopyEntity(comMscGrade, plGz);
					plGz.setDesign_key(designKey);
					plGzMapper.insert(plGz);
				}
			}catch(Exception ex){
				msg.setMessage("保存炼钢牌号出错："+ex.getMessage());
				return false;
			}
			
		}
		return true;
	}
	/**
	 * 添加验证是否维护炼钢牌号逻辑
	 * @param processCodes
	 * @return
	 */
	private boolean judgeGrade(String[] processCodes) {


		for (String string : processCodes) {
			ComBaseProcess com=this.getMapper(ComBaseProcessMapper.class).findById(string);
			if ("1".equals(com.getIs_grade())) {
				return false;
			}
			
		}
		return true;
	}

}
