package org.jk.rag.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.jk.common.excel.utils.ExcelUtil;
import org.jk.rag.domain.bo.EvaluationTaskHistoryBo;
import org.jk.rag.domain.vo.EvaluationTaskHistoryVo;
import org.jk.rag.service.IEvaluationTaskHistoryService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.jk.common.idempotent.annotation.RepeatSubmit;
import org.jk.common.log.annotation.Log;
import org.jk.common.web.core.BaseController;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.common.core.domain.R;
import org.jk.common.core.validate.AddGroup;
import org.jk.common.core.validate.EditGroup;
import org.jk.common.log.enums.BusinessType;
import org.jk.common.mybatis.core.page.TableDataInfo;

/**
 * 评测任务历史
 *
 * @author Lion Li
 * @date 2026-01-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/taskHistory")
public class EvaluationTaskHistoryController extends BaseController {

    private final IEvaluationTaskHistoryService evaluationTaskHistoryService;

    /**
     * 查询评测任务历史列表
     */
    //@SaCheckPermission("system:taskHistory:list")
    @GetMapping("/list")
    public TableDataInfo<EvaluationTaskHistoryVo> list(EvaluationTaskHistoryBo bo, PageQuery pageQuery) {
        return evaluationTaskHistoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出评测任务历史列表
     */
    @SaCheckPermission("system:taskHistory:export")
    @Log(title = "评测任务历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EvaluationTaskHistoryBo bo, HttpServletResponse response) {
        List<EvaluationTaskHistoryVo> list = evaluationTaskHistoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "评测任务历史", EvaluationTaskHistoryVo.class, response);
    }

    /**
     * 获取评测任务历史详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:taskHistory:query")
    @GetMapping("/{id}")
    public R<EvaluationTaskHistoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(evaluationTaskHistoryService.queryById(id));
    }

    /**
     * 新增评测任务历史
     */
    @SaCheckPermission("system:taskHistory:add")
    @Log(title = "评测任务历史", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EvaluationTaskHistoryBo bo) {
        return toAjax(evaluationTaskHistoryService.insertByBo(bo));
    }

    /**
     * 修改评测任务历史
     */
    @SaCheckPermission("system:taskHistory:edit")
    @Log(title = "评测任务历史", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EvaluationTaskHistoryBo bo) {
        return toAjax(evaluationTaskHistoryService.updateByBo(bo));
    }

    /**
     * 删除评测任务历史
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:taskHistory:remove")
    @Log(title = "评测任务历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(evaluationTaskHistoryService.deleteWithValidByIds(List.of(ids), true));
    }
}
