feat(device): refactor device management API and implement CRUD operations

- Rename CreateDriveRequest to CreateDeviceRequest
- Add DeviceQueryRequest for pagination and filtering
- Add UpdateDeviceRequest for device updates
- Refactor DeviceController with RESTful endpoints
- Implement getAllDevices with pagination and search
- Implement createDevice endpoint
- Implement updateDevice endpoint
- Implement deleteDevice endpoint
- Remove real delete endpoint
- Add DeviceResponse and OptionResponse DTOs
- Update DeviceService interface and implementation
- Add device type options endpoint
- Update device type controller mappings
This commit is contained in:
Chaos
2025-11-27 17:11:57 +08:00
parent 79ef40bd34
commit af8959220a
11 changed files with 231 additions and 33 deletions

View File

@@ -3,31 +3,39 @@ 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;
import cn.nopj.chaos_api.dto.request.CreateDeviceRequest;
import cn.nopj.chaos_api.dto.request.DeviceQueryRequest;
import cn.nopj.chaos_api.dto.request.UpdateDeviceRequest;
import cn.nopj.chaos_api.dto.response.DeviceResponse;
import cn.nopj.chaos_api.mapper.DeviceMapper;
import cn.nopj.chaos_api.service.DeviceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Objects;
@Service
@Slf4j
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
@Override
@Transactional
public DeviceResponse createDevice(CreateDriveRequest createDriveRequest) {
public DeviceResponse createDevice(CreateDeviceRequest createDeviceRequest) {
Device device = new Device();
device.setName(createDriveRequest.getName());
device.setModel(createDriveRequest.getModel());
device.setTypeId(createDriveRequest.getTypeId());
device.setLocationId(createDriveRequest.getLocationId());
device.setSnmpCommunity(createDriveRequest.getSnmpCommunity());
device.setManufacturer(createDriveRequest.getManufacturer());
device.setPurchaseDate(createDriveRequest.getPurchaseDate());
device.setStatus(createDriveRequest.getStatus());
device.setRemark(createDriveRequest.getRemark());
device.setName(createDeviceRequest.getName());
device.setModel(createDeviceRequest.getModel());
device.setTypeId(createDeviceRequest.getTypeId());
device.setLocationId(createDeviceRequest.getLocationId());
device.setSnmpCommunity(createDeviceRequest.getSnmpCommunity());
device.setManufacturer(createDeviceRequest.getManufacturer());
device.setPurchaseDate(createDeviceRequest.getPurchaseDate());
device.setStatus(createDeviceRequest.getStatus());
device.setRemark(createDeviceRequest.getRemark());
int rows = this.baseMapper.insert(device);
if (rows > 0){
@@ -70,4 +78,86 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
}
log.info("删除设备成功");
}
public IPage<DeviceResponse> getAllDevices(DeviceQueryRequest request) {
if (request == null) {
request = new DeviceQueryRequest();
}
long current = Objects.requireNonNullElse(request.getPageNum(), 1);
long size = Objects.requireNonNullElse(request.getPageSize(), 10);
Page<Device> page = new Page<>(current, size);
LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(request.getType() != null && request.getType() > 0, Device::getTypeId, request.getType());
if (StringUtils.hasText(request.getKeyword())) {
String keyword = request.getKeyword();
wrapper.and(w -> w
.like(Device::getName, keyword)
.or()
.like(Device::getModel, keyword)
.or()
.like(Device::getManufacturer, keyword)
.or()
.like(Device::getRemark, keyword)
.or()
.like(Device::getSnmpCommunity, keyword)
);
}
IPage<Device> deviceIPage = this.baseMapper.selectPage(page, wrapper);
return deviceIPage.convert(DeviceResponse::new);
}
@Override
public DeviceResponse updateDevice(UpdateDeviceRequest updateDeviceRequest) {
Device device = new Device();
//判断各字段是否为空 不为空再更新
if (updateDeviceRequest.getId() != null){
device.setId(updateDeviceRequest.getId());
}
if (updateDeviceRequest.getName() != null){
device.setName(updateDeviceRequest.getName());
}
if (updateDeviceRequest.getModel() != null){
device.setModel(updateDeviceRequest.getModel());
}
if (updateDeviceRequest.getTypeId() != null){
device.setTypeId(updateDeviceRequest.getTypeId());
}
if (updateDeviceRequest.getLocationId() != null){
device.setLocationId(updateDeviceRequest.getLocationId());
}
if (updateDeviceRequest.getSnmpCommunity() != null){
device.setSnmpCommunity(updateDeviceRequest.getSnmpCommunity());
}
if (updateDeviceRequest.getManufacturer() != null){
device.setManufacturer(updateDeviceRequest.getManufacturer());
}
if (updateDeviceRequest.getPurchaseDate() != null){
device.setPurchaseDate(updateDeviceRequest.getPurchaseDate());
}
if (updateDeviceRequest.getStatus() >= 0 && updateDeviceRequest.getStatus() <= 2){
device.setStatus(updateDeviceRequest.getStatus());
}
if (updateDeviceRequest.getRemark() != null){
device.setRemark(updateDeviceRequest.getRemark());
}
int rows = this.baseMapper.updateById(device);
if (rows > 0){
return new DeviceResponse(device);
}else {
throw new BizException("更新设备失败");
}
}
}

View File

@@ -4,8 +4,10 @@ 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.dto.response.OptionResponse;
import cn.nopj.chaos_api.mapper.DriveTypeMapper;
import cn.nopj.chaos_api.service.DeviceTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@@ -22,4 +24,16 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DriveTypeMapper, DeviceTy
}
return deviceTypes.stream().map(DeviceTypeResponse::new).toList();
}
@Override
public List<OptionResponse> getDeviceTypeOptions() {
LambdaQueryWrapper<DeviceType> wrapper = new LambdaQueryWrapper<>();
return this.lambdaQuery()
.select(DeviceType::getId, DeviceType::getName)
//todo
;
}
}