package nc.bs.arap.busireg;

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.NCLocator;
import nc.cmp.utils.SettleUtils;
import nc.itf.pubapp.pub.smart.IBillQueryService;
import nc.vo.arap.pay.AggPayBillVO;
import nc.vo.cmp.settlement.SettlementAggVO;
import nc.vo.pu.m21.entity.OrderItemVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pu.m21.enumeration.EnumActive;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.tmpub.util.ArrayUtil;
/**
 * @author 符耀铭
 * @date 2020年10月14日
 * @description 碧彩京东接口改造接口，付款单签字校验
 */
public class WYBCSCPaybillSignCheckListener 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 SettlementAggVO) {
					SettlementAggVO settlementAggVO=(SettlementAggVO) bill;

					AggregatedValueObject valueObject=SettleUtils.getBuisAggVO(ValueUtils.getString(settlementAggVO.getParentVO().getAttributeValue("pk_tradetype")),ValueUtils.getString(settlementAggVO.getParentVO().getAttributeValue("pk_busibill")),AppContext.getInstance().getPkGroup());
					if(valueObject instanceof AggPayBillVO) {
						AggPayBillVO payBillVO=(AggPayBillVO) valueObject;
						if(payBillVO.getHeadVO().getMoney().doubleValue()>0) {
							if("21".equals(payBillVO.getBodyVOs()[0].getTop_billtype())) {
								IBillQueryService billQuery = NCLocator.getInstance().lookup(IBillQueryService.class);
								OrderVO orderVO = billQuery.querySingleBillByPk(OrderVO.class, payBillVO.getBodyVOs()[0].getTop_billid());
								
								if((ValueUtils.getUFBoolean(orderVO.getHVO().getVdef31()).booleanValue() || (ValueUtils.getUFBoolean(orderVO.getHVO().getVdef17()).booleanValue() && isFinalClosable(orderVO)))) {
									ExceptionUtils.wrappBusinessException("该笔订单已取消，请将付款单驳回制单人（财务），由制单人（财务）删除单据！");
								}
							}
						}
					}

				}
			}
		}
	}

	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;
	}
	/**判断采购订单是否整单关闭
	 * @author 符耀铭
	 * @date 2020年10月14日
	 * @param vo
	 * @return
	 */
	public static boolean isFinalClosable(OrderVO vo) {
		OrderItemVO[] itemVOs=vo.getBVO();
		
		for(int i=0;i<itemVOs.length;i++) {
			OrderItemVO item = itemVOs[i];
			if (!item.getFisactive().equals(EnumActive.CLOSE.value())) {
				return false;
			}
		}
		return true;
	}
}
