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.safetylog.SafetyLogCreateReq;
import org.dromara.safety.domain.req.safetylog.SafetyLogQueryReq;
import org.dromara.safety.domain.req.safetylog.SafetyLogUpdateReq;
import org.dromara.safety.domain.vo.BusSafetyLogVo;
import org.dromara.safety.service.IBusSafetyLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 安全日志
 *
 * @author lcj
 * @date 2025-03-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/safety/safetyLog")
public class BusSafetyLogController extends BaseController {

    private final IBusSafetyLogService busSafetyLogService;

    /**
     * 查询安全日志列表
     */
    @SaCheckPermission("safety:safetyLog:list")
    @GetMapping("/list")
    public TableDataInfo<BusSafetyLogVo> list(SafetyLogQueryReq req, PageQuery pageQuery) {
        return busSafetyLogService.queryPageList(req, pageQuery);
    }

    /**
     * 导出安全日志列表
     */
    @SaCheckPermission("safety:safetyLog:export")
    @Log(title = "安全日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SafetyLogQueryReq req, HttpServletResponse response) {
        List<BusSafetyLogVo> list = busSafetyLogService.queryList(req);
        ExcelUtil.exportExcel(list, "安全日志", BusSafetyLogVo.class, response);
    }

    /**
     * 获取安全日志详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("safety:safetyLog:query")
    @GetMapping("/{id}")
    public R<BusSafetyLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busSafetyLogService.queryById(id));
    }

    /**
     * 新增安全日志
     */
    @SaCheckPermission("safety:safetyLog:add")
    @Log(title = "安全日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody SafetyLogCreateReq req) {
        return R.ok(busSafetyLogService.insertByBo(req));
    }

    /**
     * 修改安全日志
     */
    @SaCheckPermission("safety:safetyLog:edit")
    @Log(title = "安全日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SafetyLogUpdateReq req) {
        return toAjax(busSafetyLogService.updateByBo(req));
    }

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