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

/**
 * 业主房屋关联Controller
 *
 * @author bs
 * @date 2025-10-04
 */
@Api(tags = "业主房屋关联")
@RestController
@RequestMapping("/owner/rel")
public class PmOwnerHouseRelController extends BaseController {
    @Resource
    private IPmOwnerHouseRelService pmOwnerHouseRelService;

    /**
     * 分页查询业主房屋关联列表
     */
    @ApiOperation("分页查询业主房屋关联列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmOwnerHouseRel pmOwnerHouseRel) {
        startPage();
        LambdaQueryWrapper<PmOwnerHouseRel> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmOwnerHouseRel);
        List<PmOwnerHouseRel> list = pmOwnerHouseRelService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询业主房屋关联列表
     */
    @ApiOperation("查询业主房屋关联列表")
    @GetMapping("/list")
    public AjaxResult list(PmOwnerHouseRel pmOwnerHouseRel) {
        LambdaQueryWrapper<PmOwnerHouseRel> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmOwnerHouseRel);
        List<PmOwnerHouseRel> list = pmOwnerHouseRelService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出业主房屋关联列表
     */
    @ApiOperation("导出业主房屋关联列表")
    @Log(title = "业主房屋关联导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmOwnerHouseRel pmOwnerHouseRel) {
        LambdaQueryWrapper<PmOwnerHouseRel> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmOwnerHouseRel);
        List<PmOwnerHouseRel> list = pmOwnerHouseRelService.list(queryWrapper);
        ExcelUtil<PmOwnerHouseRel> util = new ExcelUtil<PmOwnerHouseRel>(PmOwnerHouseRel. class);
        util.exportExcel(response, list, "业主房屋关联数据");
    }

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

    /**
     * 新增业主房屋关联
     */
    @ApiOperation("新增业主房屋关联")
    @Log(title = "业主房屋关联新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmOwnerHouseRel pmOwnerHouseRel) {
        return toAjax(pmOwnerHouseRelService.save(pmOwnerHouseRel));
    }

    /**
     * 修改业主房屋关联
     */
    @ApiOperation("修改业主房屋关联")
    @Log(title = "业主房屋关联修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmOwnerHouseRel pmOwnerHouseRel) {
        return toAjax(pmOwnerHouseRelService.updateById(pmOwnerHouseRel));
    }

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

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


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

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

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

        //关联类型
        if(StringUtils.isNotEmpty(pmOwnerHouseRel.getRelType())){
            queryWrapper.eq(PmOwnerHouseRel::getRelType,pmOwnerHouseRel.getRelType());
        }

        //绑定时间
        if(null != (pmOwnerHouseRel.getBindTime())){
            queryWrapper.eq(PmOwnerHouseRel::getBindTime,pmOwnerHouseRel.getBindTime());
        }

        //解绑时间
        if(null != (pmOwnerHouseRel.getUnbindTime())){
            queryWrapper.eq(PmOwnerHouseRel::getUnbindTime,pmOwnerHouseRel.getUnbindTime());
        }

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

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

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

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

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

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

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

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

        }

}
