package org.agt.module.license.controller.admin.license;

import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import org.agt.framework.common.pojo.PageParam;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.util.object.BeanUtils;
import static org.agt.framework.common.pojo.CommonResult.success;

import org.agt.framework.excel.core.util.ExcelUtils;

import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import static org.agt.framework.apilog.core.enums.OperateTypeEnum.*;

import org.agt.module.license.controller.admin.license.vo.*;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.module.license.service.license.LicenseService;

@Tag(name = "管理后台 - License")
@RestController
@RequestMapping("/license/license")
@Validated
public class LicenseController {

    @Resource
    private LicenseService licenseService;

    @PostMapping("/create")
    @Operation(summary = "创建License")
    @PreAuthorize("@ss.hasPermission('license:license:create')")
    public CommonResult<Long> createLicense(@Valid @RequestBody LicenseSaveReqVO createReqVO) {
        return success(licenseService.createLicense(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新License")
    @PreAuthorize("@ss.hasPermission('license:license:update')")
    public CommonResult<Boolean> updateLicense(@Valid @RequestBody LicenseSaveReqVO updateReqVO) {
        licenseService.updateLicense(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除License")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('license:license:delete')")
    public CommonResult<Boolean> deleteLicense(@RequestParam("id") Long id) {
        licenseService.deleteLicense(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得License")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('license:license:query')")
    public CommonResult<LicenseRespVO> getLicense(@RequestParam("id") Long id) {
        LicenseDO license = licenseService.getLicense(id);
        return success(BeanUtils.toBean(license, LicenseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得License分页")
    @PreAuthorize("@ss.hasPermission('license:license:query')")
    public CommonResult<PageResult<LicenseRespVO>> getLicensePage(@Valid LicensePageReqVO pageReqVO) {
        PageResult<LicenseDO> pageResult = licenseService.getLicensePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LicenseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出License Excel")
    @PreAuthorize("@ss.hasPermission('license:license:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLicenseExcel(@Valid LicensePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LicenseDO> list = licenseService.getLicensePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "License.xls", "数据", LicenseRespVO.class,
                        BeanUtils.toBean(list, LicenseRespVO.class));
    }

}