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.safetyweeklyreport.SafetyWeeklyReportCreateReq;
import org.dromara.safety.domain.req.safetyweeklyreport.SafetyWeeklyReportQueryReq;
import org.dromara.safety.domain.req.safetyweeklyreport.SafetyWeeklyReportUpdateReq;
import org.dromara.safety.domain.vo.BusSafetyWeeklyReportVo;
import org.dromara.safety.service.IBusSafetyWeeklyReportService;
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/safetyWeeklyReport")
public class BusSafetyWeeklyReportController extends BaseController {

    private final IBusSafetyWeeklyReportService busSafetyWeeklyReportService;

    /**
     * 查询安全周报列表
     */
    @SaCheckPermission("safety:safetyWeeklyReport:list")
    @GetMapping("/list")
    public TableDataInfo<BusSafetyWeeklyReportVo> list(SafetyWeeklyReportQueryReq req, PageQuery pageQuery) {
        return busSafetyWeeklyReportService.queryPageList(req, pageQuery);
    }

    /**
     * 导出安全周报列表
     */
    @SaCheckPermission("safety:safetyWeeklyReport:export")
    @Log(title = "安全周报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SafetyWeeklyReportQueryReq req, HttpServletResponse response) {
        List<BusSafetyWeeklyReportVo> list = busSafetyWeeklyReportService.queryList(req);
        ExcelUtil.exportExcel(list, "安全周报", BusSafetyWeeklyReportVo.class, response);
    }

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

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

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

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