package org.dromara.web.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.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.web.domain.bo.SysDeviceBo;
import org.dromara.web.domain.vo.SysDeviceVo;
import org.dromara.web.service.ISysDeviceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理
 *
 * @author Lion Li
 * @date 2025-08-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/parameter/device")
public class SysDeviceController extends BaseController {

    private final ISysDeviceService sysDeviceService;

    /**
     * 查询设备管理列表
     */
    @SaCheckPermission("parameter:device:list")
    @GetMapping("/list")
    public TableDataInfo<SysDeviceVo> list(SysDeviceBo bo, PageQuery pageQuery) {
        return sysDeviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备管理列表
     */
    @SaCheckPermission("parameter:device:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysDeviceBo bo, HttpServletResponse response) {
        List<SysDeviceVo> list = sysDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备管理", SysDeviceVo.class, response);
    }

    /**
     * 获取设备管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("parameter:device:query")
    @GetMapping("/{id}")
    public R<SysDeviceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysDeviceService.queryById(id));
    }

    /**
     * 新增设备管理
     */
    @SaCheckPermission("parameter:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysDeviceBo bo) {
        return toAjax(sysDeviceService.insertByBo(bo));
    }

    /**
     * 修改设备管理
     */
    @SaCheckPermission("parameter:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysDeviceBo bo) {
        return toAjax(sysDeviceService.updateByBo(bo));
    }

    /**
     * 删除设备管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("parameter:device:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysDeviceService.deleteWithValidByIds(List.of(ids), true));
    }
}
