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.OrderRecordVo;
import org.dromara.inspection.service.impl.OpsInspectionOrderServiceImpl;
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.OpsInspectionOrderVo;
import org.dromara.inspection.domain.bo.OpsInspectionOrderBo;
import org.dromara.inspection.service.IOpsInspectionOrderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 运维-巡检-工单
 * 前端访问路由地址为:/inspection/order
 *
 * @author LionLi
 * @date 2025-09-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/order")
public class OpsInspectionOrderController extends BaseController {

    private final OpsInspectionOrderServiceImpl opsInspectionOrderService;

    /**
     * 查询运维-巡检-工单列表
     */
    @SaCheckPermission("inspection:order:list")
    @GetMapping("/list")
    public TableDataInfo<OpsInspectionOrderVo> list(OpsInspectionOrderBo bo, PageQuery pageQuery) {
        return opsInspectionOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出运维-巡检-工单列表
     */
    @SaCheckPermission("inspection:order:export")
    @Log(title = "运维-巡检-工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsInspectionOrderBo bo, HttpServletResponse response) {
        List<OpsInspectionOrderVo> list = opsInspectionOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "运维-巡检-工单", OpsInspectionOrderVo.class, response);
    }

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

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

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

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

    /**
     * 获取纪录
     */
    @SaCheckPermission("inspection:order:record")
    @GetMapping("/record")
    public R<OrderRecordVo> record(Long projectId) {
        return opsInspectionOrderService.record(projectId);
    }
}
