package org.dromara.inspection.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.dromara.inspection.domain.vo.OpsInspectionReportRecordVo;
import org.dromara.inspection.service.impl.OpsInspectionReportServiceImpl;
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.inspection.domain.vo.OpsInspectionReportVo;
import org.dromara.inspection.domain.bo.OpsInspectionReportBo;
import org.dromara.inspection.service.IOpsInspectionReportService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 运维-巡检-报修
 * 前端访问路由地址为:/inspection/report
 *
 * @author LionLi
 * @date 2025-09-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/report")
public class OpsInspectionReportController extends BaseController {

    private final OpsInspectionReportServiceImpl opsInspectionReportService;

    /**
     * 查询运维-巡检-报修列表
     */
    @SaCheckPermission("inspection:report:list")
    @GetMapping("/list")
    public TableDataInfo<OpsInspectionReportVo> list(OpsInspectionReportBo bo, PageQuery pageQuery) {
        return opsInspectionReportService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出运维-巡检-报修列表
     */
    @SaCheckPermission("inspection:report:export")
    @Log(title = "运维-巡检-报修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsInspectionReportBo bo, HttpServletResponse response) {
        List<OpsInspectionReportVo> list = opsInspectionReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "运维-巡检-报修", OpsInspectionReportVo.class, response);
    }

    /**
     * 获取运维-巡检-报修详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("inspection:report:query")
    @GetMapping("/{id}")
    public R<OpsInspectionReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(opsInspectionReportService.queryById(id));
    }

    /**
     * 新增运维-巡检-报修
     */
    @SaCheckPermission("inspection:report:add")
    @Log(title = "运维-巡检-报修", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OpsInspectionReportBo bo) {
        return toAjax(opsInspectionReportService.insertByBo(bo));
    }

    /**
     * 修改运维-巡检-报修
     */
    @SaCheckPermission("inspection:report:edit")
    @Log(title = "运维-巡检-报修", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OpsInspectionReportBo bo) {
        return toAjax(opsInspectionReportService.updateByBo(bo));
    }

    /**
     * 删除运维-巡检-报修
     *
     * @param ids 主键串
     */
    @SaCheckPermission("inspection:report:remove")
    @Log(title = "运维-巡检-报修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(opsInspectionReportService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 生成纪录
     */
    @SaCheckPermission("inspection:report:record")
    @GetMapping("/record")
    public R<OpsInspectionReportRecordVo> record(@RequestParam Long projectId){

        return opsInspectionReportService.record(projectId);
    }

}
