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

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 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.constraints.*;
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.service.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.service.ServicePackageDO;
import cn.iocoder.yudao.module.system.service.service.ServicePackageService;

@Tag(name = "管理后台 - 服务套餐")
@RestController
@RequestMapping("/system/service-package")
@Validated
public class ServicePackageController {

    @Resource
    private ServicePackageService servicePackageService;

    @PostMapping("/create")
    @Operation(summary = "创建服务套餐")
    @PreAuthorize("@ss.hasPermission('system:service-package:create')")
    public CommonResult<String> createServicePackage(@Valid @RequestBody ServicePackageSaveReqVO createReqVO) {
        return success(servicePackageService.createServicePackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务套餐")
    @PreAuthorize("@ss.hasPermission('system:service-package:update')")
    public CommonResult<Boolean> updateServicePackage(@Valid @RequestBody ServicePackageSaveReqVO updateReqVO) {
        servicePackageService.updateServicePackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:service-package:delete')")
    public CommonResult<Boolean> deleteServicePackage(@RequestParam("id") String id) {
        servicePackageService.deleteServicePackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:service-package:query')")
    public CommonResult<ServicePackageRespVO> getServicePackage(@RequestParam("id") String id) {
        ServicePackageDO servicePackage = servicePackageService.getServicePackage(id);
        return success(BeanUtils.toBean(servicePackage, ServicePackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务套餐分页")
    @PreAuthorize("@ss.hasPermission('system:service-package:query')")
    public CommonResult<PageResult<ServicePackageRespVO>> getServicePackagePage(@Valid ServicePackagePageReqVO pageReqVO) {
        PageResult<ServicePackageDO> pageResult = servicePackageService.getServicePackagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServicePackageRespVO.class));
    }

    @GetMapping({"/get-simple-list", "simple-list"})
    @Operation(summary = "获取服务套餐精简信息列表", description = "只包含被开启的服务套餐，主要用于前端的下拉选项")
    public CommonResult<List<ServicePackageSimpleRespVO>> getTenantPackageList() {
        List<ServicePackageDO> list = servicePackageService.getServicePackageListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(BeanUtils.toBean(list, ServicePackageSimpleRespVO.class));
    }

}