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

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.datarefluxconfig.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.datarefluxconfig.DataRefluxConfigDO;
import cn.iocoder.yudao.module.llm.service.datarefluxconfig.DataRefluxConfigService;

@Tag(name = "管理后台 - 数据回流配置")
@RestController
@RequestMapping("/llm/data-reflux-config")
@Validated
public class DataRefluxConfigController {

    @Resource
    private DataRefluxConfigService dataRefluxConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建数据回流配置")
    @PreAuthorize("@ss.hasPermission('llm:data-reflux-config:create')")
    public CommonResult<Long> createDataRefluxConfig(@Valid @RequestBody DataRefluxConfigSaveReqVO createReqVO) {
        return success(dataRefluxConfigService.createDataRefluxConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据回流配置")
    @PreAuthorize("@ss.hasPermission('llm:data-reflux-config:update')")
    public CommonResult<Boolean> updateDataRefluxConfig(@Valid @RequestBody DataRefluxConfigSaveReqVO updateReqVO) {
        dataRefluxConfigService.updateDataRefluxConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据回流配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:data-reflux-config:delete')")
    public CommonResult<Boolean> deleteDataRefluxConfig(@RequestParam("id") Long id) {
        dataRefluxConfigService.deleteDataRefluxConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据回流配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:data-reflux-config:query')")
    public CommonResult<DataRefluxConfigRespVO> getDataRefluxConfig(@RequestParam("id") Long id) {
        DataRefluxConfigDO dataRefluxConfig = dataRefluxConfigService.getDataRefluxConfig(id);
        return success(BeanUtils.toBean(dataRefluxConfig, DataRefluxConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据回流配置分页")
    @PreAuthorize("@ss.hasPermission('llm:data-reflux-config:query')")
    public CommonResult<PageResult<DataRefluxConfigRespVO>> getDataRefluxConfigPage(@Valid DataRefluxConfigPageReqVO pageReqVO) {
        PageResult<DataRefluxConfigDO> pageResult = dataRefluxConfigService.getDataRefluxConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataRefluxConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据回流配置 Excel")
    @PreAuthorize("@ss.hasPermission('llm:data-reflux-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataRefluxConfigExcel(@Valid DataRefluxConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataRefluxConfigDO> list = dataRefluxConfigService.getDataRefluxConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据回流配置.xls", "数据", DataRefluxConfigRespVO.class,
                        BeanUtils.toBean(list, DataRefluxConfigRespVO.class));
    }

}