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.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.inspection.domain.bo.ArStepRecordBo;
import org.dromara.inspection.domain.vo.ArStepRecordVo;
import org.dromara.inspection.service.IArStepRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
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/stepRecord")
public class ArStepRecordController extends BaseController {

    private final IArStepRecordService arStepRecordService;

    /**
     * 查询步骤执行记录列表
     */
    @SaCheckPermission("inspection:stepRecord:list")
    @GetMapping("/list")
    public TableDataInfo<ArStepRecordVo> list(@Validated(QueryGroup.class) ArStepRecordBo bo, PageQuery pageQuery) {
        return arStepRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出步骤执行记录列表
     */
    @SaCheckPermission("inspection:stepRecord:export")
    @Log(title = "步骤执行记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ArStepRecordBo bo, HttpServletResponse response) {
        List<ArStepRecordVo> list = arStepRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "步骤执行记录", ArStepRecordVo.class, response);
    }

    /**
     * 获取步骤执行记录详细信息
     *
     * @param id 记录ID
     */
    @SaCheckPermission("inspection:stepRecord:query")
    @GetMapping("/{id}")
    public R<ArStepRecordVo> getInfo(@NotNull(message = "记录ID不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(arStepRecordService.queryById(id));
    }

    /**
     * 新增步骤执行记录
     */
    @SaCheckPermission("inspection:stepRecord:add")
    @Log(title = "步骤执行记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArStepRecordBo bo) {
        return toAjax(arStepRecordService.insertByBo(bo));
    }

    /**
     * 修改步骤执行记录
     */
    @SaCheckPermission("inspection:stepRecord:edit")
    @Log(title = "步骤执行记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArStepRecordBo bo) {
        return toAjax(arStepRecordService.updateByBo(bo));
    }

    /**
     * 删除步骤执行记录
     *
     * @param ids 记录ID串
     */
    @SaCheckPermission("inspection:stepRecord:remove")
    @Log(title = "步骤执行记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "记录ID不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(arStepRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
