package com.ruoyi.web.controller.houseInfo;

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.domain.entity.ResidentInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.houseInfo.HouseInfo;
import com.ruoyi.system.service.IHouseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 房屋信息Controller
 *
 * @author boman
 * @date 2025-02-20
 */
@RestController
@RequestMapping("/wuYe/houseInfo")
public class HouseInfoController extends BaseController {
    @Autowired
    private IHouseInfoService houseInfoService;

    /**
     * 查询房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:houseInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResidentInfo residentInfo) {
        startPage();
        List<ResidentInfo> list = houseInfoService.selectHouseInfoListAndResidentInfo(residentInfo);
        return getDataTable(list);
    }

    /**
     * 查询房屋信息列表不分页
     */
    @PreAuthorize("@ss.hasPermi('wuYe:houseInfo:listNoPage')")
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(HouseInfo houseInfo) {
        List<HouseInfo> list = houseInfoService.selectHouseInfoList(houseInfo);
        return getDataTable(list);
    }

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

    /**
     * 导入房屋信息列表
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<HouseInfo> util = new ExcelUtil<HouseInfo>(HouseInfo.class);
        List<HouseInfo> houseInfoList = util.importExcel(file.getInputStream());
        return houseInfoService.importHouseInfo(houseInfoList);
    }

    /**
     * 获取房屋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wuYe:houseInfo:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") Long houseId) {
        return success(houseInfoService.selectHouseInfoByHouseId(houseId));
    }

    /**
     * 新增房屋信息
     */
    @PreAuthorize("@ss.hasPermi('wuYe:houseInfo:add')")
    @Log(title = "房屋信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseInfo houseInfo) {
        return toAjax(houseInfoService.insertHouseInfo(houseInfo));
    }

    /**
     * 修改房屋信息
     */
    @PreAuthorize("@ss.hasPermi('wuYe:houseInfo:edit')")
    @Log(title = "房屋信息", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody HouseInfo houseInfo) {
        return toAjax(houseInfoService.updateHouseInfo(houseInfo));
    }

    /**
     * 删除房屋信息
     */
    @PreAuthorize("@ss.hasPermi('wuYe:houseInfo:remove')")
    @Log(title = "房屋信息", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{houseIds}")
    public AjaxResult remove(@PathVariable Long[] houseIds) {
        return toAjax(houseInfoService.deleteHouseInfoByHouseIds(houseIds));
    }
}
