RtMallHttpClient.java 2.98 KB
package com.diligrp.cashier.mall.client;

import com.diligrp.cashier.mall.domain.rtmall.vo.OrderDetailResponseVO;
import com.diligrp.cashier.mall.exception.RtMartMallException;
import com.diligrp.cashier.mall.util.HttpClientUtils;
import com.diligrp.cashier.mall.util.RtMallSignMd5Utils;
import com.diligrp.cashier.mall.type.RtMarkErrorCode;
import com.diligrp.cashier.shared.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 大润发HTTP客户端
 * 调用大润发订单详情接口
 */
public class RtMallHttpClient {
    private static final Logger log = LoggerFactory.getLogger(RtMallHttpClient.class);

    private final String apiUrl;
    private final String appKey;
    private final String appSecret;

    public RtMallHttpClient(String apiUrl, String appKey, String appSecret) {
        this.apiUrl = apiUrl;
        this.appKey = appKey;
        this.appSecret = appSecret;
    }

    /**
     * 查询订单详情
     *
     * @param orderId 订单号
     * @param tradeId 支付流水号
     * @param userCode 会员编号
     * @return 订单详情
     */
    public OrderDetailResponseVO getOrderDetail(String orderId, String tradeId, String userCode) {
        log.info("调用大润发订单详情接口: order_id={}, trade_id={}, user_code={}", orderId, tradeId, userCode);

        //构建请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("app_key", appKey);
        params.put("version", "v1");
        params.put("timestamp", System.currentTimeMillis() / 1000);
        params.put("method", "scanbuy.order.detail");
        params.put("user_code", userCode);
        params.put("order_id", orderId);
        params.put("trade_id", tradeId);

        //生成签名
        String sign = RtMallSignMd5Utils.generateSign(params, appSecret);
        params.put("sign", sign);

        //发送HTTP请求
        String responseBody = HttpClientUtils.postJson(apiUrl, params, null, "大润发订单详情");

        //解析响应
        OrderDetailResponseVO detailResponse = JsonUtils.fromJsonString(responseBody, OrderDetailResponseVO.class);
        if (detailResponse == null) {
            log.error("解析大润发响应失败");
            throw new RtMartMallException(RtMarkErrorCode.E4004);
        }

        //检查响应状态
        if (!"success".equals(detailResponse.getResult()) || !"E0000".equals(detailResponse.getCode())) {
            log.error("调用大润发订单详情接口失败: code={}, msg={}",
                    detailResponse.getCode(), detailResponse.getMsg());
            throw new RtMartMallException(detailResponse.getCode(), detailResponse.getMsg());
        }

        log.info("订单详情查询成功: order_id={}, item_count={}", orderId,
                detailResponse.getData() != null && detailResponse.getData().getItemList() != null
                        ? detailResponse.getData().getItemList().size() : 0);

        return detailResponse;
    }
}