package nc.bs.arap.busireg;

import nc.bs.arap.util.RecBillShareCheckUtil;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.vo.arap.payable.AggPayableBillVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.tmpub.util.ArrayUtil;

/**
 * 校验上游费用分摊应收单是否生成正式凭证
 * @author chenjm 2021-05-27
 *
 */
public class CheckRecBillGenerateVoucherListener implements IBusinessListener{

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		AggregatedValueObject[] objects= getAggVO(event);
		if(!ArrayUtil.isNull(objects)) {
			for (AggregatedValueObject bill : objects) {
				if(bill instanceof AggPayableBillVO) {
					AggPayableBillVO paybill = (AggPayableBillVO) bill;
					// 费用分摊应付单
					if("F1-Cxx-W06".equals(paybill.getHeadVO().getPk_tradetype())) {
						RecBillShareCheckUtil.checkVoucher(paybill);
					}
				}
			}
		}
	}
	
	private AggregatedValueObject[] getAggVO(IBusinessEvent event){
		AggregatedValueObject[] billvos = null;
		Object obj = null;
		if (event instanceof BusinessEvent) {
			BusinessEvent business = (BusinessEvent) event;
			obj = (AggregatedValueObject[]) business.getObject();
		} else if (event instanceof BdUpdateEvent) {
			BdUpdateEvent business = (BdUpdateEvent) event;
			obj = (AggregatedValueObject[]) business.getNewObject();
		}
		if (obj != null) {
			if (obj.getClass().isArray()) {
				billvos = (AggregatedValueObject[]) obj;
			} else {
				billvos = new AggregatedValueObject[] { (AggregatedValueObject) obj };
			}
			if (billvos == null || billvos.length == 0)
				return null;
		}
		return billvos;
	}

}
