package nc.bs.arap.busireg;

import java.util.ArrayList;
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.bs.resa.sql.SqlUtil;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.util.mmf.framework.base.MMVOUtil;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.util.mmf.framework.base.MMValueConvert;
import nc.vo.arap.gathering.AggGatheringBillVO;
import nc.vo.arap.pay.AggPayBillVO;
import nc.vo.arap.payable.AggPayableBillVO;
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;
/**
 * 应收应付校验余额调节表
 * @author fuymc
 * @date 2021年4月1日
 */
public class ArapBillCheckBalanceListener implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		AggregatedValueObject[] objects=getBills(event);
		if(MMValueCheck.isNotEmpty(objects)) {
			for(int i=0;i<objects.length;i++) {
				if((objects[i] instanceof AggPayBillVO || objects[i] instanceof AggGatheringBillVO || objects[i] instanceof AggPayableBillVO)) {
					List<String> accountList=new ArrayList<String>();
					String pk_org = objects[i].getParentVO().getAttributeValue("pk_org").toString();
					UFDate bill_date = (UFDate) objects[i].getParentVO().getAttributeValue("billdate");
					Integer  syscode=MMValueConvert.toInteger(objects[i].getParentVO().getAttributeValue("syscode"));
					Object pk_tradetype =objects[i].getParentVO().getAttributeValue("pk_tradetype");
					Object ishasticket=objects[i].getParentVO().getAttributeValue("def29");
					if(syscode==1) {
						if("F1-Cxx-W03".equals(pk_tradetype) && MMValueConvert.toUFBoolean(ishasticket).booleanValue()) {
							break;
						}
						accountList.add(objects[i].getParentVO().getAttributeValue("payaccount")+"");
					}else if(syscode==0) {
						accountList.add(objects[i].getParentVO().getAttributeValue("recaccount")+"");
					}
					
					if(objects[i] instanceof AggPayBillVO) {
						AggPayBillVO vo =((AggPayBillVO)objects[i]);
						if("F3-Cxx-W02".equals(vo.getHeadVO().getPk_tradetype())) {
							if(MMValueConvert.toUFBoolean(vo.getHeadVO().getDef3()).booleanValue()) {
								break;
							}
						}
						accountList.addAll(MMVOUtil.getStrValue(((AggPayBillVO)objects[i]).getBodyVOs(), "payaccount"));
					}
					if(objects[i] instanceof AggGatheringBillVO) {
						accountList.addAll(MMVOUtil.getStrValue(((AggGatheringBillVO)objects[i]).getBodyVOs(), "recaccount"));
					}
					String checkSql="SELECT count(1) FROM GL_BALANCEADJUST gb INNER JOIN GL_ACCOUNTLINK ga ON gb.PK_ACCOUNT = ga.pk_contrastaccount	WHERE gb.PK_ORG ='"+pk_org+"'  and ("+SqlUtil.buildInSql("ga.BANKACCOUNT", accountList, true)+" or  ga.BANKACCOUNT in (select PK_BANKACCBAS  from  BD_BANKACCSUB  where "+SqlUtil.buildInSql("PK_BANKACCSUB", accountList, true)+")) AND '"+bill_date.toString().substring(0, 10)+"'>= gb.BEGDATE  AND '"+bill_date.toString().substring(0, 10)+"'<= gb.ENDDATE  AND gb.APPROVEDATE IS NOT NULL AND gb.APPROVER IS NOT NULL";
					Object isExists =new BaseDAO().executeQuery(checkSql, new ColumnProcessor());
					if(ValueUtils.getInt(isExists, 0)>0) {
						ExceptionUtils.wrappBusinessException("单据日期对应期间的余额调节表对应的记录已经审批，不能新增或取消结算");
					}
				}
			}
		}
	}
	protected AggregatedValueObject[] getBills(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;
	}

}
