package market.Qms.slmord.impl;

import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.PscRMscMapper;
import market.Qms.com.vo.PscRMsc;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.qcm.service.impl.ComMscExemptionServiceImpl;
import market.Qms.qcm.vo.ComMscExemption;
import market.Qms.slmord.mapper.SlmOrdDesignMscMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStsTklgLstMapper;
import market.Qms.slmord.service.QualityDesignMscBaseService;
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.SlmOrdDesignMsc;
import market.Qms.slmord.vo.SlmOrdDesignStsTklgLst;
import market.Slm.ContractManagement.vo.SlmContractPsc;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;

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

	@Override
	public boolean ValidationMsc(SlmContractProcessPlan orderLine,SlmOrdDesignStsTklgLst tklgLst, ReturnMsg msg, ComMsc _msc) {
		tklgLst.setDesign_sts("F");
		tklgLst.setLst_item("MSC(0)");
		tklgLst.setMsc(orderLine.getMsc());
		PscRMsc pscMsc=new PscRMsc();
		pscMsc.setValidflag("1");
		pscMsc.setPsc(orderLine.getPsc());
		List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).find(pscMsc, 0, 0);
		if(listPM.size()<1){
			tklgLst.setDesign_sts_rs("90");
			tklgLst.setDesign_sts_desc("没有基础制造规范码，待人工确认");
			msg.setMessage("没有基础制造规范码，待人工确认");
			return false;
		}
		ComMscMapper mscMapper=this.getMapper(ComMscMapper.class);
		try{
			ComMsc msc=mscMapper.findById(orderLine.getMsc());
			if(msc.getValidflag().equals("D")){
				msg.setMessage("订单冶金规范码已无效，请重新提交订单");
				return false;
			}
			
			if(!DataHelper.IsNullOrSpace(msc.getMsc_seq())){//如果有引用制造规范码则表示是用的mscN
				orderLine.setMsc_n(msc.getMsc());
				orderLine.setMsc(msc.getMsc_seq());
				//将明细表里面的msc字段填充
				tklgLst.setMsc(msc.getMsc_seq());
				msc=mscMapper.findById(msc.getMsc_seq());
			}
			DataHelper.CopyEntity(msc, _msc);
		}catch(Exception ex){
			msg.setMessage("基础制造规范码评审出错："+ex.getMessage());
			return false;
		}
		if(_msc==null||_msc.getValidflag().equals("D")){
			tklgLst.setDesign_sts_rs("90");
			tklgLst.setDesign_sts_desc("没有基础制造规范码，待人工确认");
			msg.setMessage("没有基础制造规范码，待人工确认");
			return false;
		}
		tklgLst.setSection_code(_msc.getSection_code());
		tklgLst.setSection_name(_msc.getSection_name());
		tklgLst.setDesign_obj_key(_msc.getMsc());
		//有客户特殊要求，要求制造规范码状态为审核状态
		if (orderLine.getMsc_n()!=null && !_msc.getValidflag().equals("C")) {
			tklgLst.setDesign_sts_rs("91");
			tklgLst.setDesign_sts_desc("有客户技术特殊要求，基础制造规范码：" + _msc.getMsc() + "，未通过审核，待人工确认");
			msg.setMessage(tklgLst.getDesign_sts_desc());
			return false;		
		}else if(!DataHelper.IsNullOrSpace(orderLine.getCustomer_alpha_no())&&!_msc.getValidflag().equals("C")){
			tklgLst.setDesign_sts_rs("91");
			tklgLst.setDesign_sts_desc("有客户技术特殊要求，基础制造规范码："+_msc.getMsc()+"，未通过审核，待人工确认");
			msg.setMessage(tklgLst.getDesign_sts_desc());
			return false;
		}else{
			tklgLst.setDesign_sts("S");
			tklgLst.setDesign_sts_rs("11");
			//修改质量设计 msc 信息表
			SlmOrdDesignMsc designMsc=new SlmOrdDesignMsc();
			designMsc.setMsc(_msc.getMsc());
			designMsc.setMsc_desc(_msc.getMsc_desc());
			designMsc.setDesign_key(tklgLst.getDesign_key());
			//表中 无制造规范类型
			//designMsc.setMsc_style(_msc.getMsc_style());
			this.getMapper(SlmOrdDesignMscMapper.class).update(designMsc);
		}
		return true;
	}
	
	/**
	 * 验证交付标准
	 * @param msg 返回信息
	 * @param listPsc 副产品码
	 * @param msc 制造规范码
	 * @param design 合同行信息
	 * @param tklgLst 设计状态明细
	 * @param tklgLstMapper 
	 * @return
	 */
	@Override
	public boolean ValidationMscStd(ReturnMsg msg,List<SlmContractPsc> listPsc,ComMsc msc,SlmContractProcessPlan design,SlmOrdDesignStsTklgLst tklgLst,SlmOrdDesignStsTklgLstMapper tklgLstMapper){		
		
		//C:短节+附件 D:接箍 没有交付标准
		if (design.getMsc_type_no().equals("C") || design.getMsc_type_no().equals("D")) {
			DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","S","00","成分标准无要求","");
			tklgLstMapper.update(tklgLst);
			DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","S","00","材质标准无要求","");
			tklgLstMapper.update(tklgLst);
			DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","S","00","公差标准无要求","");
			tklgLstMapper.update(tklgLst);
			DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","S","00","探伤标准无要求","");
			tklgLstMapper.update(tklgLst);
		}else{ //验证交付标准
			//免检验证逻辑
			
			CommonPage<SlmContractProcessPlan> slm=new CommonPage<SlmContractProcessPlan>();
			slm.setObject(design);
			ResultModel rm=this.getBean(ComMscExemptionServiceImpl.class).judgeExemptedInfo(slm);
			ComMscExemption cp=(ComMscExemption) rm.getData();
			
			if (cp!=null) {
				boolean pass=true;
				
				if ("1".equals(cp.getIs_cic_exmt())) {
					DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","S","11","成分标准已免检","");
					tklgLstMapper.update(tklgLst);
				}else {
					pass=this.getService(QualityDesignStdCicService.class).ValidationStdCIC(msg,msc,design,tklgLst.getDesign_key(),"0");
					if (!pass) {
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","F","91",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
						return false;
					} else {
						//验证副产品码
						/*for (SlmContractPsc slmContractPsc : listPsc) {
							tklgLst.setPsc(slmContractPsc.getPsc());
							SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
							String condition="PSC='"+slmContractPsc.getPsc()+"'";
							List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
							ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
							pass=this.getService(QualityDesignStdCicService.class).ValidationStdCIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
							if(!pass){
								DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","F","11","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");

								tklgLstMapper.update(tklgLst);
								return false;
							}
						}*/
						
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","S","11",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
					}
					
				}
				
				//判断材质
				if ("1".equals(cp.getIs_pic_exmt())) {
					tklgLst.setPsc(design.getPsc());
					DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","S","11","材质标准已免检","");
					tklgLstMapper.update(tklgLst);
					
				}else {
					pass=this.getService(QualityDesignStdPicService.class).ValidationStdPIC(msg,msc,design,tklgLst.getDesign_key(),"0");
					if (!pass) {
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","F","91",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
						return false;
					} else {
						//验证副产品码
						/*for (SlmContractPsc slmContractPsc : listPsc) {
							tklgLst.setPsc(slmContractPsc.getPsc());
							SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
							String condition="PSC='"+slmContractPsc.getPsc()+"'";
							List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
							ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
							pass=this.getService(QualityDesignStdPicService.class).ValidationStdPIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
							if(!pass){
								DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","F","91","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");
								tklgLstMapper.update(tklgLst);
								return false;
							}
						}*/
						tklgLst.setPsc(design.getPsc());
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","S","11",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
					}
					
					
				}
				//公差项目是否免检
				if ("1".equals(cp.getIs_sic_exmt())) {
					tklgLst.setPsc(design.getPsc());
					DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","S","11","公差标准已免检","");
					tklgLstMapper.update(tklgLst);
	
				}else {
					
					pass=this.getService(QualityDesignStdSicService.class).ValidationStdSIC(msg,msc,design,tklgLst.getDesign_key(),"0");
					if (!pass) {
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","F","91",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
						return false;
					} else {
						//验证副产品码
						/*for (SlmContractPsc slmContractPsc : listPsc) {
							tklgLst.setPsc(slmContractPsc.getPsc());
							SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
							String condition="PSC='"+slmContractPsc.getPsc()+"'";
							List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
							ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
							pass=this.getService(QualityDesignStdSicService.class).ValidationStdSIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
							if(!pass){
								DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","F","91","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");
								tklgLstMapper.update(tklgLst);
								return false;
							}
						}*/
						tklgLst.setPsc(design.getPsc());
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","S","11",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
					}
					
				}
				
				//探伤项目是否免检
				
				if ("1".equals(cp.getIs_dic_exmt())) {
					
					tklgLst.setPsc(design.getPsc());
					DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","S","11","探伤标准已免检","");
					tklgLstMapper.update(tklgLst);
				}else {
					pass=this.getService(QualityDesignStdDicService.class).ValidationStdDIC(msg,msc,design,tklgLst.getDesign_key(),"0");
					if (!pass) {
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","F","91",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
						return false;
					} else {
						//验证副产品码
						/*for (SlmContractPsc slmContractPsc : listPsc) {
							tklgLst.setPsc(slmContractPsc.getPsc());
							SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
							String condition="PSC='"+slmContractPsc.getPsc()+"'";
							List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
							ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
							pass=this.getService(QualityDesignStdDicService.class).ValidationStdDIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
							if(!pass){
								DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","F","91","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");
								tklgLstMapper.update(tklgLst);
								return false;
							}
						}*/
						tklgLst.setPsc(design.getPsc());
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","S","11",msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
					}
					
				}
				
				
			}else {
			
			//成分标准
			boolean pass=this.getService(QualityDesignStdCicService.class).ValidationStdCIC(msg,msc,design,tklgLst.getDesign_key(),"0");
			if (!pass) {
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","F","91",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
				return false;
			} else {
				//验证副产品码
				/*for (SlmContractPsc slmContractPsc : listPsc) {
					tklgLst.setPsc(slmContractPsc.getPsc());
					SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
					String condition="PSC='"+slmContractPsc.getPsc()+"'";
					List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
					ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
					pass=this.getService(QualityDesignStdCicService.class).ValidationStdCIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
					if(!pass){
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","F","11","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");

						tklgLstMapper.update(tklgLst);
						return false;
					}
				}*/
				
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"成分标准","S","11",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
			}
			//材质标准
			pass=this.getService(QualityDesignStdPicService.class).ValidationStdPIC(msg,msc,design,tklgLst.getDesign_key(),"0");
			if (!pass) {
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","F","91",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
				return false;
			} else {
				//验证副产品码
				/*for (SlmContractPsc slmContractPsc : listPsc) {
					tklgLst.setPsc(slmContractPsc.getPsc());
					SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
					String condition="PSC='"+slmContractPsc.getPsc()+"'";
					List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
					ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
					pass=this.getService(QualityDesignStdPicService.class).ValidationStdPIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
					if(!pass){
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","F","91","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
						return false;
					}
				}*/
				tklgLst.setPsc(design.getPsc());
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"材质标准","S","11",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
			}

			//公差标准
			pass=this.getService(QualityDesignStdSicService.class).ValidationStdSIC(msg,msc,design,tklgLst.getDesign_key(),"0");
			if (!pass) {
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","F","91",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
				return false;
			} else {
				//验证副产品码
				/*for (SlmContractPsc slmContractPsc : listPsc) {
					tklgLst.setPsc(slmContractPsc.getPsc());
					SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
					String condition="PSC='"+slmContractPsc.getPsc()+"'";
					List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
					ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
					pass=this.getService(QualityDesignStdSicService.class).ValidationStdSIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
					if(!pass){
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","F","91","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
						return false;
					}
				}*/
				tklgLst.setPsc(design.getPsc());
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"公差标准","S","11",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
			}

			//探伤标准
			pass=this.getService(QualityDesignStdDicService.class).ValidationStdDIC(msg,msc,design,tklgLst.getDesign_key(),"0");
			if (!pass) {
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","F","91",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
				return false;
			} else {
				//验证副产品码
				/*for (SlmContractPsc slmContractPsc : listPsc) {
					tklgLst.setPsc(slmContractPsc.getPsc());
					SlmOrdDesignMscDMapper  mscDMapper=this.getMapper(SlmOrdDesignMscDMapper.class);
					String condition="PSC='"+slmContractPsc.getPsc()+"'";
					List<PscRMsc> listPM=this.getMapper(PscRMscMapper.class).findWithCondition(null, condition, " create_time", 0, 0);
					ComMsc _fMsc=this.getMapper(ComMscMapper.class).findById(listPM.get(0).getMsc());
					pass=this.getService(QualityDesignStdDicService.class).ValidationStdDIC(msg,_fMsc,design,tklgLst.getDesign_key(),"0");
					if(!pass){
						DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","F","91","【副产品："+slmContractPsc.getPsc()+"，制程规范码："+_fMsc.getMsc()+"】"+msg.getMessage(),"");
						tklgLstMapper.update(tklgLst);
						return false;
					}
				}*/
				tklgLst.setPsc(design.getPsc());
				DataHelper.SetDesignStsTklgLst(design,tklgLst,"探伤标准","S","11",msg.getMessage(),"");
				tklgLstMapper.update(tklgLst);
			}
			
			}
		}
		return true;
	}
}
