package com.huimv.apiservice.controller;

import com.huimv.apiservice.entity.vo.*;
import com.huimv.apiservice.limit.annotation.Limit;
import com.huimv.apiservice.service.PigService;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.R;
import com.huimv.common.validate.ListValue;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * @author yinhao
 * @date 2021/5/8 16:09
 */
@CrossOrigin
@RestController
@RequestMapping("/pig")
@Validated
public class PigController {

    @Autowired
    private PigService pigService;

    /**
     * 获取猪基本信息
     *
     * @param accessToken token
     * @param pigEarTagNo 耳标号
     * @return
     */
    //@Limit(key = "pig_getPigInfo",name = "pig/getPigInfo",prefix = "apiservice")
    @GetMapping("/getPigInfo")
    public R getPigInfo(@RequestParam(value = "accessToken") String accessToken,
                        @NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        PigHealthStatusVo pigHealthStatusVo = pigService.getPigInfoByEarTagNo(pigEarTagNo);
        return Objects.requireNonNull(R.ok().put("code", 1000)).put("data", pigHealthStatusVo);
    }


    /**
     * 获取多媒体接口(照片/视频)
     *
     * @param accessToken token
     * @param pigEarTagNo 耳标号
     * @return
     */
    @GetMapping("/getImage")
    public R getImage(@RequestParam(value = "accessToken") String accessToken,
                      @NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        PigImageVo pigImageVo = pigService.getImageByEarTagNo(pigEarTagNo);
        return Objects.requireNonNull(R.ok().put("code", 1000)).put("data", pigImageVo);
    }

    /**
     * 根据品种名称获取猪的数量和基本信息
     *
     * @param accessToken token
     * @param breedName   品种名称
     * @return
     */
    @GetMapping("/getListByBreed")
    public R getListByBreed(@RequestParam(value = "accessToken") String accessToken,
                            @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) {
        PageUtils page = pigService.getPageByBreedAndFence(breedName, currentPage, pageSize, period);
        return Objects.requireNonNull(R.ok().put("code", 1000)).put("page", page);
    }

    /**
     * 获取环境数据
     *
     * @param accessToken token
     * @param pigEarTagNo 耳标号
     * @return
     */
    @GetMapping("/getIndoorEnv")
    public R getIndoorEnv(@RequestParam(value = "accessToken") String accessToken,
                          @NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        EnvVo envVo = pigService.getIndoorEnv(pigEarTagNo);
        return Objects.requireNonNull(R.ok().put("code", 1000)).put("data", envVo);
    }

    /**
     * 修改认养状态
     *
     * @param accessToken token
     * @param pigEarTagNo 耳标号
     * @return
     */
    @GetMapping("/adopt")
    public R adopt(@RequestParam(value = "accessToken") String accessToken,
                   @NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo) {
        pigService.adopt(pigEarTagNo);
        return Objects.requireNonNull(R.ok().put("code", 1000)).put("msg", "认养成功!");
    }

    /**
     * 出栏
     *
     * @param accessToken token
     * @param pigEarTagNo 耳标号
     * @return
     */
    @GetMapping("/outFence")
    public R outFence(@RequestParam(value = "accessToken", required = false) String accessToken,
                      @NotBlank(message = "耳标号不能为空!") @RequestParam("pigEarTagNo") String pigEarTagNo,
                      @NotNull(message = "出栏状态不能为空!") @ListValue(vals = {1, 2, 3}) @RequestParam("status") Integer status) {
        pigService.outFence(pigEarTagNo, status);
        return Objects.requireNonNull(R.ok().put("code", 1000)).put("msg", "出栏成功!");
    }
}
