package com.bs.pm.controller;

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

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.PmTenantHouseRel;
import com.bs.pm.service.IPmTenantHouseRelService;
import javax.annotation.Resource;

/**
 * 租赁房屋信息Controller
 *
 * @author bs
 * @date 2025-10-05
 */
@Api(tags = "租赁房屋信息")
@RestController
@RequestMapping("/tenant/rel")
public class PmTenantHouseRelController extends BaseController {
    @Resource
    private IPmTenantHouseRelService pmTenantHouseRelService;

    /**
     * 分页查询租赁房屋信息列表
     */
    @ApiOperation("分页查询租赁房屋信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmTenantHouseRel pmTenantHouseRel) {
        startPage();
        LambdaQueryWrapper<PmTenantHouseRel> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmTenantHouseRel);
        List<PmTenantHouseRel> list = pmTenantHouseRelService.list(queryWrapper);
        return getDataTable(list);
    }

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

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

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

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

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

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

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


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

        //租赁信息id
        if(null != (pmTenantHouseRel.getTenantHouseId())){
            queryWrapper.eq(PmTenantHouseRel::getTenantHouseId,pmTenantHouseRel.getTenantHouseId());
        }

        //业主id
        if(null != (pmTenantHouseRel.getOwnerId())){
            queryWrapper.eq(PmTenantHouseRel::getOwnerId,pmTenantHouseRel.getOwnerId());
        }

        //房屋id
        if(null != (pmTenantHouseRel.getHouseId())){
            queryWrapper.eq(PmTenantHouseRel::getHouseId,pmTenantHouseRel.getHouseId());
        }

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

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

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

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

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

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

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

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

        }

}
