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

/**
 * 租户信息Controller
 *
 * @author bs
 * @date 2025-10-05
 */
@Api(tags = "租户信息")
@RestController
@RequestMapping("/pm/tenant")
public class PmTenantController extends BaseController {
    @Resource
    private IPmTenantService pmTenantService;

    /**
     * 分页查询租户信息列表
     */
    @ApiOperation("分页查询租户信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmTenant pmTenant) {
        startPage();
        LambdaQueryWrapper<PmTenant> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmTenant);
        List<PmTenant> list = pmTenantService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询租户信息列表
     */
    @ApiOperation("查询租户信息列表")
    @GetMapping("/list")
    public AjaxResult list(PmTenant pmTenant) {
        LambdaQueryWrapper<PmTenant> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmTenant);
        List<PmTenant> list = pmTenantService.list(queryWrapper);
        return success(list);
    }

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

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

    /**
     * 新增租户信息
     */
    @ApiOperation("新增租户信息")
    @Log(title = "租户信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmTenant pmTenant) {
        return toAjax(pmTenantService.save(pmTenant));
    }

    /**
     * 修改租户信息
     */
    @ApiOperation("修改租户信息")
    @Log(title = "租户信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmTenant pmTenant) {
        return toAjax(pmTenantService.updateById(pmTenant));
    }

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

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


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

        //租户姓名
        if(StringUtils.isNotEmpty(pmTenant.getTenantName())){
            queryWrapper.eq(PmTenant::getTenantName,pmTenant.getTenantName());
        }

        //租户类型(个人、企业)
        if(StringUtils.isNotEmpty(pmTenant.getTenantType())){
            queryWrapper.eq(PmTenant::getTenantType,pmTenant.getTenantType());
        }

        //租户身份证
        if(StringUtils.isNotEmpty(pmTenant.getIdCard())){
            queryWrapper.eq(PmTenant::getIdCard,pmTenant.getIdCard());
        }

        //联系电话
        if(StringUtils.isNotEmpty(pmTenant.getPhome())){
            queryWrapper.eq(PmTenant::getPhome,pmTenant.getPhome());
        }

        //工作单位
        if(StringUtils.isNotEmpty(pmTenant.getCompany())){
            queryWrapper.eq(PmTenant::getCompany,pmTenant.getCompany());
        }

        //状态
        if(StringUtils.isNotEmpty(pmTenant.getStatus())){
            queryWrapper.eq(PmTenant::getStatus,pmTenant.getStatus());
        }

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

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

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

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

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

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

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

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

        }

}
