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

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.training.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.training.TrainingDO;
import cn.iocoder.yudao.module.llm.service.training.TrainingService;

@Tag(name = "管理后台 - 训练")
@RestController
@RequestMapping("/llm/training")
@Validated
public class TrainingController {

    @Resource
    private TrainingService trainingService;

    @PostMapping("/create")
    @Operation(summary = "创建训练")
//    @PreAuthorize("@ss.hasPermission('llm:training:create')")
    public CommonResult<Integer> createTraining(@Valid @RequestBody TrainingSaveReqVO createReqVO) {
        return success(trainingService.createTraining(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新训练")
//    @PreAuthorize("@ss.hasPermission('llm:training:update')")
    public CommonResult<Boolean> updateTraining(@Valid @RequestBody TrainingSaveReqVO updateReqVO) {
        trainingService.updateTraining(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除训练")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('llm:training:delete')")
    public CommonResult<Boolean> deleteTraining(@RequestParam("id") Integer id) {
        trainingService.deleteTraining(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得训练")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:training:query')")
    public CommonResult<TrainingRespVO> getTraining(@RequestParam("id") Integer id) {
        TrainingDO training = trainingService.getTraining(id);
        return success(BeanUtils.toBean(training, TrainingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得训练分页")
//    @PreAuthorize("@ss.hasPermission('llm:training:query')")
    public CommonResult<PageResult<TrainingRespVO>> getTrainingPage(@Valid TrainingPageReqVO pageReqVO) {
        PageResult<TrainingDO> pageResult = trainingService.getTrainingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TrainingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出训练 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:training:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTrainingExcel(@Valid TrainingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TrainingDO> list = trainingService.getTrainingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "训练.xls", "数据", TrainingRespVO.class,
                        BeanUtils.toBean(list, TrainingRespVO.class));
    }

}