package com.rehome.mqttclienttemperature.controller.api;


import com.rehome.mqttclienttemperature.dto.RequestNowTemperatureDto;
import com.rehome.mqttclienttemperature.dto.RequestTemperatureDto;
import com.rehome.mqttclienttemperature.entity.Temperature;
import com.rehome.mqttclienttemperature.response.Result;
import com.rehome.mqttclienttemperature.service.TemperatureService;
import jakarta.annotation.Resource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author HuangWenfei
 * App自动更新
 * 2024-10-16 11:11
 */
@RestController
@CrossOrigin
@RequestMapping("/api/temperature")
public class TemperatureApiController {
    @Resource
    private TemperatureService temperatureService;

    /**
     * APP更新版本号
     */
    @PostMapping("/getHistoryTemperatureByTen")
    private List<Temperature> getHistoryTemperatureByTen(@RequestBody @Validated RequestTemperatureDto dto) {
        return temperatureService.findFirst10ByDataDateAndLocationDesc(dto.getDataDate(),dto.getLocationDesc());
    }

    /**
     * APP更新版本号
     */
    @PostMapping("/getRealTemperatureByTen")
    private List<Temperature> getRealTemperatureByTen(@RequestBody @Validated RequestNowTemperatureDto dto) {
        return temperatureService.findFirst10ByLocationDesc(dto.getLocationDesc());
    }

    /**
     *
     */
    @PostMapping("/findFirstByOrderByLocationDescAsc")
    private Temperature findFirstByOrderByLocationDescAsc(@RequestBody @Validated RequestNowTemperatureDto dto) {
        return temperatureService.findFirstByOrderByLocationDescAsc(dto.getLocationDesc());
    }

    /**
     * APP更新版本号
     */
    @PostMapping("/queryFirst10ByDataDateAndLocationDesc")
    private List<Temperature> queryFirst10ByDataDateAndLocationDesc(@RequestBody @Validated RequestTemperatureDto dto) {
        return temperatureService.queryFirst10ByDataDateAndLocationDesc(dto.getDataDate(),dto.getLocationDesc());
    }

    /**
     * APP更新版本号
     */
    @PostMapping("/queryFirst10ByDataDateAndLocationDescBindingResult")
    private Result queryFirst10ByDataDateAndLocationDescBindingResult(@RequestBody @Validated RequestTemperatureDto dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            //处理参数校验失败错误信息，有种处理方式。
            //1.通过捕获异常处理校验结果，详情见 com.rehome.mqttclienttemperature.response.GlobalExceptionHandler
            //2.通过在方法形参里加入BindingResult
            return Result.failedByParamValidated(bindingResult);
        }
        List<Temperature> listResult = temperatureService.queryFirst10ByDataDateAndLocationDesc(dto.getDataDate(),dto.getLocationDesc());
        return Result.success(listResult);
    }
}
