package org.dromara.safety.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
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.safety.domain.req.questionuseranswer.QuestionUserAnswerCreateReq;
import org.dromara.safety.domain.req.questionuseranswer.QuestionUserAnswerQueryReq;
import org.dromara.safety.domain.req.questionuseranswer.QuestionUserAnswerUpdateReq;
import org.dromara.safety.domain.vo.BusQuestionUserAnswerVo;
import org.dromara.safety.service.IBusQuestionUserAnswerService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户试卷存储
 *
 * @author lcj
 * @date 2025-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/safety/questionUserAnswer")
public class BusQuestionUserAnswerController extends BaseController {

    private final IBusQuestionUserAnswerService busQuestionUserAnswerService;

    /**
     * 查询用户试卷存储列表
     */
    @SaCheckPermission("safety:questionUserAnswer:list")
    @GetMapping("/list")
    public TableDataInfo<BusQuestionUserAnswerVo> list(QuestionUserAnswerQueryReq req, PageQuery pageQuery) {
        return busQuestionUserAnswerService.queryPageList(req, pageQuery);
    }

    /**
     * 导出用户试卷存储列表
     */
    @SaCheckPermission("safety:questionUserAnswer:export")
    @Log(title = "用户试卷存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QuestionUserAnswerQueryReq req, HttpServletResponse response) {
        List<BusQuestionUserAnswerVo> list = busQuestionUserAnswerService.queryList(req);
        ExcelUtil.exportExcel(list, "用户试卷存储", BusQuestionUserAnswerVo.class, response);
    }

    /**
     * 获取用户试卷存储详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("safety:questionUserAnswer:query")
    @GetMapping("/{id}")
    public R<BusQuestionUserAnswerVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable Long id) {
        return R.ok(busQuestionUserAnswerService.queryById(id));
    }

    /**
     * 新增用户试卷存储
     */
    @SaCheckPermission("safety:questionUserAnswer:add")
    @Log(title = "用户试卷存储", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody QuestionUserAnswerCreateReq req) {
        return R.ok(busQuestionUserAnswerService.insertByBo(req));
    }

    /**
     * 修改用户试卷存储
     */
    @SaCheckPermission("safety:questionUserAnswer:edit")
    @Log(title = "用户试卷存储", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QuestionUserAnswerUpdateReq req) {
        return toAjax(busQuestionUserAnswerService.updateByBo(req));
    }

    /**
     * 删除用户试卷存储
     *
     * @param ids 主键串
     */
    @SaCheckPermission("safety:questionUserAnswer:remove")
    @Log(title = "用户试卷存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busQuestionUserAnswerService.deleteWithValidByIds(List.of(ids), true));
    }
}
