/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.11.21
 * 描述               :        			成品卷包装明细
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/

package market.Mes.Packing.Performance.impl;

import java.util.ArrayList;
import java.util.List;

import market.Api.Lms.impl.LmsApiServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Mes.Lengthwise.Plan.mapper.PlnMaterielDetailMapper;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielDetail;
import market.Mes.Packing.Performance.mapper.MesCoilPackingDMapper;
import market.Mes.Packing.Performance.mapper.MesCoilPackingMMapper;
import market.Mes.Packing.Performance.service.MesCoilPackingDService;
import market.Mes.Packing.Performance.vo.MesCoilPackingD;
import market.Mes.Packing.Performance.vo.MesCoilPackingM;
import market.Mes.Packing.Plan.mapper.PlnPackingPlanDetailMapper;
import market.Mes.Packing.Plan.mapper.PlnPackingPlanMapper;
import market.Mes.Packing.Plan.vo.PlnPackingPlan;
import market.Mes.Packing.Plan.vo.PlnPackingPlanDetail;
import market.Mes.Packing.Plan.vo.PlnPackingPlanSum;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="MesCoilPackingDService")
public class MesCoilPackingDServiceImpl extends BaseServiceImpl<MesCoilPackingD,Object> implements MesCoilPackingDService {
	/**
	 * ******车间MES********
	 * 查询包装实绩信息
	 * startTime 开始时间
	 * endTime 结束时间
	 * memo1 coil_no 包含卷号
	 * */
	public ResultModel doQueryWeb(CommonPage<MesCoilPackingD> oci){
		ResultModel rm=new ResultModel();
		try{
			//班次 wrk_ord  班组 wrk_grp 日期 wrk_date可从session得到，此处为前端传后期考虑调整
			rm.setData(this.getMapper(MesCoilPackingDMapper.class).doQuery(oci.getObject()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * ******车间MES********
	 * 新增钢卷号信息
	 * memo1 machine_code 机台号
	 * memo2 成品卷包装计量主实绩 成品卷包号packing_no
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<PlnPackingPlanSum> oci) throws Exception {
		ResultModel rm=new ResultModel();
		
		//验证选中的包装主实绩记录存在，且为未入库状态
		MesCoilPackingM mcpm=this.getMapper(MesCoilPackingMMapper.class).findById(oci.getMemo2());
		if(mcpm!=null&&mcpm.getPacking_no()!=null){
			if(!"0".equals(mcpm.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("包装主实绩不为待入库状态！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("包装主实绩记录不存在！");
			return rm;
		}
		
		//调用物流模块接口，验证选中的包装后成品卷包库存明细记录存在，且为待入库状态，否则终止操作
		CommonPage<YdmProductDetail> cp=new CommonPage<YdmProductDetail>();
		YdmProductDetail ypdValid=new YdmProductDetail();
		ypdValid.setCoilno(oci.getMemo2());
		cp.setObject(ypdValid);
		ResultModel rmValid = this.getBean(LmsApiServiceImpl.class).doConfirmWaitIn(cp);
		if(!"200".equals(rmValid.getState())){
			return rmValid;
		}
		
		
		//验证订单号
		String sOrder="";
		List<PlnPackingPlanSum> lppps=oci.getObjectList();
		List<MesCoilPackingD> lmcpd=new ArrayList<MesCoilPackingD>();
		List<PlnPackingPlanDetail> lpppdu=new ArrayList<PlnPackingPlanDetail>();
		
		for(PlnPackingPlanSum ppps:lppps){
			//逐一验证勾选的作业计划主表记录存在
			PlnPackingPlan ppp=new PlnPackingPlan();
			ppp.setMachine_code(oci.getMemo1());
			ppp.setPk_plan_no(ppps.getPk_plan_no());
			List<PlnPackingPlan> lppp=this.getMapper(PlnPackingPlanMapper.class).find(ppp, 0, 0);
			if(lppp==null||lppp.size()==0){
				rm.setState("500");
				rm.setMsgInfo("作业计划主表记录不存在！");
				return rm;
			}
			
			//逐一验证勾选的作业计划明细表记录存在
			PlnPackingPlanDetail pppd=new PlnPackingPlanDetail();
			pppd.setMachine_code(oci.getMemo1());
			pppd.setPk_plan_no(ppps.getPk_plan_no());
			pppd.setObject_no_vt(ppps.getObject_no_vt());
			pppd.setObject_no(ppps.getObject_no());
			pppd.setStatus("0PPK02");
			List<PlnPackingPlanDetail> lpppd=this.getMapper(PlnPackingPlanDetailMapper.class).find(pppd, 0, 0);
			if(lpppd==null||lpppd.size()==0){
				rm.setState("500");
				rm.setMsgInfo("作业计划明细表记录不存在！");
				return rm;
			}
			
			//逐一验证库存是否存在
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoilno(ppps.getObject_no());
			ypd.setState("0");
			List<YdmProductDetail> lypd=this.getMapper(YdmProductDetailMapper.class).findWithCondition(ypd, " packing_group is null ", "", 0, 0);
			if(lypd==null||lypd.size()==0){
				rm.setState("500");
				rm.setMsgInfo("钢卷库存明细记录不存在！");
				return rm;
			}
			
			//逐一验证选中的钢卷无包装实绩
			MesCoilPackingD mcpd=new MesCoilPackingD();
			//----------------主键值------------
			mcpd.setCoil_no(ppps.getObject_no());
			
			int icount=this.getMapper(MesCoilPackingDMapper.class).count(mcpd);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("钢卷已存在包装实绩！");
				return rm;
			}
			
			//逐一验证选中的钢卷都属于同一订单号
			if(!"".equals(sOrder)){
				if(!sOrder.equals(ppps.getOrder_no())){
					rm.setState("500");
					rm.setMsgInfo("同一订单的卷才能在一个包装内！");
					return rm;
				}
			}else{
				sOrder=ppps.getOrder_no();
			}
			
			
			PlnMaterielDetail pmd=new PlnMaterielDetail();
			pmd.setObject_no_vt(lppps.get(0).getObject_no_vt());
			List<PlnMaterielDetail> lpmd=this.getMapper(PlnMaterielDetailMapper.class).find(pmd, 0, 0);
			
			
			//子表数据新增
			mcpd.setOrd_thk(lpmd.get(0).getThick());
			mcpd.setOrd_wdt(lpmd.get(0).getWidth());
			mcpd.setOrd_len(lpmd.get(0).getLength());
			mcpd.setOrd_inn_dia(lpmd.get(0).getDiameter_inn());
			mcpd.setOrd_out_dia(lpmd.get(0).getDiameter_out());
			
			mcpd.setAct_len(lypd.get(0).getLength());
			mcpd.setAct_thk(lypd.get(0).getThick());
			mcpd.setAct_wdt(lypd.get(0).getWidth());
			mcpd.setAct_inn_dia(lypd.get(0).getDiameter_inn());
			mcpd.setAct_out_dia(lypd.get(0).getDiameter_out());
			mcpd.setProduce_time(lypd.get(0).getProducedate());
			mcpd.setCoil_thr_wgt(lypd.get(0).getTheoryweight());//理重
			mcpd.setCoil_act_wgt(lypd.get(0).getPackweight());//实重
			
			mcpd.setOrder_no(ppps.getOrder_no());
			mcpd.setProd_order_no(ppps.getProd_order_no());
			
			mcpd.setPacking_no(oci.getMemo2());
			mcpd.setSchedule_no(lppp.get(0).getPk_plan_no());//***********
			mcpd.setCoil_no_vt(lpmd.get(0).getObject_no_vt());//***********
			
			lmcpd.add(mcpd);
			
			//逐一修改包装作业计划明细记录状态为包装作业完成
			PlnPackingPlanDetail pppdu=new PlnPackingPlanDetail();
			pppdu.setSerial_no(lpppd.get(0).getSerial_no());
			pppdu.setStatus("0PPK10");
			lpppdu.add(pppdu);
		}
		
		List<YdmProductDetail> lypd=new ArrayList<YdmProductDetail>();
		//包装明细数据插入
		for(MesCoilPackingD mcpd:lmcpd){
			this.getMapper(MesCoilPackingDMapper.class).insert(mcpd);
			
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoilno(mcpd.getCoil_no());
			ypd.setPacking_group(mcpm.getPacking_group());
			lypd.add(ypd);
		}
		
		//调用物流模块接口，逐一修改库存明细的包装等级
		cp.setObjectList(lypd);
		this.getBean(LmsApiServiceImpl.class).doUpdatePackingGroup(cp);
	
		//原始钢卷进行出库操作
		this.getBean(LmsApiServiceImpl.class).doRollOut(cp);
		
		//逐一修改包装作业计划明细记录状态为包装作业完成
		for(PlnPackingPlanDetail pppdu:lpppdu){
			this.getMapper(PlnPackingPlanDetailMapper.class).update(pppdu);
		}
		
		rm.setState("200");
		return rm;
	}
	
	/**
	 * ******车间MES********
	 * 删除钢卷号信息 Packing_no  Packing_line_code
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<MesCoilPackingM> oci) throws Exception{
		ResultModel rm=new ResultModel();
		
		MesCoilPackingM mcpmO=oci.getObject();
		
		//调用物流模块接口，验证选中的包装后成品卷包库存明细记录存在，且为待入库状态
		CommonPage<YdmProductDetail> cp=new CommonPage<YdmProductDetail>();
		YdmProductDetail ypd=new YdmProductDetail();
		ypd.setCoilno(oci.getMemo2());
		cp.setObject(ypd);
		ResultModel rmValid = this.getBean(LmsApiServiceImpl.class).doConfirmWaitIn(cp);
		if(!"200".equals(rmValid.getState())){
			return rmValid;
		}
		
		//验证选中的包装主实绩记录存在，且为未入库状态
		MesCoilPackingM mcpm=this.getMapper(MesCoilPackingMMapper.class).findById(mcpmO.getPacking_no());
		if(mcpm!=null&&mcpm.getPacking_no()!=null){
			if(!"0".equals(mcpm.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("包装主实绩不为待入库状态！");
				return rm;
			}
		}else{
			rm.setState("500");
			rm.setMsgInfo("包装主实绩记录不存在！");
			return rm;
		}
		
		
		
		List<PlnPackingPlanDetail> lpppd=new ArrayList<PlnPackingPlanDetail>();
		int imcpmCount=0;
		for(MesCoilPackingD mcpd:mcpmO.getChild()){
			//调用物流模块接口，逐一验证选中的成品卷包对应的钢卷明细库存记录存在，且为包装出库状态
			ypd.setCoilno(mcpd.getCoil_no());
			cp.setObject(ypd);
			rmValid = this.getBean(LmsApiServiceImpl.class).doConfirmPackingOut(cp);
			if(!"200".equals(rmValid.getState())){
				return rmValid;
			}
			
			//逐一验证勾选的作业计划主表记录存在
			PlnPackingPlan ppp=new PlnPackingPlan();
			ppp.setPk_plan_no(mcpd.getSchedule_no());
			ppp.setMachine_code(mcpm.getPacking_line_code());
			int icount=this.getMapper(PlnPackingPlanMapper.class).count(ppp);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("钢卷["+mcpd.getCoil_no()+"]对应的包装作业计划主记录不存在！");
				return rm;
			}
			
			PlnPackingPlanDetail pppd=new PlnPackingPlanDetail();
			pppd.setMachine_code(mcpm.getPacking_line_code());
			pppd.setPk_plan_no(mcpd.getSchedule_no());
			pppd.setObject_no_vt(mcpd.getCoil_no_vt());
			pppd.setObject_no(mcpd.getCoil_no());
			pppd.setStatus("0PPK10");
			//有且仅有一条
			List<PlnPackingPlanDetail> lpppdQ=this.getMapper(PlnPackingPlanDetailMapper.class).find(pppd, 0, 0);
			if(lpppdQ==null||lpppdQ.size()==0){
				rm.setState("500");
				rm.setMsgInfo("不存在包装作业完成的钢卷["+mcpd.getCoil_no()+"]明细包装作业计划明细记录！");
				return rm;
			}
			lpppd.add(lpppdQ.get(0));
			
			//验证勾选的需要删除的钢卷数量小于卷包明细钢卷总数
			MesCoilPackingD mcd=new MesCoilPackingD();
			mcd.setPacking_no(mcpmO.getPacking_no());
			icount=this.getMapper(MesCoilPackingDMapper.class).count(mcd);
			imcpmCount=icount-mcpmO.getChild().size();
			if(mcpmO.getChild().size()>=icount){
				rm.setState("500");
				rm.setMsgInfo("勾选数量需小于卷包明细钢卷总数，如有必要，删除包装实绩！");
				return rm;
			}
			
		}
		//逐一修改表格3中勾选的成品卷包对应的钢卷明细包装作业计划明细记录状态为计划已下发
		for(PlnPackingPlanDetail pppd:lpppd){
			PlnPackingPlanDetail pppdU=new PlnPackingPlanDetail();
			pppdU.setStatus("0PPK02");
			pppdU.setSerial_no(pppd.getSerial_no());
			this.getMapper(PlnPackingPlanDetailMapper.class).update(pppdU);
		}
		
		CommonPage<YdmProductDetail> cpDelete=new CommonPage<YdmProductDetail>();
		List<YdmProductDetail> lypd=new ArrayList<YdmProductDetail>();
		//逐一删除表格3中勾选的成品卷包对应的包装计量明细记录
		for(MesCoilPackingD mcpd:mcpmO.getChild()){
			this.getMapper(MesCoilPackingDMapper.class).delete(mcpd.getCoil_no());
			
			//调用物流模块接口，逐一修改表格3中勾选的成品卷包进行撤销包装出库、并同时清空包装等级操作
			YdmProductDetail ypdDelete=new YdmProductDetail();
			ypdDelete.setCoilno(mcpd.getCoil_no());
			lypd.add(ypdDelete);
		}
		
		cpDelete.setObjectList(lypd);
		rmValid = this.getBean(LmsApiServiceImpl.class).doCancelPackingOut(cpDelete);
		if(!"200".equals(rmValid.getState())){
			throw new Exception(rmValid.getMsgInfo());
		}
		
		//重新计算卷包中的卷数，并更新包装计量主实绩表对应字段
		MesCoilPackingM mcpmU=new MesCoilPackingM();
		mcpmU.setPacking_no(mcpmO.getPacking_no());
		mcpmU.setCoil_cnt(imcpmCount);
		this.getMapper(MesCoilPackingMMapper.class).update(mcpmU);
		
		rm.setState("200");
		return rm;
	}
	
}
