package market.Qms.slmord.impl;

import java.util.ArrayList;
import java.util.Date;
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.Qms.common.DataHelper;
import market.Qms.common.DataHelper.Enum_StationType;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscMapper;
import market.Qms.metallurgy.mapper.ComMscPhySampleMapper;
import market.Qms.metallurgy.mapper.ComMscProcessMapper;
import market.Qms.metallurgy.mapper.ComMscStationMapper;
import market.Qms.metallurgy.mapper.ComMscStdChemMapper;
import market.Qms.metallurgy.mapper.ComMscStdCicMapper;
import market.Qms.metallurgy.mapper.ComMscStdDetectionCMapper;
import market.Qms.metallurgy.mapper.ComMscStdDetectionMapper;
import market.Qms.metallurgy.mapper.ComMscStdPhyMapper;
import market.Qms.metallurgy.mapper.ComMscStdPicMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecBiasCMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecBiasMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecCMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComMscPhySample;
import market.Qms.metallurgy.vo.ComMscProcess;
import market.Qms.metallurgy.vo.ComMscStation;
import market.Qms.metallurgy.vo.ComMscStdChem;
import market.Qms.metallurgy.vo.ComMscStdCic;
import market.Qms.metallurgy.vo.ComMscStdDetection;
import market.Qms.metallurgy.vo.ComMscStdDetectionC;
import market.Qms.metallurgy.vo.ComMscStdPhy;
import market.Qms.metallurgy.vo.ComMscStdPic;
import market.Qms.metallurgy.vo.ComMscStdSpec;
import market.Qms.metallurgy.vo.ComMscStdSpecBias;
import market.Qms.metallurgy.vo.ComMscStdSpecBiasC;
import market.Qms.metallurgy.vo.ComMscStdSpecC;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlPrcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdCicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdDicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicSmpMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdSicMapper;
import market.Qms.slmord.service.QualityDesignInsideStdService;
import market.Qms.slmord.service.QualityDesignStdCicService;
import market.Qms.slmord.service.QualityDesignStdDicService;
import market.Qms.slmord.service.QualityDesignStdPicService;
import market.Qms.slmord.service.QualityDesignStdSicService;
import market.Qms.slmord.vo.SlmOrdDesignMscPl;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdCic;
import market.Qms.slmord.vo.SlmOrdDesignStdDic;
import market.Qms.slmord.vo.SlmOrdDesignStdPic;
import market.Qms.slmord.vo.SlmOrdDesignStdPicSmp;
import market.Qms.slmord.vo.SlmOrdDesignStdSic;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

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

@RemoteService(serviceInterface=QualityDesignInsideStdService.class)
public class QualityDesignInsideStdServiceImpl extends BaseServiceImpl<Object,Object> implements QualityDesignInsideStdService{

