package market.Mes.Packing.Plan.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Api.Mes.impl.MesApiServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Lengthwise.Plan.mapper.PlnMaterielDetailMapper;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielDetail;
import market.Mes.OrderManager.Plan.mapper.PlnProdorderMapper;
import market.Mes.OrderManager.Plan.vo.PlnProdorder;
import market.Mes.Packing.Plan.mapper.PlnPackingPlanDetailMapper;
import market.Mes.Packing.Plan.mapper.PlnPackingPlanMapper;
import market.Mes.Packing.Plan.service.PlnPackingPlanService;
import market.Mes.Packing.Plan.vo.PlnPackingPlan;
import market.Mes.Packing.Plan.vo.PlnPackingPlanDetail;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="PlnPackingPlanService")
public class PlnPackingPlanServiceImpl extends BaseServiceImpl<PlnPackingPlan,Object> implements PlnPackingPlanService {
	/**
	 * ******车间MES********
	 * 查询计划信息
	 * memo1 钢卷号 object_no
	 * */
	public ResultModel doQueryWeb(CommonPage<PlnPackingPlan> oci){
		ResultModel rm=new ResultModel();
		try{
			PlnPackingPlan mcsm=oci.getObject();
			rm.setData(this.getMapper(PlnPackingPlanMapper.class).doQuery(mcsm,oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 包装计划编制
	 * String[] 虚拟物料编号
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<String[]> oci){
		ResultModel rm=new ResultModel();
		
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		PlnMaterielDetail pmd=new PlnMaterielDetail();
		List<PlnPackingPlan> lppp=new ArrayList<PlnPackingPlan>();
		
		List<PlnPackingPlan> lp=this.getMapper(PlnPackingPlanMapper.class).findWithCondition(null, "", " serial_no desc ", 0, 1);
		long lNo=1;
		if(lp!=null&&lp.size()>0){
			lNo=lp.get(0).getSerial_no()+1;
		}
		
		List<PlnPackingPlanDetail> lpd=this.getMapper(PlnPackingPlanDetailMapper.class).findWithCondition(null, "", " serial_no desc ", 0, 1);
		long lNoD=1;
		if(lpd!=null&&lpd.size()>0){
			lNoD=lpd.get(0).getSerial_no()+1;
		}
		
		for(String s:oci.getObject()){
			pmd.setObject_no_vt(s);
			//有且仅有一条
			List<PlnMaterielDetail> lpmdc=this.getMapper(PlnMaterielDetailMapper.class).find(pmd, 0, 0);
			if(lpmdc==null||lpmdc.size()==0){
				rm.setState("500");
				rm.setMsgInfo("虚拟物料["+s+"]信息不存在");
				return rm;
			}
			PlnPackingPlanDetail pppd=new PlnPackingPlanDetail();
			pppd.setObject_no_vt(s);
			List<PlnPackingPlanDetail> lpppd=this.getMapper(PlnPackingPlanDetailMapper.class).findWithCondition(pppd," STATUS!='0PPKXX' ","", 0, 0);
			//查询包装计划明细表，如果存在 OBJECT_NO_VT=传入虚拟物料号，且 STATUS<>'0PPKXX'的记录，则继续下一循环
			if(lpppd!=null&&lpppd.size()>0){
				continue;
			}
			//同订单的成品放入同一包装计划中
			if(lppp.size()==0){
				PlnPackingPlan ppp=new PlnPackingPlan();
				ppp.setOrder_no(lpmdc.get(0).getOrder_no());
				ppp.setLpmd(lpmdc);
				lppp.add(ppp);
			}else{
				//是否已存在
				boolean flag=false;
				for(PlnPackingPlan ppp:lppp){
					if(ppp.getOrder_no().equals(lpmdc.get(0).getOrder_no())){
						flag=true;
						ppp.getLpmd().add(lpmdc.get(0));
						break;
					}
				}
				if(!flag){
					PlnPackingPlan ppp=new PlnPackingPlan();
					ppp.setOrder_no(lpmdc.get(0).getOrder_no());
					ppp.setLpmd(lpmdc);
					lppp.add(ppp);
				}
			}
		}
		
		
		long lNoSeq=1;
		for(PlnPackingPlan ppp:lppp){
			lNoSeq=1;
			String pkPlanNo="";
			// 查询虚拟物料的订单号，如果包装计划表中存在，则将包装计划编号插入到虚拟物料中；
			List<PlnPackingPlan> lpppQ=this.getMapper(PlnPackingPlanMapper.class).find(ppp, 0, 0);
			if(lpppQ!=null && lpppQ.size()>0){
				PlnPackingPlanDetail pppd=new PlnPackingPlanDetail();
				pppd.setPk_plan_no(lpppQ.get(0).getPk_plan_no());
				List<PlnPackingPlanDetail> lpds=this.getMapper(PlnPackingPlanDetailMapper.class).findWithCondition(pppd, "", " serial_no desc ", 0, 1);
				
				if(lpds!=null&&lpds.size()>0){
					lNoSeq=lpds.get(0).getInner_seq()+1;
				}
				pkPlanNo=lpppQ.get(0).getPk_plan_no();
				
				double matQuantity=0,matWeight=0;
				for(PlnMaterielDetail pmdc:ppp.getLpmd()){
					PlnPackingPlanDetail plppd=new PlnPackingPlanDetail();
					plppd.setPk_plan_no(pkPlanNo);
					plppd.setSerial_no(lNoD);
					plppd.setInner_seq(lNoSeq);
					plppd.setObject_no_vt(pmdc.getObject_no_vt());
					plppd.setCreate_man_name(userName);;
					plppd.setCreate_time(new Date());
					plppd.setStatus("0PPK01");
					this.getMapper(PlnPackingPlanDetailMapper.class).insert(plppd);
					
					lNoSeq++;
					lNoD++;
					matQuantity+=1;
					matWeight=pmdc.getSingle_weight();
					
					//修改 虚拟物料表[PLN_MATERIEL_DETAIL]状态:[STATUS]=0PPK01
					PlnMaterielDetail pmdUp=new PlnMaterielDetail();
					pmdUp.setSerial_no(pmdc.getSerial_no());
					pmdUp.setStatus("0PPK01");
					this.getMapper(PlnMaterielDetailMapper.class).update(pmdUp);
					
					//写虚拟物料履历，状态填:0PPK01
					this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pmdc.getObject_no_vt(),"0PPK01",userName,new Date());
				}
				
				//修改重量
				PlnPackingPlan pppUp=new PlnPackingPlan();
				pppUp.setSerial_no(lpppQ.get(0).getSerial_no());
				pppUp.setMat_quantity(lpppQ.get(0).getMat_quantity()+matQuantity);
				pppUp.setMat_weight(lpppQ.get(0).getMat_weight()+matWeight);
				this.getMapper(PlnPackingPlanMapper.class).update(pppUp);
			}else{
				PlnProdorder pp=new PlnProdorder();
				pp.setOrder_no(ppp.getOrder_no());
				//取任意一条的规格参数
				List<PlnProdorder> lpp=this.getMapper(PlnProdorderMapper.class).find(pp, 0, 0);
				
				pkPlanNo="";
				pkPlanNo="PK"+String.format("%015d", lNo);
				PlnPackingPlan pppAdd=new PlnPackingPlan();
				pppAdd.setSerial_no(lNo);
				pppAdd.setPlan_seq(lNo);
				pppAdd.setPk_plan_no(pkPlanNo);
				pppAdd.setOrder_no(ppp.getOrder_no());
				pppAdd.setStatus("0PPK01");
				//取任意一条的规格参数
				pppAdd.setStd_code(lpp.get(0).getStd_code());
				pppAdd.setStd_name(lpp.get(0).getStd_name());
				pppAdd.setReciver_unit_no(lpp.get(0).getReciver_unit_no());
				pppAdd.setReciver_unit_name(lpp.get(0).getReciver_unit_name());
				
				pppAdd.setCreate_man_name(userName);
				pppAdd.setCreate_time(new Date());
				lNo++;
				double matQuantity=0,matWeight=0;
				for(PlnMaterielDetail pmdc:ppp.getLpmd()){
					PlnPackingPlanDetail plppd=new PlnPackingPlanDetail();
					plppd.setPk_plan_no(pkPlanNo);
					plppd.setSerial_no(lNoD);
					plppd.setInner_seq(lNoSeq);
					plppd.setObject_no_vt(pmdc.getObject_no_vt());
					plppd.setCreate_man_name(userName);;
					plppd.setCreate_time(new Date());
					plppd.setStatus("0PPK01");
					this.getMapper(PlnPackingPlanDetailMapper.class).insert(plppd);
					
					matQuantity+=1;
					matWeight=pmdc.getSingle_weight();
					
					lNoSeq++;
					lNoD++;
					
					//写虚拟物料履历，状态填:0PPK01
					this.getBean(MesApiServiceImpl.class).InsertMatVtProc(pmdc.getObject_no_vt(),"0PPK01",userName,new Date());
				}
				
				pppAdd.setMat_weight(matWeight);
				pppAdd.setMat_quantity(matQuantity);
				this.getMapper(PlnPackingPlanMapper.class).insert(pppAdd);
				
			}
			
			
			//
		}
		
		rm.setState("200");
		
		return rm;
	}
}
