package com.huimv.management.controller;

import com.huimv.common.utils.R;
import com.huimv.common.validate.ListValue;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * apiService本地测试接口
 * </p>
 *
 * @author yinhao
 * @date 2021/5/31 15:11
 */
@Validated
@RestController
@RequestMapping("management/localTest")
public class ApiServiceLocalTestController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String PREFIX = "http://115.238.57.190:8020/pig/";

    @Value("${local.token}")
    private String accessToken;

    @RequestMapping("/testGetPigInfo")
    public R testGetPigInfo(@NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        return restTemplate.getForObject(PREFIX + "getPigInfo?accessToken=" + accessToken + "&pigEarTagNo=" + pigEarTagNo, R.class);
    }

    @RequestMapping("/testGetImage")
    public R testGetImage(@NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        return restTemplate.getForObject(PREFIX + "getImage?accessToken=" + accessToken + "&pigEarTagNo=" + pigEarTagNo, R.class);
    }

    @RequestMapping("/testGetListByBreed")
    public R testGetListByBreed(@NotBlank(message = "品种名称不能为空!") @RequestParam("breedName") String breedName,
                                @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                @Range(min = 1, max = 1000, message = "每页条数的范围为1-1000") @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                @RequestParam(value = "period", required = false) String period) {
        String url = PREFIX + "getListByBreed?accessToken=" + accessToken + "&breedName=" + breedName + "&currentPage=" + currentPage + "&pageSize=" + pageSize;
        if (StringUtils.isNotEmpty(period)) {
            url = url + "&period=" + period;
        }
        return restTemplate.getForObject(url, R.class);
    }

    @RequestMapping("/testGetIndoorEnv")
    public R testGetIndoorEnv(@NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        return restTemplate.getForObject(PREFIX + "getIndoorEnv?accessToken=" + accessToken + "&pigEarTagNo=" + pigEarTagNo, R.class);
    }

    @RequestMapping("/testAdopt")
    public R testAdopt(@NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        return restTemplate.getForObject(PREFIX + "adopt?accessToken=" + accessToken + "&pigEarTagNo=" + pigEarTagNo, R.class);
    }

    @RequestMapping("/testOutFence")
    public R testOutFence(@NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo,
                          @NotNull(message = "出栏状态不能为空!") @ListValue(vals = {1, 2, 3}) @RequestParam("status") Integer status) {
        return restTemplate.getForObject(PREFIX + "outFence?accessToken=" + accessToken + "&pigEarTagNo=" + pigEarTagNo + "&status=" + status, R.class);
    }
}