	/**
	 * 根据工序点类型寻找相应内控标准
	 * @param designStd 产品设计主信息
	 * @param process 工序
	 * @param list 工序点集合
	 * @param stdPrcMapper 内控标准Mapper
	 * @param designKey 
	 * @param msg 返回信息
	 * @return
	 */
	private boolean ValidationStart(SlmOrdDesignStd designStd,SlmOrdDesignMscPl slmOrdDesignMscPl,ComMscProcess process,List<ComMscStation> list,SlmOrdDesignMscPlPrcMapper stdPrcMapper,String designKey,ReturnMsg msg)
	{
		for (ComMscStation station : list) {
			//成分类工序点
			if(DataHelper.getStationType(station.getProcess_point_type()).equals(Enum_StationType.Group)){
				if(DataHelper.IsNullOrSpace(designStd.getCic())){
					msg.setMessage("存在成分类工序点，但是不存在成分标准");
					return false;
				}
				if(DataHelper.IsNullOrSpace(process.getCic())&&process.getIs_stdcic().equals("0")){
					msg.setMessage("制程：‘"+process.getMsc_pline()+"’，工序：’"+process.getProcess_desc()+"‘存在成分类工序点，但是不存在成分内控标准");
					return false;
				}
			//材质类
			}else 
			if(DataHelper.getStationType(station.getProcess_point_type()).equals(Enum_StationType.Material)){
				if(DataHelper.IsNullOrSpace(designStd.getPic())){
					msg.setMessage("存在材质类工序点，但是不存在材质标准");
					return false;
				}
				if(DataHelper.IsNullOrSpace(process.getPic())&&process.getIs_stdpic().equals("0")){
					msg.setMessage("制程：‘"+process.getMsc_pline()+"’，工序：’"+process.getProcess_desc()+"‘存在材质类工序点，但是不存在材质内控标准");
					return false;
				}
			//公差类
			}else 
			if(DataHelper.getStationType(station.getProcess_point_type()).equals(Enum_StationType.Size)){
				if(DataHelper.IsNullOrSpace(designStd.getSic())){
					msg.setMessage("存在公差类工序点，但是不存在公差标准");
					return false;
				}
				if(DataHelper.IsNullOrSpace(process.getSic())&&process.getIs_stdsic().equals("0")){
					msg.setMessage("制程：‘"+process.getMsc_pline()+"’，工序：’"+process.getProcess_desc()+"‘存在公差类工序点，但是不存在公差内控标准");
					return false;
				}
			//探伤类
			}else 
			if(DataHelper.getStationType(station.getProcess_point_type()).equals(Enum_StationType.Detection)){
				if(DataHelper.IsNullOrSpace(designStd.getPic())){
					msg.setMessage("存在探伤类工序点，但是不存在探伤标准");
					return false;
				}
				if(DataHelper.IsNullOrSpace(process.getDic())&&process.getIs_stddic().equals("0")){
					msg.setMessage("制程：‘"+process.getMsc_pline()+"’，工序：’"+process.getProcess_desc()+"‘存在探伤类工序点，但是不存在探伤内控标准");
					return false;
				}
			}else{/*不需要验证的工序点类型*/}
		}
		return true;
	}
	
	
	@Override
	public boolean ValidationInsideStd(ReturnMsg msg,List<SlmOrdDesignMscPl> listOrdMscPl,SlmContractProcessPlan orderLine, String designKey) {
		// TODO Auto-generated method stub
		ComMsc msc=null;
		if (orderLine.getMsc_n()!=null) {
			msc=this.getMapper(ComMscMapper.class).findById(orderLine.getMsc_n());
			
		} else {
			msc=this.getMapper(ComMscMapper.class).findById(orderLine.getMsc());
	

		}
		if(msc.getAllow_nk().equals("0")){//冶金规范码不需要内控
			msg.setMessage("内控标准无要求");
			return true;
		}
		
		SlmOrdDesignStdMapper stdMapper=this.getMapper(SlmOrdDesignStdMapper.class);
		SlmOrdDesignStd _designStd=new SlmOrdDesignStd();
		_designStd.setDesign_key(designKey);
		_designStd.setPsc(orderLine.getPsc());
		List<SlmOrdDesignStd> listDesign=stdMapper.find(_designStd, 0, 0);
		//每个制程中的工序都可能需要进行内控验证
		ComMscProcessMapper processMapper=this.getMapper(ComMscProcessMapper.class);
		ComMscStationMapper stationMapper=this.getMapper(ComMscStationMapper.class);
		SlmOrdDesignMscPlPrcMapper stdPrcMapper=this.getMapper(SlmOrdDesignMscPlPrcMapper.class);
		for (SlmOrdDesignMscPl slmOrdDesignMscPl : listOrdMscPl) {
			
			//验证制程向下是存在炼钢工序
			/*if (slmOrdDesignMscPl.getWhole_backlog().contains("A")) {
				//验证制程向下的是否存在guid码
				//List<slmorddesign>
				
			}*/
			
			ComMscProcess _process=new ComMscProcess();
			_process.setMsc(msc.getMsc());
			_process.setMsc_pline(slmOrdDesignMscPl.getMsc_pline());
			//查询制程下所有工序集合
			List<ComMscProcess> listMscProcess=processMapper.find(_process, 0, 0);
			for (ComMscProcess comMscProcess : listMscProcess) {
				//查询工序下所有工序点集合
				ComMscStation _station=new ComMscStation();
				_station.setMsc(msc.getMsc());
				_station.setMsc_pline(slmOrdDesignMscPl.getMsc_pline());
				_station.setProcess_code(comMscProcess.getProcess_code());
				_station.setValidflag("1");
				List<ComMscStation> _listStation=stationMapper.find(_station, 0, 0);
				boolean pass=ValidationStart(listDesign.get(0),slmOrdDesignMscPl,comMscProcess,_listStation,stdPrcMapper,designKey,msg);
				if(!pass){
					return false;
				}
				//验证材质/成分/公差/探伤
				if(!DataHelper.IsNullOrSpace(comMscProcess.getCic())){
					pass=ValidationCic(comMscProcess,orderLine,designKey,msg);
					if(!pass){
						return false;
					}
				}
				if(!DataHelper.IsNullOrSpace(comMscProcess.getPic())){
					pass=ValidationPic(comMscProcess, orderLine, designKey, msg);
					if(!pass){
						return false;
					}
				}
				if(!DataHelper.IsNullOrSpace(comMscProcess.getSic())){
					pass=ValidationSic(comMscProcess, orderLine, designKey, msg);
					if(!pass){
						return false;
					}
				}
				if(!DataHelper.IsNullOrSpace(comMscProcess.getDic())){ 
					pass=ValidationDic(comMscProcess, orderLine, designKey, msg);
					if(!pass){
						return false;
					}
				}
			}
		}
		return true;
	}
	

