/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-30
 * 描述            ：质量设计-交付标准-成分标准
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.slmord.impl;
import java.util.ArrayList;
import java.util.List;

import market.Com.mapper.ComBasePlineMapper;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlPrcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdCicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.service.SlmOrdDesignStdCicService;
import market.Qms.slmord.vo.OrdJudgeStd;
import market.Qms.slmord.vo.SlmOrdDesignMscPlPrc;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdCic;

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

@RemoteService(path="SlmOrdDesignStdCicService")
public class SlmOrdDesignStdCicServiceImpl extends BaseServiceImpl<SlmOrdDesignStdCic,Object> implements SlmOrdDesignStdCicService {
	
	/**
	 * 获取成分判定标准（交付/客户/内控）
	 * @param designKey
	 * @param psc 产品码
	 * @param plineCode 产线编号
	 * @param mscPline 制程号
	 * @return
	 */
	public ResultModel GetOrdJudgeStdCic(String designKey,String psc,String plineCode,String mscPline){
		ResultModel rm=new ResultModel("200","获取成分判定标准成功");
		try{
			SlmOrdDesignStd _designStd=this.getMapper(SlmOrdDesignStdMapper.class).findById(designKey,psc);
			if(DataHelper.IsNullOrSpace(_designStd.getCic())){
				rm.setState("403");
				rm.setMsgInfo("成分索引号不能为空");
				return rm;
			}
			OrdJudgeStd<SlmOrdDesignStdCic> stdCic=new OrdJudgeStd<>();
			SlmOrdDesignStdCicMapper cicMapper=this.getMapper(SlmOrdDesignStdCicMapper.class);
			//交付标准. 
			String condition="";
			if(!DataHelper.IsNullOrSpace(_designStd.getCic())){
				condition=DataHelper.CreateSqlCondition(_designStd.getCic().split(";"),"cic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
				List<SlmOrdDesignStdCic> listStdGB=cicMapper.findWithCondition(null, condition, null, 0, 0);
				stdCic.setListStdGB(listStdGB);
			}

			//客户标准
			if(!DataHelper.IsNullOrSpace(_designStd.getCic_k())){
				condition=DataHelper.CreateSqlCondition(_designStd.getCic_k().split(";"),"cic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
				List<SlmOrdDesignStdCic> listStdKH=cicMapper.findWithCondition(null, condition, null, 0, 0);
				stdCic.setListStdKH(listStdKH);
			}
			//内控标准
			String process=this.getMapper(ComBasePlineMapper.class).findById(plineCode).getProcess_code();
			SlmOrdDesignMscPlPrc _prc=new SlmOrdDesignMscPlPrc();
			_prc.setDesign_key(designKey);
			_prc.setProcess_code(process);
			_prc.setMsc_pline(mscPline);
			List<SlmOrdDesignMscPlPrc> listPrc=this.getMapper(SlmOrdDesignMscPlPrcMapper.class).find(_prc, 0, 0);
			if(!listPrc.get(0).getIs_stdcic().equals("1")){
				stdCic.setNkQuote(true);
			}else{//非引用标准
				if(!DataHelper.IsNullOrSpace(listPrc.get(0).getCic())){
					condition=DataHelper.CreateSqlCondition(listPrc.get(0).getCic().split(","),"cic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
					List<SlmOrdDesignStdCic> listStdNK=cicMapper.findWithCondition(null, condition, null, 0, 0);
					stdCic.setListStdNK(listStdNK);
				}
			}
			rm.setData(stdCic);
		}catch(Exception ex){
			rm.setState("500");
			rm.setErrorInfo(ex.getMessage());
			rm.setMsgInfo("获取数据出错");
		}
		return rm;
	}
	/**仅用于对成分判定界面查询成分信息的展示问题(成品成分)----蒋崇波
	 * 获取成分判定标准（交付/客户/内控）
	 * @param designKey
	 * @param psc 产品码
	 * @param plineCode 产线编号
	 * @param mscPline 制程号
	 * @return
	 */
	public List<ResultModel> GetOrdJudgeStdCic1(String designKey,String psc,String plineCode,String mscPline){
		List<ResultModel> listrm=new ArrayList<>();
		ResultModel rm=new ResultModel("200","获取成分判定标准成功");
		listrm.add(rm);
		ResultModel rm1=new ResultModel("200","获取成分判定标准成功");
		listrm.add(rm1);
		try{
			SlmOrdDesignStd _designStd=this.getMapper(SlmOrdDesignStdMapper.class).findById(designKey,psc);
			if(DataHelper.IsNullOrSpace(_designStd.getCic())){
				rm.setState("403");
				rm.setMsgInfo("成分索引号不能为空");
				return listrm;
			}
			List<String> liststr=new ArrayList<>();
			OrdJudgeStd<SlmOrdDesignStdCic> stdCic=new OrdJudgeStd<>();
			SlmOrdDesignStdCicMapper cicMapper=this.getMapper(SlmOrdDesignStdCicMapper.class);
			//交付标准. 
			String condition="";
			if(!DataHelper.IsNullOrSpace(_designStd.getCic())){
				condition=DataHelper.CreateSqlCondition(_designStd.getCic().split(";"),"cic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
				List<SlmOrdDesignStdCic> listStdGB=cicMapper.findWithCondition(null, condition, null, 0, 0);
				for (int i = 0; i < listStdGB.size(); i++) {
					if (!liststr.contains(listStdGB.get(i).getChem_code())) {
						liststr.add(listStdGB.get(i).getChem_name());
					}
				}
				stdCic.setListStdGB(listStdGB);
			}

			//客户标准
			if(!DataHelper.IsNullOrSpace(_designStd.getCic_k())){
				condition=DataHelper.CreateSqlCondition(_designStd.getCic_k().split(";"),"cic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
				List<SlmOrdDesignStdCic> listStdKH=cicMapper.findWithCondition(null, condition, null, 0, 0);
				for (int i = 0; i < listStdKH.size(); i++) {
					if (!liststr.contains(listStdKH.get(i).getChem_code())) {
						liststr.add(listStdKH.get(i).getChem_name());
					}
				}
				stdCic.setListStdKH(listStdKH);
			}
			//内控标准
			String process=this.getMapper(ComBasePlineMapper.class).findById(plineCode).getProcess_code();
			SlmOrdDesignMscPlPrc _prc=new SlmOrdDesignMscPlPrc();
			_prc.setDesign_key(designKey);
			_prc.setProcess_code(process);
			_prc.setMsc_pline(mscPline);
			List<SlmOrdDesignMscPlPrc> listPrc=this.getMapper(SlmOrdDesignMscPlPrcMapper.class).find(_prc, 0, 0);
			 if (listPrc.size()>0) {
				
			
			if(!listPrc.get(0).getIs_stdcic().equals("1")){
				stdCic.setNkQuote(true);
			}else{//非引用标准
				if(!DataHelper.IsNullOrSpace(listPrc.get(0).getCic())){
					condition=DataHelper.CreateSqlCondition(listPrc.get(0).getCic().split(","),"cic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
					List<SlmOrdDesignStdCic> listStdNK=cicMapper.findWithCondition(null, condition, null, 0, 0);
					for (int i = 0; i < listStdNK.size(); i++) {
						if (!liststr.contains(listStdNK.get(i).getChem_name())) {
							liststr.add(listStdNK.get(i).getChem_name());
						}
					}
					stdCic.setListStdNK(listStdNK);
				}
				
			}
			 }
			rm1.setData(liststr);
			rm.setData(stdCic);
		}catch(Exception ex){
			rm.setState("500");
			rm.setErrorInfo(ex.getMessage());
			rm.setMsgInfo("获取数据出错");
		}
		return listrm;
	}
	/**
	 * 查询成分标准
	 * @param cp
	 * @return
	 */
	public ResultModel GetOrdDesignStdCic(CommonPage<SlmOrdDesignStd> cp) {
		ResultModel rm = new ResultModel();
		rm.setState("200");
		try {

			SlmOrdDesignStd _designStd=this.getMapper(SlmOrdDesignStdMapper.class).findById(cp.getObject().getDesign_key(),cp.getObject().getPsc());
			if(DataHelper.IsNullOrSpace(_designStd.getCic())){
				rm.setState("403");
				/*rm.setMsgInfo("成分索引号不能为空");*/
				return rm;
			}

			String condition=DataHelper.CreateSqlCondition(_designStd.getCic().split(";"),"cic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(cp.getObject().getDesign_key())+"'";
			List<SlmOrdDesignStdCic> listStdCic=this.getMapper(SlmOrdDesignStdCicMapper.class).findWithCondition(null, condition, null, 0, 0);
			/*if(!DataHelper.IsNullOrSpace(_designStd.getCic_k())){
				condition=DataHelper.CreateSqlCondition(_designStd.getPic_k().split(";"),"cic");
				List<SlmOrdDesignStdCic> listCic=this.getMapper(SlmOrdDesignStdCicMapper.class).findWithCondition(null, condition, null, 0, 0);
				for (SlmOrdDesignStdCic slmOrdDesignStdCic : listCic) {
					for (SlmOrdDesignStdCic _slmOrdDesignStdCic : listStdCic) {
						if(_slmOrdDesignStdCic.getChem_code().equals(slmOrdDesignStdCic.getChem_code())){
							_slmOrdDesignStdCic=slmOrdDesignStdCic;
						}
					}
				}
			}*/
			rm.setData(listStdCic);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 获取成分内控信息
	 * @param cp
	 * @return
	 */
	public ResultModel GetOrdDesignStdCicByCic(String cic,String designKey) {
		ResultModel rm = new ResultModel();
		try {
			if(DataHelper.IsNullOrSpace(cic)){
				rm.setState("404");
				rm.setMsgInfo("成分索引不能为空");
				return rm;
			}
			String condition=DataHelper.CreateSqlCondition(cic, "cic",",")+" and design_key='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
			List<SlmOrdDesignStdCic> listStdCic=this.getMapper(SlmOrdDesignStdCicMapper.class).findWithCondition(null, condition, "cic,chem_code", 0, 0);
			rm.setState("200");
			rm.setData(listStdCic);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("获取成分内控标准信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	
}
