package com.dsk.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.util.ObjectUtil;
import com.dsk.common.annotation.Log;
import com.dsk.common.annotation.RepeatSubmit;
import com.dsk.common.constant.TenantConstants;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.core.validate.AddGroup;
import com.dsk.common.core.validate.EditGroup;
import com.dsk.common.enums.BusinessType;
import com.dsk.system.domain.SysTenantPackage;
import com.dsk.system.domain.bo.SysTenantPackageBo;
import com.dsk.system.domain.vo.SysTenantPackageSelectVo;
import com.dsk.system.domain.vo.SysTenantPackageVo;
import com.dsk.system.service.ISysTenantPackageService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 租户套餐管理
 *
 * @author sxk
 * @date 2023.08.23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/tenant/package")
public class SysTenantPackageController extends BaseController {

    @Resource
    private final ISysTenantPackageService tenantPackageService;

    /**
     * 查询租户套餐列表
     */
    @SaCheckRole(value = {TenantConstants.SUPER_ADMIN_ROLE_KEY, TenantConstants.DSK_TENANT_ADMIN_ROLE_KEY}, mode = SaMode.OR)
    @SaCheckPermission(value = "system:tenantPackage:list", orRole = "accountAdmin")
    @GetMapping("/list")
    public TableDataInfo<SysTenantPackageVo> list(SysTenantPackageBo bo, PageQuery pageQuery) {
        return tenantPackageService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询租户套餐下拉选列表
     */
    @SaCheckRole(value = {TenantConstants.SUPER_ADMIN_ROLE_KEY, TenantConstants.DSK_TENANT_ADMIN_ROLE_KEY}, mode = SaMode.OR)
    @SaCheckPermission(value = "system:tenantPackage:list", orRole = "accountAdmin")
    @GetMapping("/selectList")
    public R<List<SysTenantPackageSelectVo>> selectList() {
        return R.ok(tenantPackageService.selectList());
    }

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

    /**
     * 获取租户套餐详细信息
     *
     * @param packageId 主键
     */
    @SaCheckRole(value = {TenantConstants.SUPER_ADMIN_ROLE_KEY, TenantConstants.DSK_TENANT_ADMIN_ROLE_KEY}, mode = SaMode.OR)
    @SaCheckPermission(value = "system:tenantPackage:query", orRole = "accountAdmin")
    @GetMapping("/{packageId}")
    public R<SysTenantPackageVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long packageId) {
        return R.ok(tenantPackageService.queryById(packageId));
    }

    /**
     * 新增租户套餐
     */
    @SaCheckRole(value = {TenantConstants.SUPER_ADMIN_ROLE_KEY, TenantConstants.DSK_TENANT_ADMIN_ROLE_KEY}, mode = SaMode.OR)
    @SaCheckPermission(value = "system:tenantPackage:add", orRole = "accountAdmin")
    @Log(title = "租户套餐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTenantPackageBo bo) {
        if (tenantPackageService.checkDeptNameUnique(bo)) {
            return R.fail("新增方案'" + bo.getPackageName() + "'失败，方案名称已存在");
        }
        return toAjax(tenantPackageService.insertByBo(bo));
    }

    /**
     * 修改租户套餐
     */
    @SaCheckRole(value = {TenantConstants.SUPER_ADMIN_ROLE_KEY, TenantConstants.DSK_TENANT_ADMIN_ROLE_KEY}, mode = SaMode.OR)
    @SaCheckPermission(value = "system:tenantPackage:edit", orRole = "accountAdmin")
    @Log(title = "租户套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTenantPackageBo bo) {
        SysTenantPackage tenantPackage = tenantPackageService.selectTenantPackageByName(bo.getPackageName());
        if (ObjectUtil.isNotNull(tenantPackage) && !tenantPackage.getPackageId().equals(bo.getPackageId())) {
            return R.fail("修改方案'" + bo.getPackageName() + "'失败，方案名称已存在");
        }
        return toAjax(tenantPackageService.updateByBo(bo));
    }

//    /**
//     * 状态修改
//     */
//    @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
//    @SaCheckPermission("system:tenantPackage:edit")
//    @Log(title = "租户套餐", businessType = BusinessType.UPDATE)
//    @PutMapping("/changeStatus")
//    public R<Void> changeStatus(@RequestBody SysTenantPackageBo bo) {
//        return toAjax(tenantPackageService.updatePackageStatus(bo));
//    }

    /**
     * 删除租户套餐
     *
     * @param packageIds 主键串
     */
    @SaCheckRole(value = {TenantConstants.SUPER_ADMIN_ROLE_KEY, TenantConstants.DSK_TENANT_ADMIN_ROLE_KEY}, mode = SaMode.OR)
    @SaCheckPermission(value = "system:tenantPackage:remove", orRole = "accountAdmin")
    @Log(title = "租户套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{packageIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] packageIds) {
        return toAjax(tenantPackageService.deleteWithValidByIds(Arrays.asList(packageIds), true));
    }
}
