package nc.bs.arap.busireg;

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.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.IEventType;
import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.arap.pay.PayBillVO;
import nc.vo.esb.ArapRequestToEsbAnnotatioVO;
import nc.vo.esb.ResultEsbMyToNcVO;
import nc.vo.esb.ResultMyToNcVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;

/** 
 * @author zhengyao3/郑瑶
 * @date 2018-10-10 下午3:58:35
 * @Description:物业的付款单取消审批,删除调用明源接口
 *
 */
public class PaybillOfMyResversibleListener extends PayablebillOfMyResversibleListener implements IBusinessListener  {

	@Override
	public void doAction(IBusinessEvent event)
			throws BusinessException {
		AggregatedValueObject[] billvos = getAggVO(event);
		for (AggregatedValueObject bill : billvos) {
			PayBillVO vo =  (PayBillVO) bill.getParentVO();
			String tradeType = vo.getPk_tradetype();
			//有票不调用明源
			String def3 = vo.getDef3();//def59改为3
			if("Y".equals(def3)){
				continue;
			}
			if(PropertyNCBilltypeConstant.F3W01BILLTYPE.equals(tradeType)){
				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";
				}
				//修改调用的方式,不适用注解进行调用Xstream begin
				businessGUID = vo.getDef1();//def60改为1
				XStream x = new XStream(new DomDriver("utf-8"));
				x.alias("requestBaseEntity", ArapRequestToEsbAnnotatioVO.class);
				if(businessGUID == null || businessGUID.length() == 0){
					String sql = "select a.def1 from ap_paybill a where a.pk_paybill = '"+vo.getPk_paybill()+"'";
					Object value = new BaseDAO().executeQuery(sql, new ColumnProcessor());
					if(value != null && value.toString().length() > 0){
						businessGUID = value.toString();
					}
				}
				String def37 = vo.getDef37();//def63改为def37 明源单据编号
				String requestXML = x.toXML(getEsbVO(billType, businessGUID, operatorType,null,def37));
				String resultXML = NCToMyXmlUtil.sendXMLToMy(requestXML, URLCode, businessGUID);
				//改调用的方式,不适用注解进行调用Xstream end 
				ResultEsbMyToNcVO resultVO = NCToMyXmlUtil
						.getReverseResultEsbMyToNcVO(resultXML,elementResponse,elementResult);// 明源返回的XML转换成对应的VO
				if (resultVO != null) {
					ResultMyToNcVO resultInfoVO = resultVO.getResultInfo();
					if (!"S".equals(resultInfoVO.getStatus())) {// 如果不是S,则就是失败
						throw new BusinessException("明源单据"
								+ resultInfoVO.getBusinessGUID()
								+ "调用取消审批或者删除接口失败,错误为:" + resultInfoVO.getReturnMsg());
					}
				}
			}
		}
	}

}
