package nc.bs.arap.twxt;

import java.util.HashMap;
import java.util.Map;

import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.BusinessEvent.BusinessUserObj;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.util.mmf.framework.base.MMValueConvert;
import nc.util.uap.twxt.WYTwxtUtil;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.arap.receivable.AggReceivableBillVO;
import nc.vo.arap.receivable.ReceivableBillVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

/**
 * NC删除应收单同步到天问系统
 * 
 * @author 周蓝萍
 * @date 2019-04-27	
 */
public class TWXTDeleteRecbillListener implements IBusinessListener {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		BusinessEvent erEvent = (BusinessEvent) event;
		BusinessUserObj obj = (BusinessUserObj) (erEvent).getUserObject();
		if (obj.getUserObj() instanceof AggReceivableBillVO[]) {
			AggReceivableBillVO[] logVO = (AggReceivableBillVO[]) obj
					.getUserObj();
			ReceivableBillVO vos = (ReceivableBillVO) logVO[0].getParent();
			if("1005".equals(erEvent.getEventType()) && vos.getStatus()==VOStatus.DELETED) {
				if(("F0-Cxx-D57".equals(logVO[0].getHeadVO().getPk_tradetype()) || "F0-Cxx-D59".equals(logVO[0].getHeadVO().getPk_tradetype()) || "F0-Cxx-D38".equals(logVO[0].getHeadVO().getPk_tradetype()))){
					if(MMValueConvert.toUFBoolean(logVO[0].getBodyVOs()[0].getDef20()).booleanValue()) {
						ExceptionUtils.wrappBusinessException("该单据为代收单据，不能直接删除！");
					}
					return;
				}
			}
				
			// F0-Cxx-D57 天问应收单（物业）
			if(vos != null && "1006".equals(erEvent.getEventType())){
				Map paraMap=new HashMap();
//				String nccds =InvocationInfoProxy.getInstance().getUserDataSource();
//				String ncds = SysInitQuery.getParaString("GLOBLE00000000000000","NC63DS");
//				BaseDAO ncBaseDAO=new BaseDAO(ncds);
//				String ncBillSql="select billno from ap_payablebill where def1=? and nvl(dr,0)=0";
				for(int i=0;i<logVO.length;i++){
					if(logVO[i].getHeadVO().getStatus()!=VOStatus.DELETED) {
						continue;
					}
					if(("F0-Cxx-D57".equals(logVO[i].getHeadVO().getPk_tradetype()) || "F0-Cxx-D59".equals(logVO[i].getHeadVO().getPk_tradetype()) || "F0-Cxx-D38".equals(logVO[i].getHeadVO().getPk_tradetype()))){
//						SQLParameter parameter=new SQLParameter();
//						parameter.addParam(logVO[i].getHeadVO().getBillno());
//						List<String> existNCBillVOs =(List<String>) ncBaseDAO.executeQuery(ncBillSql, parameter, new ColumnListProcessor());
						
//						if(MMValueCheck.isNotEmpty(existNCBillVOs)) {
//							nccloud.framework.core.exception.ExceptionUtils.wrapBusinessException("该单据已在NC63系统生成了对应的应付单:"+MMStringUtil.mergeString(existNCBillVOs.toArray(), "null", ",")+",请先在NC63系统删除！");
//						}
//						InvocationInfoProxy.getInstance().setUserDataSource(nccds);
						paraMap.put("ORDERID", logVO[i].getHeadVO().getDef1());
						paraMap.put("TYPE", "F0-Cxx-D59".equals(logVO[i].getHeadVO().getPk_tradetype())?"1":"2");
						paraMap.put("REMARK", "F0-Cxx-D59".equals(logVO[i].getHeadVO().getPk_tradetype())?"应收计提单删除回写":"应收单删除回写");
						WYTwxtUtil.callWyTwService(paraMap, "NCBILL_REVOKE", "应收单/应收计提单删除回写");
					}
				}
//				InvocationInfoProxy.getInstance().setUserDataSource(nccds);
			}
		}

		else if(obj.getUserObj() instanceof BaseAggVO[]){
			BaseAggVO[] baseAggVOs=(BaseAggVO[]) obj.getUserObj();
			if("1005".equals(erEvent.getEventType()) && baseAggVOs[0].getHeadVO().getStatus()==VOStatus.DELETED) {
				if(("F0-Cxx-D57".equals(baseAggVOs[0].getHeadVO().getPk_tradetype()) || "F0-Cxx-D59".equals(baseAggVOs[0].getHeadVO().getPk_tradetype()) || "F0-Cxx-D38".equals(baseAggVOs[0].getHeadVO().getPk_tradetype()))){
					if(MMValueConvert.toUFBoolean(baseAggVOs[0].getChildrenVO()[0].getAttributeValue("def20")).booleanValue()) {
						ExceptionUtils.wrappBusinessException("该单据为代收单据，不能直接删除！");
					}
					return;
				}
			}
			if(null!=baseAggVOs && "1006".equals(erEvent.getEventType())){
				Map paraMap=new HashMap();
//				String nccds =InvocationInfoProxy.getInstance().getUserDataSource();
//				String ncds = SysInitQuery.getParaString("GLOBLE00000000000000","NC63DS");
//				BaseDAO ncBaseDAO=new BaseDAO(ncds);
//				String ncBillSql="select billno from ap_payablebill where def1=? and nvl(dr,0)=0";
				
//				IArapPayableBillService service=NCLocator.getInstance().lookup(IArapPayableBillService.class);
				for(int i=0;i<baseAggVOs.length;i++){
					if(baseAggVOs[i].getHeadVO().getStatus()!=VOStatus.DELETED) {
						continue;
					}
					if(("F0-Cxx-D57".equals(baseAggVOs[i].getHeadVO().getPk_tradetype()) || "F0-Cxx-D59".equals(baseAggVOs[i].getHeadVO().getPk_tradetype()) || "F0-Cxx-D38".equals(baseAggVOs[i].getHeadVO().getPk_tradetype()))){
						paraMap.put("ORDERID",baseAggVOs[i].getHeadVO().getAttributeValue("def1"));
						paraMap.put("TYPE", "F0-Cxx-D59".equals(baseAggVOs[i].getHeadVO().getPk_tradetype())?"1":"2");
						paraMap.put("REMARK", "F0-Cxx-D59".equals(baseAggVOs[i].getHeadVO().getPk_tradetype())?"应收计提单删除回写":"应收单删除回写");
						WYTwxtUtil.callWyTwService(paraMap, "NCBILL_REVOKE", "应收单/应收计提单删除回写");
						
//						SQLParameter parameter=new SQLParameter();
//						parameter.addParam(baseAggVOs[i].getHeadVO().getBillno());
//						List<String> existNCBillVOs=(List<String>) ncBaseDAO.executeQuery(ncBillSql, parameter, new ColumnListProcessor());
						
//						if(MMValueCheck.isNotEmpty(existNCBillVOs)) {
//							nccloud.framework.core.exception.ExceptionUtils.wrapBusinessException("该单据已在NC63系统生成了对应的应付单:"+MMStringUtil.mergeString(existNCBillVOs.toArray(), "null",",")+",请先在NC63系统删除！");
//						}
					}
				}
//				InvocationInfoProxy.getInstance().setUserDataSource(nccds);
			}
		}
	}
}