package com.ruoyi.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.domain.dto.environmentWarningThresholdRole.EnvironmentWarningThresholdRoleAddRequest;
import com.ruoyi.web.domain.dto.environmentWarningThresholdRole.EnvironmentWarningThresholdRoleEditRequest;
import com.ruoyi.web.domain.dto.environmentWarningThresholdRole.EnvironmentWarningThresholdRoleQueryRequest;
import com.ruoyi.web.domain.entity.EnvironmentWarningThresholdRole;
import com.ruoyi.web.service.EnvironmentWarningThresholdRoleService;
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.List;
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/environmentWarning/warning/threshold/role")
public class EnvironmentWarningThresholdRoleController {

    @Autowired
    private EnvironmentWarningThresholdRoleService environmentWarningThresholdRoleService;

    /**
     * 添加人居环境负责人类型
     *
     * @param environmentWarningThresholdRoleAddRequest
     * @return
     */
    @ApiOperation("添加人居环境负责人类型")
    @PostMapping("/add")
    public AjaxResult addEnvironmentWarningThresholdRole(@RequestBody EnvironmentWarningThresholdRoleAddRequest environmentWarningThresholdRoleAddRequest) {
        if (environmentWarningThresholdRoleAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = environmentWarningThresholdRoleService.addEnvironmentWarningThresholdRole(environmentWarningThresholdRoleAddRequest);
        return success(id);
    }

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

    /**
     * 编辑人居环境负责人类型
     *
     * @param environmentWarningThresholdRoleEditRequest
     * @return
     */
    @ApiOperation("编辑人居环境负责人类型")
    @PostMapping("/edit")
    public AjaxResult editEnvironmentWarningThresholdRole(@RequestBody EnvironmentWarningThresholdRoleEditRequest environmentWarningThresholdRoleEditRequest) {
        if (environmentWarningThresholdRoleEditRequest == null) {
            return error("请求参数为空");
        }
        environmentWarningThresholdRoleService.editEnvironmentWarningThresholdRole(environmentWarningThresholdRoleEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取人居环境负责人类型
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取人居环境负责人类型")
    @GetMapping("/get")
    public AjaxResult getEnvironmentWarningThresholdRoleById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        EnvironmentWarningThresholdRole environmentWarningThresholdRoleById = environmentWarningThresholdRoleService.getEnvironmentWarningThresholdRoleById(id);
        return success(environmentWarningThresholdRoleById);
    }


    /**
     * 获取人居环境负责人类型列表
     *
     * @return
     */
    @ApiOperation("获取人居环境负责人类型列表")
    @PostMapping("/list")
    public AjaxResult listEnvironmentWarningThresholdRoleByPage(@RequestBody EnvironmentWarningThresholdRoleQueryRequest environmentWarningThresholdRoleQueryRequest) {
        if (environmentWarningThresholdRoleQueryRequest == null) {
            throw new ServiceException("请求参数未空");
        }
        List<EnvironmentWarningThresholdRole> list = environmentWarningThresholdRoleService.getListEnvironmentWarningThresholdRole(environmentWarningThresholdRoleQueryRequest);
        return success(list);
    }
}
