package org.dromara.inspection.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.core.validate.QueryGroup;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.inspection.domain.bo.ArStepBo;
import org.dromara.inspection.domain.vo.ArStepTreeVo;
import org.dromara.inspection.domain.vo.ArStepVo;
import org.dromara.inspection.service.IArStepService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.util.Arrays;
import java.util.List;

/**
 * 巡检步骤Controller
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/inspection/step")
public class ArStepController extends BaseController {

    private final IArStepService arStepService;

    /**
     * 查询巡检步骤列表
     */
    @SaCheckPermission("inspection:step:list")
    @GetMapping("/list")
    public TableDataInfo<ArStepVo> list(@Validated(QueryGroup.class) ArStepBo bo, PageQuery pageQuery) {
        return arStepService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询任务的步骤树
     *
     * @param taskId 任务ID
     */
    @SaCheckPermission("inspection:step:list")
    @GetMapping("/tree/{taskId}")
    public R<List<ArStepTreeVo>> tree(@NotNull(message = "任务ID不能为空")
                                      @PathVariable("taskId") Long taskId) {
        return R.ok(arStepService.queryStepTree(taskId));
    }

    /**
     * 获取巡检步骤详细信息
     *
     * @param id 步骤ID
     */
    @SaCheckPermission("inspection:step:query")
    @GetMapping("/{id}")
    public R<ArStepVo> getInfo(@NotNull(message = "步骤ID不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(arStepService.queryById(id));
    }

    /**
     * 新增巡检步骤
     */
    @SaCheckPermission("inspection:step:add")
    @Log(title = "巡检步骤", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArStepBo bo) {
        return toAjax(arStepService.insertByBo(bo));
    }

    /**
     * 修改巡检步骤
     */
    @SaCheckPermission("inspection:step:edit")
    @Log(title = "巡检步骤", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArStepBo bo) {
        return toAjax(arStepService.updateByBo(bo));
    }

    /**
     * 删除巡检步骤（级联删除子步骤）
     *
     * @param ids 步骤ID串
     */
    @SaCheckPermission("inspection:step:remove")
    @Log(title = "巡检步骤", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "步骤ID不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(arStepService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
