package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.QaHistoryBo;
import org.eco.als.domain.vo.QaHistoryVo;
import org.eco.als.service.IQaHistoryService;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问答历史记录Controller
 *
 * @author wgk
 * @date 2024-12-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/qaHistory")
public class QaHistoryController extends BaseController {
    @Resource
    private IQaHistoryService qaHistoryService;

    /**
     * 查询问答历史记录列表
     */
    @GetMapping("/list")
    public CommonResult<PageResult<QaHistoryVo>> list(QaHistoryBo qaHistoryBo) {
        return CommonResult.success(qaHistoryService.selectPage(qaHistoryBo));
    }

    /**
     * 查询问答历史记录列表
     */
    @GetMapping("/listName")
    public CommonResult<PageResult<QaHistoryVo>> listName(QaHistoryBo qaHistoryBo) {
        return CommonResult.success(qaHistoryService.selectPage(qaHistoryBo));
    }
    /**
     * 查询问答历史记录列表不分页
     */
    @GetMapping("/list/all")
    public CommonResult<List<QaHistoryVo>> listAll(QaHistoryBo qaHistoryBo) {
        return CommonResult.success(qaHistoryService.selectList(qaHistoryBo));
    }

    /**
     * 获取问答历史记录详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult<QaHistoryVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(qaHistoryService.selectById(id));
    }

    /**
     * 新增问答历史记录
     */
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody QaHistoryBo qaHistoryBo) {
        boolean inserted = qaHistoryService.insert(qaHistoryBo);
        if (!inserted) {
            return CommonResult.fail("新增问答历史记录记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改问答历史记录
     */
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody QaHistoryBo qaHistoryBo) {
        boolean updated = qaHistoryService.update(qaHistoryBo);
        if (!updated) {
            return CommonResult.fail("修改问答历史记录记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除问答历史记录
     */
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = qaHistoryService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除问答历史记录记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 查询问答历史记录列表不分页
     */
    @SaCheckPermission("als:qaHistory:list")
    @GetMapping("/group")
    public CommonResult<List<QaHistoryVo>> questionList() {
        return CommonResult.success(qaHistoryService.groupQuestion());
    }
}
