/**
   * 回调通知信息入库
   *
   * @param notifyParams 支付宝回调信息
   */
  private void insertPayNotify(Map<String, String> notifyParams) throws Exception {
    PayNotify payNotify = new PayNotify();

    payNotify.setNotifyText(notifyParams.toString());
    payNotify.setCreateTime(new Date());
    payNotify.setTradeNo(notifyParams.get("transaction_id"));

    if (!daoService.insertPayNotify(payNotify)) {
      LOGGER.error("insertPayNotify fail: notifyParams=" + notifyParams);
    }
  }
  /**
   * 更新订单信息
   *
   * @param notifyParams 微信支付回调信息
   * @return 订单信息
   * @throws Exception 异常信息
   */
  private PayOrder updatePayOrderByPayChannel(Map<String, String> notifyParams, String payChannel)
      throws Exception {
    // 查询该订单号是否存在
    String outOrderId = notifyParams.get("out_trade_no");
    PayOrder queryPayOrder =
        daoService.queryPayOrderByOutOrderIdAndPayChannel(outOrderId, payChannel);

    if (queryPayOrder == null) {
      throw new RuntimeException(
          " updatePayOrder error: outOrderId=" + outOrderId + ", pay_channel=" + payChannel);
    }

    /* 更新订单交易信息 */
    PayOrder payOrderForUpdate = new PayOrder();
    payOrderForUpdate.setPayStatus(Constants.PAY_SUCCESS);
    payOrderForUpdate.setPayChnDesc(notifyParams.get("result_code"));
    payOrderForUpdate.setUpdateTime(new Date());
    payOrderForUpdate.setTradeNo(notifyParams.get("transaction_id"));

    if (!daoService.updatePayOrderByOutOrderIdAndPayChannel(
        payOrderForUpdate, outOrderId, payChannel)) {
      throw new RuntimeException(
          " updatePayOrder -> update pay_order error: outOrderId="
              + outOrderId
              + ", payChannel="
              + payChannel);
    }

    // 返回必须的订单信息
    PayOrder retPayOrder = new PayOrder();

    retPayOrder.setOutNotifyUrl(queryPayOrder.getOutNotifyUrl());
    retPayOrder.setPayId(queryPayOrder.getPayId());
    retPayOrder.setOutOrderId(queryPayOrder.getOutOrderId());
    retPayOrder.setMerPriv(queryPayOrder.getMerPriv());
    retPayOrder.setPayStatus(payOrderForUpdate.getPayStatus());
    retPayOrder.setPayChnDesc(payOrderForUpdate.getPayChnDesc());

    return retPayOrder;
  }
  public String umpNotify(HttpServletRequest request, NotifyReq reqParam) {
    try {
      // 记录回调日志
      PayNotify payNotify = new PayNotify();
      payNotify.setNotifyText(reqParam.toString());
      if (!daoService.insertPayNotify(payNotify)) {
        LOGGER.error("insertPayNotify fail: notifyText=" + reqParam.toString());
      }

      // 验证签名
      if (!UmpUtils.validateNotifySign(request, reqParam)) {
        return UmpUtils.buildNotifyResponse(
            reqParam, Constants.UMP_CODE_VERIFY_SIGN_FAIL, Constants.UMP_MSG_VERIFY_SIGN_FAIL);
      }

      // 查询该订单号是否存在
      String outOrderId = reqParam.getOrderId();
      String outSysId = Constants.OUT_SYS_ID_UMP;

      PayOrder payOrder = daoService.queryPayOrderByOutOrderIdAndOutSysId(outOrderId, outSysId);
      if (payOrder == null) {
        return UmpUtils.buildNotifyResponse(
            reqParam,
            Constants.UMP_CODE_QUERY_BY_OUT_ORDER_ID_NOT_EXIT,
            Constants.UMP_MSG_QUERY_BY_OUT_ORDER_ID_NOT_EXIT);
      }

      // 用户首次支付, 则记录用户协议编号
      if (reqParam.getUsrPayAgreementId() != null) {
        String userId = payOrder.getMerUserId();
        PayAgreement userPayAgreement = daoService.queryPayAgreementByUserId(userId);
        // 未签订协议,则插入协议表
        if (userPayAgreement == null) {
          // 插入失败,则记录日志信息
          if (!daoService.insertPayAgreement(userId, reqParam)) {
            LOGGER.error("notify insert pay_agreement fail");
          }
        }
      }

      // 更新交易状态
      String tradeStatus = reqParam.getTradeState();
      String tradeCode = CommonUtils.getTransCodeByTradeStatus(tradeStatus);
      PayOrder payOrderUpdate = UmpUtils.buildNotifyUpdatePayOrder(tradeCode, tradeStatus);

      if (!daoService.updatePayOrderByOutOrderIdAndOutSysId(payOrderUpdate, outOrderId, outSysId)) {
        return UmpUtils.buildNotifyResponse(
            reqParam, Constants.UMP_CODE_UPDATE_ORDER_FAIL, Constants.UMP_MSG_UPDATE_ORDER_FAIL);
      }

      // 通知O2O
      String retCode = payOrderUpdate.getPayStatus();
      String retMsg = payOrderUpdate.getPayChnDesc();
      String outNotifyUrl = payOrder.getOutNotifyUrl();
      int payId = payOrder.getPayId();
      String merPriv = payOrder.getMerPriv();

      notifyService.notifyO2O(outNotifyUrl, payId, outOrderId, retCode, retMsg, merPriv);

      // 构建返回信息
      return UmpUtils.buildNotifyResponse(
          reqParam,
          Constants.UMP_RETURN_CODE_CLIENT_SUCCESS,
          Constants.UMP_RETURN_MSG_CLIENT_SUCCESS);
    } catch (Exception e) {
      LOGGER.error("umpNotify error: reqParam=" + reqParam, e);
      return UmpUtils.buildNotifyResponse(
          reqParam, Constants.UMP_RETURN_CODE_CLIENT_FAIL, Constants.UMP_RETURN_MSG_CLIENT_FAIL);
    }
  }