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.safetyinspection.SafetyInspectionCreateReq;
import org.dromara.safety.domain.req.safetyinspection.SafetyInspectionQueryReq;
import org.dromara.safety.domain.req.safetyinspection.SafetyInspectionUpdateReq;
import org.dromara.safety.domain.vo.BusSafetyInspectionVo;
import org.dromara.safety.service.IBusSafetyInspectionService;
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/safetyInspection")
public class BusSafetyInspectionController extends BaseController {

    private final IBusSafetyInspectionService busSafetyInspectionService;

    /**
     * 查询安全巡检工单列表
     */
    @SaCheckPermission("safety:safetyInspection:list")
    @GetMapping("/list")
    public TableDataInfo<BusSafetyInspectionVo> list(SafetyInspectionQueryReq req, PageQuery pageQuery) {
        return busSafetyInspectionService.queryPageList(req, pageQuery);
    }

    /**
     * 导出安全巡检工单列表
     */
    @SaCheckPermission("safety:safetyInspection:export")
    @Log(title = "安全巡检工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SafetyInspectionQueryReq req, HttpServletResponse response) {
        List<BusSafetyInspectionVo> list = busSafetyInspectionService.queryList(req);
        ExcelUtil.exportExcel(list, "安全巡检工单", BusSafetyInspectionVo.class, response);
    }

    /**
     * 获取安全巡检工单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("safety:safetyInspection:query")
    @GetMapping("/{id}")
    public R<BusSafetyInspectionVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(busSafetyInspectionService.queryById(id));
    }

    /**
     * 新增安全巡检工单
     */
    @SaCheckPermission("safety:safetyInspection:add")
    @Log(title = "安全巡检工单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody SafetyInspectionCreateReq req) {
        return R.ok(busSafetyInspectionService.insertByBo(req));
    }

    /**
     * 修改安全巡检工单
     */
    @SaCheckPermission("safety:safetyInspection:edit")
    @Log(title = "安全巡检工单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SafetyInspectionUpdateReq req) {
        return toAjax(busSafetyInspectionService.updateByBo(req));
    }

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