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.earthquakeWarning.EarthquakeWarningAddRequest;
import com.ruoyi.web.domain.dto.warning.earthquakeWarning.EarthquakeWarningEditRequest;
import com.ruoyi.web.domain.dto.warning.earthquakeWarning.EarthquakeWarningQueryRequest;
import com.ruoyi.web.domain.vo.EarthquakeWarningVO;
import com.ruoyi.web.service.EarthquakeWarningService;
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/earthquake/warning")
public class EarthquakeWarningController {

    @Autowired
    private EarthquakeWarningService earthquakeWarningService;

    /**
     * 添加地震监测预警
     *
     * @param earthquakeWarningAddRequest
     * @return
     */
    @ApiOperation("添加地震监测预警")
    @PostMapping("/add")
    public AjaxResult addEarthquakeWarning(@RequestBody EarthquakeWarningAddRequest earthquakeWarningAddRequest) {
        if (earthquakeWarningAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = earthquakeWarningService.addEarthquakeWarning(earthquakeWarningAddRequest);
        return success(id);
    }

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


    /**
     * 编辑地震监测预警
     *
     * @param earthquakeWarningEditRequest
     * @return
     */
    @ApiOperation("编辑地震监测预警")
    @PostMapping("/edit")
    public AjaxResult editEarthquakeWarning(@RequestBody EarthquakeWarningEditRequest earthquakeWarningEditRequest) {
        if (earthquakeWarningEditRequest == null) {
            return error("请求参数为空");
        }
        earthquakeWarningService.editEarthquakeWarning(earthquakeWarningEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取地震监测预警
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取地震监测预警")
    @GetMapping("/get")
    public AjaxResult getEarthquakeWarningById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        EarthquakeWarningVO earthquakeWarningVO = earthquakeWarningService.getEarthquakeWarningById(id);
        return success(earthquakeWarningVO);
    }

    /**
     * 分页获取地震监测预警列表
     *
     * @param earthquakeWarningQueryRequest
     * @return
     */
    @ApiOperation("分页获取地震监测预警列表")
    @PostMapping("/list/page")
    public AjaxResult listEarthquakeWarningByPage(@RequestBody EarthquakeWarningQueryRequest earthquakeWarningQueryRequest) {
        if (earthquakeWarningQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<EarthquakeWarningVO> listEarthquakeWarningVOByPage = earthquakeWarningService.getListEarthquakeWarningByPage(earthquakeWarningQueryRequest);
        return success(listEarthquakeWarningVOByPage);
    }
}
