package ink.wgink.module.wechat.request.pay.v3.order;

import com.alibaba.fastjson.JSONObject;
import ink.wgink.exceptions.base.SystemException;
import ink.wgink.module.wechat.pojo.pay.v3.PayErrorResponse;
import ink.wgink.module.wechat.pojo.pay.v3.order.OrderSearch;
import ink.wgink.module.wechat.request.pay.v3.AbstractPayRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;

/**
 * @ClassName: OrderSearchPayRequestImpl
 * @Description: 订单查询
 * @Author: wanggeng
 * @Date: 2021/8/27 10:21 上午
 * @Version: 1.0
 */
public class OrderSearchPayRequestImpl extends AbstractPayRequest<String, String, OrderSearch> {

    @Override
    public String bodyJson(String s) {
        return null;
    }

    @Override
    public OrderSearch response(ResponseEntity<String> responseEntity) {
        LOG.debug("Response success status code: {}", responseEntity.getStatusCode());
        if (responseEntity.getStatusCode() == HttpStatus.OK) {
            return JSONObject.parseObject(responseEntity.getBody(), OrderSearch.class);
        }
        return null;
    }

    @Override
    public void error(HttpClientErrorException e) {
        LOG.debug("Response error status code：{}", e.getStatusCode());
        PayErrorResponse payErrorResponse = JSONObject.parseObject(e.getResponseBodyAsString(), PayErrorResponse.class);
        LOG.debug("Response error detail: {}", payErrorResponse.getDetail());
        throw new SystemException(payErrorResponse.getMessage());
    }
}
