package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ISysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理
 */
@RestController
@RequestMapping("/device")
public class SysDeviceController extends BaseController {

    @Autowired
    private ISysDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @RequiresPermissions("system:device:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDevice device) {
        startPage();
        List<SysDevice> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 查询设备详情
     */
    @RequiresPermissions("system:device:query")
    @GetMapping("/{deviceId}")
    public AjaxResult getInfo(@PathVariable Long deviceId) {
        return success(deviceService.selectDeviceById(deviceId));
    }

    /**
     * 新增设备
     */
    @RequiresPermissions("system:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDevice device) {
        device.setCreateBy(SecurityUtils.getUsername());
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备
     */
    @RequiresPermissions("system:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDevice device) {
        device.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备
     */
    @RequiresPermissions("system:device:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds) {
        return toAjax(deviceService.deleteDeviceByIds(deviceIds));
    }
}
