package nc.bs.erm.accruedexpense.listener;

import java.util.Collection;

import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.dao.BaseDAO;
import nc.bs.erm.event.ErmBusinessEvent;
import nc.bs.erm.event.ErmEventType;
import nc.bs.erm.event.ErmBusinessEvent.ErmCommonUserObj;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.sscrp.rpbill.util.RPBillConvertUtil;
import nc.bs.sscrp.rpbill.util.RPBillSyncUtil;
import nc.md.persist.framework.MDPersistenceService;
import nc.nccloud.dto.erm.expenseaccount.BXBillVO;
import nc.pubitf.initgroup.InitGroupQuery;
import nc.vo.ep.bx.BXVO;
import nc.vo.ep.bx.JKBXHeaderVO;
import nc.vo.ep.bx.JKBXVO;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.sscrp.rpbill.AggRPBillVO;
import nccloud.pubitf.sscrp.rpbill.IRPBillSyncService;



/**  
 * @ClassName: JkbxBillSyncListener_bgywy
 * @Description: 资金需求：报销单要将签字生效改为支付后生效，导致没有没有同步工作台信息
 * @author 熊国文
 * @date 2021-12-11 01:47:08 
*/  
public class JkbxBillSyncListener_bgywy implements IBusinessListener{

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		JKBXHeaderVO[] hvos = (JKBXHeaderVO[]) ((ErmCommonUserObj) event.getUserObject()).getNewObjects();
		if (hvos == null || hvos.length == 0)
			return;// 获取到的vo为空时
		String[] keys=new String[]{hvos[0].getPrimaryKey()};
		Collection<?> vos = MDPersistenceService.lookupPersistenceQueryService().queryBillOfVOByPKs(BXVO.class, keys, false);
		AggregatedValueObject[] aggVOs=vos.toArray(new AggregatedValueObject[0]);
		//云原生：
		//1 校验是否安装报账平台	
		boolean isInit = InitGroupQuery.isEnabled(InvocationInfoProxy.getInstance().getGroupId(), "1056");
		if (!isInit) {
			return;
		}
		String operType = "";
		if (!StringUtil.isEmptyWithTrim(event.getEventType())) {
			switch (event.getEventType()) {
				/*** 新增后 回写工作平台 */
				case ErmEventType.TYPE_INSERT_AFTER:
				case ErmEventType.TYPE_TEMPSAVE_AFTER:
					operType = RPBillSyncUtil.OPER_INSERT;
					break;
					
				/*** 修改后 回写工作平台 */
				case ErmEventType.TYPE_UPDATE_AFTER:
				case ErmEventType.TYPE_TEMPUPDATE_AFTER:
					//审批后回写工作平台
				case ErmEventType.TYPE_WRITEOFF_AFTER:
					//反审批后回写工作平台
				case ErmEventType.TYPE_UNWRITEOFF_AFTER:
					operType = RPBillSyncUtil.OPER_UPDATE;
					break;
					
				/*** 删除后 回写工作平台 */
				case ErmEventType.TYPE_DELETE_AFTER:
					operType = RPBillSyncUtil.OPER_DELETE;
					break;
					
				/*** 生效后 回写工作平台 */
				case ErmEventType.TYPE_SIGN_AFTER:
					
					/*** 生效后 回写工作平台 */
				case "ERM1050":
					operType = RPBillSyncUtil.OPER_FINISH;
					break;
				/*** 作废后 回写工作平台 */
				case ErmEventType.TYPE_INVALID_AFTER:
					operType = RPBillSyncUtil.OPER_FINISH;
					break;
					
				/** 反生效后 回写工作平台 ***/
				case ErmEventType.TYPE_UNSIGN_AFTER:
					operType = RPBillSyncUtil.OPER_UNFINISH;
					break;
			}
		}
		
		if (!StringUtil.isEmptyWithTrim(operType)) {
			//2 转换报账平台需要的VO
			String pk_tradetype = (String) aggVOs[0].getParentVO().getAttributeValue(JKBXHeaderVO.DJLXBM);
			IRPBillSyncService rpBillSyncService = (IRPBillSyncService) NCLocator.getInstance().lookup(IRPBillSyncService.class);
			AggRPBillVO aggRPBillVO = RPBillConvertUtil.convertBill2AggRPBillVO(pk_tradetype, aggVOs[0]);
			rpBillSyncService.syncRPBill(pk_tradetype, aggRPBillVO, operType);
		}
	}

}
