package cn.iocoder.byzs.module.ai.controller.admin.virtualdevice;

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 cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;

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

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

import cn.iocoder.byzs.module.ai.controller.admin.virtualdevice.vo.*;
import cn.iocoder.byzs.module.ai.dal.dataobject.virtualdevice.VirtualDeviceDO;
import cn.iocoder.byzs.module.ai.service.virtualdevice.VirtualDeviceService;

@Tag(name = "管理后台 - AI-虚拟实验室")
@RestController
@RequestMapping("/ai/virtual-device")
@Validated
public class VirtualDeviceController {

    @Resource
    private VirtualDeviceService virtualDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建AI-虚拟实验室")
    @PreAuthorize("@ss.hasPermission('ai:virtual-device:create')")
    public CommonResult<Integer> createVirtualDevice(@Valid @RequestBody VirtualDeviceSaveReqVO createReqVO) {
        return success(virtualDeviceService.createVirtualDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI-虚拟实验室")
    @PreAuthorize("@ss.hasPermission('ai:virtual-device:update')")
    public CommonResult<Boolean> updateVirtualDevice(@Valid @RequestBody VirtualDeviceSaveReqVO updateReqVO) {
        virtualDeviceService.updateVirtualDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI-虚拟实验室")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:virtual-device:delete')")
    public CommonResult<Boolean> deleteVirtualDevice(@RequestParam("id") Integer id) {
        virtualDeviceService.deleteVirtualDevice(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI-虚拟实验室")
                @PreAuthorize("@ss.hasPermission('ai:virtual-device:delete')")
    public CommonResult<Boolean> deleteVirtualDeviceList(@RequestParam("ids") List<Integer> ids) {
        virtualDeviceService.deleteVirtualDeviceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI-虚拟实验室")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:virtual-device:query')")
    public CommonResult<VirtualDeviceRespVO> getVirtualDevice(@RequestParam("id") Integer id) {
        VirtualDeviceDO virtualDevice = virtualDeviceService.getVirtualDevice(id);
        return success(BeanUtils.toBean(virtualDevice, VirtualDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI-虚拟实验室分页")
    @PreAuthorize("@ss.hasPermission('ai:virtual-device:query')")
    public CommonResult<PageResult<VirtualDeviceRespVO>> getVirtualDevicePage(@Valid VirtualDevicePageReqVO pageReqVO) {
        PageResult<VirtualDeviceDO> pageResult = virtualDeviceService.getVirtualDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VirtualDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI-虚拟实验室 Excel")
    @PreAuthorize("@ss.hasPermission('ai:virtual-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVirtualDeviceExcel(@Valid VirtualDevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VirtualDeviceDO> list = virtualDeviceService.getVirtualDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI-虚拟实验室.xls", "数据", VirtualDeviceRespVO.class,
                        BeanUtils.toBean(list, VirtualDeviceRespVO.class));
    }

}