package nc.bs.arap.busireg;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import nc.bs.arap.constant.PropertyNCBilltypeConstant;
import nc.bs.arap.util.NCToMyXmlUtil;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.IEventType;
import nc.vo.arap.payable.PayableBillVO;
import nc.vo.esb.ArapRequestToEsbAnnotatioVO;
import nc.vo.esb.ArapRequestToMyInfoVO;
import nc.vo.esb.EsbInfoAnnotatioVO;
import nc.vo.esb.QueryInfoVO;
import nc.vo.esb.ResultEsbMyToNcVO;
import nc.vo.esb.ResultMyToNcVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;

/**
 * @author zhengyao3/郑瑶
 * @date 2018-9-27 下午3:52:10
 * @Description:物业应付单,物业应付计提单取消审批,删除前调用明源事件
 * 
 */
public class PayablebillOfMyResversibleListener implements IBusinessListener {
	public static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss:SSS");
	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		AggregatedValueObject[] billvos = getAggVO(event);
		for (AggregatedValueObject bill : billvos) {
			PayableBillVO vo = (PayableBillVO) bill.getParentVO();
			String tradetype = vo.getPk_tradetype();
			//回调MY红蓝区分
			UFDouble sum = vo.getMoney();
			String sumflag = sum.toDouble() < 0 ? "红字" :"蓝字"; 
			String billType = "";
			String businessGUID = "";
			String operatorType = "";
			//优化对应的方法,增加参数
			String URLCode = "";
			String elementResult = "";
			String elementResponse = "";
			//应付单,应付计提单取消审批或者删除调用明源接口
			if(IEventType.TYPE_UNAPPROV_BEFORE.equals(event.getEventType())){//取消审批前
				operatorType = "取消审批";
				URLCode = "MY-NC-PM-003";
				elementResponse = "CheckOrderResponse";
				elementResult = "CheckOrderResult";
			}else if(IEventType.TYPE_DELETE_AFTER.equals(event.getEventType())){//删除后
				operatorType = "删除";
				URLCode = "MY-NC-PM-002";
				elementResponse = "DeleteOrderResponse";
				elementResult = "DeleteOrderResult";
			}
			if (PropertyNCBilltypeConstant.F1W01BILLTYPE.equals(tradetype) 
					|| PropertyNCBilltypeConstant.F1W02BILLTYPE.equals(tradetype) ) {
				if(PropertyNCBilltypeConstant.F1W01BILLTYPE.equals(tradetype)){
				//回调MY红蓝区分
					billType = sumflag+"应付计提单";
				} else if(PropertyNCBilltypeConstant.F1W02BILLTYPE.equals(tradetype)){
					billType = "应付单";
				}
				//修改调用的方式,不适用注解进行调用Xstream
				businessGUID = vo.getDef1();
				XStream x = new XStream(new DomDriver("utf-8"));
				x.alias("requestBaseEntity", ArapRequestToEsbAnnotatioVO.class);
				String def45 = vo.getDef45();
				String requestXML = x.toXML(getEsbVO(billType, businessGUID, operatorType,def45,null));
				String resultXML = NCToMyXmlUtil.sendXMLToMy(requestXML, URLCode, businessGUID);
				
				ResultEsbMyToNcVO resultVO = NCToMyXmlUtil
						.getReverseResultEsbMyToNcVO(resultXML,elementResponse,elementResult);// 明源返回的XML转换成对应的VO
				//u修改调用的方式,不适用注解进行调用Xstream end
				if (resultVO != null) {
					ResultMyToNcVO resultInfoVO = resultVO.getResultInfo();
					if(null == resultInfoVO) 
					{
						throw new BusinessException("调用明源接口返回信息为空:");
					}
					if (!"S".equals(resultInfoVO.getStatus())) {// 如果不是S,则就是失败
						throw new BusinessException("明源单据"
								+ resultInfoVO.getBusinessGUID()
								+ "调用取消审批或者删除接口失败,错误为:" + resultInfoVO.getReturnMsg());
					}
				}
			}
		}

	}
	// 修改private为protected begin
	/**
	 * 获取调用明源的VO,作为后续转换为XML使用
	 * 
	 * @param guid
	 * @return
	 */
	protected ArapRequestToEsbAnnotatioVO getEsbVO(String billType,String businessGUID,String operatorType,String def45,String def37) {
		ArapRequestToEsbAnnotatioVO esb = new ArapRequestToEsbAnnotatioVO();
		EsbInfoAnnotatioVO esbInfo = new EsbInfoAnnotatioVO();
		QueryInfoVO queryInfo = new QueryInfoVO();
		Date date = Calendar.getInstance().getTime();// 获取当前时间
		// 设置请求时间
		esbInfo.setRequestTime(dateFormat.format(date));
		esbInfo.setInstId(UUID.randomUUID().toString());// 使用UUID作为流水号
		esb.setEsbInfo(esbInfo);
		esb.setQueryInfo(queryInfo);
		ArapRequestToMyInfoVO infovo = new ArapRequestToMyInfoVO();
		infovo.setBillType(billType);
		infovo.setBusinessGUID(businessGUID);
		infovo.setOperatorType(operatorType);
		infovo.setDef45(def45);
		infovo.setDef37(def37);
		esb.setRequestInfo(infovo);
		return esb;
	}
	protected AggregatedValueObject[] getAggVO(IBusinessEvent event){

		AggregatedValueObject[] billvos = null;
		Object obj = null;
		if (event instanceof BusinessEvent) {
			BusinessEvent business = (BusinessEvent) event;
			obj = (AggregatedValueObject[]) business.getObject();
		} else if (event instanceof BdUpdateEvent) {
			BdUpdateEvent business = (BdUpdateEvent) event;
			obj = (AggregatedValueObject[]) business.getNewObject();
		}
		if (obj != null) {
			if (obj.getClass().isArray()) {
				billvos = (AggregatedValueObject[]) obj;
			} else {
				billvos = new AggregatedValueObject[] { (AggregatedValueObject) obj };
			}
		if (billvos == null || billvos.length == 0)
			return null;
		}
		return billvos;
	}

}
