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.lotEnvironment.LotEnvironmentAddRequest;
import com.ruoyi.web.domain.dto.lotEnvironment.LotEnvironmentEditRequest;
import com.ruoyi.web.domain.dto.lotEnvironment.LotEnvironmentQueryRequest;
import com.ruoyi.web.domain.vo.LotEnvironmentVO;
import com.ruoyi.web.service.LotEnvironmentService;
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/lot/environment")
public class LotEnvironmentController {

    @Autowired
    private LotEnvironmentService lotEnvironmentService;

    /**
     * 添加物联网实时数据人居环境监测
     *
     * @param lotEnvironmentAddRequest
     * @return
     */
    @ApiOperation("添加物联网实时数据人居环境监测")
    @PostMapping("/add")
    public AjaxResult addLotEnvironment(@RequestBody LotEnvironmentAddRequest lotEnvironmentAddRequest) {
        if (lotEnvironmentAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = lotEnvironmentService.addLotEnvironment(lotEnvironmentAddRequest);
        return success(id);
    }

    /**
     * 删除物联网实时数据人居环境监测
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除物联网实时数据人居环境监测")
    @PostMapping("/delete")
    public AjaxResult deleteLotEnvironment(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        boolean b = lotEnvironmentService.deleteLotEnvironment(ids);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }


    /**
     * 编辑物联网实时数据人居环境监测
     *
     * @param lotEnvironmentEditRequest
     * @return
     */
    @ApiOperation("编辑物联网实时数据人居环境监测")
    @PostMapping("/edit")
    public AjaxResult editLotEnvironment(@RequestBody LotEnvironmentEditRequest lotEnvironmentEditRequest) {
        if (lotEnvironmentEditRequest == null) {
            return error("请求参数为空");
        }
        lotEnvironmentService.editLotEnvironment(lotEnvironmentEditRequest);
        return AjaxResult.success();
    }


    /**
     * 根据id获取物联网实时数据人居环境监测
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取物联网实时数据人居环境监测")
    @GetMapping("/get")
    public AjaxResult getLotEnvironmentById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        LotEnvironmentVO lotEnvironmentVO = lotEnvironmentService.getLotEnvironmentById(id);
        return success(lotEnvironmentVO);
    }

    /**
     * 分页获取物联网实时数据人居环境监测列表
     *
     * @param lotEnvironmentQueryRequest
     * @return
     */
    @ApiOperation("分页获取物联网实时数据人居环境监测列表")
    @PostMapping("/list/page")
    public AjaxResult listLotEnvironmentByPage(@RequestBody LotEnvironmentQueryRequest lotEnvironmentQueryRequest) {
        if (lotEnvironmentQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<LotEnvironmentVO> listLotEnvironmentVOByPage = lotEnvironmentService.getListLotEnvironmentByPage(lotEnvironmentQueryRequest);
        return success(listLotEnvironmentVOByPage);
    }
}
