package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LoginSysTenant;
import com.ruoyi.system.domain.SysTenant;
import com.ruoyi.system.domain.vo.SysTenantVo;
import com.ruoyi.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户管理Controller
 *
 * @author fengcheng
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController {
    @Autowired
    private ISysTenantService sysTenantService;


    /**
     * 查询登录租户管理列表（不分页）
     */
    @GetMapping("/loginSysTenantList")
    public AjaxResult loginSysTenantList() {
        List<LoginSysTenant> list = sysTenantService.loginSysTenantList();
        return success(list);
    }


    /**
     * 查询租户管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTenant sysTenant) {
        startPage();
        List<SysTenant> list = sysTenantService.selectSysTenantList(sysTenant);
        return getDataTable(list);
    }

    /**
     * 获取租户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:query')")
    @GetMapping(value = "/{tenantId}")
    public AjaxResult getInfo(@PathVariable("tenantId") String tenantId) {
        return success(sysTenantService.selectSysTenantByTenantId(tenantId));
    }

    /**
     * 新增租户管理
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @Log(title = "租户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTenantVo sysTenantVo) {
        return toAjax(sysTenantService.insertSysTenant(sysTenantVo));
    }

    /**
     * 修改租户管理
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:edit')")
    @Log(title = "租户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTenantVo sysTenantVo) {
        return toAjax(sysTenantService.updateSysTenant(sysTenantVo));
    }

    /**
     * 删除租户管理
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:remove')")
    @Log(title = "租户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tenantIds}")
    public AjaxResult remove(@PathVariable String[] tenantIds) {
        return toAjax(sysTenantService.deleteSysTenantByTenantIds(tenantIds));
    }

    /**
     * 重置租户密码
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:resetPassword')")
    @Log(title = "租户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetTenantPwd")
    public AjaxResult resetTenantPwd(@RequestBody SysTenantVo sysTenantVo) {
        return toAjax(sysTenantService.resetTenantPwd(sysTenantVo.getTenantId(), sysTenantVo.getPassword()));
    }

}
