ClientCustomerServiceHttpClient.java 1.9 KB
package com.infoloop.tianting.service.client;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.infoloop.tianting.model.dto.ClientCustomerDbDTO.CurrentClientCustomerDto;
import com.infoloop.tianting.model.dto.ClientCustomerDbDTO.CurrentClientCustomerListDto;
import lombok.RequiredArgsConstructor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClientCustomerServiceHttpClient {
  private final OkHttpClient client = new OkHttpClient();

  public List<CurrentClientCustomerDto> getCurrentClientCustomer(String token, boolean isHis) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {
      String url = "";
      if (isHis) {
        url = "https://nutri.amcare.com.cn/v3/hiscustomers/current/query";
      } else {
        url = "https://nutri.amcare.com.cn/v3/clientcustomers/current/query";
      }

      MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
      RequestBody body = RequestBody.create(mediaType, "{}");
      Request request = new Request.Builder()
          .url(url)
          .post(body)
          .addHeader("Authorization", token)
          .build();

      Response response = client.newCall(request).execute();

      String jsonData = response.body().string();
      objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      return objectMapper.readValue(jsonData, CurrentClientCustomerListDto.class).getRows();
    } catch (Exception e) {
      System.out.println("error"+e.getMessage());
      throw new IllegalArgumentException("获取当日在住客户失败");
    }
  }
}