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.facility.FacilityAddRequest;
import com.ruoyi.web.domain.dto.facility.FacilityEditRequest;
import com.ruoyi.web.domain.dto.facility.FacilityQueryRequest;
import com.ruoyi.web.domain.vo.FacilityManagement.BaseFacilityVO;
import com.ruoyi.web.service.FacilityManagementService;
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/facility")
public class FacilityManagementController {

    @Autowired
    private FacilityManagementService facilityManagementService;

    /**
     * 添加设施
     *
     * @param facilityAddRequest
     * @return
     */
    @ApiOperation("添加设施")
    @PostMapping("/add")
    public AjaxResult addFacility(@RequestBody FacilityAddRequest facilityAddRequest) {
        if (facilityAddRequest == null) {
            return error("请求参数为空");
        }
        Integer id = facilityManagementService.addFacility(facilityAddRequest);
        return success(id);
    }

    /**
     * 删除设施
     *
     * @param paramsMap
     * @return
     */
    @ApiOperation("删除设施")
    @PostMapping("/delete")
    public AjaxResult deleteFacility(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        if (StrUtil.isBlank(ids)) {
            return error("请求参数为空");
        }
        boolean b = facilityManagementService.deleteFacility(ids);
        if (b) {
            return success("删除成功");
        }
        return error("删除失败");
    }


    /**
     * 编辑设施
     *
     * @param facilityEditRequest
     * @return
     */
    @ApiOperation("编辑设施")
    @PostMapping("/edit")
    public AjaxResult editFacility(@RequestBody FacilityEditRequest facilityEditRequest) {
        if (facilityEditRequest == null) {
            return error("请求参数为空");
        }
        facilityManagementService.editFacility(facilityEditRequest);
        return AjaxResult.success();
    }

    /**
     * 根据id获取设施
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取设施")
    @GetMapping("/get")
    public AjaxResult getFacilityById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        BaseFacilityVO facilityVO = facilityManagementService.getFacilityById(id);
        return success(facilityVO);
    }


    /**
     * 分页获取设施列表
     *
     * @param facilityQueryRequest
     * @return
     */
    @ApiOperation("分页获取设施列表")
    @PostMapping("/list/page")
    public AjaxResult listFacilityByPage(@RequestBody FacilityQueryRequest facilityQueryRequest) {
        if (facilityQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<? extends BaseFacilityVO> listFacilityManagementByPage = facilityManagementService.getListFacilityManagementByPage(facilityQueryRequest);
        return success(listFacilityManagementByPage);
    }
}
