package cn.iocoder.yudao.module.product.controller.admin.envdata;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.envdata.vo.EnvDataPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.envdata.vo.EnvDataRespVO;
import cn.iocoder.yudao.module.product.convert.envdata.EnvDataConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.envdata.EnvDataDO;
import cn.iocoder.yudao.module.product.service.envdata.EnvDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 环境数据")
@RestController
@RequestMapping("/product/env-data")
@Validated
public class EnvDataController {

    @Resource
    private EnvDataService envDataService;

    @GetMapping("/get")
    @ApiOperation("获得环境数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:env-data:query')")
    public CommonResult<EnvDataRespVO> getEnvData(@RequestParam("id") Long id) {
        EnvDataDO envData = envDataService.getEnvData(id);
        return success(EnvDataConvert.INSTANCE.convert(envData));
    }

    @GetMapping("/page")
    @ApiOperation("获得环境数据分页")
    @PreAuthorize("@ss.hasPermission('product:env-data:query')")
    public CommonResult<PageResult<EnvDataRespVO>> getEnvDataPage(@Valid EnvDataPageReqVO pageVO) {
        return success(envDataService.getEnvDataPage(pageVO));
    }

    @GetMapping("/getAVGEnvDatasByTypeIds")
    @ApiOperation("获得N天内环境数据的均值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "landId", value = "地块编码", required = true, example = "AECD202305041501UE6F", dataTypeClass = String.class),
            @ApiImplicitParam(name = "day", value = "最近X天", example = "7", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "typeIds", value = "类型id集合", required = true, example = "1,2,3", dataTypeClass = List.class),
    })
    @PreAuthorize("@ss.hasPermission('product:env-data:query')")
    public CommonResult<List<EnvDataRespVO>> getAVGEnvDatasByTypeIds(@RequestParam("landId") String landId, @RequestParam(value = "day", required = false) Integer day,
            @RequestParam("typeIds") List<Long> typeIds) {
        return CommonResult.success(envDataService.getAVGEnvDatas(landId, day, typeIds));
    }

}
