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

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.mapper.ComBasePlineMapper;
import market.Com.vo.ComBaseInfo;
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.SlmOrdDesignStdMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicMapper;
import market.Qms.slmord.service.SlmOrdDesignStdPicService;
import market.Qms.slmord.vo.OrdJudgeStd;
import market.Qms.slmord.vo.SlmOrdDesignMscPlPrc;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdPic;

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

@RemoteService(path="SlmOrdDesignStdPicService")
public class SlmOrdDesignStdPicServiceImpl extends BaseServiceImpl<SlmOrdDesignStdPic,Object> implements SlmOrdDesignStdPicService {
	
	/**
	 * 获取材质判定标准（交付/客户/内控）
	 * @param designKey
	 * @param psc 产品码
	 * @param plineCode 产线编号
	 * @param mscPline 制程号
	 * @return
	 */
	public ResultModel GetOrdJudgeStdPic(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.getPic())){
				rm.setState("403");
				rm.setMsgInfo("材质索引号不能为空");
				return rm;
			}
			OrdJudgeStd<SlmOrdDesignStdPic> stdPic=new OrdJudgeStd<>();
			SlmOrdDesignStdPicMapper picMapper=this.getMapper(SlmOrdDesignStdPicMapper.class);
			String condition="";
			//交付标准. 
			if(!DataHelper.IsNullOrSpace(_designStd.getPic())){
				condition=DataHelper.CreateSqlCondition(_designStd.getPic().split(";"),"pic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
				List<SlmOrdDesignStdPic> listStdGB=picMapper.findWithCondition(null, condition, null, 0, 0);
				stdPic.setListStdGB(listStdGB);
			}

			//客户标准
			if(!DataHelper.IsNullOrSpace(_designStd.getCic_k())){
				condition=DataHelper.CreateSqlCondition(_designStd.getCic_k().split(";"),"pic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
				List<SlmOrdDesignStdPic> listStdKH=picMapper.findWithCondition(null, condition, null, 0, 0);
				stdPic.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")){
				stdPic.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<SlmOrdDesignStdPic> listStdNK=picMapper.findWithCondition(null, condition, null, 0, 0);
					stdPic.setListStdNK(listStdNK);
				}
			}
			rm.setData(stdPic);
		}catch(Exception ex){
			rm.setState("500");
			rm.setErrorInfo(ex.getMessage());
			rm.setMsgInfo("获取数据出错");
		}
		return rm;
	}
	
	
	/**
	 * 获取材质内控信息
	 * @param pic
	 * @param designKey
	 * @return
	 */
	public ResultModel GetOrdDesignStdPicByPic(String pic,String designKey) {
		ResultModel rm = new ResultModel();
		try {
			if(DataHelper.IsNullOrSpace(pic)){
				rm.setState("404");
				//rm.setMsgInfo("探伤索引不能为空");
				return rm;
			}
			String condition=DataHelper.CreateSqlCondition(pic, "pic",",")+" and design_key='"+DataHelper.TrimSqlDangerMark(designKey)+"'";
			List<SlmOrdDesignStdPic> listStdPic=this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null, condition, "pic", 0, 0);
			rm.setState("200");
			rm.setData(listStdPic);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败："+e.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 查询
	 * @param cp
	 * @return
	 */
	public ResultModel GetOrdDesignStdPic(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.getPic())){
				rm.setState("403");
				//rm.setMsgInfo("材质索引号不能为空");
				return rm;
			}
			String condition=DataHelper.CreateSqlCondition(_designStd.getPic().split(";"),"pic")+" And DESIGN_KEY='"+DataHelper.TrimSqlDangerMark(cp.getObject().getDesign_key())+"'";
			List<SlmOrdDesignStdPic> listStdPic=this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null, condition, null, 0, 0);
/*			if(!DataHelper.IsNullOrSpace(_designStd.getPic_k())){
				condition=DataHelper.CreateSqlCondition(_designStd.getPic_k().split(";"),"pic");
				List<SlmOrdDesignStdPic> listPic=this.getMapper(SlmOrdDesignStdPicMapper.class).findWithCondition(null, condition, null, 0, 0);
				for (SlmOrdDesignStdPic slmOrdDesignStdPic : listPic) {
					for (SlmOrdDesignStdPic _slmOrdDesignStdPic : listStdPic) {
						if(_slmOrdDesignStdPic.getPhy_code_s().equals(slmOrdDesignStdPic.getPhy_code_s())){
							_slmOrdDesignStdPic=slmOrdDesignStdPic;
						}
					}
				}
			}*/

			String[] addStd=new String[listStdPic.size()];//附加标准编码集
			for (int i = 0; i < listStdPic.size(); i++) {
				if(!DataHelper.IsNullOrSpace(listStdPic.get(i).getAddstdcon_code())){
					addStd[i]=listStdPic.get(i).getAddstdcon_code();
				}
			}
			condition=DataHelper.CreateSqlCondition(addStd, "base_code");
			List<ComBaseInfo> listBase=this.getMapper(ComBaseInfoMapper.class).findWithCondition(null, condition, null, 0, 0);
			//构造附加标准项信息
			for (SlmOrdDesignStdPic stdPic : listStdPic) {
				if(DataHelper.IsNullOrSpace(stdPic.getAddstdcon_code())){
					continue;
				}
				for (ComBaseInfo comBaseInfo : listBase) {
					if(comBaseInfo.getBase_code().equals(stdPic.getAddstdcon_code())){
						stdPic.setAddstdcon_name(comBaseInfo.getBase_name());
						break;
					}
				}
			}
			rm.setData(listStdPic);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败："+e.getMessage());
		}
		return rm;
	}
}
