package org.dromara.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.domain.vo.SenEventsReportVo;
import org.dromara.domain.bo.SenEventsReportBo;
import org.dromara.service.ISenEventsReportService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 推送记录
 *
 * @author luogang
 * @date 2024-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sense/eventsReport")
public class SenEventsReportController extends BaseController {

    private final ISenEventsReportService senEventsReportService;

    /**
     * 查询推送记录列表
     */
    @SaCheckPermission("sense:eventsReport:list")
    @GetMapping("/list")
    public TableDataInfo<SenEventsReportVo> list(SenEventsReportBo bo, PageQuery pageQuery) {
        return senEventsReportService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出推送记录列表
     */
    @SaCheckPermission("sense:eventsReport:export")
    @Log(title = "推送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SenEventsReportBo bo, HttpServletResponse response) {
        List<SenEventsReportVo> list = senEventsReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "推送记录", SenEventsReportVo.class, response);
    }

    /**
     * 获取推送记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sense:eventsReport:query")
    @GetMapping("/{id}")
    public R<SenEventsReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(senEventsReportService.queryById(id));
    }

    /**
     * 新增推送记录
     */
    @SaCheckPermission("sense:eventsReport:add")
    @Log(title = "推送记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SenEventsReportBo bo) {
        return toAjax(senEventsReportService.insertByBo(bo));
    }

    /**
     * 修改推送记录
     */
    @SaCheckPermission("sense:eventsReport:edit")
    @Log(title = "推送记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SenEventsReportBo bo) {
        return toAjax(senEventsReportService.updateByBo(bo));
    }

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

    /**
     * 批量重试失败推送
     * @param ids 主键串
     */
    @Log(title = "批量重试失败推送")
    @GetMapping("/againReport/{ids}")
    public R<Void> againReport(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        senEventsReportService.againReport(List.of(ids));
        return toAjax(true);
    }
}
