feat: 实现设备管理员获取设备列表接口
This commit is contained in:
parent
a89d452074
commit
bf973f3d28
@ -1,12 +1,21 @@
|
||||
package github.benjamin.equipreservebackend.constant;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 设备内部状态
|
||||
*/
|
||||
@Getter
|
||||
public enum DeviceStatus {
|
||||
|
||||
AVAILABLE,
|
||||
MAINTENANCE,
|
||||
DISABLED
|
||||
AVAILABLE("可用"),
|
||||
MAINTENANCE("维护中"),
|
||||
DISABLED("停用");
|
||||
|
||||
final String label;
|
||||
|
||||
DeviceStatus(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import github.benjamin.equipreservebackend.entity.Device;
|
||||
import github.benjamin.equipreservebackend.response.ResponseResult;
|
||||
import github.benjamin.equipreservebackend.service.DeviceService;
|
||||
import github.benjamin.equipreservebackend.service.ReservationService;
|
||||
import github.benjamin.equipreservebackend.vo.DeviceAdminVO;
|
||||
import github.benjamin.equipreservebackend.vo.DeviceUserVO;
|
||||
import github.benjamin.equipreservebackend.vo.TimeRangeVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -26,11 +27,11 @@ public class DeviceController {
|
||||
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@GetMapping
|
||||
public ResponseResult<Page<DeviceUserVO>> getDevices(@RequestParam(defaultValue = "1") Integer page,
|
||||
public ResponseResult<Page<DeviceUserVO>> getUserDevices(@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String name) {
|
||||
Page<Device> pageRequest = new Page<>(page, size);
|
||||
Page<DeviceUserVO> res = deviceService.getDeviceVO(pageRequest, name);
|
||||
Page<DeviceUserVO> res = deviceService.getUserDevices(pageRequest, name);
|
||||
return ResponseResult.success(res);
|
||||
}
|
||||
|
||||
@ -64,6 +65,17 @@ public class DeviceController {
|
||||
return ResponseResult.success(updatedDevice);
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('DEVICE_ADMIN')")
|
||||
@GetMapping("/{userId}")
|
||||
public ResponseResult<Page<DeviceAdminVO>> getAdminDevice(@PathVariable("userId") Long userId,
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
@RequestParam(required = false) String name) {
|
||||
|
||||
Page<Device> pageRequest = new Page<>(page, size);
|
||||
return ResponseResult.success(deviceService.getAdminDevice(pageRequest, userId, name));
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('DEVICE_ADMIN')")
|
||||
@PostMapping("/{id}/image")
|
||||
public ResponseResult<?> uploadImage(@PathVariable("id") Long id,
|
||||
|
@ -1,14 +1,16 @@
|
||||
package github.benjamin.equipreservebackend.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import github.benjamin.equipreservebackend.entity.Device;
|
||||
import github.benjamin.equipreservebackend.vo.DeviceAdminVO;
|
||||
import github.benjamin.equipreservebackend.vo.DeviceUserVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface DeviceService {
|
||||
Page<DeviceUserVO> getDeviceVO(Page<Device> pageRequest, String name);
|
||||
Page<DeviceUserVO> getUserDevices(Page<Device> pageRequest, String name);
|
||||
|
||||
void addDevice(Device device);
|
||||
|
||||
@ -17,4 +19,13 @@ public interface DeviceService {
|
||||
Device updateDevice(Device device);
|
||||
|
||||
String saveImage(Long id, MultipartFile image) throws IOException;
|
||||
|
||||
/**
|
||||
* 获取用户所属团队的所有设备
|
||||
* @param pageRequest
|
||||
* @param userId
|
||||
* @param name
|
||||
* @return 分页后的设备列表
|
||||
*/
|
||||
Page<DeviceAdminVO> getAdminDevice(Page<Device> pageRequest, Long userId, String name);
|
||||
}
|
||||
|
@ -7,13 +7,16 @@ import github.benjamin.equipreservebackend.constant.DeviceReservationState;
|
||||
import github.benjamin.equipreservebackend.constant.DeviceStatus;
|
||||
import github.benjamin.equipreservebackend.entity.Device;
|
||||
import github.benjamin.equipreservebackend.entity.Reservation;
|
||||
import github.benjamin.equipreservebackend.entity.User;
|
||||
import github.benjamin.equipreservebackend.exception.ApiException;
|
||||
import github.benjamin.equipreservebackend.mapper.DeviceMapper;
|
||||
import github.benjamin.equipreservebackend.mapper.UserMapper;
|
||||
import github.benjamin.equipreservebackend.response.ResponseCode;
|
||||
import github.benjamin.equipreservebackend.service.DeviceService;
|
||||
import github.benjamin.equipreservebackend.service.ReservationService;
|
||||
import github.benjamin.equipreservebackend.utils.FileUtil;
|
||||
import github.benjamin.equipreservebackend.utils.PageUtil;
|
||||
import github.benjamin.equipreservebackend.vo.DeviceAdminVO;
|
||||
import github.benjamin.equipreservebackend.vo.DeviceUserVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -26,6 +29,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -37,8 +41,10 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
|
||||
private final ReservationService reservationService;
|
||||
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public Page<DeviceUserVO> getDeviceVO(Page<Device> pageRequest, String name) {
|
||||
public Page<DeviceUserVO> getUserDevices(Page<Device> pageRequest, String name) {
|
||||
LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<Device>()
|
||||
.eq(Device::getStatus, DeviceStatus.AVAILABLE)
|
||||
.orderByAsc(Device::getName);
|
||||
@ -49,11 +55,15 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
List<Long> deviceIds = devices.getRecords().stream()
|
||||
.map(Device::getId)
|
||||
.toList();
|
||||
Page<DeviceUserVO> res = PageUtil.copyPage(devices);
|
||||
if (deviceIds.isEmpty()) {
|
||||
res.setRecords(new ArrayList<>());
|
||||
return res;
|
||||
}
|
||||
List<Reservation> reservations = reservationService.getApprovedReservationsByDeviceIds(deviceIds);
|
||||
List<DeviceUserVO> deviceUserVOS = devices.getRecords().stream()
|
||||
.map(device -> buildDeviceVO(device, reservations))
|
||||
.toList();
|
||||
Page<DeviceUserVO> res = PageUtil.copyPage(devices);
|
||||
res.setRecords(deviceUserVOS);
|
||||
return res;
|
||||
}
|
||||
@ -121,6 +131,21 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<DeviceAdminVO> getAdminDevice(Page<Device> pageRequest, Long userId, String name) {
|
||||
User user = userMapper.selectById(userId);
|
||||
LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<Device>()
|
||||
.eq(Device::getTeamId, user.getTeamId())
|
||||
.orderByAsc(Device::getName);
|
||||
if (StringUtils.hasText(name)) {
|
||||
wrapper.like(Device::getName, name);
|
||||
}
|
||||
Page<Device> devicePage = deviceMapper.selectPage(pageRequest, wrapper);
|
||||
Page<DeviceAdminVO> res = PageUtil.copyPage(devicePage);
|
||||
res.setRecords(devicePage.getRecords().stream().map(DeviceAdminVO::new).toList());
|
||||
return res;
|
||||
}
|
||||
|
||||
private DeviceUserVO buildDeviceVO(Device device, List<Reservation> reservations) {
|
||||
DeviceUserVO vo = new DeviceUserVO(device);
|
||||
// TODO 显示设备状态逻辑,根据客户需求修改
|
||||
|
@ -0,0 +1,29 @@
|
||||
package github.benjamin.equipreservebackend.vo;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import github.benjamin.equipreservebackend.constant.DeviceStatus;
|
||||
import github.benjamin.equipreservebackend.entity.Device;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class DeviceAdminVO {
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long deviceId;
|
||||
private String name;
|
||||
private String usageRequirement;
|
||||
private String location;
|
||||
private String imagePath;
|
||||
private String status;
|
||||
|
||||
public DeviceAdminVO(Device d) {
|
||||
this.deviceId = d.getId();
|
||||
this.name = d.getName();
|
||||
this.usageRequirement = d.getUsageRequirement();
|
||||
this.location = d.getLocation();
|
||||
this.imagePath = d.getImagePath();
|
||||
this.status = d.getStatus();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user