/***文档注释***********************************************
 * 作者               :                  
 * 创建日期      :                   2019.02.24
 * 描述               :                   物流和质量模块调用的公共接口
 * 注意事项      :                    无
 * 遗留BUG :                     无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/

package market.Api.Lms.impl;

import java.util.HashMap;
import java.util.List;

import market.Api.Lms.service.LmsQmsApiService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.vo.YdmProductDetail;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="LmsQmsApiService")
public class LmsQmsApiServiceImpl extends BaseServiceImpl<Object,Object> implements LmsQmsApiService {
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2019年2月24日
	 * @parameter memo1:销售订单号orderno  检验号inspection_lot
	 * @return 200 ✔  500✘
	 * @description 判定销售合同号和检验号的综合判定结果是否都存在
	 */
	public ResultModel doJudgeIsPhysresult(CommonPage<YdmProductDetail> cp) throws Exception {
		ResultModel rm = new ResultModel();
		String orderno = cp.getMemo1();//销售合同号
		String inspection_lot = cp.getMemo2();//检验号
		try {
			YdmProductDetail ypd = new YdmProductDetail();
			ypd.setOrderno(orderno);
			ypd.setState("0");
			ypd.setInspection_lot(inspection_lot);
			List<YdmProductDetail> ypdList = this.getMapper(YdmProductDetailMapper.class).find(ypd, 0, 0);
			if (ypdList!=null && ypdList.size()>0) {
				boolean b = false;
				for (YdmProductDetail ypD : ypdList) {
					if (ypD.getPhysresult()!=null || !"".equals(ypD.getPhysresult())) {
						b=true;//如果有综合判定结果不为空，则跳出循环
						continue;
					}
				}
				if (!b) {//b=true
					rm.setState("500");
					rm.setMsgInfo("销售订单号【"+orderno+"】对应的库存信息已经存在综合判定结果");
					return rm;
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("未找到销售订单号【"+orderno+"】对应的在库库存信息");
				return rm;
			}
		} catch (Exception e) {
			throw new Exception("判断销售订单号的综合判定结果失败");
		}
		rm.setState("200");
		rm.setMsgInfo("销售订单号【"+orderno+"】都有对应的综合判定结果");
		return rm;
	}
	

	/**
	 * 根据卷号更新库存表 的 材质判定结果和材质判定结果id(PHYSRESULT)
	 * @param coilno 卷号
	 * @param result 材质判定结果
	 * @param resultId 材质判定结果id
	 * @return
	 */
	public ResultModel updatePhysResultByCoilno(String coilno,String result,String resultId){
		ResultModel rm = new ResultModel();
		if (coilno==null || "".equals(coilno)) {
			rm.setData("500");
			rm.setMsgInfo("卷号不能为空！");
		}
		if (result==null || "".equals(result)) {
			rm.setData("500");
			rm.setMsgInfo("材质检验结果不能为空！");
		}	
		if (resultId==null || "".equals(resultId)) {
			rm.setData("500");
			rm.setMsgInfo("材质判定结果ID不能为空！");
		}
		this.getMapper(YdmProductDetailMapper.class).updatePhysResultByCoilno(coilno,result,resultId);
		rm.setMsgInfo("更新材质判定结果成功");
		rm.setState("200");
		return rm;		
	}
	
	
	
	/**
	 * 根据卷号更新库存表 的 探伤判定结果和探伤判定结果id(SPYRESULT)
	 * @param coilno 卷号
	 * @param result 探伤判定结果
	 * @param resultId 探伤判定结果id
	 * @return
	 */
	public ResultModel updateSpyResultByCoilno(String coilno,String result,String resultId){
		ResultModel rm = new ResultModel();
		if (coilno==null || "".equals(coilno)) {
			rm.setData("500");
			rm.setMsgInfo("卷号不能为空！");
		}
		if (result==null || "".equals(result)) {
			rm.setData("500");
			rm.setMsgInfo("探伤检验结果不能为空！");
		}	
		if (resultId==null || "".equals(resultId)) {
			rm.setData("500");
			rm.setMsgInfo("探伤判定结果ID不能为空！");
		}
		this.getMapper(YdmProductDetailMapper.class).updateSpyResultByCoilno(coilno,result,resultId);
		rm.setMsgInfo("更新探伤判定结果成功");
		rm.setState("200");
		return rm;		
	}
	
	/**
	 * 根据卷号更新库存表 的 卡量判定结果和卡量判定结果id(MEASURE_RESULT)
	 * @param coilno 卷号
	 * @param result 卡量判定结果
	 * @param resultId 卡量判定结果id
	 * @return
	 */
	public ResultModel updateMeaResultByCoilno(String coilno,String result,String resultId){
		ResultModel rm = new ResultModel();
		if (coilno==null || "".equals(coilno)) {
			rm.setData("500");
			rm.setMsgInfo("卷号不能为空！");
			return rm; 
		}
		if (result==null || "".equals(result)) {
			rm.setData("500");
			rm.setMsgInfo("卡量检验结果不能为空！");
			return rm; 
		}	
		if (resultId==null || "".equals(resultId)) {
			rm.setData("500");
			rm.setMsgInfo("卡量判定结果ID不能为空！");
			return rm; 
		}
		this.getMapper(YdmProductDetailMapper.class).updateMeaResultByCoilno(coilno,result,resultId);
		rm.setMsgInfo("更新卡量判定结果成功");
		rm.setState("200");
		return rm;		
	}
	
	/**
	 * 分机台 查询投料卷下的物料的质量信息
	 * @param cp coilno:投料卷号 machine_code:机台代码
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ResultModel doQuaryQmsDetailByCoilno(CommonPage<YdmProductDetail> cp){
		ResultModel rm = new ResultModel();
		try {
			YdmProductDetail ypd = cp.getObject();
			if (ypd.getCoilno()==null || "".equals(ypd.getCoilno())) {
				rm.setState("200");
				rm.setMsgInfo("请传入投料大卷号!");
				return rm;
			}
			if (ypd.getMachine_code()==null || "".equals(ypd.getMachine_code())) {
				rm.setState("200");
				rm.setMsgInfo("请传入机台代码!");
				return rm;
			}
			String swhere = "";
			List<HashMap> list = null;
			if ("M0001".equals(ypd.getMachine_code()) || 
				"M0002".equals(ypd.getMachine_code())) {
				//4mm分条机组 12mm分条机组
				swhere += " and mcm.material_no = '"+ypd.getCoilno()+"'";
				list = this.getMapper(YdmProductDetailMapper.class).doQuaryDetailZJ(swhere);
			}else if ("M0003".equals(ypd.getMachine_code())) {
				//3mm横剪机组
				swhere += " and mccm.material_no = '"+ypd.getCoilno()+"'";
				list = this.getMapper(YdmProductDetailMapper.class).doQuaryDetailHJ(swhere);
			}else if ("M0007".equals(ypd.getMachine_code())) {
				//平整机组
				swhere += " and mtm.material_no = '"+ypd.getCoilno()+"'";
				list = this.getMapper(YdmProductDetailMapper.class).doQuaryDetailPZ(swhere);
			}else if ("M0008".equals(ypd.getMachine_code())) {
				//轧机机组
				swhere += " and mrm.material_no = '"+ypd.getCoilno()+"'";
				list = this.getMapper(YdmProductDetailMapper.class).doQuaryDetailZhaJ(swhere);
			}else if ("M0004".equals(ypd.getMachine_code()) || "M000T".equals(ypd.getMachine_code()) ||
					  "M0005".equals(ypd.getMachine_code()) || 
					  "M0006".equals(ypd.getMachine_code()) || 
					  "M0009".equals(ypd.getMachine_code())) {
				//退火炉宝信  退火炉南方D 退火炉艾伯纳  退火炉南方G
				swhere += " and mcam.material_no = '"+ypd.getCoilno()+"'";
				list = this.getMapper(YdmProductDetailMapper.class).doQuaryDetailTH(swhere);
			}else if ("M0010".equals(ypd.getMachine_code())) {
				//酸洗
				swhere += " and mpm.material_no = '"+ypd.getCoilno()+"'";
				list = this.getMapper(YdmProductDetailMapper.class).doQuaryDetailSX(swhere);
			}
			rm.setData(list);
			rm.setState("200");
		} catch (Exception e) {
			rm.setMsgInfo("查询失败!");
			rm.setState("500");
		}
		return rm;
	}
	
	/**
	 * 新增质量接口：根据卷号清除库存表质量信息
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel removeDetailQmsInfo(CommonPage<YdmProductDetail> cp) throws Exception{
		ResultModel rm = new ResultModel();
		List<YdmProductDetail> ypdList = cp.getObjectList();
		if (ypdList==null || ypdList.size()<=0) {
			throw new Exception("请传入参数");
		}
		for (YdmProductDetail ypd : ypdList) {
			if (ypd.getCoilno()==null || "".equals(ypd.getCoilno())) {
				throw new Exception("请传入卷号");
			}			
			this.getMapper(YdmProductDetailMapper.class).removeDetailQmsInfo(ypd.getCoilno());		
		}		
		rm.setMsgInfo("清空库存表的质量信息成功");
		rm.setState("200");
		return rm;		
	}

	/**
	 * 查询 脱单、在库、没有被质量锁定的物料库存信息
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQuaryQmsDetailInfo(CommonPage<YdmProductDetail> cp) throws Exception{
		ResultModel rm = new ResultModel();
		YdmProductDetail ypd = cp.getObject();
		try {
			List<YdmProductDetail> list = this.getMapper(YdmProductDetailMapper.class).doQuaryQmsDetailInfo(ypd);
			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		rm.setMsgInfo("查询质量信息成功");
		rm.setState("200");
		return rm;		
	}
	
	/**
	 * 质量模块，根据卷号修改库存状态
	 * @param coilno
	 * @param state
	 */
	public void doUpdateLmsQmsState(String coilno,String state){
		 YdmProductDetail ypd = new YdmProductDetail();
		 ypd.setCoilno(coilno);
		 ypd.setState(state);
		 this.getMapper(YdmProductDetailMapper.class).update(ypd);
	}
	
	/**
	 * 质量模块，根据卷号修改库存信息
	 * @param coilno
	 * @param state
	 * @throws Exception 
	 */
	public void doUpdateLmsQmsInfo(YdmProductDetail ypd) throws Exception{
		if (ypd.getCoilno()==null || "".equals(ypd.getCoilno())) {
			throw new Exception("卷号不能为null");
		}
		this.getMapper(YdmProductDetailMapper.class).update(ypd);
	}
	
}

