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.dto.safetyinspection.HseSafetyInspectionCreateReq;
import org.dromara.safety.domain.dto.safetyinspection.HseSafetyInspectionGisReq;
import org.dromara.safety.domain.dto.safetyinspection.HseSafetyInspectionQueryReq;
import org.dromara.safety.domain.dto.safetyinspection.HseSafetyInspectionUpdateReq;
import org.dromara.safety.domain.vo.safetyinspection.HseSafetyInspectionVo;
import org.dromara.safety.domain.vo.safetyinspection.HseSafetyInspectionListGisVo;
import org.dromara.safety.service.IHseSafetyInspectionService;
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 HseSafetyInspectionController extends BaseController {

    private final IHseSafetyInspectionService safetyInspectionService;

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

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

    /**
     * 根据主键导出安全巡检工单
     */
    @SaCheckPermission("safety:safetyInspection:export")
    @Log(title = "安全巡检工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export/word")
    public void exportWordById(@NotNull(message = "主键不能为空") Long id,
                               HttpServletResponse response) {
        safetyInspectionService.exportWordById(id, response);
    }

    /**
     * 查询大屏安全信息
     */
    @GetMapping("/gis")
    public R<HseSafetyInspectionListGisVo> queryGisList(HseSafetyInspectionGisReq req) {
        return R.ok(safetyInspectionService.queryGisList(req));
    }

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

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

    /**
     * 修改安全巡检工单
     */
    @SaCheckPermission("safety:safetyInspection:edit")
    @Log(title = "安全巡检工单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HseSafetyInspectionUpdateReq req) {
        return toAjax(safetyInspectionService.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(safetyInspectionService.deleteWithValidByIds(List.of(ids), true));
    }
}
