package nc.bs.arap.plugin.tocmp;

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

import nc.bs.arap.util.NCToMyXmlUtil;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.BusinessEvent.BusinessUserObj;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.dao.BaseDAO;
import nc.ui.pub.print.version55.print.output.excel.core.ExcelException;
import nc.vo.arap.pay.PayBillVO;
import nc.vo.cmp.settlement.SettlementAggVO;
import nc.vo.cmp.settlement.SettlementHeadVO;
import nc.vo.esb.EsbInfoAnnotatioVO;
import nc.vo.esb.QueryInfoVO;
import nc.vo.esb.RequestInfoVO;
import nc.vo.esb.RequestToEsbVO;
import nc.vo.esb.ResultEsbMyToNcVO;
import nc.vo.esb.ResultMyToNcVO;
import nc.vo.pub.BusinessException;

/**
 * @author zhengyao3/郑瑶
 * @date 2018-9-25 上午11:10:47
 * @Description:物业付款单结算回写明源插件
 * 
 */
public class SettleAfterWriteToMyPlugin implements IBusinessListener {

	public static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss:SSS");

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		SettlementAggVO[] settlementAggVOs = getVO(event);
		if (settlementAggVOs != null && settlementAggVOs.length >= 0) {
			for (SettlementAggVO aggvo : settlementAggVOs) {
				SettlementHeadVO headVO = (SettlementHeadVO) aggvo
						.getParentVO();
				String pk_busibill = headVO.getPk_busibill();
				String guidSql = " pk_paybill = '" + pk_busibill
						+ "' and nvl(dr,0) = 0";
				Collection<PayBillVO> paybillVOs = new BaseDAO()
						.retrieveByClause(PayBillVO.class, guidSql);
				for (PayBillVO paybillVO : paybillVOs) {
					if ( "F3-Cxx-W01".equals(paybillVO.getPk_tradetype())) {
						String guid = paybillVO.getDef1();
						//add by zhengyao3@郑瑶 增加逆向调用结算接口 2018年10月25日18:03:36 begin
						if(null == guid) {
							throw new BusinessException("guid为空，请检查");
						}
						RequestToEsbVO ruquestVO = getEsbVO(guid);
						ruquestVO.getRequestInfo().setPayStatus("未支付");
						if("CMPsettleAfter".equals(event.getEventType())){
							ruquestVO.getRequestInfo().setPayStatus("完全支付");
						}
						
						String resultXML = NCToMyXmlUtil.sendHttpXmlToMy(
								ruquestVO, "MY-NC-PM-001");// 获取调用返回的明源的XML
						ResultEsbMyToNcVO resultVO = NCToMyXmlUtil
								.getResultEsbMyToNcVO(resultXML);// 明源返回的XML转换成对应的VO
						if (resultVO != null) {
							ResultMyToNcVO vo = resultVO.getResultInfo();
							if (!"S".equals(vo.getStatus())) {// 如果不是S,则就是失败
								throw new BusinessException("明源单据"
										+ vo.getBusinessGUID()
										+ "调用实付回写失败,错误为:" + vo.getReturnMsg());
							}
						}
						//add by zhengyao3@郑瑶 增加逆向调用结算接口 2018年10月25日18:03:36 end
					}
				}
			}
		}

	}

	/**
	 * 获取调用明源的VO,作为后续转换为XML使用
	 * 
	 * @param guid
	 * @return
	 */
	private RequestToEsbVO getEsbVO(String guid) {
		String status = "完全支付";
		RequestToEsbVO esb = new RequestToEsbVO();
		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);
		RequestInfoVO infovo = new RequestInfoVO();
		infovo.setFKApplyGUID(guid);
		infovo.setPayStatus(status);
		esb.setRequestInfo(infovo);
		return esb;
	}

	/**
	 * 获取结算VO
	 * 
	 * @param event
	 * @return
	 */
	private SettlementAggVO[] getVO(IBusinessEvent event) {
		if (!(event instanceof BusinessEvent)) {
			return null;
		}
		BusinessEvent businessEvent = (BusinessEvent) event;
		Object obj = ((BusinessUserObj) businessEvent.getUserObject())
				.getUserObj();
		if (obj == null) {
			return null;
		}
		SettlementAggVO[] settlementAggVOs = null;
		if (obj instanceof SettlementAggVO[]) {
			settlementAggVOs = (SettlementAggVO[]) obj;
		} else if (obj instanceof SettlementAggVO) {
			settlementAggVOs = new SettlementAggVO[] { (SettlementAggVO) obj };
		} else {
			return null;
		}
		if (settlementAggVOs == null || settlementAggVOs.length <= 0) {
			return null;
		}
		return settlementAggVOs;
	}

}
