mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 15:13:55 +08:00
管理员控制层完善的查找全部设备的接口
--AdminController.java 完善查找全部设备接口 --DateUtils.java 编写了一个返回简短时间描述的方法,如刚刚,几分钟前 --DevicePageUserDTO.java 定义了需要什么查找参数 --DeviceService.java 定义了分页方法 --DeviceServiceImpl.java 实现了分页获取全部设备的方法 --ExtendPageData.java 扩展的分页对象,多返回一个页数 --UserShowDeviceListVO.java 定义了返回全部设备每个对象的字段
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package xiaozhi.common.page;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 扩展的分页对象
|
||||
* @author zjy
|
||||
* @since 2025-4-2
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "分页数据")
|
||||
public class ExtendPageData<T> implements Serializable {
|
||||
@Schema(description = "总记录数")
|
||||
private int totalCount;
|
||||
|
||||
@Schema(description = "页数")
|
||||
private int totalPage;
|
||||
|
||||
@Schema(description = "列表数据")
|
||||
private List<T> list;
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param list 列表数据
|
||||
* @param total 总记录数
|
||||
* @param page 页数
|
||||
*/
|
||||
public ExtendPageData(List<T> list, long total, long page) {
|
||||
this.list = list;
|
||||
this.totalCount = (int) total;
|
||||
this.totalPage = (int) page;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package xiaozhi.common.utils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
@@ -60,4 +63,39 @@ public class DateUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取简短的时间字符串:10秒前返回刚刚,多少秒前,几小时前,超过一周返回年月日时分秒
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String getShortTime(Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
// 将 Date 转换为 Instant
|
||||
LocalDateTime localDateTime = date.toInstant()
|
||||
// 获取系统默认时区
|
||||
.atZone(ZoneId.systemDefault())
|
||||
// 转换为 LocalDateTime
|
||||
.toLocalDateTime();
|
||||
// 当前时间
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
// 时间差,单位为秒
|
||||
long secondsBetween = ChronoUnit.SECONDS.between(localDateTime, now);
|
||||
|
||||
if (secondsBetween <= 10) {
|
||||
return "刚刚";
|
||||
} else if (secondsBetween < 60) {
|
||||
return secondsBetween + "秒前";
|
||||
} else if (secondsBetween < 60 * 60) {
|
||||
return secondsBetween / 60 + "分钟前";
|
||||
} else if (secondsBetween < 86400) {
|
||||
return secondsBetween / 3600 + "小时前";
|
||||
} else if (secondsBetween < 604800) {
|
||||
return secondsBetween / 86400 + "天前";
|
||||
} else {
|
||||
// 超过一周,显示完整日期时间
|
||||
return format(date,DATE_TIME_PATTERN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package xiaozhi.modules.device.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 查询所有设备的DTO
|
||||
*
|
||||
* @author zjy
|
||||
* @since 2025-3-21
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "查询所有设备的DTO")
|
||||
public class DevicePageUserDTO {
|
||||
|
||||
@Schema(description = "设备关键词")
|
||||
private String keywords;
|
||||
|
||||
@Schema(description = "页数")
|
||||
private String page;
|
||||
|
||||
@Schema(description = "显示列数")
|
||||
private String limit;
|
||||
}
|
||||
@@ -2,10 +2,13 @@ package xiaozhi.modules.device.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import xiaozhi.common.page.ExtendPageData;
|
||||
import xiaozhi.modules.device.dto.DeviceBindDTO;
|
||||
import xiaozhi.modules.device.dto.DevicePageUserDTO;
|
||||
import xiaozhi.modules.device.dto.DeviceReportReqDTO;
|
||||
import xiaozhi.modules.device.dto.DeviceReportRespDTO;
|
||||
import xiaozhi.modules.device.entity.DeviceEntity;
|
||||
import xiaozhi.modules.device.vo.UserShowDeviceListVO;
|
||||
|
||||
public interface DeviceService {
|
||||
|
||||
@@ -52,4 +55,12 @@ public interface DeviceService {
|
||||
* @return 设备数量
|
||||
*/
|
||||
Long selectCountByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 分页获取全部设备信息
|
||||
*
|
||||
* @param dto 分页查找参数
|
||||
* @return 用户列表分页数据
|
||||
*/
|
||||
ExtendPageData<UserShowDeviceListVO> page(DevicePageUserDTO dto);
|
||||
}
|
||||
+42
-4
@@ -8,6 +8,7 @@ import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -18,33 +19,43 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.page.ExtendPageData;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.service.impl.BaseServiceImpl;
|
||||
import xiaozhi.common.user.UserDetail;
|
||||
import xiaozhi.common.utils.ConvertUtils;
|
||||
import xiaozhi.common.utils.DateUtils;
|
||||
import xiaozhi.modules.device.dao.DeviceDao;
|
||||
import xiaozhi.modules.device.dto.DeviceBindDTO;
|
||||
import xiaozhi.modules.device.dto.DevicePageUserDTO;
|
||||
import xiaozhi.modules.device.dto.DeviceReportReqDTO;
|
||||
import xiaozhi.modules.device.dto.DeviceReportRespDTO;
|
||||
import xiaozhi.modules.device.entity.DeviceEntity;
|
||||
import xiaozhi.modules.device.service.DeviceService;
|
||||
import xiaozhi.modules.device.vo.UserShowDeviceListVO;
|
||||
import xiaozhi.modules.security.user.SecurityUser;
|
||||
import xiaozhi.modules.sys.service.SysUserService;
|
||||
|
||||
@Service
|
||||
public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity> implements DeviceService {
|
||||
|
||||
private final DeviceDao deviceDao;
|
||||
|
||||
private final SysUserService sysUserService;
|
||||
|
||||
private final String frontedUrl;
|
||||
|
||||
|
||||
private final RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
// 添加构造函数来初始化 deviceMapper
|
||||
public DeviceServiceImpl(DeviceDao deviceDao,
|
||||
@Value("${app.fronted-url:http://localhost:8001}") String frontedUrl,
|
||||
RedisTemplate<String, Object> redisTemplate) {
|
||||
public DeviceServiceImpl(DeviceDao deviceDao, SysUserService sysUserService,
|
||||
@Value("${app.fronted-url:http://localhost:8001}") String frontedUrl,
|
||||
RedisTemplate<String, Object> redisTemplate) {
|
||||
this.deviceDao = deviceDao;
|
||||
this.sysUserService = sysUserService;
|
||||
this.frontedUrl = frontedUrl;
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
@@ -212,6 +223,33 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
||||
return baseDao.selectCount(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExtendPageData<UserShowDeviceListVO> page(DevicePageUserDTO dto) {
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
params.put(Constant.PAGE, dto.getPage());
|
||||
params.put(Constant.LIMIT, dto.getLimit());
|
||||
IPage<DeviceEntity> page = baseDao.selectPage(
|
||||
getPage(params, "sort", true),
|
||||
// 定义查询条件
|
||||
new QueryWrapper<DeviceEntity>()
|
||||
// 必须设备关键词查找
|
||||
.like(StringUtils.isNotBlank(dto.getKeywords()), "alias", dto.getKeywords()));
|
||||
// 循环处理page获取回来的数据,返回需要的字段
|
||||
List<UserShowDeviceListVO> list = page.getRecords().stream().map(device -> {
|
||||
UserShowDeviceListVO vo = ConvertUtils.sourceToTarget(device, UserShowDeviceListVO.class);
|
||||
// 把最后修改的时间,改为简短描述的时间
|
||||
vo.setRecentChatTime(DateUtils.getShortTime(device.getUpdateDate()));
|
||||
// 获取用户名
|
||||
String username = sysUserService.getByUserId(device.getUserId()).getUsername();
|
||||
vo.setBindUserName(username);
|
||||
vo.setDeviceType(device.getBoard());
|
||||
return vo;
|
||||
}).toList();
|
||||
//计算页数
|
||||
long num = page.getTotal() / Long.parseLong(dto.getPage());
|
||||
return new ExtendPageData<>(list, page.getTotal(), num);
|
||||
}
|
||||
|
||||
private DeviceReportRespDTO.ServerTime buildServerTime() {
|
||||
DeviceReportRespDTO.ServerTime serverTime = new DeviceReportRespDTO.ServerTime();
|
||||
TimeZone tz = TimeZone.getDefault();
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package xiaozhi.modules.device.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Schema(description = "用户显示设备列表VO")
|
||||
public class UserShowDeviceListVO {
|
||||
|
||||
@Schema(description = "app版本")
|
||||
private String appVersion;
|
||||
|
||||
@Schema(description = "绑定用户名称")
|
||||
private String bindUserName;
|
||||
|
||||
@Schema(description = "设备型号")
|
||||
private String deviceType;
|
||||
|
||||
@Schema(description = "设备唯一标识符")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "mac地址")
|
||||
private String macAddress;
|
||||
|
||||
@Schema(description = "开启OTA")
|
||||
private Integer otaUpgrade;
|
||||
|
||||
@Schema(description = "最近对话时间")
|
||||
private String recentChatTime;
|
||||
|
||||
}
|
||||
@@ -17,9 +17,13 @@ import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.AllArgsConstructor;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.page.ExtendPageData;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.utils.Result;
|
||||
import xiaozhi.common.validator.ValidatorUtils;
|
||||
import xiaozhi.modules.device.dto.DevicePageUserDTO;
|
||||
import xiaozhi.modules.device.service.DeviceService;
|
||||
import xiaozhi.modules.device.vo.UserShowDeviceListVO;
|
||||
import xiaozhi.modules.sys.dto.AdminPageUserDTO;
|
||||
import xiaozhi.modules.sys.service.SysUserService;
|
||||
import xiaozhi.modules.sys.vo.AdminPageUserVO;
|
||||
@@ -37,6 +41,8 @@ import xiaozhi.modules.sys.vo.AdminPageUserVO;
|
||||
public class AdminController {
|
||||
private final SysUserService sysUserService;
|
||||
|
||||
private final DeviceService deviceService;
|
||||
|
||||
@GetMapping("/users")
|
||||
@Operation(summary = "分页查找用户")
|
||||
@RequiresPermissions("sys:role:superAdmin")
|
||||
@@ -82,9 +88,13 @@ public class AdminController {
|
||||
@Parameter(name = Constant.PAGE, description = "当前页码,从1开始", required = true),
|
||||
@Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
|
||||
})
|
||||
public Result<Void> pageDevice(
|
||||
public Result<ExtendPageData<UserShowDeviceListVO>> pageDevice(
|
||||
@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
|
||||
// TODO 等设备功能模块写好
|
||||
return new Result<Void>().error(600, "等设备功能模块写好");
|
||||
DevicePageUserDTO dto = new DevicePageUserDTO();
|
||||
dto.setKeywords((String) params.get("keywords"));
|
||||
dto.setLimit((String) params.get(Constant.LIMIT));
|
||||
dto.setPage((String) params.get(Constant.PAGE));
|
||||
ExtendPageData<UserShowDeviceListVO> page = deviceService.page(dto);
|
||||
return new Result<ExtendPageData<UserShowDeviceListVO>>().ok(page);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user