package nc.bs.arap.busireg;

import nc.bs.arap.util.RecSAPBillOperCheck;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.impl.pubapp.env.BSContext;
import nc.util.mmf.framework.base.MMValueConvert;
import nc.vo.arap.receivable.AggReceivableBillVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
/**
 * 校验内部交易应收单传递SAP状态监听
 * @author 符耀明
 * @time 2021年4月16日
 */
public class RecBillOperCheckSapStatusListener implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		String eventtype=event.getEventType();
		AggregatedValueObject[] billVO=getAggVO(event);
		Object pk_tradetype=billVO[0].getParentVO().getAttributeValue("pk_tradetype");
		
		UFBoolean issapconfirm =MMValueConvert.toUFBoolean(InvocationInfoProxy.getInstance().getProperty("issapconfirm"));
		if(!issapconfirm.booleanValue()) {
			issapconfirm=MMValueConvert.toUFBoolean(BSContext.getInstance().getSession("issapconfirm"));
		}
		
		if(("F0-Cxx-D38".equals(pk_tradetype) || "F0-Cxx-D39".equals(pk_tradetype)) &&!issapconfirm.booleanValue()) {
			RecSAPBillOperCheck.checkBill(eventtype, (AggReceivableBillVO) billVO[0]);
		}
	}
	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;
	}
}
