package cn.iocoder.yudao.module.system.controller.admin.tenantagencypackage;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageSimpleRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantagencypackage.TenantAgencyPackageDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.tenantagencypackage.vo.*;
import cn.iocoder.yudao.module.system.service.tenantagencypackage.TenantAgencyPackageService;

@Tag(name = "管理后台 - 代理租户套餐")
@RestController
@RequestMapping("/system/tenant-agency-package")
@Validated
public class TenantAgencyPackageController {

    @Resource
    private TenantAgencyPackageService tenantAgencyPackageService;

    @PostMapping("/create")
    @Operation(summary = "创建代理租户套餐")
    @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:create')")
    public CommonResult<Long> createTenantAgencyPackage(@Valid @RequestBody TenantAgencyPackageSaveReqVO createReqVO) {
        return success(tenantAgencyPackageService.createTenantAgencyPackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理租户套餐")
    @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:update')")
    public CommonResult<Boolean> updateTenantAgencyPackage(@Valid @RequestBody TenantAgencyPackageSaveReqVO updateReqVO) {
        tenantAgencyPackageService.updateTenantAgencyPackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理租户套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:delete')")
    public CommonResult<Boolean> deleteTenantAgencyPackage(@RequestParam("id") Long id) {
        tenantAgencyPackageService.deleteTenantAgencyPackage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除代理租户套餐")
                @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:delete')")
    public CommonResult<Boolean> deleteTenantAgencyPackageList(@RequestParam("ids") List<Long> ids) {
        tenantAgencyPackageService.deleteTenantAgencyPackageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理租户套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:query')")
    public CommonResult<TenantAgencyPackageRespVO> getTenantAgencyPackage(@RequestParam("id") Long id) {
        TenantAgencyPackageDO tenantAgencyPackage = tenantAgencyPackageService.getTenantAgencyPackage(id);
        return success(BeanUtils.toBean(tenantAgencyPackage, TenantAgencyPackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理租户套餐分页")
    @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:query')")
    public CommonResult<PageResult<TenantAgencyPackageRespVO>> getTenantAgencyPackagePage(@Valid TenantAgencyPackagePageReqVO pageReqVO) {
        PageResult<TenantAgencyPackageDO> pageResult = tenantAgencyPackageService.getTenantAgencyPackagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TenantAgencyPackageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理租户套餐 Excel")
    @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantAgencyPackageExcel(@Valid TenantAgencyPackagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantAgencyPackageDO> list = tenantAgencyPackageService.getTenantAgencyPackagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "代理租户套餐.xls", "数据", TenantAgencyPackageRespVO.class,
                        BeanUtils.toBean(list, TenantAgencyPackageRespVO.class));
    }

    @GetMapping({"/get-simple-list", "代理套餐精简列表"})
    @Operation(summary = "获取代理租户套餐精简信息列表", description = "只包含被开启的租户套餐，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('system:tenant-agency-package:simple-list')")
    public CommonResult<List<AgencyTenantPackageSimpleRespVO>> getTenantPackageList() {
        List<TenantAgencyPackageDO> list = tenantAgencyPackageService.getTenantPackageListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(BeanUtils.toBean(list, AgencyTenantPackageSimpleRespVO.class));
    }

}