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.house.HouseInfoAddRequest;
import com.ruoyi.web.domain.dto.house.HouseInfoEditRequest;
import com.ruoyi.web.domain.dto.house.HouseInfoQueryRequest;
import com.ruoyi.web.domain.vo.HouseInfoVO;
import com.ruoyi.web.service.HouseInfoService;
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/houseinfo")
public class HouseInfoController {

    @Autowired
    private HouseInfoService houseInfoService;

    /**
     * 添加房屋
     *
     * @param houseInfoAddRequest
     * @return
     */
    @ApiOperation("添加房屋")
    @PostMapping("/add")
    public AjaxResult addHouseInfo(@RequestBody HouseInfoAddRequest houseInfoAddRequest) {
        if (houseInfoAddRequest == null) {
            return error("请求参数为空");
        }
        Long id = houseInfoService.addHouseInfo(houseInfoAddRequest);
        return success(id);
    }

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


    /**
     * 编辑房屋
     *
     * @param houseInfoEditRequest
     * @return
     */
    @ApiOperation("编辑房屋")
    @PostMapping("/edit")
    public AjaxResult editHouseInfo(@RequestBody HouseInfoEditRequest houseInfoEditRequest) {
        if (houseInfoEditRequest == null) {
            return error("请求参数为空");
        }
        houseInfoService.editHouseInfo(houseInfoEditRequest);
        return AjaxResult.success();
    }

    /**
     * 根据id获取房屋
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取房屋")
    @GetMapping("/get")
    public AjaxResult getHouseInfoById(@RequestParam int id) {
        if (ObjectUtil.isEmpty(id)) {
            return error("请求参数为空");
        }
        HouseInfoVO houseInfoVO = houseInfoService.getHouseInfoById(id);
        return success(houseInfoVO);
    }

    /**
     * 分页获取房屋列表  mybatis-plus
     *
     * @param houseInfoQueryRequest
     * @return
     */
    @ApiOperation("分页获取房屋列表")
    @PostMapping("/list/page")
    public AjaxResult listHouseInfoByPage(@RequestBody HouseInfoQueryRequest houseInfoQueryRequest) {
        if (houseInfoQueryRequest == null) {
            return error("请求参数为空");
        }
        Page<HouseInfoVO> listHouseInfoByPage = houseInfoService.getListHouseInfoByPage(houseInfoQueryRequest);
        return success(listHouseInfoByPage);
    }


    @ApiOperation("获取房屋列表")
    @GetMapping("/list/list")
    public AjaxResult list() {
        return success(houseInfoService.list());
    }

    @ApiOperation("获取房屋详情")
    @PostMapping("/getHouseInfoBy")
    public AjaxResult getHouseInfoBy(@RequestBody Map<String ,Integer> map ) {
        return success(houseInfoService.getHouseInfoBy(map.get("id")));
    }
    @ApiOperation("存geoData")
    @PutMapping("/saveGeoData")
    public AjaxResult saveGeoData(@RequestBody Map<String ,Object> map ) {
        houseInfoService.saveGeoData(map);
        return success();
    }
}
