package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.TblEventVo;
import org.dromara.system.domain.bo.TblEventBo;
import org.dromara.system.service.ITblEventService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 事件
 *
 * @author Lion Li
 * @date 2025-06-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/event")
public class TblEventController extends BaseController {

    private final ITblEventService tblEventService;

    /**
     * 查询事件列表
     */
    @SaCheckPermission("system:event:list")
    @GetMapping("/list")
    public TableDataInfo<TblEventVo> list(TblEventBo bo, PageQuery pageQuery) {
        return tblEventService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出事件列表
     */
    @SaCheckPermission("system:event:export")
    @Log(title = "事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblEventBo bo, HttpServletResponse response) {
        List<TblEventVo> list = tblEventService.queryList(bo);
        ExcelUtil.exportExcel(list, "事件", TblEventVo.class, response);
    }

    /**
     * 获取事件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:event:query")
    @GetMapping("/{id}")
    public R<TblEventVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tblEventService.queryById(id));
    }

    /**
     * 新增事件
     */
    @SaCheckPermission("system:event:add")
    @Log(title = "事件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TblEventBo bo) {
        return toAjax(tblEventService.insertByBo(bo));
    }

    /**
     * 修改事件
     */
    @SaCheckPermission("system:event:edit")
    @Log(title = "事件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblEventBo bo) {
        return toAjax(tblEventService.updateByBo(bo));
    }

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