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.receivable.AggReceivableBillVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.tmpub.util.ArrayUtil;


/**
 * 费用分摊应收单监听
 * @author chenjm
 *
 */
public class RecBillShareCheckListener 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 AggReceivableBillVO) {
					AggReceivableBillVO recbill = (AggReceivableBillVO) bill;
					if("F0-Cxx-D61".equals(recbill.getHeadVO().getPk_tradetype())) {
						RecBillShareCheckUtil.checkFYType(recbill);
					}
				}
			}
		}
	}
	
	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;
	}

}
