package market.Qms.slmord.impl;

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

import market.Com.mapper.ComBasePhyBMapper;
import market.Com.mapper.ComBasePhyMapper;
import market.Com.mapper.ComBasePhyRStationMapper;
import market.Com.vo.ComBasePhy;
import market.Com.vo.ComBasePhyB;
import market.Com.vo.ComBasePhyRStation;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscStdDetectionCMapper;
import market.Qms.metallurgy.mapper.ComMscStdDetectionMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComMscStdDetection;
import market.Qms.metallurgy.vo.ComMscStdDetectionC;
import market.Qms.slmord.mapper.SlmOrdDesignStdDicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.service.QualityDesignStdDicService;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdDic;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

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

@RemoteService(path="QualityDesignStdDicService")
public class QualityDesignStdDicServiceImpl extends BaseServiceImpl<Object,Object> implements QualityDesignStdDicService{
	
	/**
	 * 过滤交货状态
	 * @param listDic 探伤索引
	 * @param deliveryCode 交货状态编号
	 */
	@Override
	public boolean FilterDeliveryState(List<ComMscStdDetection> listDic,String deliveryCode){
		for (int i = 0; i < listDic.size(); i++) {
			ComMscStdDetection dic=listDic.get(i);
			if(dic.getValidflag().equals("0")){
				listDic.remove(i);
				i--;
				continue;
			}
			
			if(dic.getDeliveryStateCode()!=null){
				if(!dic.getDeliveryStateCode().contains(deliveryCode)){
					listDic.remove(i);
					i--;
				}
			}
		}
		return listDic.size()>0;
	}
	
	
	/**
	 * 验证探伤标准
	 * @param msg 返回信息
	 * @param msc 冶金规范码
	 * @param design 合同信息
	 * @param designKey 
	 * @param flag，0：国标，1：客户标准，2：内控标准
	 * @return
	 */
	@Override
	public boolean ValidationStdDIC(ReturnMsg msg,ComMsc msc,SlmContractProcessPlan design,String designKey,String flag){
		String useCode = design.getUse_code();
		String useDesc = design.getUse_desc();
		// 如果冶金规范码已经通过审批
		List<ComMscStdDetection> dics = new ArrayList<ComMscStdDetection>();
		ComMscStdDetectionMapper deMapper=this.getMapper(ComMscStdDetectionMapper.class);
		ComMscStdDetection _detec=new ComMscStdDetection();
		_detec.setMsc(msc.getMsc());
		if (msc.getValidflag().equals("C")) {
			_detec.setValidflag("3");
			dics =deMapper.find(_detec, 0, 0);
		} else {
			dics =deMapper.find(_detec, 0, 0);
			for(int index=0;index<dics.size();index++){
				if(dics.get(index).getValidflag().equals("0")){
					dics.remove(index);
					index--;
				}
			}
		}
		//2018-05-28：改为只要有索引存在才验证向下的标准信息
/*		if (dics.size() == 0&&flag.equals("0")) {//交付标准中必须有探伤标准
			msg.setMessage("制造规范‘"+msc.getMsc()+"’找不到探伤标准索引");
			return false;
		}else */
		if(dics.size()<1&&(flag.equals("1")||flag.equals("0"))){//客户标准中不存在标准索引则视为无标准要求
			msg.setMessage("探伤标准无要求");
			return true;	
		}
		
		boolean pass=FilterDeliveryState(dics,design.getDelivery_state_code());
		if(!pass){
			msg.setMessage("没有找到符合订单交货状态的探伤标准索引");
			return false;
		}
		ComMscStdDetectionCMapper deteCMapper=this.getMapper(ComMscStdDetectionCMapper.class);
		for (ComMscStdDetection dic : dics) {
			ComMscStdDetectionC _detecC=new ComMscStdDetectionC();
			_detecC.setValidflag("1");
			_detecC.setDic(dic.getDic());
			List<ComMscStdDetectionC> allDetes = deteCMapper.find(_detecC, 0, 0);
			List<ComMscStdDetectionC> detes=new ArrayList<>();
			//过滤数据
			for (ComMscStdDetectionC comMscStdDetectionC : allDetes) {
				pass=DataHelper.checkSignAndValue(comMscStdDetectionC.getMaxHSign(),DataHelper.GetDouble(comMscStdDetectionC.getMaxH()), design.getThick().toString());
				if(!pass){
					continue;
				}
				pass=DataHelper.checkSignAndValue(comMscStdDetectionC.getMinHSign(),DataHelper.GetDouble(comMscStdDetectionC.getMinH()), design.getThick().toString());
				if(!pass){
					continue;
				}
				pass=DataHelper.checkSignAndValue(comMscStdDetectionC.getMaxDSign(),DataHelper.GetDouble(comMscStdDetectionC.getMaxD()), design.getWidth().toString());
				if(!pass){
					continue;
				}
				pass=DataHelper.checkSignAndValue(comMscStdDetectionC.getMinDSign(),DataHelper.GetDouble(comMscStdDetectionC.getMinD()), design.getWidth().toString());
				if(pass){
					detes.add(comMscStdDetectionC);
				}
			}
			if (detes.size() == 0) {
				msg.setMessage("探伤标准索引： "+_detecC.getDic()+"，满足合同宽度：" + design.getWidth() + "，厚度："+ design.getThick() + "的探伤标准不存在");
				return false;
			}
			// 验证是否重复
/*			String returnInfo =checkCommon(detes, width, thick);
			if (!returnInfo.equals("")) {
				message = returnInfo;
				continue;
			}*/
			boolean phyFlag = true;
			// 验证无损检验项
			ComBasePhyMapper phyMapper=this.getMapper(ComBasePhyMapper.class); 
			ComBasePhyRStationMapper PRStation=this.getMapper(ComBasePhyRStationMapper.class);
			ComBasePhyBMapper BMapper=this.getMapper(ComBasePhyBMapper.class);
			for (ComMscStdDetectionC dete : detes) {
				ComBasePhy phyCode=phyMapper.findById(dete.getPhyCodeS());
				ComBasePhyB phyL=BMapper.findById(phyCode.getPhy_code_l());
				// 如果是无损检验项,必须维护检验项对应的工序点
				if (phyL.getPhy_type().equals("410602")) {
					ComBasePhyRStation _rstation=new ComBasePhyRStation();
					_rstation.setPhyCodeS(dete.getPhyCodeS());
					_rstation.setValidflag("1");
					List<ComBasePhyRStation> stations=PRStation.find(_rstation, 0, 0);
					if (stations.size()<1) {
						msg.setMessage("检验项：‘" + dete.getPhyNameS()	+ "’是无损检验项，需要维护检验项对应的工序点");
						return false;
					}
				}
			}
			SlmOrdDesignStdDicMapper dicMapper=this.getMapper(SlmOrdDesignStdDicMapper.class);
			Integer index=1;
			// 保存探伤标准
			for (ComMscStdDetectionC dete : detes) {
				// 是否特殊要求(0否;1是)
				dete.setSpecl_fl(flag);
				dete.setDesign_key(designKey);
				SlmOrdDesignStdDic dicEntity=new SlmOrdDesignStdDic();
				try{
					DataHelper.CopyEntity(dete, dicEntity);
					dicEntity.setXh(index);
					index++;
				}catch(Exception ex){
					msg.setMessage("写入探伤标准出错："+ex.getMessage());
					return false;
				}
				if (flag.equals("1")) {
					dicEntity.setUse_code(useCode);
					dicEntity.setUse_desc(useDesc);
				}
				dicMapper.insert(dicEntity);
				
			}
  			//修改标准索引号
			UpdateDesignStdDic(designKey,msc.getPsc(),dic.getDic(),flag);
		}
		msg.setMessage("探伤标准验证通过");
		return true;
	}
	
