package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.QaHistoryBo;
import org.eco.als.domain.vo.QaHistoryImportVo;
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.domain.model.LoginUser;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.security.utils.LoginHelper;
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 org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
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("/list/all")
    public CommonResult<List<QaHistoryVo>> listAll(QaHistoryBo qaHistoryBo) {
        return CommonResult.success(qaHistoryService.selectList(qaHistoryBo));
    }

    /**
     * 导出问答历史记录列表
     */
    @Log(title = "问答历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(QaHistoryBo qaHistoryBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<QaHistoryVo> list = qaHistoryService.selectList(qaHistoryBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        qaHistoryService.asyncExport(list, "问答历史记录", loginUser);
        return CommonResult.success();
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "问答历史记录", QaHistoryImportVo.class, response);
    }

    /**
     * 导入问答历史记录列表
     */
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        qaHistoryService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取问答历史记录详细信息
     */
    @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());
    }
}
