package com.qiaoba.module.tenant.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qiaoba.api.job.annotation.Log;
import com.qiaoba.api.job.enums.BusinessType;
import com.qiaoba.api.tenant.entity.SysTenant;
import com.qiaoba.api.tenant.entity.dto.SysTenantDto;
import com.qiaoba.api.tenant.entity.param.SysTenantParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.module.tenant.entity.dto.TenantSettingDto;
import com.qiaoba.module.tenant.service.SysTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 租户管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/30 10:52
 */
@RestController
@RequestMapping("/tenant")
@RequiredArgsConstructor
@Tag(name = "租户管理")
public class SysTenantController {

    private final SysTenantService sysTenantService;

    @PreAuthorize("hasAuthority('tenant:add')")
    @PostMapping
    @Operation(summary = "新增租户")
    @Log(title = "新增租户", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody SysTenantDto dto) {
        return AjaxResult.toAjax(sysTenantService.insert(dto));
    }

    @PreAuthorize("hasAuthority('tenant:query')")
    @GetMapping(value = "/{tenantId}")
    @Operation(summary = "获取详情")
    public AjaxResult getInfo(@PathVariable String tenantId) {
        return AjaxResult.success(sysTenantService.selectById(tenantId));
    }

    @PreAuthorize("hasAuthority('tenant:edit')")
    @PutMapping
    @Operation(summary = "修改租户")
    @Log(title = "修改租户", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody SysTenantDto dto) {
        return AjaxResult.toAjax(sysTenantService.update(dto));
    }

    @PreAuthorize("hasAuthority('tenant:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public TableDataInfo<SysTenant> list(SysTenantParam param, PageQuery pageQuery) {
        return sysTenantService.selectPageList(param, pageQuery);
    }

    @GetMapping("/normal-list")
    @Operation(summary = "获取正常列表[开放接口]")
    public TableDataInfo<SysTenant> normalPageList(@RequestParam(required = false) String companyName,PageQuery pageQuery) {
        return sysTenantService.selectPageList(SysTenantParam.buildNormalSelectParam(companyName), pageQuery);
    }

    @PreAuthorize("hasAuthority('tenant:edit')")
    @PutMapping("/update-mode")
    @Operation(summary = "修改模式")
    public AjaxResult updateMode(TenantSettingDto dto) {
        return AjaxResult.toAjax(sysTenantService.updateMode(dto));
    }

    @PreAuthorize("hasAuthority('tenant:remove')")
    @DeleteMapping("/refreshCache")
    @Operation(summary = "刷新缓存")
    public AjaxResult refreshCache() {
        sysTenantService.resetCache();
        return AjaxResult.success();
    }

}
