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

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.finetuningloss.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.finetuningloss.FineTuningLossDO;
import cn.iocoder.yudao.module.llm.service.finetuningloss.FineTuningLossService;

@Tag(name = "管理后台 - 损失记录表，用于记录每次训练的损失值")
@RestController
@RequestMapping("/llm/fine-tuning-loss")
@Validated
public class FineTuningLossController {

    @Resource
    private FineTuningLossService fineTuningLossService;

    @PostMapping("/create")
    @Operation(summary = "创建损失记录表，用于记录每次训练的损失值")
    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-loss:create')")
    public CommonResult<Long> createFineTuningLoss(@Valid @RequestBody FineTuningLossSaveReqVO createReqVO) {
        return success(fineTuningLossService.createFineTuningLoss(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新损失记录表，用于记录每次训练的损失值")
    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-loss:update')")
    public CommonResult<Boolean> updateFineTuningLoss(@Valid @RequestBody FineTuningLossSaveReqVO updateReqVO) {
        fineTuningLossService.updateFineTuningLoss(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除损失记录表，用于记录每次训练的损失值")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-loss:delete')")
    public CommonResult<Boolean> deleteFineTuningLoss(@RequestParam("id") Long id) {
        fineTuningLossService.deleteFineTuningLoss(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得损失记录表，用于记录每次训练的损失值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-loss:query')")
    public CommonResult<FineTuningLossRespVO> getFineTuningLoss(@RequestParam("id") Long id) {
        FineTuningLossDO fineTuningLoss = fineTuningLossService.getFineTuningLoss(id);
        return success(BeanUtils.toBean(fineTuningLoss, FineTuningLossRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得损失记录表，用于记录每次训练的损失值分页")
    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-loss:query')")
    public CommonResult<PageResult<FineTuningLossRespVO>> getFineTuningLossPage(@Valid FineTuningLossPageReqVO pageReqVO) {
        PageResult<FineTuningLossDO> pageResult = fineTuningLossService.getFineTuningLossPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FineTuningLossRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出损失记录表，用于记录每次训练的损失值 Excel")
    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-loss:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFineTuningLossExcel(@Valid FineTuningLossPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FineTuningLossDO> list = fineTuningLossService.getFineTuningLossPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "损失记录表，用于记录每次训练的损失值.xls", "数据", FineTuningLossRespVO.class,
                        BeanUtils.toBean(list, FineTuningLossRespVO.class));
    }

}