package nc.bs.arap.dev;

import java.util.List;

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.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.arap.basebill.BaseItemVO;
import nc.vo.arap.event.IArapBSEventType;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nccloud.commons.lang.ArrayUtils;

public class GatherbillSaveListener implements IBusinessListener{

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {

		//操作类型
		String eventType = event.getEventType();
		
		if(! (IArapBSEventType.TYPE_INSERT_BEFORE.equals(eventType)||
				IArapBSEventType.TYPE_UPDATE_BEFORE.equals(eventType) )){
			return ;
		}
		
		
		AggregatedValueObject[] obills = this.getBills(event);
		if (ArrayUtils.isEmpty(obills)) {
			return;
		}
		
		BaseAggVO aggvo = null;
//		if(obills instanceof AggGatheringBillVO[]) {
//			aggvo = (AggGatheringBillVO)obills[0];
//		}
//		AggGatheringBillVO aggvo = (AggGatheringBillVO)obills[0];
		aggvo = (BaseAggVO)obills[0];
		BaseItemVO[] bvos = (BaseItemVO[])aggvo.getChildrenVO();
		String billtype = aggvo.getHeadVO().getPk_tradetype();
		
		//xug 设置参数
		String where = "SELECT SHORTNAME FROM BD_DEFDOC WHERE nvl(dr,0)=0 and pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code = 'GXDATADZ') and code = 'Paychcekzftype'";
		List<String> chcektypes = (List<String>) new BaseDAO().executeQuery(where, new ColumnListProcessor());
		if(chcektypes == null || chcektypes.size() == 0) {
			if(billtype.equals("F3-Cxx-D01") || billtype.equals("F2-Cxx-01")) {
				
			}else {
				return;
			}
		}else {
			boolean isnochcek = true;
			for (String billkey : chcektypes) {
				String[] rules = billkey.split("#");
				for (String key : rules) {
					if(billtype.indexOf(key) >= 0) {
						isnochcek = false;
						break;
					}
				}
			}
			if(isnochcek) {
				return;
			}
		}
		if(bvos!=null && bvos.length>1) {
			String type="";
			UFDouble money = null;
			if(billtype.equals("F3-Cxx-D01")) {
				money = bvos[0].getMoney_de();
			} else {
				money = bvos[0].getMoney_cr();
			}
			if(money.doubleValue()>0) {
				type = "Z";  //正数
			}else {
				type = "F";  //负数
			}
			for(int i=1;i<bvos.length;i++) {
//				UFDouble money_cr = bvos[i].getMoney_cr();
				UFDouble money_cr = null;
				if(billtype.equals("F3-Cxx-D01")) {
					money_cr = bvos[i].getMoney_de();
				} else {
					money_cr = bvos[i].getMoney_cr();
				}
				if("Z".equals(type) && money_cr.doubleValue()<0) {
					throw new BusinessException("表体行金额不能同时存在正数和负数");
				}else if("F".equals(type) && money_cr.doubleValue()>0) {
					throw new BusinessException("表体行金额不能同时存在正数和负数");
				}
			}
		}
		
	}
	
	
	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;
	}

}
