mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
增加绑定设备,解绑设备,获取用户设备列表,获取已绑定设备,admin获取所有设备 api相关请求
This commit is contained in:
parent
a8f06f5718
commit
e8c074c3d7
5 changed files with 214 additions and 0 deletions
|
|
@ -0,0 +1,65 @@
|
|||
package xiaozhi.modules.device.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.user.UserDetail;
|
||||
import xiaozhi.common.utils.Result;
|
||||
import xiaozhi.modules.device.dto.DeviceDTO;
|
||||
import xiaozhi.modules.device.service.DeviceService;
|
||||
import xiaozhi.modules.security.user.SecurityUser;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import xiaozhi.common.validator.AssertUtils;
|
||||
import xiaozhi.common.exception.ErrorCode;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/device")
|
||||
@Tag(name = "设备管理")
|
||||
|
||||
public class DeviceController {
|
||||
private final DeviceService deviceService;
|
||||
|
||||
@PostMapping("/bind/{deviceCode}")
|
||||
@Operation(summary = "绑定设备")
|
||||
@RequiresPermissions("sys:device:bind")
|
||||
public Result<DeviceDTO> register(@RequestBody String deviceCode) {
|
||||
UserDetail user = SecurityUser.getUser();
|
||||
|
||||
AssertUtils.isBlank(deviceCode, 400, "设备编码不能为空");
|
||||
DeviceDTO device = deviceService.bindDevice(user.getId(), deviceCode);
|
||||
return new Result<DeviceDTO>().ok(device);
|
||||
}
|
||||
|
||||
@GetMapping("/bind")
|
||||
@Operation(summary = "获取已绑定设备")
|
||||
@RequiresPermissions("sys:device:bind")
|
||||
public Result<List<DeviceDTO>> getUserDevices() {
|
||||
UserDetail user = SecurityUser.getUser();
|
||||
List<DeviceDTO> devices = deviceService.getUserDevices(user.getId());
|
||||
return new Result<List<DeviceDTO>>().ok(devices);
|
||||
}
|
||||
|
||||
@PutMapping("/unbind")
|
||||
@Operation(summary = "解绑设备")
|
||||
@RequiresPermissions("sys:device:unbind")
|
||||
public Result unbindDevice(@RequestBody Long deviceId) {
|
||||
UserDetail user = SecurityUser.getUser();
|
||||
deviceService.unbindDevice(user.getId(), deviceId);
|
||||
return new Result();
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@Operation(summary = "设备列表(管理员)")
|
||||
@RequiresPermissions("sys:device:all")
|
||||
public Result<PageData<DeviceDTO>> adminDeviceList(
|
||||
@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
|
||||
PageData<DeviceDTO> page = deviceService.adminDeviceList(params);
|
||||
return new Result<PageData<DeviceDTO>>().ok(page);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
package xiaozhi.modules.device.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Schema(description = "设备信息")
|
||||
public class DeviceDTO {
|
||||
@Schema(description = "设备ID")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "关联用户ID")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "MAC地址")
|
||||
private String macAddress;
|
||||
|
||||
@Schema(description = "最后连接时间")
|
||||
private Date lastConnectedAt;
|
||||
|
||||
@Schema(description = "自动更新开关(0关闭/1开启)")
|
||||
private Integer autoUpdate;
|
||||
|
||||
@Schema(description = "设备硬件型号")
|
||||
private String board;
|
||||
|
||||
@Schema(description = "设备别名")
|
||||
private String alias;
|
||||
|
||||
@Schema(description = "智能体编码")
|
||||
private String agentCode;
|
||||
|
||||
@Schema(description = "智能体ID")
|
||||
private Long agentId;
|
||||
|
||||
@Schema(description = "固件版本号")
|
||||
private String appVersion;
|
||||
|
||||
@Schema(description = "排序")
|
||||
private Integer sort;
|
||||
|
||||
@Schema(description = "创建者")
|
||||
private Long creator;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
private Date createDate;
|
||||
|
||||
@Schema(description = "更新者")
|
||||
private Long updater;
|
||||
|
||||
@Schema(description = "更新时间")
|
||||
private Date updateDate;
|
||||
|
||||
@Schema(description = "设备编码")
|
||||
private String deviceCode;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package xiaozhi.modules.device.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
// 正确的导入路径应该是 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import xiaozhi.modules.device.dto.DeviceDTO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param; // 新增 Param 注解导入
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DeviceMapper extends BaseMapper<DeviceDTO> {
|
||||
// 新增 @Param 注解保证参数绑定
|
||||
List<DeviceDTO> selectByUserId(@Param("userId") Long userId);
|
||||
|
||||
// 与 ServiceImpl 中调用的 selectPage 方法签名保持一致
|
||||
IPage<DeviceDTO> selectPage(IPage<DeviceDTO> page, QueryWrapper<DeviceDTO> queryWrapper);
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package xiaozhi.modules.device.service;
|
||||
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.modules.device.dto.DeviceDTO;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface DeviceService {
|
||||
DeviceDTO bindDevice(Long userId, String deviceCode);
|
||||
|
||||
List<DeviceDTO> getUserDevices(Long userId);
|
||||
|
||||
void unbindDevice(Long userId, Long deviceId);
|
||||
|
||||
PageData<DeviceDTO> adminDeviceList(Map<String, Object> params);
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package xiaozhi.modules.device.service.impl;
|
||||
|
||||
import xiaozhi.modules.device.service.DeviceService;
|
||||
import xiaozhi.modules.device.dto.DeviceDTO;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.AllArgsConstructor;
|
||||
import java.util.Date;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import xiaozhi.modules.device.mapper.DeviceMapper;
|
||||
|
||||
@Service
|
||||
public class DeviceServiceImpl implements DeviceService {
|
||||
private final DeviceMapper deviceMapper;
|
||||
|
||||
// 添加构造函数来初始化 deviceMapper
|
||||
public DeviceServiceImpl(DeviceMapper deviceMapper) {
|
||||
this.deviceMapper = deviceMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeviceDTO bindDevice(Long userId, String deviceCode) {
|
||||
DeviceDTO device = new DeviceDTO();
|
||||
device.setUserId(userId);
|
||||
device.setDeviceCode(deviceCode);
|
||||
device.setCreateDate(new Date());
|
||||
deviceMapper.insert(device);
|
||||
return device;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeviceDTO> getUserDevices(Long userId) {
|
||||
return deviceMapper.selectByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unbindDevice(Long userId, Long deviceId) {
|
||||
deviceMapper.deleteById(deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<DeviceDTO> adminDeviceList(Map<String, Object> params) {
|
||||
IPage<DeviceDTO> page = deviceMapper.selectPage(
|
||||
new Page<>(
|
||||
Long.parseLong(params.get("page").toString()),
|
||||
Long.parseLong(params.get("limit").toString())
|
||||
),
|
||||
new QueryWrapper<DeviceDTO>().allEq(params)
|
||||
);
|
||||
return new PageData<>(page.getRecords(), page.getTotal());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue