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.emergency.EmergencyAddRequest;
import com.ruoyi.web.domain.dto.emergency.EmergencyEditRequest;
import com.ruoyi.web.domain.dto.emergency.EmergencyQueryRequest;
import com.ruoyi.web.domain.dto.emergency.EmergencyRevokeRequest;
import com.ruoyi.web.domain.vo.EmergencyVO;
import com.ruoyi.web.service.EmergencyService;
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/emergency")
public class EmergencyController {

    @Autowired
    private EmergencyService emergencyService;


    /**
     * 添加应急指令下达
     *
     * @param emergencyAddRequest
     * @return
     */
    @ApiOperation("添加应急指令下达")
    @PostMapping("/add")
    public AjaxResult addEmergency(@RequestBody EmergencyAddRequest emergencyAddRequest) {
        if (emergencyAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = emergencyService.addEmergency(emergencyAddRequest);
        return success(id);
    }

    /**
     * 删除应急指令下达
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除应急指令下达")
    @PostMapping("/delete")
    public AjaxResult deleteEmergency(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        boolean b = emergencyService.deleteEmergency(ids);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }

    /**
     * 编辑应急指令下达
     *
     * @param emergencyEditRequest
     * @return
     */
    @ApiOperation("编辑应急指令下达")
    @PostMapping("/edit")
    public AjaxResult editEmergency(@RequestBody EmergencyEditRequest emergencyEditRequest) {
        if (emergencyEditRequest == null) {
            return error("请求参数为空");
        }
        emergencyService.editEmergency(emergencyEditRequest);
        return AjaxResult.success();
    }

    /**
     * 撤回应急指令下达
     *
     * @param emergencyRevokeRequest
     * @return
     */
    @ApiOperation("撤回应急指令下达")
    @PostMapping("/revoke")
    public AjaxResult revokeEmergency(@RequestBody EmergencyRevokeRequest emergencyRevokeRequest) {
        if (emergencyRevokeRequest == null) {
            return error("请求参数为空");
        }
        emergencyService.revokeEmergency(emergencyRevokeRequest);
        return AjaxResult.success();
    }

    /**
     * 下达应急指令
     *
     * @param emergencyRevokeRequest
     * @return
     */
    @ApiOperation("下达应急指令")
    @PostMapping("/down")
    public AjaxResult downEmergency(@RequestBody EmergencyRevokeRequest emergencyRevokeRequest) {
        if (emergencyRevokeRequest == null) {
            return error("请求参数为空");
        }
        emergencyService.downEmergency(emergencyRevokeRequest);
        return AjaxResult.success();
    }

    /**
     * 根据id获取应急指令下达
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取应急指令下达")
    @GetMapping("/get")
    public AjaxResult getEmergencyById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        EmergencyVO emergencyById = emergencyService.getEmergencyById(id);
        return success(emergencyById);
    }

    /**
     * 分页获取应急指令下达列表
     *
     * @param emergencyQueryRequest
     * @return
     */
    @ApiOperation("分页获取应急指令下达列表")
    @PostMapping("/list/page")
    public AjaxResult listEmergencyByPage(@RequestBody EmergencyQueryRequest emergencyQueryRequest) {
        if (emergencyQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<EmergencyVO> listEmergencyVOByPage = emergencyService.getListEmergencyVOByPage(emergencyQueryRequest);
        return success(listEmergencyVOByPage);
    }
}
