优化布局

This commit is contained in:
supernijia
2026-07-24 13:26:37 +08:00
parent bca1335dc6
commit f1b7a7aae8
8 changed files with 192 additions and 43 deletions
@@ -152,6 +152,20 @@ public class ShopManageService {
return vo;
}
/**
* 查询匹配业务允许使用的后台店铺;不存在时返回统一中文业务错误。
*/
public ShopManageEntity requireShopByName(String shopName) {
String normalizedShopName = normalizeRequired(shopName, "店铺名称不能为空");
ShopManageEntity entity = shopManageMapper.selectOne(new LambdaQueryWrapper<ShopManageEntity>()
.eq(ShopManageEntity::getShopName, normalizedShopName)
.last("limit 1"));
if (entity == null) {
throw new BusinessException("后台店铺管理中未找到店铺:" + normalizedShopName + ",请先添加店铺信息");
}
return entity;
}
public String findMallNameByShopName(String shopName) {
String normalizedShopName = normalizeRequired(shopName, "shopName required");
ShopManageEntity entity = shopManageMapper.selectOne(new LambdaQueryWrapper<ShopManageEntity>()
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.ziniao.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.shopkey.service.ShopManageService;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -11,9 +12,10 @@ public class ZiniaoShopSwitchService {
private final ZiniaoAuthService ziniaoAuthService;
private final ZiniaoShopIndexService ziniaoShopIndexService;
private final ShopManageService shopManageService;
public ZiniaoShopMatchResultVo matchStoreByNameAcrossStaff(String targetShopName, Long preferUserId) {
String normalizedShopName = normalizeShopName(targetShopName);
String normalizedShopName = requireManagedShopName(targetShopName);
if (normalizedShopName.isBlank()) {
return emptyMatchResult();
}
@@ -31,11 +33,15 @@ public class ZiniaoShopSwitchService {
}
public ZiniaoShopMatchResultVo findIndexedStoreByName(String targetShopName) {
return ziniaoShopIndexService.findIndexedStoreByName(targetShopName);
return findIndexedStoreByName(targetShopName, true);
}
public ZiniaoShopMatchResultVo findIndexedStoreByName(String targetShopName, boolean buildOpenStoreUrl) {
return ziniaoShopIndexService.findIndexedStoreByName(targetShopName, buildOpenStoreUrl);
String normalizedShopName = requireManagedShopName(targetShopName);
if (normalizedShopName.isBlank()) {
return emptyMatchResult();
}
return ziniaoShopIndexService.findIndexedStoreByName(normalizedShopName, buildOpenStoreUrl);
}
public ZiniaoShopMatchResultVo emptyMatchResult() {
@@ -53,6 +59,14 @@ public class ZiniaoShopSwitchService {
return value.replace(" ", " ").trim();
}
private String requireManagedShopName(String targetShopName) {
String normalizedShopName = normalizeShopName(targetShopName);
if (!normalizedShopName.isBlank()) {
shopManageService.requireShopByName(normalizedShopName);
}
return normalizedShopName;
}
public boolean isSkippableMatchError(BusinessException ex) {
String message = ex == null ? null : ex.getMessage();
if (message == null || message.isBlank()) {
@@ -0,0 +1,50 @@
package com.nanri.aiimage.modules.shopkey.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
import com.nanri.aiimage.modules.shopkey.mapper.ShopManageMapper;
import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageEntity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class ShopManageServiceTest {
@Mock
private ShopManageMapper shopManageMapper;
@Mock
private ShopManageGroupService shopManageGroupService;
@Mock
private ShopCredentialCryptoService shopCredentialCryptoService;
@InjectMocks
private ShopManageService service;
@Test
void requireShopByNameReturnsManagedShop() {
ShopManageEntity entity = new ShopManageEntity();
entity.setShopName("测试店铺");
when(shopManageMapper.selectOne(any())).thenReturn(entity);
assertSame(entity, service.requireShopByName(" 测试店铺 "));
}
@Test
void requireShopByNameReturnsChineseErrorWhenMissing() {
when(shopManageMapper.selectOne(any())).thenReturn(null);
BusinessException exception = assertThrows(BusinessException.class,
() -> service.requireShopByName("缺失店铺"));
assertEquals("后台店铺管理中未找到店铺:缺失店铺,请先添加店铺信息", exception.getMessage());
}
}
@@ -0,0 +1,83 @@
package com.nanri.aiimage.modules.ziniao.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.shopkey.service.ShopManageService;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class ZiniaoShopSwitchServiceTest {
@Mock
private ZiniaoAuthService ziniaoAuthService;
@Mock
private ZiniaoShopIndexService ziniaoShopIndexService;
@Mock
private ShopManageService shopManageService;
@Test
void missingManagedShopStopsIndexedLookup() {
String message = "后台店铺管理中未找到店铺:缺失店铺,请先添加店铺信息";
when(shopManageService.requireShopByName("缺失店铺"))
.thenThrow(new BusinessException(message));
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
BusinessException exception = assertThrows(BusinessException.class,
() -> service.findIndexedStoreByName("缺失店铺", false));
assertEquals(message, exception.getMessage());
verifyNoInteractions(ziniaoShopIndexService);
}
@Test
void managedShopIsCheckedBeforeIndexedLookup() {
ZiniaoShopMatchResultVo expected = new ZiniaoShopMatchResultVo();
expected.setMatched(true);
when(ziniaoShopIndexService.findIndexedStoreByName("测试店铺", false)).thenReturn(expected);
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
ZiniaoShopMatchResultVo actual = service.findIndexedStoreByName(" 测试店铺 ", false);
assertSame(expected, actual);
InOrder order = inOrder(shopManageService, ziniaoShopIndexService);
order.verify(shopManageService).requireShopByName("测试店铺");
order.verify(ziniaoShopIndexService).findIndexedStoreByName("测试店铺", false);
}
@Test
void missingManagedShopStopsDirectStaffMatch() {
when(shopManageService.requireShopByName("缺失店铺"))
.thenThrow(new BusinessException("后台店铺管理中未找到店铺:缺失店铺,请先添加店铺信息"));
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
assertThrows(BusinessException.class,
() -> service.matchStoreByNameAcrossStaff("缺失店铺", 12L));
verifyNoInteractions(ziniaoAuthService);
}
@Test
void blankShopNameDoesNotQueryManagedShopOrZiniao() {
ZiniaoShopSwitchService service = new ZiniaoShopSwitchService(
ziniaoAuthService, ziniaoShopIndexService, shopManageService);
ZiniaoShopMatchResultVo result = service.findIndexedStoreByName("  ", false);
assertEquals(ZiniaoShopIndexService.MATCH_STATUS_PENDING, result.getMatchStatus());
assertEquals("店铺名为空,无法匹配索引", result.getMatchMessage());
verifyNoInteractions(shopManageService, ziniaoShopIndexService, ziniaoAuthService);
}
}