package com.ruoyi.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.dto.warning.environmentWarning.EnvironmentWarningAddRequest;
import com.ruoyi.web.domain.dto.warning.environmentWarning.EnvironmentWarningEditRequest;
import com.ruoyi.web.domain.dto.warning.environmentWarning.EnvironmentWarningQueryRequest;
import com.ruoyi.web.domain.vo.EnvironmentWarningVO;
import com.ruoyi.web.service.EnvironmentWarningService;
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.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@Api(tags = "人居环境预警")
@RestController
@RequestMapping("/system/environment/warning")
public class EnvironmentWarningController {

    @Autowired
    private EnvironmentWarningService environmentWarningService;

    /**
     * 添加人居环境预警
     *
     * @param environmentWarningAddRequest
     * @return
     */
    @ApiOperation("添加人居环境预警")
    @PostMapping("/add")
    public AjaxResult addEnvironmentWarning(@RequestBody EnvironmentWarningAddRequest environmentWarningAddRequest) {
        if (environmentWarningAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = environmentWarningService.addEnvironmentWarning(environmentWarningAddRequest);
        return success(id);
    }

    /**
     * 删除人居环境预警
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除人居环境预警")
    @PostMapping("/delete")
    public AjaxResult deleteEnvironmentWarning(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        boolean b = environmentWarningService.deleteEnvironmentWarning(ids);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }


    /**
     * 编辑人居环境预警
     *
     * @param environmentWarningEditRequest
     * @return
     */
    @ApiOperation("编辑人居环境预警")
    @PostMapping("/edit")
    public AjaxResult editEnvironmentWarning(@RequestBody EnvironmentWarningEditRequest environmentWarningEditRequest) {
        if (environmentWarningEditRequest == null) {
            return error("请求参数为空");
        }
        environmentWarningService.editEnvironmentWarning(environmentWarningEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取人居环境预警
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取人居环境预警")
    @GetMapping("/get")
    public AjaxResult getEnvironmentWarningById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        EnvironmentWarningVO environmentWarningVO = environmentWarningService.getEnvironmentWarningById(id);
        return success(environmentWarningVO);
    }

    /**
     * 分页获取人居环境预警列表
     *
     * @param environmentWarningQueryRequest
     * @return
     */
    @ApiOperation("分页获取人居环境预警列表")
    @PostMapping("/list/page")
    public AjaxResult listEnvironmentWarningByPage(@RequestBody EnvironmentWarningQueryRequest environmentWarningQueryRequest) {
        if (environmentWarningQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<EnvironmentWarningVO> listEnvironmentWarningVOByPage = environmentWarningService.getListEnvironmentWarningByPage(environmentWarningQueryRequest);
        return success(listEnvironmentWarningVOByPage);
    }

}
