package com.yfd.platform.modules.basedata.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.basedata.domain.WeatherLog;
import com.yfd.platform.modules.basedata.service.IWeatherLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 微气象数据日志表 前端控制器
 * </p>
 *
 *
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/basedata/weather-log")
@Api(value = "WeatherLogController", tags = "微气象数据日志")
public class WeatherLogController {

    @Resource
    private IWeatherLogService weatherLogService;

    @GetMapping("/getWeatherLog")
    @ApiOperation("查询环境信息")
    public ResponseResult getWeatherLogList(String stationId) {

        if (StrUtil.isBlank(stationId)) {
            return ResponseResult.error("未传变电站信息");
        }
        Map<String, Object> weatherDevice = weatherLogService.getWeatherLogList(stationId);
        return ResponseResult.successData(weatherDevice);
    }

    @GetMapping("/getWeatherLogPage")
    @ApiOperation("分页查询环境信息")
    public ResponseResult getWeatherLogPage(Page<Map<String, Object>> page, String stationId, String patroldeviceName
            , String startDate, String endDate) {
        if (StrUtil.isBlank(stationId)) {
            return ResponseResult.error("未传变电站信息");
        }
        String startFormat = "";
        if (StrUtil.isNotBlank(startDate)) {
            Date parseStart = DateUtil.parse(startDate);
            //一天的开始
            Date beginOfDay = DateUtil.beginOfDay(parseStart);
            startFormat = DateUtil.format(beginOfDay, "yyyy-MM-dd HH:mm:ss");
        }
        String endFormat = "";
        if (StrUtil.isNotBlank(startDate)) {
            Date parseEnd = DateUtil.parse(endDate);
            //一天的结束
            Date endOfDay = DateUtil.endOfDay(parseEnd);
            endFormat = DateUtil.format(endOfDay, "yyyy-MM-dd HH:mm:ss");
        }
        LambdaQueryWrapper<WeatherLog> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(stationId)) {
            queryWrapper.eq(WeatherLog::getStationId, stationId);
        }
        if (StrUtil.isNotBlank(patroldeviceName)) {
            queryWrapper.like(WeatherLog::getPatroldeviceName, patroldeviceName);
        }
        if (StrUtil.isNotBlank(startFormat) && StrUtil.isNotBlank(endFormat)) {
            queryWrapper.le(WeatherLog::getTime, endFormat);
            queryWrapper.ge(WeatherLog::getTime, startFormat);
        }
        Page<Map<String, Object>> mapPage = weatherLogService.pageMaps(page, queryWrapper);
        return ResponseResult.successData(mapPage);
    }

}
