package com.ruoyi.szxc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.szxc.domain.SzxcHouseInfo;
import com.ruoyi.szxc.service.ISzxcHouseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 房屋信息Controller
 *
 * @author hs
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/szxc/houseinfo")
public class SzxcHouseInfoController extends BaseController
{
    @Autowired
    private ISzxcHouseInfoService szxcHouseInfoService;

    /**
     * 查询房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:houseinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcHouseInfo szxcHouseInfo)
    {
        startPage();
        List<SzxcHouseInfo> list = szxcHouseInfoService.selectSzxcHouseInfoList(szxcHouseInfo);
        return getDataTable(list);
    }

    /**
     * 导出房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:houseinfo:export')")
    @Log(title = "房屋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcHouseInfo szxcHouseInfo)
    {
        List<SzxcHouseInfo> list = szxcHouseInfoService.selectSzxcHouseInfoList(szxcHouseInfo);
        ExcelUtil<SzxcHouseInfo> util = new ExcelUtil<SzxcHouseInfo>(SzxcHouseInfo.class);
        util.exportExcel(response, list, "房屋信息数据");
    }

    /**
     * 获取房屋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:houseinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcHouseInfoService.selectSzxcHouseInfoById(id));
    }

    /**
     * 新增房屋信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:houseinfo:add')")
    @Log(title = "房屋信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SzxcHouseInfo szxcHouseInfo)
    {
        //根据前端传递过来的部门存储，如果没传则存储当前用户的部门
        if(szxcHouseInfo.getDeptId()==null){
            szxcHouseInfo.setDeptId(getDeptId());
        }
        szxcHouseInfo.setUserId(getUserId());
        return toAjax(szxcHouseInfoService.insertSzxcHouseInfo(szxcHouseInfo));
    }

    /**
     * 修改房屋信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:houseinfo:edit')")
    @Log(title = "房屋信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcHouseInfo szxcHouseInfo)
    {
        return toAjax(szxcHouseInfoService.updateSzxcHouseInfo(szxcHouseInfo));
    }

    /**
     * 删除房屋信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:houseinfo:remove')")
    @Log(title = "房屋信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcHouseInfoService.deleteSzxcHouseInfoByIds(ids));
    }
}
