package com.bs.pm.controller;

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

import com.bs.pm.domain.PmFacility;
import com.bs.pm.service.IPmFacilityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import com.bs.pm.domain.PmHouse;
import com.bs.pm.service.IPmHouseService;
import javax.annotation.Resource;

/**
 * 房屋信息Controller
 *
 * @author bs
 * @date 2025-10-04
 */
@Api(tags = "房屋信息")
@RestController
@RequestMapping("/pm/house")
public class PmHouseController extends BaseController {
    @Resource
    private IPmHouseService pmHouseService;
    @Resource
    private IPmFacilityService pmFacilityService;
    /**
     * 分页查询房屋信息列表
     */
    @ApiOperation("分页查询房屋信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmHouse pmHouse) {
        startPage();
        LambdaQueryWrapper<PmHouse> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmHouse);
        List<PmHouse> list = pmHouseService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询房屋信息列表
     */
    @ApiOperation("查询房屋信息列表")
    @GetMapping("/list")
    public AjaxResult list(PmHouse pmHouse) {
        LambdaQueryWrapper<PmHouse> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmHouse);
        List<PmHouse> list = pmHouseService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出房屋信息列表
     */
    @ApiOperation("导出房屋信息列表")
    @Log(title = "房屋信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmHouse pmHouse) {
        LambdaQueryWrapper<PmHouse> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmHouse);
        List<PmHouse> list = pmHouseService.list(queryWrapper);
        ExcelUtil<PmHouse> util = new ExcelUtil<PmHouse>(PmHouse. class);
        util.exportExcel(response, list, "房屋信息数据");
    }

    /**
     * 获取房屋信息详细信息
     */
    @ApiOperation("获取房屋信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pmHouseService.getById(id));
    }

    /**
     * 新增房屋信息
     */
    @ApiOperation("新增房屋信息")
    @Log(title = "房屋信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmHouse pmHouse) {
        return toAjax(pmHouseService.save(pmHouse));
    }

    /**
     * 修改房屋信息
     */
    @ApiOperation("修改房屋信息")
    @Log(title = "房屋信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmHouse pmHouse) {
        return toAjax(pmHouseService.updateById(pmHouse));
    }

    /**
     * 删除房屋信息
     */
    @ApiOperation("删除房屋信息")
    @Log(title = "房屋信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(pmHouseService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<PmHouse> queryWrapper,PmHouse pmHouse){


        //id
        if(null != (pmHouse.getId())){
            queryWrapper.eq(PmHouse::getId,pmHouse.getId());
        }

        //楼栋id
        if(null != (pmHouse.getBuildingId())){
            queryWrapper.eq(PmHouse::getBuildingId,pmHouse.getBuildingId());
        }

        //项目id
        if(null != (pmHouse.getProjectId())){
            queryWrapper.eq(PmHouse::getProjectId,pmHouse.getProjectId());
        }

        //单元号
        if(StringUtils.isNotEmpty(pmHouse.getUnitNo())){
            queryWrapper.eq(PmHouse::getUnitNo,pmHouse.getUnitNo());
        }

        //楼层号
        if(null != (pmHouse.getFloorNo())){
            queryWrapper.eq(PmHouse::getFloorNo,pmHouse.getFloorNo());
        }

        //房号
        if(StringUtils.isNotEmpty(pmHouse.getHouseNo())){
            queryWrapper.eq(PmHouse::getHouseNo,pmHouse.getHouseNo());
        }

        //户型
        if(StringUtils.isNotEmpty(pmHouse.getHouseType())){
            queryWrapper.eq(PmHouse::getHouseType,pmHouse.getHouseType());
        }

        //实用面积
        if(null != (pmHouse.getUsableArea())){
            queryWrapper.eq(PmHouse::getUsableArea,pmHouse.getUsableArea());
        }

        //产权类型
        if(StringUtils.isNotEmpty(pmHouse.getPropertyType())){
            queryWrapper.eq(PmHouse::getPropertyType,pmHouse.getPropertyType());
        }

        //房屋状态
        if(StringUtils.isNotEmpty(pmHouse.getHouseStatus())){
            queryWrapper.eq(PmHouse::getHouseStatus,pmHouse.getHouseStatus());
        }

        //房产证号
        if(StringUtils.isNotEmpty(pmHouse.getPropertyCertNo())){
            queryWrapper.eq(PmHouse::getPropertyCertNo,pmHouse.getPropertyCertNo());
        }

        //备注
        if(StringUtils.isNotEmpty(pmHouse.getRemark())){
            queryWrapper.eq(PmHouse::getRemark,pmHouse.getRemark());
        }

        //删除标志（0代表存在 2代表删除）
        if(StringUtils.isNotEmpty(pmHouse.getDelFlag())){
            queryWrapper.eq(PmHouse::getDelFlag,pmHouse.getDelFlag());
        }

        //创建部门
        if(null != (pmHouse.getCreateDept())){
            queryWrapper.eq(PmHouse::getCreateDept,pmHouse.getCreateDept());
        }

        //创建人
        if(null != (pmHouse.getCreateBy())){
            queryWrapper.eq(PmHouse::getCreateBy,pmHouse.getCreateBy());
        }

        //创建时间
        if(null != (pmHouse.getCreateTime())){
            queryWrapper.eq(PmHouse::getCreateTime,pmHouse.getCreateTime());
        }

        //修改人
        if(null != (pmHouse.getUpdateBy())){
            queryWrapper.eq(PmHouse::getUpdateBy,pmHouse.getUpdateBy());
        }

        //修改时间
        if(null != (pmHouse.getUpdateTime())){
            queryWrapper.eq(PmHouse::getUpdateTime,pmHouse.getUpdateTime());
        }

        //租户ID
        if(StringUtils.isNotEmpty(pmHouse.getTenantId())){
            queryWrapper.eq(PmHouse::getTenantId,pmHouse.getTenantId());
        }

        }

}
