feat(device): 添加设备类型管理功能

- 在Device实体中添加了deviceType字段,并使用@TableField注解标记为非数据库字段
- 新增DeviceType实体类,用于表示设备类型信息
- 创建DeviceTypeController控制器,提供获取所有设备类型的接口
- 新增DeviceTypeService接口及其实现类DeviceTypeServiceImpl,实现设备类型相关业务逻辑
- 添加DriveTypeMapper接口,继承BaseMapper以支持对DeviceType的数据库操作
- 在ErrorCode常量类中增加了设备相关的错误码:DEVICE_NOT_FOUND和DEVICE_TYPE_NOT_FOUND
- 更新DeviceController,引入DeviceTypeResponse并优化代码结构
- 在DeviceMapper中新增selectOneWithTypeById方法,通过MyBatis注解实现关联查询设备及其类型信息
This commit is contained in:
Chaos
2025-11-21 06:54:12 +08:00
parent e23434ab48
commit 72a1e4d309
12 changed files with 190 additions and 2 deletions

View File

@@ -50,7 +50,14 @@ public enum ErrorCode {
CAPTCHA_ERROR(400, "SYS-301", "验证码错误"),
SMS_SEND_FAILED(500, "SYS-302", "短信发送失败"),
FILE_UPLOAD_FAILED(500, "SYS-303", "文件上传失败"),
RATE_LIMIT_EXCEEDED(429, "SYS-304", "操作过于频繁");
RATE_LIMIT_EXCEEDED(429, "SYS-304", "操作过于频繁"),
// 设备
DEVICE_NOT_FOUND(404, "DEVICE-301", "设备不存在"),
DEVICE_TYPE_NOT_FOUND(404, "DEVICE-302", "设备类型不存在")
;
private final int httpStatus;
private final String code; // 业务错误码(领域-编号)

View File

@@ -2,8 +2,29 @@ package cn.nopj.chaos_api.mapper;
import cn.nopj.chaos_api.domain.entity.Device;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.*;
@Mapper
public interface DeviceMapper extends BaseMapper<Device> {
/**
* 根据id查询设备信息
* @param id 设备id
* @return 设备信息
*/
@Select("SELECT * FROM t_device WHERE id = #{id}")
@Results(id = "DeviceResultMap", value ={
@Result(id = true, property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "model", column = "model"),
@Result(property = "snmpCommunity", column = "snmp_community"),
@Result(property = "manufacturer", column = "manufacturer"),
@Result(property = "purchaseDate", column = "purchase_date"),
@Result(property = "status", column = "status"),
@Result(property = "remark", column = "remark"),
@Result(property = "createTime", column = "create_time"),
@Result(property = "updateTime", column = "update_time"),
@Result(property = "deviceType", column = "type_id",
one = @One(select = "cn.nopj.chaos_api.mapper.DriveTypeMapper.selectById"))
})
Device selectOneWithTypeById(Long id);
}

View File

@@ -0,0 +1,11 @@
package cn.nopj.chaos_api.mapper;
import cn.nopj.chaos_api.domain.entity.DeviceType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DriveTypeMapper extends BaseMapper<DeviceType> {
}

View File

@@ -1,6 +1,7 @@
package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@@ -22,4 +23,8 @@ public class Device {
private LocalDate purchaseDate;
private int status;
private String remark;
@TableField(exist = false)
private DeviceType deviceType;
}

View File

@@ -0,0 +1,18 @@
package cn.nopj.chaos_api.domain.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDate;
@Data
@TableName("t_device_type")
public class DeviceType {
private Long id;
private String name;
private String code;
private Long parentId;
private String remark;
private LocalDate createTime;
private LocalDate updateTime;
}

View File

@@ -0,0 +1,25 @@
package cn.nopj.chaos_api.dto.response;
import cn.nopj.chaos_api.domain.entity.DeviceType;
import lombok.Data;
import java.time.LocalDate;
@Data
public class DeviceTypeResponse {
private Long id;
private String name;
private String code;
private Long parentId;
private String remark;
private LocalDate createTime;
public DeviceTypeResponse(DeviceType type){
this.id = type.getId();
this.name = type.getName();
this.code = type.getCode();
this.parentId = type.getParentId();
this.remark = type.getRemark();
this.createTime = type.getCreateTime();
}
}

View File

@@ -10,4 +10,11 @@ public interface DeviceService {
* @return 新建设备信息结果
*/
DeviceResponse createDevice(CreateDriveRequest createDriveRequest);
/**
* 根据id查询设备信息
* @param id 设备id
* @return 设备信息
*/
DeviceResponse getDeviceById(Long id);
}

View File

@@ -0,0 +1,13 @@
package cn.nopj.chaos_api.service;
import cn.nopj.chaos_api.dto.response.DeviceTypeResponse;
import java.util.List;
public interface DeviceTypeService {
/**
* 获取所有设备类型
* @return 所有设备类型
*/
List<DeviceTypeResponse> getAllDeviceTypes();
}

View File

@@ -1,5 +1,6 @@
package cn.nopj.chaos_api.service.impl;
import cn.nopj.chaos_api.common.constants.ErrorCode;
import cn.nopj.chaos_api.common.exceotion.BizException;
import cn.nopj.chaos_api.domain.entity.Device;
import cn.nopj.chaos_api.dto.request.CreateDriveRequest;
@@ -37,4 +38,14 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
}
@Override
public DeviceResponse getDeviceById(Long id) {
Device device = this.baseMapper.selectOneWithTypeById(id);
if (device == null){
throw new BizException(ErrorCode.DEVICE_NOT_FOUND);
}
return new DeviceResponse(device);
}
}

