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

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.dataprocesstask.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataprocesstask.DataProcessTaskDO;
import cn.iocoder.yudao.module.llm.service.dataprocesstask.DataProcessTaskService;

@Tag(name = "管理后台 - 数据处理任务")
@RestController
@RequestMapping("/llm/data-process-task")
@Validated
public class DataProcessTaskController {

    @Resource
    private DataProcessTaskService dataProcessTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建数据处理任务")
    @PreAuthorize("@ss.hasPermission('llm:data-process-task:create')")
    public CommonResult<Long> createDataProcessTask(@Valid @RequestBody DataProcessTaskSaveReqVO createReqVO) {
        return success(dataProcessTaskService.createDataProcessTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据处理任务")
    @PreAuthorize("@ss.hasPermission('llm:data-process-task:update')")
    public CommonResult<Boolean> updateDataProcessTask(@Valid @RequestBody DataProcessTaskSaveReqVO updateReqVO) {
        dataProcessTaskService.updateDataProcessTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据处理任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:data-process-task:delete')")
    public CommonResult<Boolean> deleteDataProcessTask(@RequestParam("id") Long id) {
        dataProcessTaskService.deleteDataProcessTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据处理任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:data-process-task:query')")
    public CommonResult<DataProcessTaskRespVO> getDataProcessTask(@RequestParam("id") Long id) {
        DataProcessTaskDO dataProcessTask = dataProcessTaskService.getDataProcessTask(id);
        return success(BeanUtils.toBean(dataProcessTask, DataProcessTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据处理任务分页")
    @PreAuthorize("@ss.hasPermission('llm:data-process-task:query')")
    public CommonResult<PageResult<DataProcessTaskRespVO>> getDataProcessTaskPage(@Valid DataProcessTaskPageReqVO pageReqVO) {
        PageResult<DataProcessTaskDO> pageResult = dataProcessTaskService.getDataProcessTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataProcessTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据处理任务 Excel")
    @PreAuthorize("@ss.hasPermission('llm:data-process-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataProcessTaskExcel(@Valid DataProcessTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataProcessTaskDO> list = dataProcessTaskService.getDataProcessTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据处理任务.xls", "数据", DataProcessTaskRespVO.class,
                        BeanUtils.toBean(list, DataProcessTaskRespVO.class));
    }

}