package com.ruoyi.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysTenantPackage;
import com.ruoyi.system.service.ISysTenantPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 租户套餐Controller
 *
 * @author fengcheng
 * @date 2025-01-04
 */
@RestController
@RequestMapping("/system/tenantPackage")
public class SysTenantPackageController extends BaseController {
    @Autowired
    private ISysTenantPackageService sysTenantPackageService;

    /**
     * 查询租户套餐列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('system:tenantPackage:list')")
    @GetMapping("/listSysTenantPackageVo")
    public AjaxResult listSysTenantPackageVo() {
        List<SysTenantPackage> list = sysTenantPackageService.selectSysTenantPackageListVo();
        return success(list);
    }

    /**
     * 查询租户套餐列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenantPackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTenantPackage sysTenantPackage) {
        startPage();
        List<SysTenantPackage> list = sysTenantPackageService.selectSysTenantPackageList(sysTenantPackage);
        return getDataTable(list);
    }

    /**
     * 导出租户套餐列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenantPackage:export')")
    @Log(title = "租户套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTenantPackage sysTenantPackage) {
        List<SysTenantPackage> list = sysTenantPackageService.selectSysTenantPackageList(sysTenantPackage);
        ExcelUtil<SysTenantPackage> util = new ExcelUtil<SysTenantPackage>(SysTenantPackage.class);
        util.exportExcel(response, list, "租户套餐数据");
    }

    /**
     * 获取租户套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenantPackage:query')")
    @GetMapping(value = "/{tenantPackageId}")
    public AjaxResult getInfo(@PathVariable("tenantPackageId") Long tenantPackageId) {
        return success(sysTenantPackageService.selectSysTenantPackageByTenantPackageId(tenantPackageId));
    }

    /**
     * 新增租户套餐
     */
    @PreAuthorize("@ss.hasPermi('system:tenantPackage:add')")
    @Log(title = "租户套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTenantPackage sysTenantPackage) {
        if (ObjectUtil.isNotNull(sysTenantPackageService.selectSysTenantPackageByName(sysTenantPackage.getName()))) {
            return error("新增租户套餐'" + sysTenantPackage.getName() + "'失败，租户套餐名称已存在");
        }
        sysTenantPackage.setCreator(getUsername());
        return toAjax(sysTenantPackageService.insertSysTenantPackage(sysTenantPackage));
    }

    /**
     * 修改租户套餐
     */
    @PreAuthorize("@ss.hasPermi('system:tenantPackage:edit')")
    @Log(title = "租户套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTenantPackage sysTenantPackage) {
        sysTenantPackage.setUpdater(getUsername());
        return toAjax(sysTenantPackageService.updateSysTenantPackage(sysTenantPackage));
    }

    /**
     * 删除租户套餐
     */
    @PreAuthorize("@ss.hasPermi('system:tenantPackage:remove')")
    @Log(title = "租户套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tenantPackageIds}")
    public AjaxResult remove(@PathVariable Long[] tenantPackageIds) {
        return toAjax(sysTenantPackageService.deleteSysTenantPackageByTenantPackageIds(tenantPackageIds));
    }
}
