package com.ruoyi.web.v2.v1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.model.ProtSj;
import com.ruoyi.web.v2.v1.service.IProtSjService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/prot-sj")
@Api("环保数据")
public class ProtSjController {
    @Autowired
    private  IProtSjService protSjService;

    @ApiOperation("环保数据分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "startDate", required = false) String startDate
            , @RequestParam(value = "endDate", required = false) String endDate) {
        QueryWrapper<ProtSj> queryWrapper = new QueryWrapper<>();
        Page<ProtSj> page = new Page<>(pageNum, pageSize);
        queryWrapper.ge(StringUtils.isNotEmpty(endDate), "create_time", endDate);
        queryWrapper.ge(StringUtils.isNotEmpty(startDate), "create_time", endDate);
        queryWrapper.orderByDesc("id");
        return success(protSjService.page(page,queryWrapper));
    }

    @ApiOperation("环保数据列表")
    @PostMapping("/list")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String startDate = paramsMap.get("startDate");
        String endDate = paramsMap.get("endDate");
        QueryWrapper<ProtSj> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(endDate)){
            endDate  += " 23:59:59";
        }

        queryWrapper.le(StringUtils.isNotEmpty(endDate), "create_time", endDate);
        queryWrapper.ge(StringUtils.isNotEmpty(startDate), "create_time", startDate);
        queryWrapper.orderByDesc("id");
        return success(protSjService.list(queryWrapper));
    }
    @ApiOperation("环保数据最新")
    @GetMapping("/getOne")
    public AjaxResult listById(){

        QueryWrapper<ProtSj> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit 1");
        queryWrapper.orderByDesc("id");
        return success(protSjService.getOne(queryWrapper));
    }
}
