package nc.bs.arap.busireg;

import java.util.Collection;

import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.IEventType;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.arap.receivable.IArapReceivableBillQueryService;
import nc.itf.pubapp.pub.smart.IBillQueryService;
import nc.itf.uap.pf.IPFBusiAction;
import nc.vo.arap.pay.AggPayBillVO;
import nc.vo.arap.payable.AggPayableBillVO;
import nc.vo.arap.payable.PayableBillItemVO;
import nc.vo.arap.payable.PayableBillVO;
import nc.vo.arap.receivable.AggReceivableBillVO;
import nc.vo.cmp.settlement.SettlementHeadVO;
import nc.vo.pu.m21.entity.OrderHeaderVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.tmpub.util.ArrayUtil;
import nccloud.commons.lang.ArrayUtils;

/**
 * @author xionggw
 * @date 2021-5-25
 * @Description:应付单审批通过后回写状态给应收单，应收单自动审批生成凭证
 * 
 */
public class payablebillApproverListenerBYGWY implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		if (IEventType.TYPE_APPROV_AFTER.equals(event.getEventType()))
		{
			AggregatedValueObject[] obills = getBills(event);
			if (ArrayUtils.isEmpty(obills)) {
				return;
			}
			for (AggregatedValueObject aggregatedValueObject : obills) {
				if(!(aggregatedValueObject instanceof AggPayableBillVO)){
					continue;
				}
				AggPayableBillVO aggvo = (AggPayableBillVO) aggregatedValueObject;
				PayableBillVO hvo = (PayableBillVO) aggvo.getParentVO();
				PayableBillItemVO[] bvos = (PayableBillItemVO[]) aggvo.getChildrenVO();
				if("F1-Cxx-W06".equals(hvo.getPk_tradetype())){
					if(bvos != null && bvos.length > 0){
						for (PayableBillItemVO PayableBillItemVO : bvos) {
							if (null!=bvos[0].getSrc_billid())
							{
							String pk_recbill=bvos[0].getSrc_billid();
							IArapReceivableBillQueryService reserv=NCLocator.getInstance().lookup(IArapReceivableBillQueryService.class);
							AggReceivableBillVO[] vos = reserv.queryVOsByWhere(" pk_recbill='"+pk_recbill+"'");
							
							IPFBusiAction baService = NCLocator.getInstance().lookup(IPFBusiAction.class);
							if (vos.length!=0) {
								Object result3 = baService.processAction("APPROVE", "F0-Cxx-D61",null , vos[0], null, null);
							}
							
						}
					}
				}
				}
			}
		}
	}
	
	protected AggregatedValueObject[] getBills(IBusinessEvent event) {
		Object object = null;
		if ((event instanceof BusinessEvent)) {
			BusinessEvent e = (BusinessEvent) event;
			object = e.getObject();
		} else if ((event instanceof BdUpdateEvent)) {
			BdUpdateEvent e = (BdUpdateEvent) event;
			object = e.getNewObject();
		}

		AggregatedValueObject[] bills = null;
		if (null != object) {
			if (object.getClass().isArray()) {
				bills = (AggregatedValueObject[]) object;
			} else {
				bills = new AggregatedValueObject[1];
				bills[0] = ((AggregatedValueObject) object);
			}
		}
		return bills;
	}
	
}
