package nc.bs.arap.busireg;

import nc.bs.arap.closeacc.CloseAccCtrlBO;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.vo.arap.event.IArapBSEventType;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;

import nccloud.commons.lang.ArrayUtils;

/**
 * 关帐控制类
 * 说明：
 */
public class CloseAccCtrlListener  implements IBusinessListener {


	public void doAction(final IBusinessEvent event) throws BusinessException {
		if(! (IArapBSEventType.TYPE_INSERT_BEFORE.equals(event.getEventType()) ||
				IArapBSEventType.TYPE_DELETE_BEFORE.equals(event.getEventType())|| 
				IArapBSEventType.TYPE_UPDATE_BEFORE.equals(event.getEventType()) )){
			return ;
		}
		
		AggregatedValueObject[] obills = this.getBills(event);
		if (ArrayUtils.isEmpty(obills)) {
			return;
		}
		
		//关帐控制接口
		new CloseAccCtrlBO().checkIsCloseAcc(obills[0]);
		
	}
	  protected AggregatedValueObject[] getBills(final 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;
	  }

}