package org.dromara.inspection.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.inspection.domain.bo.ArStepMediaBo;
import org.dromara.inspection.domain.vo.ArStepMediaVo;
import org.dromara.inspection.service.IArStepMediaService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 步骤媒体文件Controller
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/inspection/stepMedia")
public class ArStepMediaController extends BaseController {

    private final IArStepMediaService arStepMediaService;

    /**
     * 查询步骤媒体文件列表
     */
    @SaCheckPermission("inspection:stepMedia:list")
    @GetMapping("/list")
    public TableDataInfo<ArStepMediaVo> list(@Validated(QueryGroup.class) ArStepMediaBo bo, PageQuery pageQuery) {
        return arStepMediaService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出步骤媒体文件列表
     */
    @SaCheckPermission("inspection:stepMedia:export")
    @Log(title = "步骤媒体文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ArStepMediaBo bo, HttpServletResponse response) {
        List<ArStepMediaVo> list = arStepMediaService.queryList(bo);
        ExcelUtil.exportExcel(list, "步骤媒体文件", ArStepMediaVo.class, response);
    }

    /**
     * 获取步骤媒体文件详细信息
     *
     * @param id 媒体ID
     */
    @SaCheckPermission("inspection:stepMedia:query")
    @GetMapping("/{id}")
    public R<ArStepMediaVo> getInfo(@NotNull(message = "媒体ID不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(arStepMediaService.queryById(id));
    }

    /**
     * 新增步骤媒体文件
     */
    @SaCheckPermission("inspection:stepMedia:add")
    @Log(title = "步骤媒体文件", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArStepMediaBo bo) {
        return toAjax(arStepMediaService.insertByBo(bo));
    }

    /**
     * 修改步骤媒体文件
     */
    @SaCheckPermission("inspection:stepMedia:edit")
    @Log(title = "步骤媒体文件", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArStepMediaBo bo) {
        return toAjax(arStepMediaService.updateByBo(bo));
    }

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