package org.eco.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import org.eco.system.domain.bo.SysTenantPackageBo;
import org.eco.system.domain.vo.SysTenantPackageVo;
import org.eco.common.core.constant.TenantConstants;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.web.core.BaseController;
import org.eco.system.service.ISysTenantPackageService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 租户套餐管理
 *
 * @author wgk
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/tenant/package")
public class SysTenantPackageController extends BaseController {
    @Resource
    private ISysTenantPackageService tenantPackageService;

    /**
     * 查询租户套餐列表
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysTenantPackageVo>> list(SysTenantPackageBo sysTenantPackageBo) {
        return CommonResult.success(tenantPackageService.selectPage(sysTenantPackageBo));
    }

    /**
     * 查询租户套餐下拉选列表
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:list")
    @GetMapping("/selectList")
    public CommonResult<List<SysTenantPackageVo>> selectList() {
        return CommonResult.success(tenantPackageService.selectList());
    }

    /**
     * 导出租户套餐列表
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:export")
    @Log(title = "租户套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysTenantPackageBo sysTenantPackageBo, HttpServletResponse response) {
        List<SysTenantPackageVo> list = tenantPackageService.queryList(sysTenantPackageBo);
        ExcelUtil.exportExcel(list, "租户套餐", SysTenantPackageVo.class, response);
    }

    /**
     * 获取租户套餐详细信息
     *
     * @param packageId 主键
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:query")
    @GetMapping("/{packageId}")
    public CommonResult<SysTenantPackageVo> getInfo(@NotNull(message = "主键不能为空")
                                                    @PathVariable Long packageId) {
        return CommonResult.success(tenantPackageService.selectById(packageId));
    }

    /**
     * 新增租户套餐
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:add")
    @Log(title = "租户套餐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public CommonResult<Void> add(@Validated @RequestBody SysTenantPackageBo sysTenantPackageBo) {
        boolean inserted = tenantPackageService.insert(sysTenantPackageBo);
        if (!inserted) {
            return CommonResult.fail("新增租户套餐记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改租户套餐
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:edit")
    @Log(title = "租户套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public CommonResult<Void> edit(@Validated @RequestBody SysTenantPackageBo sysTenantPackageBo) {
        boolean updated = tenantPackageService.update(sysTenantPackageBo);
        if (!updated) {
            return CommonResult.fail("修改租户套餐记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 状态修改
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:edit")
    @Log(title = "租户套餐", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public CommonResult<Void> changeStatus(@RequestBody SysTenantPackageBo sysTenantPackageBo) {
        boolean updated = tenantPackageService.updatePackageStatus(sysTenantPackageBo);
        if (!updated) {
            return CommonResult.fail("修改租户套餐记录状态失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除租户套餐
     *
     * @param packageIds 主键串
     */
    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
    @SaCheckPermission("system:tenantPackage:remove")
    @Log(title = "租户套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{packageIds}")
    public CommonResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                     @PathVariable Long[] packageIds) {
        boolean deleted = tenantPackageService.deleteByIds(packageIds, true);
        if (!deleted) {
            return CommonResult.fail("删除租户套餐记录失败!");
        }
        return CommonResult.success();
    }
}
