/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-04-03
 * 描述            ：试样号与合同信息关系表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBasePhyBMapper;
import market.Com.vo.ComBasePhyB;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.qcm.mapper.QcmJhySampleConsignDItemMapper;
import market.Qms.qcm.mapper.QcmJhySampleConsignDMapper;
import market.Qms.qcm.mapper.QcmJhySampleROrdMapper;
import market.Qms.qcm.service.QcmJhySampleROrdService;
import market.Qms.qcm.vo.QcmJhySampleConsignD;
import market.Qms.qcm.vo.QcmJhySampleConsignDItem;
import market.Qms.qcm.vo.QcmJhySampleROrd;
import market.Qms.qcm.vo.QcmJhySampleROrdTemp;
import market.Qms.qcm.vo.QcmJudgePhysicalTemp;

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

@RemoteService(path="QcmJhySampleROrdService")
public class QcmJhySampleROrdServiceImpl extends BaseServiceImpl<QcmJhySampleROrd,Object> implements QcmJhySampleROrdService {
	
	/**
	 * 获取待成分判定数据
	 * @param sample
	 * @return
	 */
	public ResultModel GetSampleChemInfo(QcmJhySampleROrd sample){
		ResultModel rm=new ResultModel("200","获取待成分判定数据成功");
		try{
			String condition="";
			if(!DataHelper.IsNullOrSpace(sample.getHeat_no())){
				condition+="and m.heat_no like '%"+DataHelper.TrimSqlDangerMark(sample.getHeat_no())+"%' ";
			}
			if(!DataHelper.IsNullOrSpace(sample.getInspection_lot())){
				condition+="and m.inspection_lot like '%"+DataHelper.TrimSqlDangerMark(sample.getInspection_lot())+"%' ";	
			}
			if(!DataHelper.IsNullOrSpace(sample.getSmp_no())){
				condition+="and m.smp_no like '%"+DataHelper.TrimSqlDangerMark(sample.getSmp_no())+"%' ";
			}
			if(!DataHelper.IsNullOrSpace(sample.getCert_inst_code())){
				condition+="and m.cert_inst_code like '%"+DataHelper.TrimSqlDangerMark(sample.getCert_inst_code())+"%' ";
			}
			if(!DataHelper.IsNullOrSpace(sample.getJudge_status())){
				condition+="and m.judge_status like '%"+DataHelper.TrimSqlDangerMark(sample.getJudge_status())+"%' ";
			}
			if(condition.startsWith("and")){
				condition=condition.replaceFirst("and", "");
			}
			List<QcmJhySampleROrd> listSampleOrd=this.getMapper(QcmJhySampleROrdMapper.class).GetSampleChemInfo(condition);
			rm.setData(listSampleOrd);			
		}catch(Exception ex){
			rm.setErrorInfo(ex.getMessage());
			rm.setMsgInfo("获取待成分判定信息失败");
			rm.setState("500");
		}
		return rm;
	}
	
	
	/**
	 * 材质判定管理-查询试样信息
	 * sum.smp_no 试样号
	 * sum.heat_no 炉号
	 * sum.inspection_lot 检验号
	 * sum.cert_inst_name 认证机构名称
	 * sum.judge_result 判定结果
	 * */
	public ResultModel getSampleInformation(CommonPage<QcmJhySampleROrd> sum){
		ResultModel rm=new ResultModel();
		QcmJhySampleROrd qsr=sum.getObject();
		try {
			String con=""; 
			//销售订单号
			if (!DataHelper.IsNullOrSpace(qsr.getOrder_no())) {
				con+=" and t1.order_no like '%"+qsr.getOrder_no()+"%'";
			}
			//产品序号
			if (!DataHelper.IsNullOrSpace(qsr.getMaterial_no())) {
				con+=" and t1.material_no like '%"+qsr.getOrder_no()+"%'";
			}
			//开始时间
			if(sum.getStartTime()!=null&&!sum.getStartTime().equals("")){
				con+=" and t3.create_time>=to_date(concat('"+sum.getStartTime()+"',' 00:00:00'),'yyyy-MM-dd HH24:mi:ss')"; 
			}
			//结束时间
			if(sum.getEndTime()!=null&&!sum.getEndTime().equals("")){
				con+=" and t3.create_time<=to_date(concat('"+sum.getEndTime()+"',' 23:59:59'),'yyyy-MM-dd HH24:mi:ss') ";
			}
			//判定状态
			if (!DataHelper.IsNullOrSpace(qsr.getJudge_status())) {
				con+=" and t1.JUDGE_STATUS='"+qsr.getJudge_status()+"'";
			}
			// 判定结果
			if (!DataHelper.IsNullOrSpace(sum.getMemo1()) && "1".equals(sum.getMemo1())) {
				con+=" and (t1.JUDGE_RESULT_CODE='40740203' or t1.JUDGE_RESULT_CODE is null)";
			} else if (!DataHelper.IsNullOrSpace(qsr.getJudge_result_code())) {
				con+=" and t1.JUDGE_RESULT_CODE='"+qsr.getJudge_result_code()+"'";
			}
			//炉号
			if(!DataHelper.IsNullOrSpace(qsr.getHeat_no())){
				con+=" and t1.heat_no like '%"+DataHelper.TrimSqlDangerMark(qsr.getHeat_no())+"%' ";
			}
			//检验号
			if(!DataHelper.IsNullOrSpace(qsr.getInspection_lot())){
				con+=" and t1.inspection_lot like '%"+DataHelper.TrimSqlDangerMark(qsr.getInspection_lot())+"%' ";	
			} 
			//试样号
			if(!DataHelper.IsNullOrSpace(qsr.getSmp_no())){
				con+=" and t1.smp_no like '%"+DataHelper.TrimSqlDangerMark(qsr.getSmp_no())+"%' ";
			}
			//轧批号
			if(!DataHelper.IsNullOrSpace(qsr.getBatch_no())){
				con+=" and t1.batch_no like '%"+DataHelper.TrimSqlDangerMark(qsr.getBatch_no())+"%' ";
			}
			//是否判定(0未判定 1已判定)
			if (!DataHelper.IsNullOrSpace(qsr.getJudge_status())) {
				con += " and t1.judge_status = '"+ qsr.getJudge_status() +"'";
			}
			//投料卷号
			if(!DataHelper.IsNullOrSpace(qsr.getObject_no())){
				con += " and pmr.object_no like '%"+DataHelper.TrimSqlDangerMark(qsr.getObject_no())+"%' ";
			}
			//原料卷号
			if (!DataHelper.IsNullOrSpace(qsr.getMaterailcoilno())) {
				con += " and ypd.materailcoilno like '%"+DataHelper.TrimSqlDangerMark(qsr.getMaterailcoilno())+"%' ";
			}
			List<QcmJhySampleROrdTemp> st = this.getMapper(QcmJhySampleROrdMapper.class).getSampleInformation(con);
			rm.setData(st);
			rm.setState("200");
		} catch (Exception e) {
			rm.setMsgInfo("获取试样信息失败!");
			rm.setState("500");
			e.printStackTrace();
		}
		return rm;
	}
	/**
	 * 人工判定后把结果更新到QCM_JHY_SAMPLE_R_ORD
	 * 
	 * */
	public ResultModel updateQcmSample(CommonPage<QcmJudgePhysicalTemp> sum){
		ResultModel rm=new ResultModel();
		try {
			QcmJudgePhysicalTemp qpt=sum.getObject();
			//this.getMapper(QcmJhySampleROrdMapper.class).updateQcmSample(qpt);
			
			this.getMapper(QcmJhySampleROrdMapper.class).updateQcmSample(qpt);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			rm.setState("500");
			rm.setErrorInfo(e.getMessage());
			rm.setMsgInfo("更新判定结果失败");
			
		}
		return rm;
	}
	/**
	 * 材质重取样
	 * @param sum
	 * @return
	 */
	public ResultModel AgainPhySendSample(List<QcmJhySampleConsignDItem> sum,QcmJhySampleConsignD sum1){
		ResultModel rm=new ResultModel();
		List<QcmJhySampleConsignDItem> listDItem = new ArrayList<>();
		//去除重复的检验项
		for(QcmJhySampleConsignDItem qjsc : sum){
			Boolean flag = false;  //检验项是否已存在
			for(QcmJhySampleConsignDItem q : listDItem){
				if(q.getPhy_code_s().equals(qjsc.getPhy_code_s())){
					flag = true;
					break;
				}
			}
			if(!flag){
				listDItem.add(qjsc);
			}
		}
		QcmJhySampleConsignD consignD=sum1;
		//获取材质大类基础表com_base_phy_b获取复样倍数
//		int a=listDItem.size();
//		for (QcmJhySampleConsignDItem q : listDItem) {
//			
//		}
//		this.getMapper(ComBasePhyBMapper.class).findById();
		QcmJhySampleConsignDMapper consignDMapper=this.getMapper(QcmJhySampleConsignDMapper.class);
		try {
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			//在QCM_JHY_SAMPLE_CONSIGN_D表根据试样号新增一条跟最大取样类似的数据
			List<QcmJhySampleConsignD> listConsignD=consignDMapper.find(consignD, 0, 0);
			DataHelper.CopyEntity(listConsignD.get(0), consignD);
			consignD.setConsign_no(null);
            consignD.setConsign_no_seq(null);
            consignD.setSmp_type_code("409802");
			consignD.setSmp_type_name("复样");
            //获取最大取样编号
            int count=0;
//			获取材质结果集编号最大值
			List<QcmJhySampleConsignD> qrlist=consignDMapper
					.findWithCondition(null, "SMP_NO ='"+consignD.getSmp_no()+"'", "SPECIMEN_NO DESC", 0, 1);
			if(qrlist!=null&&qrlist.size()>0){
				String s=qrlist.get(0).getSpecimen_no();
				count=Integer.parseInt(s.substring(s.length()-2, s.length()));
			}
			count++;
			String specimen_no=consignD.getSmp_no()+String.format("%02d",count);
			consignD.setSpecimen_no(specimen_no);
			consignD.setOld_consign_no(listConsignD.get(0).getSpecimen_no());
			consignD.setCreate_name(username);
			consignD.setCreate_time(new Date());
			consignD.setUpdate_name(null);
			consignD.setUpdate_time(null);
			consignD.setStatus("0");
			consignD.setValidflag("1");
			consignD.setTest_qty((long)2);
			consignDMapper.insert(consignD);
            //根据最大取样编号新增项目
			Long seq=(long) 1;//检验项目流水号
			for (QcmJhySampleConsignDItem ditem : listDItem) {
				ComBasePhyB findById = this.getMapper(ComBasePhyBMapper.class).findById(ditem.getPhy_code_l());
				if (findById!=null) {
					if (findById.getRe_smp_mult()!=null) {
						for(int flag=1;flag<=findById.getRe_smp_mult();flag++) {
							QcmJhySampleConsignDItem qcm=new QcmJhySampleConsignDItem();
							qcm.setSpecimen_no(specimen_no);
							qcm.setPhy_code_l(ditem.getPhy_code_l());
							qcm.setPhy_name_l(ditem.getPhy_name_l());
							qcm.setPhy_code_m(ditem.getPhy_code_m());
							qcm.setPhy_name_m(ditem.getPhy_name_m());
							qcm.setPhy_code_s(ditem.getPhy_code_s());
							qcm.setPhy_name_s(ditem.getPhy_name_s());
							qcm.setItem_code_d(ditem.getItem_code_d());
							qcm.setItem_name_d(ditem.getItem_name_d());
							qcm.setItem_code_s(ditem.getItem_code_s());
							qcm.setItem_name_s(ditem.getItem_name_s());
							qcm.setItem_code_t(ditem.getItem_code_t());
							qcm.setItem_name_t(ditem.getItem_name_t());
							qcm.setCreate_name(username);
							qcm.setCreate_time(new Date());
							qcm.setSeq(seq);
							qcm.setIs_send("1");
							seq++;
							this.getMapper(QcmJhySampleConsignDItemMapper.class).insert(qcm);
						}
					}
				}
			}
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("重取样失败");
			rm.setErrorInfo(e.getMessage());
		}
		
		return rm;
	}
}