View File

@@ -0,0 +1,25 @@
package cn.nopj.chaos_api.service.impl;
import cn.nopj.chaos_api.common.constants.ErrorCode;
import cn.nopj.chaos_api.common.exceotion.BizException;
import cn.nopj.chaos_api.domain.entity.DeviceType;
import cn.nopj.chaos_api.dto.response.DeviceTypeResponse;
import cn.nopj.chaos_api.mapper.DriveTypeMapper;
import cn.nopj.chaos_api.service.DeviceTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeviceTypeServiceImpl extends ServiceImpl<DriveTypeMapper, DeviceType> implements DeviceTypeService {
@Override
public List<DeviceTypeResponse> getAllDeviceTypes() {
List<DeviceType> deviceTypes = this.baseMapper.selectList(null);
if (deviceTypes == null || deviceTypes.isEmpty()){
throw new BizException(ErrorCode.DEVICE_TYPE_NOT_FOUND);
}
return deviceTypes.stream().map(DeviceTypeResponse::new).toList();
}
}

View File

@@ -32,4 +32,14 @@ public class DeviceController {
public ApiResult<DeviceResponse> createDevice(@RequestBody CreateDriveRequest createDriveRequest){
return ApiResult.success(deviceService.createDevice(createDriveRequest));
}
/**
* 查询指定设备信息
* @return 指定设备信息
*/
@RequestMapping("/get")
public ApiResult<DeviceResponse> getDeviceById(Long id){
return ApiResult.success(deviceService.getDeviceById(id));
}
}

View File

@@ -0,0 +1,35 @@
package cn.nopj.chaos_api.controller;
import cn.nopj.chaos_api.dto.response.DeviceTypeResponse;
import cn.nopj.chaos_api.model.ApiResult;
import cn.nopj.chaos_api.service.DeviceTypeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 设备类型管理
*
* @author nopj
*/
@RestController
@RequestMapping("/api/deviceType")
public class DeviceTypeController {
private final DeviceTypeService deviceTypeService;
public DeviceTypeController(DeviceTypeService deviceTypeService) {
this.deviceTypeService = deviceTypeService;
}
/**
* 获取所有设备类型
*
* @return 所有设备类型
*/
@RequestMapping("/all")
public ApiResult<List<DeviceTypeResponse>> getAllDeviceTypes() {
return ApiResult.success(deviceTypeService.getAllDeviceTypes());
}
}