feat(client): 按页面保存首页布局位置与隐藏状态
家属端 pages 改为包含 slot、position、hidden 的对象数组,移除 hiddenPages。家属端需同步更新。老人端专用读取接口返回可见分页顺序。
This commit is contained in:
1 parent
9a1ce783f2
commit
d9b923afc5
10 files changed
+195
-102
No files matched your search
@@ -1,12 +1,12 @@
|
||||
-- =============================================================================
|
||||
-- 设备首页分页布局表(sys_sn_home_layout)
|
||||
-- 说明:家属端可远程调整老人端(ElderlyDialer)桌面首页的分页顺序与默认页。
|
||||
-- page_order 为逗号分隔的页面槽位顺序,取值:
|
||||
-- page_order 为逗号分隔的固定分页顺序,隐藏页带 ! 前缀(如 home,!contact,settings):
|
||||
-- settings - 设置页
|
||||
-- contact - 联系人页
|
||||
-- home - 首页(天气/时间/应用宫格)
|
||||
-- apps - 应用页,数量随安装应用动态生成,恒排在最后,不参与排序
|
||||
-- default_page 为开机 / 按 Home 键时展示的页面,必须是 page_order 中的槽位。
|
||||
-- default_page 为开机 / 按 Home 键时展示的页面,必须是可见的固定分页。
|
||||
-- 无记录的设备按默认布局处理(settings,contact,home + home),
|
||||
-- 与历史版本行为保持一致。
|
||||
-- =============================================================================
|
||||
@@ -16,7 +16,7 @@ CREATE TABLE IF NOT EXISTS `sys_sn_home_layout`
|
||||
`serialno` VARCHAR(64) NOT NULL COMMENT '设备序列号(关联 sys_sn.serialno)',
|
||||
|
||||
-- ===================== 布局配置 =====================
|
||||
`page_order` VARCHAR(128) NOT NULL DEFAULT 'settings,contact,home' COMMENT '首页分页顺序(逗号分隔:settings/contact/home,apps 恒在最后)',
|
||||
`page_order` VARCHAR(128) NOT NULL DEFAULT 'settings,contact,home' COMMENT '固定分页顺序(逗号分隔,隐藏页带 ! 前缀)',
|
||||
`default_page` VARCHAR(32) NOT NULL DEFAULT 'home' COMMENT '默认展示页(home/contact/settings)',
|
||||
|
||||
-- ===================== 通用字段 =====================
|
||||
|
||||
@@ -915,7 +915,7 @@ public class ClientController {
|
||||
// ===================== 首页布局(家属端) =====================
|
||||
|
||||
@Operation(summary = "获取设备首页布局(家属端)",
|
||||
description = "返回首页分页顺序(pages)与默认展示页(defaultPage),未配置时返回默认布局")
|
||||
description = "返回全部固定分页及其位置和隐藏状态,以及默认展示页")
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping("/home-layout")
|
||||
public Result<DeviceHomeLayoutVO> getHomeLayout(
|
||||
@@ -933,7 +933,7 @@ public class ClientController {
|
||||
}
|
||||
|
||||
@Operation(summary = "设置设备首页布局(家属端)",
|
||||
description = "保存首页分页顺序与默认展示页,并推送设备(contentType = 31);设备收到后重建分页")
|
||||
description = "保存全部固定分页的位置、隐藏状态及默认展示页,并推送设备重建分页(contentType = 31)")
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@PutMapping("/home-layout")
|
||||
public Result<DeviceHomeLayoutVO> updateHomeLayout(
|
||||
@@ -945,7 +945,8 @@ public class ClientController {
|
||||
if (req == null) {
|
||||
return Result.failed("首页布局不能为空");
|
||||
}
|
||||
DeviceHomeLayoutVO vo = deviceHomeLayoutService.saveLayout(sn, req.getPages(), req.getDefaultPage());
|
||||
DeviceHomeLayoutVO vo = deviceHomeLayoutService.saveLayout(
|
||||
sn, req.getPages(), req.getDefaultPage());
|
||||
if (vo == null) {
|
||||
return Result.failed("首页布局保存失败");
|
||||
}
|
||||
@@ -955,8 +956,8 @@ public class ClientController {
|
||||
log.warn("updateHomeLayout saved but push failed, sn: {}", sn);
|
||||
}
|
||||
return Result.success(vo);
|
||||
} catch (BusinessException be) {
|
||||
return Result.failed(be.getMessage());
|
||||
} catch (BusinessException | IllegalArgumentException e) {
|
||||
return Result.failed(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("updateHomeLayout error, sn: {}", sn, e);
|
||||
return Result.failed("设置首页布局失败: " + e.getMessage());
|
||||
|
||||
@@ -699,14 +699,14 @@ public class MobileController {
|
||||
@Operation(summary = "获取设备首页布局配置",
|
||||
description = "返回首页分页顺序(pages,按展示先后)与默认展示页(defaultPage);未配置时返回默认布局")
|
||||
@GetMapping("/get_home_layout")
|
||||
public Result<DeviceHomeLayoutVO> getHomeLayout(
|
||||
public Result<DeviceHomeLayoutVO.DeviceView> getHomeLayout(
|
||||
@RequestHeader(value = "X-Device-SN") String sn
|
||||
) {
|
||||
try {
|
||||
if (sn == null || sn.trim().isEmpty()) {
|
||||
return Result.failed("设备序列号不能为空");
|
||||
}
|
||||
return Result.success(deviceHomeLayoutService.getBySn(sn.trim()));
|
||||
return Result.success(deviceHomeLayoutService.getBySn(sn.trim()).toDeviceView());
|
||||
} catch (Exception e) {
|
||||
logger.error("getHomeLayout error, sn: {}", sn, e);
|
||||
return Result.failed("获取首页布局失败: " + e.getMessage());
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.youlai.boot.device.model;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
@Schema(description = "首页布局中的固定分页")
|
||||
public record DeviceHomeLayoutPage(
|
||||
@NotNull @Schema(description = "分页槽位(home/contact/settings)") String slot,
|
||||
@NotNull @Schema(description = "从 0 开始的位置") Integer position,
|
||||
@NotNull @Schema(description = "是否隐藏") Boolean hidden
|
||||
) {
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public class SnDeviceHomeLayout extends BaseEntity {
|
||||
private String serialno;
|
||||
|
||||
/**
|
||||
* 首页分页顺序(逗号分隔:settings/contact/home;apps 恒在最后,不参与排序)
|
||||
* 固定分页顺序(逗号分隔,隐藏页带 ! 前缀)
|
||||
*/
|
||||
private String pageOrder;
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package com.youlai.boot.device.model.req;
|
||||
|
||||
import com.youlai.boot.device.model.DeviceHomeLayoutPage;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -15,16 +19,11 @@ import java.util.List;
|
||||
@Data
|
||||
public class DeviceHomeLayoutReq {
|
||||
|
||||
/**
|
||||
* 首页分页顺序,按展示先后排列;取值 home/contact/settings。
|
||||
* 非法值、重复项会被忽略;home 为必备项,缺失时自动补到首位。
|
||||
*/
|
||||
@Schema(description = "首页分页顺序(home/contact/settings)")
|
||||
private List<String> pages;
|
||||
@NotNull
|
||||
@Size(min = 3, max = 3)
|
||||
@Schema(description = "全部三个固定分页,每项包含 slot、position 和 hidden")
|
||||
private List<@NotNull @Valid DeviceHomeLayoutPage> pages;
|
||||
|
||||
/**
|
||||
* 默认展示页;不在 pages 中时回退为 home
|
||||
*/
|
||||
@Schema(description = "默认展示页(home/contact/settings)")
|
||||
private String defaultPage;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.youlai.boot.device.model.vo;
|
||||
|
||||
import com.youlai.boot.device.model.DeviceHomeLayoutPage;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -18,15 +19,20 @@ public class DeviceHomeLayoutVO {
|
||||
@Schema(description = "设备序列号")
|
||||
private String serialno;
|
||||
|
||||
/**
|
||||
* 首页分页顺序,按展示先后排列;取值 home/contact/settings
|
||||
*/
|
||||
@Schema(description = "首页分页顺序(home/contact/settings,按展示先后排列)")
|
||||
private List<String> pages;
|
||||
@Schema(description = "按位置排列的全部固定分页,包含隐藏页")
|
||||
private List<DeviceHomeLayoutPage> pages;
|
||||
|
||||
/**
|
||||
* 默认展示页,必须是 pages 中的一项
|
||||
*/
|
||||
@Schema(description = "默认展示页(home/contact/settings)")
|
||||
private String defaultPage;
|
||||
|
||||
public record DeviceView(String serialno, List<String> pages, String defaultPage) {
|
||||
}
|
||||
|
||||
public DeviceView toDeviceView() {
|
||||
List<String> visiblePages = pages.stream()
|
||||
.filter(page -> !page.hidden())
|
||||
.map(DeviceHomeLayoutPage::slot)
|
||||
.toList();
|
||||
return new DeviceView(serialno, visiblePages, defaultPage);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.youlai.boot.device.service;
|
||||
|
||||
import com.baomidou.mybatisplus.spring.service.IService;
|
||||
import com.youlai.boot.device.model.DeviceHomeLayoutPage;
|
||||
import com.youlai.boot.device.model.entity.SnDeviceHomeLayout;
|
||||
import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO;
|
||||
|
||||
@@ -39,7 +40,7 @@ public interface DeviceHomeLayoutService extends IService<SnDeviceHomeLayout> {
|
||||
* 查询设备首页布局配置;无记录时返回默认布局,不会返回 null。
|
||||
*
|
||||
* @param sn 设备序列号
|
||||
* @return 首页布局视图对象(pages 已规范化、defaultPage 一定包含其中)
|
||||
* @return 首页布局视图对象(包含全部固定分页及其位置和隐藏状态)
|
||||
*/
|
||||
DeviceHomeLayoutVO getBySn(String sn);
|
||||
|
||||
@@ -47,11 +48,11 @@ public interface DeviceHomeLayoutService extends IService<SnDeviceHomeLayout> {
|
||||
* 保存设备首页布局配置(按 SN 新增或更新)。
|
||||
*
|
||||
* @param sn 设备序列号
|
||||
* @param pages 首页分页顺序(可含非法值,内部会规范化)
|
||||
* @param pages 全部固定分页及其位置和隐藏状态
|
||||
* @param defaultPage 默认展示页(不在 pages 中时回退为 home)
|
||||
* @return 保存后的布局视图对象;参数异常或保存失败时返回 null
|
||||
*/
|
||||
DeviceHomeLayoutVO saveLayout(String sn, List<String> pages, String defaultPage);
|
||||
DeviceHomeLayoutVO saveLayout(String sn, List<DeviceHomeLayoutPage> pages, String defaultPage);
|
||||
|
||||
/**
|
||||
* 恢复设备首页布局为系统默认值(删除该 SN 的自定义配置)。
|
||||
|
||||
+56
-71
@@ -6,6 +6,7 @@ import cn.jiguang.sdk.bean.push.PushSendResult;
|
||||
import cn.jiguang.sdk.bean.push.message.custom.CustomMessage;
|
||||
import com.baomidou.mybatisplus.spring.service.impl.ServiceImpl;
|
||||
import com.youlai.boot.device.mapper.DeviceHomeLayoutMapper;
|
||||
import com.youlai.boot.device.model.DeviceHomeLayoutPage;
|
||||
import com.youlai.boot.device.model.entity.SnDeviceHomeLayout;
|
||||
import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO;
|
||||
import com.youlai.boot.device.service.DeviceHomeLayoutService;
|
||||
@@ -15,28 +16,21 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 设备首页分页布局服务实现
|
||||
*
|
||||
* <p>布局以「一个 SN 一行」存储({@code sys_sn_home_layout}),{@code page_order} 为逗号分隔的
|
||||
* 槽位顺序,{@code default_page} 为默认展示页。保存后通过 JPush 推送变更通知
|
||||
* (contentType = 31),设备端收到后主动拉取并重建分页。
|
||||
*
|
||||
* <p>规范化规则(服务端为唯一数据源,脏数据不得下发到设备):
|
||||
* <ul>
|
||||
* <li>只接受 home / contact / settings,其余(含 apps)忽略;</li>
|
||||
* <li>重复项去重;</li>
|
||||
* <li>home 为必备页,缺失时补到首位,避免设备端没有任何固定分页;</li>
|
||||
* <li>defaultPage 不在顺序中时回退为 home。</li>
|
||||
* </ul>
|
||||
* <p>每台设备存储全部固定分页及其顺序。隐藏分页在 page_order 中以 ! 开头,
|
||||
* 读取原有配置时,将缺失的固定分页作为隐藏页追加到末尾。
|
||||
*
|
||||
* @author TTSTD
|
||||
* @since 2026-11-25
|
||||
@@ -54,9 +48,7 @@ public class DeviceHomeLayoutServiceImpl
|
||||
private static final String PUSH_APP_KEY = "d779178d9900d4fb5d633678";
|
||||
private static final String PUSH_MASTER_SECRET = "be0e197d30fec7bec118a70d";
|
||||
|
||||
/** 可排序的固定分页槽位(apps 由设备端动态生成,恒在最后,不参与排序) */
|
||||
private static final List<String> SORTABLE_SLOTS =
|
||||
List.of(SLOT_HOME, SLOT_CONTACT, SLOT_SETTINGS);
|
||||
private static final String HIDDEN_PREFIX = "!";
|
||||
|
||||
private final PushApi pushApi = new PushApi.Builder()
|
||||
.setAppKey(PUSH_APP_KEY)
|
||||
@@ -70,21 +62,23 @@ public class DeviceHomeLayoutServiceImpl
|
||||
}
|
||||
SnDeviceHomeLayout layout = getRecordBySn(sn);
|
||||
if (layout == null) {
|
||||
return buildVo(sn, new ArrayList<>(DEFAULT_PAGES), DEFAULT_PAGE);
|
||||
return buildVo(sn, decodePages(String.join(",", DEFAULT_PAGES)), DEFAULT_PAGE);
|
||||
}
|
||||
List<String> pages = sanitizePages(splitOrder(layout.getPageOrder()));
|
||||
return buildVo(sn, pages, normalizeDefaultPage(layout.getDefaultPage(), pages));
|
||||
return buildVo(sn, decodePages(layout.getPageOrder()), layout.getDefaultPage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeviceHomeLayoutVO saveLayout(String sn, List<String> pages, String defaultPage) {
|
||||
public DeviceHomeLayoutVO saveLayout(String sn, List<DeviceHomeLayoutPage> pages, String defaultPage) {
|
||||
if (StringUtils.isBlank(sn)) {
|
||||
log.warn("首页布局保存失败, 设备序列号为空");
|
||||
return null;
|
||||
}
|
||||
List<String> order = sanitizePages(pages);
|
||||
String page = normalizeDefaultPage(defaultPage, order);
|
||||
String orderText = String.join(",", order);
|
||||
List<DeviceHomeLayoutPage> order = validateAndSort(pages);
|
||||
DeviceHomeLayoutVO result = buildVo(sn, order, defaultPage);
|
||||
String page = result.getDefaultPage();
|
||||
String orderText = order.stream()
|
||||
.map(item -> (item.hidden() ? HIDDEN_PREFIX : "") + item.slot())
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
SnDeviceHomeLayout existing = getRecordBySn(sn);
|
||||
boolean saved;
|
||||
@@ -106,7 +100,7 @@ public class DeviceHomeLayoutServiceImpl
|
||||
return null;
|
||||
}
|
||||
log.info("首页布局保存成功, sn: {}, order: {}, default: {}", sn, orderText, page);
|
||||
return buildVo(sn, order, page);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -122,7 +116,7 @@ public class DeviceHomeLayoutServiceImpl
|
||||
int removed = this.baseMapper.deleteBySnPhysically(sn);
|
||||
log.info("首页布局恢复默认: 删除旧配置 {} 行, sn: {}", removed, sn);
|
||||
log.info("首页布局已恢复默认, sn: {}", sn);
|
||||
return buildVo(sn, new ArrayList<>(DEFAULT_PAGES), DEFAULT_PAGE);
|
||||
return buildVo(sn, decodePages(String.join(",", DEFAULT_PAGES)), DEFAULT_PAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -153,66 +147,57 @@ public class DeviceHomeLayoutServiceImpl
|
||||
return rows.isEmpty() ? null : rows.get(0);
|
||||
}
|
||||
|
||||
private DeviceHomeLayoutVO buildVo(String sn, List<String> pages, String defaultPage) {
|
||||
private DeviceHomeLayoutVO buildVo(String sn, List<DeviceHomeLayoutPage> pages, String defaultPage) {
|
||||
DeviceHomeLayoutVO vo = new DeviceHomeLayoutVO();
|
||||
vo.setSerialno(sn);
|
||||
vo.setPages(pages);
|
||||
vo.setDefaultPage(defaultPage);
|
||||
String requestedDefault = StringUtils.trimToEmpty(defaultPage).toLowerCase(Locale.ROOT);
|
||||
boolean defaultVisible = pages.stream()
|
||||
.anyMatch(page -> page.slot().equals(requestedDefault) && !page.hidden());
|
||||
vo.setDefaultPage(defaultVisible ? requestedDefault : DEFAULT_PAGE);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把逗号分隔的顺序文本拆成列表(空值安全)。
|
||||
*/
|
||||
private List<String> splitOrder(String orderText) {
|
||||
if (StringUtils.isBlank(orderText)) {
|
||||
return new ArrayList<>();
|
||||
private List<DeviceHomeLayoutPage> validateAndSort(List<DeviceHomeLayoutPage> pages) {
|
||||
Set<String> slots = new HashSet<>();
|
||||
Set<Integer> positions = new HashSet<>();
|
||||
if (pages == null || pages.size() != DEFAULT_PAGES.size()) {
|
||||
throw new IllegalArgumentException("必须提交全部固定分页");
|
||||
}
|
||||
return Arrays.asList(orderText.split(","));
|
||||
}
|
||||
|
||||
/**
|
||||
* 规范化分页顺序:过滤非法槽位、去重、保证 home 存在。
|
||||
*
|
||||
* @param pages 原始顺序(可为 null / 含非法值)
|
||||
* @return 可安全下发给设备的顺序列表(保证非空且包含 home)
|
||||
*/
|
||||
private List<String> sanitizePages(List<String> pages) {
|
||||
List<String> order = new ArrayList<>(SORTABLE_SLOTS.size());
|
||||
if (pages != null) {
|
||||
for (String raw : pages) {
|
||||
if (StringUtils.isBlank(raw)) {
|
||||
continue;
|
||||
}
|
||||
String slot = raw.trim().toLowerCase(Locale.ROOT);
|
||||
if (!SORTABLE_SLOTS.contains(slot) || order.contains(slot)) {
|
||||
continue;
|
||||
}
|
||||
order.add(slot);
|
||||
for (DeviceHomeLayoutPage page : pages) {
|
||||
if (page == null || page.slot() == null || !DEFAULT_PAGES.contains(page.slot())
|
||||
|| !slots.add(page.slot()) || page.position() == null || page.position() < 0
|
||||
|| page.position() >= DEFAULT_PAGES.size() || !positions.add(page.position())
|
||||
|| page.hidden() == null || (SLOT_HOME.equals(page.slot()) && page.hidden())) {
|
||||
throw new IllegalArgumentException("首页布局的分页、位置或隐藏状态无效");
|
||||
}
|
||||
}
|
||||
if (order.isEmpty()) {
|
||||
return new ArrayList<>(DEFAULT_PAGES);
|
||||
}
|
||||
if (!order.contains(SLOT_HOME)) {
|
||||
// 首页承载天气/时间/应用宫格,不允许被隐藏
|
||||
order.add(0, SLOT_HOME);
|
||||
}
|
||||
return order;
|
||||
return pages.stream().sorted(Comparator.comparingInt(DeviceHomeLayoutPage::position)).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 规范化默认展示页:不在顺序中时回退为 home,再退化为顺序首项。
|
||||
*/
|
||||
private String normalizeDefaultPage(String defaultPage, List<String> pages) {
|
||||
String page = defaultPage == null ? "" : defaultPage.trim().toLowerCase(Locale.ROOT);
|
||||
if (pages.contains(page)) {
|
||||
return page;
|
||||
private List<DeviceHomeLayoutPage> decodePages(String orderText) {
|
||||
Map<String, Boolean> slots = new LinkedHashMap<>();
|
||||
if (StringUtils.isNotBlank(orderText)) {
|
||||
for (String raw : orderText.split(",")) {
|
||||
String token = raw.trim().toLowerCase(Locale.ROOT);
|
||||
boolean hidden = token.startsWith(HIDDEN_PREFIX);
|
||||
String slot = hidden ? token.substring(HIDDEN_PREFIX.length()) : token;
|
||||
if (DEFAULT_PAGES.contains(slot)) {
|
||||
slots.putIfAbsent(slot, hidden && !SLOT_HOME.equals(slot));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pages.contains(DEFAULT_PAGE)) {
|
||||
return DEFAULT_PAGE;
|
||||
if (slots.isEmpty()) {
|
||||
DEFAULT_PAGES.forEach(slot -> slots.put(slot, false));
|
||||
}
|
||||
return pages.isEmpty() ? DEFAULT_PAGE : pages.get(0);
|
||||
List<DeviceHomeLayoutPage> pages = new ArrayList<>(DEFAULT_PAGES.size());
|
||||
if (!slots.containsKey(SLOT_HOME)) {
|
||||
pages.add(new DeviceHomeLayoutPage(SLOT_HOME, 0, false));
|
||||
}
|
||||
DEFAULT_PAGES.stream().filter(slot -> !SLOT_HOME.equals(slot))
|
||||
.forEach(slot -> slots.putIfAbsent(slot, true));
|
||||
slots.forEach((slot, hidden) -> pages.add(new DeviceHomeLayoutPage(slot, pages.size(), hidden)));
|
||||
return pages;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.youlai.boot.device;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
import com.youlai.boot.device.model.DeviceHomeLayoutPage;
|
||||
import com.youlai.boot.device.model.entity.SnDeviceHomeLayout;
|
||||
import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO;
|
||||
import com.youlai.boot.device.service.impl.DeviceHomeLayoutServiceImpl;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class DeviceHomeLayoutServiceTest {
|
||||
|
||||
private final DeviceHomeLayoutServiceImpl service = spy(new DeviceHomeLayoutServiceImpl());
|
||||
private LambdaQueryChainWrapper<SnDeviceHomeLayout> query;
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings("unchecked")
|
||||
void setUp() {
|
||||
query = mock(LambdaQueryChainWrapper.class);
|
||||
when(query.eq(any(), eq("device-a"))).thenReturn(query);
|
||||
when(query.orderByAsc(ArgumentMatchers.<SFunction<SnDeviceHomeLayout, ?>>any())).thenReturn(query);
|
||||
doReturn(query).when(service).lambdaQuery();
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsHiddenPageInItsPositionAfterSavingAndLoading() {
|
||||
when(query.list()).thenReturn(List.of());
|
||||
doReturn(true).when(service).save(any(SnDeviceHomeLayout.class));
|
||||
DeviceHomeLayoutPage home = new DeviceHomeLayoutPage("home", 0, false);
|
||||
DeviceHomeLayoutPage contact = new DeviceHomeLayoutPage("contact", 1, true);
|
||||
DeviceHomeLayoutPage settings = new DeviceHomeLayoutPage("settings", 2, false);
|
||||
|
||||
DeviceHomeLayoutVO saved = service.saveLayout("device-a", List.of(settings, contact, home), "contact");
|
||||
|
||||
ArgumentCaptor<SnDeviceHomeLayout> stored = ArgumentCaptor.forClass(SnDeviceHomeLayout.class);
|
||||
verify(service).save(stored.capture());
|
||||
when(query.list()).thenReturn(List.of(stored.getValue()));
|
||||
|
||||
assertEquals(List.of(home, contact, settings), saved.getPages());
|
||||
DeviceHomeLayoutVO loaded = service.getBySn("device-a");
|
||||
assertEquals(saved, loaded);
|
||||
assertEquals("home", loaded.getDefaultPage());
|
||||
assertEquals(List.of("home", "settings"), loaded.toDeviceView().pages());
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsPreviouslySavedVisibleOrderAndAppendsMissingPagesAsHidden() {
|
||||
SnDeviceHomeLayout stored = new SnDeviceHomeLayout();
|
||||
stored.setPageOrder("settings,home");
|
||||
stored.setDefaultPage("settings");
|
||||
when(query.list()).thenReturn(List.of(stored));
|
||||
|
||||
DeviceHomeLayoutVO loaded = service.getBySn("device-a");
|
||||
|
||||
assertEquals(List.of(
|
||||
new DeviceHomeLayoutPage("settings", 0, false),
|
||||
new DeviceHomeLayoutPage("home", 1, false),
|
||||
new DeviceHomeLayoutPage("contact", 2, true)
|
||||
), loaded.getPages());
|
||||
assertEquals("settings", loaded.getDefaultPage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsDuplicatePositionsAndHiddenHome() {
|
||||
DeviceHomeLayoutPage home = new DeviceHomeLayoutPage("home", 0, false);
|
||||
DeviceHomeLayoutPage contact = new DeviceHomeLayoutPage("contact", 1, true);
|
||||
DeviceHomeLayoutPage settings = new DeviceHomeLayoutPage("settings", 2, false);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> service.saveLayout(
|
||||
"device-a", List.of(home, contact, new DeviceHomeLayoutPage("settings", 1, false)), "home"));
|
||||
assertThrows(IllegalArgumentException.class, () -> service.saveLayout(
|
||||
"device-a", List.of(new DeviceHomeLayoutPage("home", 0, true), contact, settings), "home"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user