package nc.bs.arap.busireg;

import java.util.Collection;

import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.pubapp.pub.smart.IBillQueryService;
import nc.vo.arap.pay.AggPayBillVO;
import nc.vo.cmp.settlement.SettlementHeadVO;
import nc.vo.pu.m21.entity.OrderHeaderVO;
import nc.vo.pu.m21.entity.OrderVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.tmpub.util.ArrayUtil;

/**
 * @author xionggw
 * @date 2021-3-25
 * @Description:帐扣付款单审批生效后生效日期与支付日期一致
 * 
 */
public class PayBillApproverListenerBYGWY 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 AggPayBillVO) {
					AggPayBillVO payBillVO=(AggPayBillVO) bill;
						if("F3-Cxx-W04".equals(payBillVO.getHeadVO().getPk_tradetype())) {	
							UFDate Billdate=payBillVO.getHeadVO().getBilldate();
							InvocationInfoProxy.getInstance().setBizDateTime(Billdate.getMillis());	
							payBillVO.getHeadVO().setEffectdate(Billdate);
							payBillVO.getHeadVO().setPaydate(Billdate);
							payBillVO.getHeadVO().setSigndate(Billdate);
 							BaseDAO baseDAO=new BaseDAO();
							baseDAO.updateVO(payBillVO.getHeadVO(), new String[] {"paydate","effectdate","signdate"});
							String wheresql=" pk_busibill='"+payBillVO.getHeadVO().getPk_paybill()+"'";
							 Collection<?> vo=baseDAO.retrieveByClause(SettlementHeadVO.class, wheresql);
							 SettlementHeadVO[] SettlementHeadupdateVO= (SettlementHeadVO[])vo.toArray(new SettlementHeadVO[0]);
							 if (SettlementHeadupdateVO.length>0) {
								 SettlementHeadupdateVO[0].setSettledate(Billdate);
								 SettlementHeadupdateVO[0].setBusi_auditdate(Billdate);
								 SettlementHeadupdateVO[0].setSigndate(Billdate);
								 baseDAO.updateVO(SettlementHeadupdateVO[0],new String[] {"settledate","busi_auditdate","signdate"});
							 }
							
						}
					}
			}
		}
		
	}
	
	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;
	}
}
