package nc.bs.arap.busireg;

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

import com.alibaba.fastjson.JSONObject;

import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.framework.common.NCLocator;
import nc.itf.arap.psapi.IPSApiServer;
import nc.vo.obm.employeesalary.AggEmployeesalary_h;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.commons.lang.ArrayUtils;
/**
 * 实发与代扣结转单完成审批后回传ps系统
 * @author xuwb
 * @return
 */
public class RealPayWithholdingApproverListener implements IBusinessListener{

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		AggregatedValueObject[] objects=getBills(event);
		if (ArrayUtils.isEmpty(objects)) {
			return;
		}
	    Map param=new HashMap();	    
	    for(int i=0;i<objects.length;i++) {
	    	String pk_tradetype=objects[i].getParentVO().getAttributeValue("pk_tradetype")==null?"":objects[i].getParentVO().getAttributeValue("pk_tradetype").toString();
	      if(pk_tradetype!=null) {
	    	if("F1-Cxx-W07".equals(pk_tradetype)) {//判断交易类型是否为 F1-Cxx-W07
			     param.put("itfcode", "NC-PS-ARAP-002");
			     param.put("transtypecode", "NC-PS-ARAP-002");
			     param.put("itfname","审批完成状态回传");
			     JSONObject returnReasonJson=new JSONObject();
			     if(objects[i].getParentVO().getAttributeValue("def57")==null) {
			    	 ExceptionUtils.wrappBusinessException("PS来源单据号不能为空");
			     }
			     returnReasonJson.put("C_FF_SEQ", objects[i].getParentVO().getAttributeValue("def57").toString());
			     returnReasonJson.put("STATUS", "10");
			     param.put("billdata", returnReasonJson );
			     try {
					NCLocator.getInstance().lookup(IPSApiServer.class).PostData2PS(param);
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					ExceptionUtils.wrappBusinessException("审批后事件:"+e.getMessage());
				} 
	    	}else {
	    		continue;
	    	}
	      }
	     }
	}

	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;
	}
}
