package nc.rule.mmpps.plo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.pubitf.org.IDeptPubService;
import nc.util.mmf.busi.service.MaterialPubService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.bd.material.prod.MaterialProdVO;
import nc.vo.mmpac.pickm.entity.AggPickmVO;
import nc.vo.mmpps.mps0202.AggregatedPoVO;
import nc.vo.mmpps.mps0202.PoSourceEnum;
import nc.vo.mmpps.mps0202.PoVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.pubitf.mmpac.pickm.pub.IPickmPubQueryService;
import nc.vo.mmpac.pickm.entity.PickmItemVO;

/**
 * 对运算产生的计划订单补全生产部门，需要对计划订单先按生产工厂进行分组，然后调用服务进行设置
 * 
 * @since 6.3
 * @version 2013-2-26 下午12:28:29
 * @author duanxf
 */

public class PloProdDeptRule implements IRule<AggregatedPoVO> {

	@Override
	public void process(AggregatedPoVO[] vos) {
		if (null == vos) {
			return;
		}
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		ArrayList<String> vsrcbidList = new ArrayList<>();
		for (AggregatedPoVO agg : vos) {
			PoVO plo = agg.getParentVO();
			String vid = plo.getCmaterialvid();
			String stockorgid = plo.getCstockorgid();
			String stockorgvid = plo.getCstockorgvid();
			String prodDeptid = plo.getCproddeptid();
			String prodDeptvid = plo.getCproddeptvid();
			Integer posource = plo.getFposource();
			if (PoSourceEnum.COMPUT == posource && MMValueCheck.isEmpty(prodDeptid) && MMValueCheck.isEmpty(prodDeptvid)
					&& MMValueCheck.isNotEmpty(stockorgid) && MMValueCheck.isNotEmpty(stockorgvid)) {
				List<String> vids = map.get(stockorgid);
				if (null == vids) {
					List<String> list = new ArrayList<String>();
					list.add(vid);
					map.put(stockorgid, list);
				} else {
					vids.add(vid);
				}
			}
			String vsrcbid = plo.getVsrcbid();// 来源单据表体主键
			// 需求来源为备料计划的才回写
			if (PoSourceEnum.COMPUT == posource && plo.getFdemandbill() != null && plo.getFdemandbill() == 1) {
				vsrcbidList.add(vsrcbid);
			}
		}
		if (vsrcbidList != null && vsrcbidList.size() > 0) {
			String[] vsrcbids = vsrcbidList.toArray(new String[vsrcbidList.size()]);
			Map<String, AggPickmVO> pickmVOMap;
			try {
				pickmVOMap = NCLocator.getInstance().lookup(IPickmPubQueryService.class).queryPickm4Issue(vsrcbids);
			} catch (BusinessException e) {
				throw new RuntimeException(e);
			}
			pickmVOMap.getClass();
			for (AggregatedPoVO agg : vos) {
				PoVO plo = agg.getParentVO();
				String vsrcbid = plo.getVsrcbid();// 来源单据表体主键
				AggPickmVO aggPickmVOData = pickmVOMap.get(vsrcbid);
				if (aggPickmVOData != null) {
					PickmItemVO[] children = (PickmItemVO[]) aggPickmVOData.getChildren(PickmItemVO.class);
					String vbdef5 = "";// 合同编号
					String vbdef6 = "";// 合同销售订单号
					String vbdef7 = "";// 标前项目号
					for (PickmItemVO pickmItemVO : children) {
						if (pickmItemVO.getCpickm_bid().equals(vsrcbid)) {
							vbdef5 = pickmItemVO.getVbdef5();// 合同编号
							vbdef6 = pickmItemVO.getVbdef6();// 合同销售订单号
							vbdef7 = pickmItemVO.getVbdef7();// 标前项目号
						}
					}
					plo.setVdef2(vbdef5);// 合同编号
					plo.setVdef3(vbdef6);// 合同销售订单号
					plo.setVdef4(vbdef7);// 标前项目号
				}
			}
		}
		Set<String> stockorgids = map.keySet();
		Iterator<String> i = stockorgids.iterator();
		while (i.hasNext()) {
			String key = i.next();
			List<String> vids = map.get(key);
			Map<String, MaterialProdVO> results = MaterialPubService.queryMaterialProduceInfoByPks(
					vids.toArray(new String[0]), key, new String[] { MaterialProdVO.PK_PRODEPTDOC });
			for (AggregatedPoVO agg : vos) {
				PoVO plo = agg.getParentVO();
				String vid = plo.getCmaterialvid();
				String stockorgid = plo.getCstockorgid();
				String stockorgvid = plo.getCstockorgvid();
				Integer posource = plo.getFposource();
				if (PoSourceEnum.COMPUT == posource && MMValueCheck.isNotEmpty(stockorgid)
						&& MMValueCheck.isNotEmpty(stockorgvid)) {
					if (null != results) {
						MaterialProdVO prodvo = results.get(vid);
						if (null != prodvo) {
							plo.setCproddeptid(prodvo.getPk_prodeptdoc());// 生产部门ID
						}
					}
				}
			}
		}
		// 根据生产部门ID查询设置VID
		List<String> proddeptids = new ArrayList<String>();
		for (AggregatedPoVO agg : vos) {
			PoVO plo = agg.getParentVO();
			String prodDeptid = plo.getCproddeptid();
			Integer posource = plo.getFposource();
			if (PoSourceEnum.COMPUT == posource && MMValueCheck.isNotEmpty(prodDeptid)) {
				proddeptids.add(prodDeptid);
			}
		}
		IDeptPubService deptPubService = this.getDeptPubService();
		try {
			HashMap<String, String> results = deptPubService.getLastVIDSByDeptIDS(proddeptids.toArray(new String[0]));
			if (null != results) {
				for (AggregatedPoVO agg : vos) {
					PoVO plo = agg.getParentVO();
					String prodDeptid = plo.getCproddeptid();
					Integer posource = plo.getFposource();
					if (PoSourceEnum.COMPUT == posource && MMValueCheck.isNotEmpty(prodDeptid)) {
						plo.setCproddeptvid(results.get(prodDeptid));
					}
				}
			}
		} catch (BusinessException e) {
			ExceptionUtils.wrappException(e);
		}

	}

	private IDeptPubService getDeptPubService() {
		return NCLocator.getInstance().lookup(IDeptPubService.class);
	}
}
