package org.dromara.ygpassport.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.ygpassport.domain.bo.BugBo;
import org.dromara.ygpassport.domain.vo.BugVo;
import org.dromara.ygpassport.service.IBugService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Bug信息
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/bug")
public class BugController extends BaseController {

    private final IBugService bugService;

    /**
     * 查询Bug信息列表
     */
    @SaCheckPermission("ygpassport:bug:list")
    @GetMapping("/list")
    public TableDataInfo<BugVo> list(BugBo bo, PageQuery pageQuery) {
        return bugService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出Bug信息列表
     */
    @SaCheckPermission("ygpassport:bug:export")
    @Log(title = "Bug信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BugBo bo, HttpServletResponse response) {
        List<BugVo> list = bugService.queryList(bo);
        ExcelUtil.exportExcel(list, "Bug信息", BugVo.class, response);
    }

    /**
     * 获取Bug信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:bug:query")
    @GetMapping("/{id}")
    public R<BugVo> getInfo(@NotNull(message = "主键不能为空")
                            @PathVariable Long id) {
        return R.ok(bugService.queryById(id));
    }

    /**
     * 新增Bug信息
     */
    @SaCheckPermission("ygpassport:bug:add")
    @Log(title = "Bug信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BugBo bo) {
        return toAjax(bugService.insertByBo(bo));
    }

    /**
     * 修改Bug信息
     */
    @SaCheckPermission("ygpassport:bug:edit")
    @Log(title = "Bug信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BugBo bo) {
        return toAjax(bugService.updateByBo(bo));
    }

    /**
     * 删除Bug信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ygpassport:bug:remove")
    @Log(title = "Bug信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(bugService.deleteWithValidByIds(List.of(ids), true));
    }
}