	/**
	 *  修改索引
	 * @param designKey
	 * @param psc 产品码
	 * @param dic 探伤索引号
	 * @param flag 标准类型
	 */
	private void UpdateDesignStdDic(String designKey,String psc,String dic,String flag){
		SlmOrdDesignStdMapper stdMapper=this.getMapper(SlmOrdDesignStdMapper.class);
		SlmOrdDesignStd designStd=stdMapper.findById(designKey,psc);//根据designKey与psc找到当前标准索引信息
		SlmOrdDesignStd std=new SlmOrdDesignStd();
		std.setDesign_key(designKey);
		std.setPsc(psc);
		// 保存索引号
		if (flag.equals("0")) {
			std.setDic(designStd.getDic()==null?dic:designStd.getDic()+";"+dic);
		}else if(flag.equals("1")){
			std.setDic_k(designStd.getDic_k()==null?dic:designStd.getDic_k()+";"+dic);			
		}
		this.getMapper(SlmOrdDesignStdMapper.class).update(std);
	}
	
	
	/**
	 * 查询探伤项目
	 */
	public ResultModel getDectionItem() {
		ResultModel rm=new ResultModel();
		
		
		try {
			List<ComBasePhy> phys=this.getMapper(ComBasePhyMapper.class).findBySql();
			
			rm.setData(phys);
			rm.setState("200");
			
		} catch (Exception e) {
			rm.setMsgInfo("查询失败");
			rm.setState("500");
		}
		return rm;
		
	}

}
