package com.fjhx.demo;

import com.fjhx.demo.entity.CreateTenantVo;
import com.fjhx.demo.entity.ResultEntity;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

public class HxClientUtil {

    private static final RestTemplate restTemplate = new RestTemplate();

    static {
        restTemplate.setErrorHandler(new ResponseErrorHandler() {
            @Override
            public boolean hasError(@NotNull ClientHttpResponse response) {
                return false;
            }

            @Override
            public void handleError(@NotNull ClientHttpResponse response) {

            }
        });
    }

    private HxClientUtil() {
    }

//    public static void main(String[] args) {
//        CreateTenantVo createTenantVo = new CreateTenantVo();
//        createTenantVo.setTenantId("test2");
//        createTenantVo.setAccount("admin");
//        createTenantVo.setTenantName("测试租户");
//        ResultEntity<CreateTenantVo> result = createTenant(createTenantVo);
//        System.out.println(result);
//    }

    /**
     * 生成租户
     *
     * @param vo 租户信息
     * @return 租户信息
     */
    public static ResultEntity<CreateTenantVo> createTenant(CreateTenantVo vo) throws ResourceAccessException {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("token", JwtUtil.generateToken(vo));
        return restTemplate.postForEntity(HxClientConstant.CREATE_TENANT_URL, map, ResultEntity.class).getBody();
    }

}