	/**
	 * 验证探伤标准
	 * @param comMscProcess
	 * @param orderLine
	 * @param designKey
	 * @param msg
	 * @return
	 */
	private boolean ValidationDic(ComMscProcess comMscProcess,SlmContractProcessPlan orderLine, String designKey,ReturnMsg msg){
		if(comMscProcess.getIs_stddic().equals("1")){
			return true;
		}
		QualityDesignStdDicService stdDicService=this.getService(QualityDesignStdDicService.class);
		String[] dics=comMscProcess.getDic().split(",");
		String condition=DataHelper.CreateSqlCondition(dics, "Dic");
		List<ComMscStdDetection> listDic=this.getMapper(ComMscStdDetectionMapper.class).findWithCondition(null, condition, null, 0, 0);
		boolean pass=stdDicService.FilterDeliveryState(listDic,orderLine.getDelivery_state_code());
		if(!pass){
			msg.setMessage("找不到符合订单交货状态的探伤内控标准索引");
			return false;
		}
		ComMscStdDetectionCMapper deteCMapper=this.getMapper(ComMscStdDetectionCMapper.class);
		for (ComMscStdDetection dic : listDic) {
			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()), orderLine.getThick().toString());
				if(!pass){
					continue;
				}
				pass=DataHelper.checkSignAndValue(comMscStdDetectionC.getMinHSign(),DataHelper.GetDouble(comMscStdDetectionC.getMinH()), orderLine.getThick().toString());
				if(!pass){
					continue;
				}
				pass=DataHelper.checkSignAndValue(comMscStdDetectionC.getMaxDSign(),DataHelper.GetDouble(comMscStdDetectionC.getMaxD()), orderLine.getWidth().toString());
				if(!pass){
					continue;
				}
				pass=DataHelper.checkSignAndValue(comMscStdDetectionC.getMinDSign(),DataHelper.GetDouble(comMscStdDetectionC.getMinD()), orderLine.getWidth().toString());
				if(pass){
					detes.add(comMscStdDetectionC);
				}
			}
			if (detes.size() == 0) {
				msg.setMessage("探伤标准索引： "+_detecC.getDic()+"，满足合同宽度：" + orderLine.getWidth() + "，厚度："+ orderLine.getThick() + "的探伤标准不存在");
				return false;
			}
			// 验证无损检验项
			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) {
				dete.setSpecl_fl("2");
				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;
				}
				dicEntity.setProcess_code(comMscProcess.getProcess_code());
				dicEntity.setMsc_pline(comMscProcess.getMsc_pline());
				dicEntity.setBacklog_seq(comMscProcess.getBacklog_seq().intValue());
				dicMapper.insert(dicEntity);
				
			}
		}
		return true;
	}
	
	
	/**
	 * 验证公差内控标准
	 * @param comMscProcess
	 * @param orderLine
	 * @param designKey
	 * @param msg
	 * @return
	 */
	private boolean ValidationSic(ComMscProcess comMscProcess,SlmContractProcessPlan orderLine, String designKey,ReturnMsg msg){
		if(comMscProcess.getIs_stdsic().equals("1")){
			return true;
		}
		QualityDesignStdSicService stdSicService=this.getService(QualityDesignStdSicService.class);
		String[] sics=comMscProcess.getSic().split(",");
		String condition=DataHelper.CreateSqlCondition(sics, "Sic");
		List<ComMscStdSpec> listSic=this.getMapper(ComMscStdSpecMapper.class).findWithCondition(null, condition, null, 0, 0);
		boolean pass=stdSicService.FilterDeliveryState(listSic,orderLine.getDelivery_state_code());
		if(!pass){
			msg.setMessage("找不到符合订单交货状态的公差内控标准索引");
			return false;
		}
		
		ComMscStdSpecCMapper specCMapper=this.getMapper(ComMscStdSpecCMapper.class);
		List<String> listBiasCode=new ArrayList<>();//存放公差项，用来确定唯一公差项
		for (ComMscStdSpec sic : listSic) {
			List<ComMscStdSpecC> specs=new ArrayList<>();//用于存放公差标准明细
			if(!DataHelper.IsNullOrSpace(sic.getSpecStdCode())){
				ComMscStdSpecBias _bias=new ComMscStdSpecBias();
				_bias.setStdCode(sic.getSpecStdCode());
				_bias.setValidflag("1");
				List<ComMscStdSpecBias> listSpecBias=this.getMapper(ComMscStdSpecBiasMapper.class).find(_bias, 0, 0);
				if(listSpecBias.size()<1){
					msg.setMessage("公差内控索引‘"+sic+"’中公差引用交付标准已无效");
					return false;
				}
				ComMscStdSpecBiasC _biasC=new ComMscStdSpecBiasC();
				_biasC.setValidflag("1");
				_biasC.setSic(listSpecBias.get(0).getSic());
				List<ComMscStdSpecBiasC> listBiasC=this.getMapper(ComMscStdSpecBiasCMapper.class).find(_biasC, 0, 0);
				//ComMscStdSpecBiasC  转换为 ComMscStdSpecC
				for (ComMscStdSpecBiasC comMscStdSpecBiasC : listBiasC) {
					ComMscStdSpecC spec=new ComMscStdSpecC();
					try{
						DataHelper.CopyEntity(comMscStdSpecBiasC, spec);
						specs.add(spec);
					}catch(Exception ex){
						msg.setMessage("公差引用标准赋值到公差标准出差："+ex.getMessage());
						return false;
					}	
				}
			}else{
				ComMscStdSpecC _spec=new ComMscStdSpecC();
				_spec.setValidflag("1");
				_spec.setSic(sic.getSic());
				specs =specCMapper.find(_spec, 0, 0);
			}
			//查詢公差交付标准
			if (specs.size()<1) {
				msg.setMessage("公差内控标准索引号：" + sic.getSic() + "不存在公差标准");
				return false;
			}
			// 过滤公差标准
			List<ComMscStdSpecC> specAs = stdSicService.FilterSpec(specs, orderLine);
			if (specAs.size()<1) {
				msg.setMessage("公差内控标准索引号：" + sic.getSic() + "找不到符合合同规格的公差标准");
				return false;
			}
			// 保存公差标准
			SlmOrdDesignStdSicMapper sicMapper=this.getMapper(SlmOrdDesignStdSicMapper.class);
			for (ComMscStdSpecC spec : specAs) {
				if(listBiasCode.contains(spec.getBiasName())){
					msg.setMessage("找到多条符合要求的内控公差项‘"+spec.getBiasName()+"’");
					return false;
				}else{
					listBiasCode.add(spec.getBiasName());
				}
				SlmOrdDesignStdSic stdSic=new SlmOrdDesignStdSic();
				stdSic.setSpecl_fl("2");
				stdSic.setDesign_key(designKey);
				try{
					DataHelper.CopyEntity(spec, stdSic);
					stdSicService.GetStdResult(stdSic,orderLine);
					stdSic.setProcess_code(comMscProcess.getProcess_code());
					stdSic.setMsc_pline(comMscProcess.getMsc_pline());
					stdSic.setBacklog_seq(comMscProcess.getBacklog_seq().intValue());
					sicMapper.insert(stdSic);
				}catch(Exception ex){
					msg.setMessage("写入公差内控标准出错："+ex.getMessage());
					return false;
				}
			}
		}
		return true;
	}
	
	
	/**
	 * 验证材质内控标准
	 * @param comMscProcess
	 * @param orderLine
	 * @param designKey
	 * @param msg
	 * @return
	 */
	private boolean ValidationPic(ComMscProcess comMscProcess,SlmContractProcessPlan orderLine, String designKey,ReturnMsg msg){
		if(comMscProcess.getIs_stdpic().equals("1")){
			return true;
		}
		QualityDesignStdPicService stdPicService=this.getService(QualityDesignStdPicService.class);
		String[] pics=comMscProcess.getPic().split(",");
		String condition=DataHelper.CreateSqlCondition(pics, "pic");
		List<ComMscStdPic> listPic=this.getMapper(ComMscStdPicMapper.class).findWithCondition(null, condition, null, 0, 0);
		boolean pass=stdPicService.FilterDeliveryState(listPic,orderLine.getDelivery_state_code());
		if(!pass){
			msg.setMessage("找不到符合订单交货状态的材质内控标准索引");
			return false;
		}
		ComMscPhySampleMapper sampleMapper=this.getMapper(ComMscPhySampleMapper.class);
	/*	ComBasePhyMapper phyMapper=this.getMapper(ComBasePhyMapper.class);*/
		List<String> listPhyName=new ArrayList<>();//用于存放唯一材质检验项
		for (ComMscStdPic pic : listPic) {
			// 通过索引号查询理化标准
			ComMscStdPhy _stdPhy=new ComMscStdPhy();
			_stdPhy.setValidflag("1");
			_stdPhy.setPic(pic.getPic());
			List<ComMscStdPhy> phys =this.getMapper(ComMscStdPhyMapper.class).find(_stdPhy, 0, 0);
			if (phys == null) {
				msg.setMessage("材质索引号:" + pic.getPic() + "找不到有效的材质标准");
				return false;
			}
			// 过滤规格
			List<ComMscStdPhy> phyAs=stdPicService.FilterSpec(phys, orderLine);
			if(phyAs.size()<1){
				msg.setMessage("材质索引："+pic.getPic()+"，找不到符合合同规格的材质标准");
				return false;
			}
			// 验证材质标准的检验项目
			List<ComMscPhySample> smps = new ArrayList<ComMscPhySample>();
			ComMscPhySample _sample=new ComMscPhySample();
			_sample.setPic(pic.getPic());
			_sample.setValidflag("1");
			List<ComMscPhySample> listSample=sampleMapper.find(_sample, 0, 0);
			for (ComMscPhySample comMscPhySample : listSample) {
				ComMscPhySample smp =stdPicService.checkSmp(comMscPhySample, orderLine);
				if (smp == null) {
					msg.setMessage("材质索引号：" + pic.getPic() + "没有取样要求 (牌号：" + orderLine.getSteel_code() + ",宽度：" + orderLine.getWidth() + ",厚度：" + orderLine.getThick()+")");
					return false;
				}else{
					if(smp.getSmp_index().equals("")){
						msg.setMessage( "材质索引号:" + pic.getPic() + "没有取样要求(牌号：" + orderLine.getSteel_name()+ ",外径：" + orderLine.getWidth() + ",厚度：" + orderLine.getThick() + ")");
						return false;
					}					
				}
				smp.setDesign_key(designKey);
				smps.add(smp);
			}
			
			// 录入理化标准检验项
			for (ComMscPhySample smp : smps) {
				SlmOrdDesignStdPicSmpMapper smpMapper=this.getMapper(SlmOrdDesignStdPicSmpMapper.class);
				SlmOrdDesignStdPicSmp _smp=new SlmOrdDesignStdPicSmp();
				try{
					DataHelper.CopyEntity(smp,_smp);
					_smp.setDesign_key(designKey);
					smpMapper.insert(_smp);
				}catch(Exception ex){
					msg.setMessage("材质内控取样要求验证出错："+ex.getMessage());
					return false;
				}
			}
			SlmOrdDesignStdPicMapper picMapper=this.getMapper(SlmOrdDesignStdPicMapper.class);
			for (ComMscStdPhy phy : phyAs) {
				if(listPhyName.contains(phy.getPhyNameS())){
					msg.setMessage("找到多条符合要求的材质内控检验项‘"+phy.getPhyNameS()+"’");
					return false;
				}else{
					listPhyName.add(phy.getPhyNameS());
				}
				phy.setDesign_key(designKey);
				phy.setSpecl_fl("2");
				SlmOrdDesignStdPic _stdPic=new SlmOrdDesignStdPic();
				try{
					DataHelper.CopyEntity(phy, _stdPic);
					_stdPic.setProcess_code(comMscProcess.getProcess_code());
					_stdPic.setMsc_pline(comMscProcess.getMsc_pline());
					_stdPic.setDelivery_state_code(orderLine.getDelivery_state_code());
					_stdPic.setDelivery_state_desc(orderLine.getDelivery_state_desc());
					_stdPic.setBacklog_seq(comMscProcess.getBacklog_seq().intValue());
					picMapper.insert(_stdPic);
				}catch(Exception ex){
					msg.setMessage("写入材质内控标准出错："+ex.getMessage());
					return false;
				}
			}
		}
		return true;
	}
	
	
	/**
	 * 验证成分内控标准
	 * @param comMscProcess
	 * @param orderLine
	 * @param designKey
	 * @param msg
	 * @return
	 */
	private boolean ValidationCic(ComMscProcess comMscProcess,SlmContractProcessPlan orderLine, String designKey,ReturnMsg msg){
		if(comMscProcess.getIs_stdcic().equals("1")){
			return true;
		}
		String[] cics=comMscProcess.getCic().split(",");
		String condition=DataHelper.CreateSqlCondition(cics, "cic");
		List<ComMscStdCic> listCic=this.getMapper(ComMscStdCicMapper.class).findWithCondition(null, condition, null, 0, 0);
		QualityDesignStdCicService stdCicService=this.getService(QualityDesignStdCicService.class);
		boolean pass=stdCicService.FilterDeliveryState(listCic,orderLine.getDelivery_state_code());
		if(!pass){
			msg.setMessage("没有找到符合订单交货状态的成分内控标准索引信息");
			return false;
		}
		ComMscStdChemMapper chemMapper=this.getMapper(ComMscStdChemMapper.class);
		SlmOrdDesignStdCicMapper stdCicMapper=this.getMapper(SlmOrdDesignStdCicMapper.class);
		List<String> listStdChem=new ArrayList<>();//用于存放唯一成分元素项
		for (ComMscStdCic mscStdCic : listCic) {
			ComMscStdChem chem=new ComMscStdChem();
			chem.setCic(mscStdCic.getCic());
			chem.setValidflag("1");
			List<ComMscStdChem> listChem=chemMapper.find(chem, 0, 0);
			if(listChem.size()<1){
				msg.setMessage("成分内控索引："+chem.getCic()+"，找不到成分标准");
				return false;
			}
			List<ComMscStdChem> chemAs=stdCicService.FilterSpec(listChem,orderLine);
			if(chemAs.size()<1){
				msg.setMessage("成分内控索引："+chem.getCic()+"，找不到符合订单规格的成分标准");
				return false;
			}
			for (ComMscStdChem _chem : chemAs) {
				if(listStdChem.contains(_chem.getChemName())){
					msg.setMessage("成分内控标准找到多条符合要求的化学元素项‘"+_chem.getChemName()+"’");
					return false;
				}else{
					listStdChem.add(_chem.getChemName());
				}				
				SlmOrdDesignStdCic stdCic=new SlmOrdDesignStdCic();
				try{
					DataHelper.CopyEntity(_chem, stdCic);
				}catch(Exception ex){
					msg.setMessage("成分内控标准赋值失败："+ex.getMessage());
					return false;
				}
				stdCic.setDesign_key(designKey);
				stdCic.setSpecl_fl("2");
				stdCic.setCreate_time(new Date());
				stdCic.setProcess_code(comMscProcess.getProcess_code());
				stdCic.setMsc_pline(comMscProcess.getMsc_pline());
				stdCic.setBacklog_seq(comMscProcess.getBacklog_seq().intValue());
				stdCicMapper.insert(stdCic);
			}
		}
		return true;
	}
}
