package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledge.domain.bo.KnowledgeJobBo;
import org.dromara.knowledge.domain.vo.KnowledgeJobVo;
import org.dromara.knowledge.service.KnowledgeJobService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库任务控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/job")
public class KnowledgeJobController extends BaseController {

    private final KnowledgeJobService knowledgeJobService;

    /**
     * 查询知识库任务列表
     */
    @SaCheckPermission("knowledge:job:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeJobVo> list(KnowledgeJobBo bo, PageQuery pageQuery) {
        return knowledgeJobService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库任务列表
     */
    @SaCheckPermission("knowledge:job:export")
    @Log(title = "知识库任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeJobBo bo, HttpServletResponse response) {
        List<KnowledgeJobVo> list = knowledgeJobService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库任务", KnowledgeJobVo.class, response);
    }

    /**
     * 获取知识库任务详细信息
     *
     * @param jobId 任务ID
     */
    @SaCheckPermission("knowledge:job:query")
    @GetMapping("/{jobId}")
    public R<KnowledgeJobVo> getInfo(@PathVariable Long jobId) {
        return R.ok(knowledgeJobService.queryById(jobId));
    }

    /**
     * 新增知识库任务
     */
    @SaCheckPermission("knowledge:job:add")
    @Log(title = "知识库任务", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeJobBo bo) {
        return toAjax(knowledgeJobService.insertByBo(bo));
    }

    /**
     * 修改知识库任务
     */
    @SaCheckPermission("knowledge:job:edit")
    @Log(title = "知识库任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeJobBo bo) {
        return toAjax(knowledgeJobService.updateByBo(bo));
    }

    /**
     * 删除知识库任务
     *
     * @param jobIds 任务ID数组
     */
    @SaCheckPermission("knowledge:job:remove")
    @Log(title = "知识库任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public R<Void> remove(@PathVariable Long[] jobIds) {
        return toAjax(knowledgeJobService.deleteWithValidByIds(List.of(jobIds), true));
    }

    /**
     * 根据知识库ID查询任务列表
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:job:query")
    @GetMapping("/knowledge/{knowledgeId}")
    public R<List<KnowledgeJobVo>> getByKnowledgeId(@PathVariable String knowledgeId) {
        return R.ok(knowledgeJobService.queryByKnowledgeId(knowledgeId));
    }

    /**
     * 根据任务状态查询任务列表
     *
     * @param status 任务状态
     */
    @SaCheckPermission("knowledge:job:query")
    @GetMapping("/status/{status}")
    public R<List<KnowledgeJobVo>> getByStatus(@PathVariable Integer status) {
        return R.ok(knowledgeJobService.queryByStatus(status));
    }

    /**
     * 根据任务类型查询任务列表
     *
     * @param jobType 任务类型
     */
    @SaCheckPermission("knowledge:job:query")
    @GetMapping("/type/{jobType}")
    public R<List<KnowledgeJobVo>> getByJobType(@PathVariable String jobType) {
        return R.ok(knowledgeJobService.queryByJobType(jobType));
    }
}