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.ArExecutionBo;
import org.dromara.inspection.domain.vo.ArExecutionVo;
import org.dromara.inspection.service.IArExecutionService;
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/execution")
public class ArExecutionController extends BaseController {

    private final IArExecutionService arExecutionService;

    /**
     * 查询任务执行记录列表
     */
    @SaCheckPermission("inspection:execution:list")
    @GetMapping("/list")
    public TableDataInfo<ArExecutionVo> list(@Validated(QueryGroup.class) ArExecutionBo bo, PageQuery pageQuery) {
        return arExecutionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出任务执行记录列表
     */
    @SaCheckPermission("inspection:execution:export")
    @Log(title = "任务执行记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ArExecutionBo bo, HttpServletResponse response) {
        List<ArExecutionVo> list = arExecutionService.queryList(bo);
        ExcelUtil.exportExcel(list, "任务执行记录", ArExecutionVo.class, response);
    }

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

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

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

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