package cn.iocoder.yudao.module.llm.controller.admin.prompttemplatesapplicationsbackup;

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.llm.controller.admin.prompttemplatesapplicationsbackup.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplatesapplicationsbackup.PromptTemplatesApplicationsBackupDO;
import cn.iocoder.yudao.module.llm.service.prompttemplatesapplicationsbackup.PromptTemplatesApplicationsBackupService;

@Tag(name = "管理后台 - Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系")
@RestController
@RequestMapping("/llm/prompt-templates-applications-backup")
@Validated
public class PromptTemplatesApplicationsBackupController {

    @Resource
    private PromptTemplatesApplicationsBackupService promptTemplatesApplicationsBackupService;

    @PostMapping("/create")
    @Operation(summary = "创建Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系")
//    @PreAuthorize("@ss.hasPermission('llm:prompt-templates-applications-backup:create')")
    public CommonResult<Long> createPromptTemplatesApplicationsBackup(@Valid @RequestBody PromptTemplatesApplicationsBackupSaveReqVO createReqVO) {
        return success(promptTemplatesApplicationsBackupService.createPromptTemplatesApplicationsBackup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系")
//    @PreAuthorize("@ss.hasPermission('llm:prompt-templates-applications-backup:update')")
    public CommonResult<Boolean> updatePromptTemplatesApplicationsBackup(@Valid @RequestBody PromptTemplatesApplicationsBackupSaveReqVO updateReqVO) {
        promptTemplatesApplicationsBackupService.updatePromptTemplatesApplicationsBackup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('llm:prompt-templates-applications-backup:delete')")
    public CommonResult<Boolean> deletePromptTemplatesApplicationsBackup(@RequestParam("id") Long id) {
        promptTemplatesApplicationsBackupService.deletePromptTemplatesApplicationsBackup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:prompt-templates-applications-backup:query')")
    public CommonResult<PromptTemplatesApplicationsBackupRespVO> getPromptTemplatesApplicationsBackup(@RequestParam("id") Long id) {
        PromptTemplatesApplicationsBackupDO promptTemplatesApplicationsBackup = promptTemplatesApplicationsBackupService.getPromptTemplatesApplicationsBackup(id);
        return success(BeanUtils.toBean(promptTemplatesApplicationsBackup, PromptTemplatesApplicationsBackupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系分页")
//    @PreAuthorize("@ss.hasPermission('llm:prompt-templates-applications-backup:query')")
    public CommonResult<PageResult<PromptTemplatesApplicationsBackupRespVO>> getPromptTemplatesApplicationsBackupPage(@Valid PromptTemplatesApplicationsBackupPageReqVO pageReqVO) {
        PageResult<PromptTemplatesApplicationsBackupDO> pageResult = promptTemplatesApplicationsBackupService.getPromptTemplatesApplicationsBackupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PromptTemplatesApplicationsBackupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:prompt-templates-applications-backup:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPromptTemplatesApplicationsBackupExcel(@Valid PromptTemplatesApplicationsBackupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PromptTemplatesApplicationsBackupDO> list = promptTemplatesApplicationsBackupService.getPromptTemplatesApplicationsBackupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Prompt 模板 应用关系表 备份表，记录模板和应用之间的多对多关系.xls", "数据", PromptTemplatesApplicationsBackupRespVO.class,
                        BeanUtils.toBean(list, PromptTemplatesApplicationsBackupRespVO.class));
    }

}
