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

/**
 * 业主信息Controller
 *
 * @author bs
 * @date 2025-10-04
 */
@Api(tags = "业主信息")
@RestController
@RequestMapping("/owner/owner")
public class PmOwnerController extends BaseController {
    @Resource
    private IPmOwnerService pmOwnerService;

    /**
     * 分页查询业主信息列表
     */
    @ApiOperation("分页查询业主信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmOwner pmOwner) {
        startPage();
        LambdaQueryWrapper<PmOwner> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmOwner);
        List<PmOwner> list = pmOwnerService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询业主信息列表
     */
    @ApiOperation("查询业主信息列表")
    @GetMapping("/list")
    public AjaxResult list(PmOwner pmOwner) {
        LambdaQueryWrapper<PmOwner> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmOwner);
        List<PmOwner> list = pmOwnerService.list(queryWrapper);
        return success(list);
    }

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

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

    /**
     * 新增业主信息
     */
    @ApiOperation("新增业主信息")
    @Log(title = "业主信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmOwner pmOwner) {
        return toAjax(pmOwnerService.save(pmOwner));
    }

    /**
     * 修改业主信息
     */
    @ApiOperation("修改业主信息")
    @Log(title = "业主信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmOwner pmOwner) {
        return toAjax(pmOwnerService.updateById(pmOwner));
    }

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

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


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

        //业主姓名
        if(StringUtils.isNotEmpty(pmOwner.getOwnerName())){
            queryWrapper.eq(PmOwner::getOwnerName,pmOwner.getOwnerName());
        }

        //业主类型
        if(StringUtils.isNotEmpty(pmOwner.getOwnerType())){
            queryWrapper.eq(PmOwner::getOwnerType,pmOwner.getOwnerType());
        }

        //业主身份证
        if(StringUtils.isNotEmpty(pmOwner.getOwnerIdcard())){
            queryWrapper.eq(PmOwner::getOwnerIdcard,pmOwner.getOwnerIdcard());
        }

        //联系电话
        if(StringUtils.isNotEmpty(pmOwner.getPhone())){
            queryWrapper.eq(PmOwner::getPhone,pmOwner.getPhone());
        }

        //电子邮箱
        if(StringUtils.isNotEmpty(pmOwner.getEmail())){
            queryWrapper.eq(PmOwner::getEmail,pmOwner.getEmail());
        }

        //紧急联系人
        if(StringUtils.isNotEmpty(pmOwner.getEmergencyContact())){
            queryWrapper.eq(PmOwner::getEmergencyContact,pmOwner.getEmergencyContact());
        }

        //紧急联系电话
        if(StringUtils.isNotEmpty(pmOwner.getEmergencyPhone())){
            queryWrapper.eq(PmOwner::getEmergencyPhone,pmOwner.getEmergencyPhone());
        }

        //业主状态
        if(StringUtils.isNotEmpty(pmOwner.getStatus())){
            queryWrapper.eq(PmOwner::getStatus,pmOwner.getStatus());
        }

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

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

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

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

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

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

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

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

        }

}
