package org.dromara.personnel.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.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.personnel.domain.vo.OpsUserFilesVo;
import org.dromara.personnel.domain.bo.OpsUserFilesBo;
import org.dromara.personnel.service.IOpsUserFilesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 运维人员文件
 * 前端访问路由地址为:/personnel/userFiles
 *
 * @author LionLi
 * @date 2025-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/userFiles")
public class OpsUserFilesController extends BaseController {

    private final IOpsUserFilesService opsUserFilesService;

    /**
     * 查询运维人员文件列表
     */
    @SaCheckPermission("personnel:userFiles:list")
    @GetMapping("/list")
    public TableDataInfo<OpsUserFilesVo> list(OpsUserFilesBo bo, PageQuery pageQuery) {
        return opsUserFilesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出运维人员文件列表
     */
    @SaCheckPermission("personnel:userFiles:export")
    @Log(title = "运维人员文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OpsUserFilesBo bo, HttpServletResponse response) {
        List<OpsUserFilesVo> list = opsUserFilesService.queryList(bo);
        ExcelUtil.exportExcel(list, "运维人员文件", OpsUserFilesVo.class, response);
    }

    /**
     * 获取运维人员文件详细信息
     *
     * @param opsUserId 主键
     */
    @SaCheckPermission("personnel:userFiles:query")
    @GetMapping("/{opsUserId}")
    public R<OpsUserFilesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("opsUserId") Long opsUserId) {
        return R.ok(opsUserFilesService.queryById(opsUserId));
    }

    /**
     * 新增运维人员文件
     */
    @SaCheckPermission("personnel:userFiles:add")
    @Log(title = "运维人员文件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OpsUserFilesBo bo) {
        return toAjax(opsUserFilesService.insertByBo(bo));
    }

    /**
     * 修改运维人员文件
     */
    @SaCheckPermission("personnel:userFiles:edit")
    @Log(title = "运维人员文件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OpsUserFilesBo bo) {
        return toAjax(opsUserFilesService.updateByBo(bo));
    }

    /**
     * 删除运维人员文件
     *
     * @param opsUserIds 主键串
     */
    @SaCheckPermission("personnel:userFiles:remove")
    @Log(title = "运维人员文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{opsUserIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("opsUserIds") Long[] opsUserIds) {
        return toAjax(opsUserFilesService.deleteWithValidByIds(List.of(opsUserIds), true));
    }
}
