修复:修复标签店铺绑定与作品优惠券选择
This commit is contained in:
@@ -270,6 +270,7 @@ export interface Tag {
|
|||||||
bgColor?: string
|
bgColor?: string
|
||||||
borderColor?: string
|
borderColor?: string
|
||||||
shopId?: number
|
shopId?: number
|
||||||
|
shopName?: string
|
||||||
createTime: string
|
createTime: string
|
||||||
updateTime: string
|
updateTime: string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<template v-else-if="column.key === 'shop'">
|
<template v-else-if="column.key === 'shop'">
|
||||||
<span>{{ getShopName(record.shopId) }}</span>
|
<span>{{ record.shopName || getShopName(record.shopId) }}</span>
|
||||||
</template>
|
</template>
|
||||||
<template v-else-if="column.key === 'price'">
|
<template v-else-if="column.key === 'price'">
|
||||||
<span style="color: #cf1322; font-weight: 600;">
|
<span style="color: #cf1322; font-weight: 600;">
|
||||||
@@ -234,7 +234,7 @@
|
|||||||
placeholder="请选择优惠券"
|
placeholder="请选择优惠券"
|
||||||
style="width: 100%"
|
style="width: 100%"
|
||||||
:loading="loadingCoupons"
|
:loading="loadingCoupons"
|
||||||
:disabled="!isShopOwner()"
|
:disabled="isCouponSelectionDisabled"
|
||||||
>
|
>
|
||||||
<a-select-option
|
<a-select-option
|
||||||
v-if="coupons.length === 0"
|
v-if="coupons.length === 0"
|
||||||
@@ -251,6 +251,24 @@
|
|||||||
{{ coupon?.couponName || '' }}
|
{{ coupon?.couponName || '' }}
|
||||||
</a-select-option>
|
</a-select-option>
|
||||||
</a-select>
|
</a-select>
|
||||||
|
<a-space class="coupon-actions">
|
||||||
|
<a-button
|
||||||
|
type="link"
|
||||||
|
size="small"
|
||||||
|
:disabled="isCouponSelectionDisabled || coupons.length === 0"
|
||||||
|
@click="handleSelectAllCoupons"
|
||||||
|
>
|
||||||
|
一键全选
|
||||||
|
</a-button>
|
||||||
|
<a-button
|
||||||
|
type="link"
|
||||||
|
size="small"
|
||||||
|
:disabled="isCouponSelectionDisabled || formState.availableCoupons.length === 0"
|
||||||
|
@click="handleClearAvailableCoupons"
|
||||||
|
>
|
||||||
|
清空
|
||||||
|
</a-button>
|
||||||
|
</a-space>
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
</template>
|
</template>
|
||||||
</a-form>
|
</a-form>
|
||||||
@@ -469,10 +487,18 @@ const columns = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
const getShopName = (shopId: number): string => {
|
const getShopName = (shopId: number): string => {
|
||||||
const shop = shops.value.find(s => s.id === shopId)
|
const normalizedShopId = normalizeShopIdValue(shopId)
|
||||||
|
const shop = shops.value.find(s => normalizeShopIdValue(s.id) === normalizedShopId)
|
||||||
return shop ? shop.shopName : '未知店铺'
|
return shop ? shop.shopName : '未知店铺'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const normalizeShopIdValue = (value: string | number | undefined | null) => {
|
||||||
|
if (value === undefined || value === null || value === '') {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
return String(value)
|
||||||
|
}
|
||||||
|
|
||||||
const getCurrentUserInfo = () => {
|
const getCurrentUserInfo = () => {
|
||||||
try {
|
try {
|
||||||
const userInfoStr = localStorage.getItem('adminUserInfo')
|
const userInfoStr = localStorage.getItem('adminUserInfo')
|
||||||
@@ -488,11 +514,13 @@ const getCurrentUserInfo = () => {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const isShopOwner = () => {
|
const canManageAvailableCoupons = () => {
|
||||||
const userInfo = getCurrentUserInfo()
|
const userInfo = getCurrentUserInfo()
|
||||||
return userInfo && userInfo.type === 2
|
return Boolean(userInfo && (userInfo.type === 2 || userInfo.type === 4))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isCouponSelectionDisabled = computed(() => !canManageAvailableCoupons())
|
||||||
|
|
||||||
const fetchShops = async () => {
|
const fetchShops = async () => {
|
||||||
loadingShops.value = true
|
loadingShops.value = true
|
||||||
try {
|
try {
|
||||||
@@ -556,12 +584,29 @@ const fetchArtists = async (shopId?: number) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchCoupons = async () => {
|
const syncAvailableCouponSelection = () => {
|
||||||
|
const availableCouponIdSet = new Set(
|
||||||
|
coupons.value
|
||||||
|
.map(coupon => coupon?.id)
|
||||||
|
.filter((id): id is number => id !== undefined && id !== null)
|
||||||
|
.map(id => String(id))
|
||||||
|
)
|
||||||
|
formState.availableCoupons = formState.availableCoupons.filter(couponId => availableCouponIdSet.has(String(couponId)))
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchCoupons = async (shopId?: number) => {
|
||||||
|
if (!shopId) {
|
||||||
|
coupons.value = []
|
||||||
|
formState.availableCoupons = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
loadingCoupons.value = true
|
loadingCoupons.value = true
|
||||||
try {
|
try {
|
||||||
const response = await adminCouponApi.listAllCoupons()
|
const response = await adminCouponApi.listAllCoupons(shopId)
|
||||||
const res = typeof response === 'object' && 'data' in response ? response.data : response
|
const res = typeof response === 'object' && 'data' in response ? response.data : response
|
||||||
coupons.value = (res as any[]) || []
|
coupons.value = (res as any[]) || []
|
||||||
|
syncAvailableCouponSelection()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取优惠券列表失败', error)
|
console.error('获取优惠券列表失败', error)
|
||||||
message.error('获取优惠券列表失败')
|
message.error('获取优惠券列表失败')
|
||||||
@@ -669,6 +714,7 @@ const showAddModal = async () => {
|
|||||||
// 如果有店铺ID,获取该店铺的美甲师列表
|
// 如果有店铺ID,获取该店铺的美甲师列表
|
||||||
if (shopId) {
|
if (shopId) {
|
||||||
fetchArtists(shopId)
|
fetchArtists(shopId)
|
||||||
|
fetchCoupons(shopId)
|
||||||
}
|
}
|
||||||
await restoreDraft()
|
await restoreDraft()
|
||||||
modalVisible.value = true
|
modalVisible.value = true
|
||||||
@@ -721,6 +767,7 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
|||||||
// 获取该店铺的美甲师列表
|
// 获取该店铺的美甲师列表
|
||||||
if (recordShopId) {
|
if (recordShopId) {
|
||||||
await fetchArtists(recordShopId)
|
await fetchArtists(recordShopId)
|
||||||
|
await fetchCoupons(recordShopId)
|
||||||
}
|
}
|
||||||
await restoreDraft()
|
await restoreDraft()
|
||||||
|
|
||||||
@@ -785,6 +832,7 @@ const showViewModal = async (record: Product | Record<string, any>) => {
|
|||||||
// 获取该店铺的美甲师列表
|
// 获取该店铺的美甲师列表
|
||||||
if (recordShopId) {
|
if (recordShopId) {
|
||||||
await fetchArtists(recordShopId)
|
await fetchArtists(recordShopId)
|
||||||
|
await fetchCoupons(recordShopId)
|
||||||
}
|
}
|
||||||
|
|
||||||
viewModalVisible.value = true
|
viewModalVisible.value = true
|
||||||
@@ -1097,11 +1145,24 @@ const handleDelete = async (record: Product | Record<string, any>) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleSelectAllCoupons = () => {
|
||||||
|
if (isCouponSelectionDisabled.value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
formState.availableCoupons = coupons.value
|
||||||
|
.map(coupon => coupon?.id)
|
||||||
|
.filter((id): id is number => id !== undefined && id !== null)
|
||||||
|
.map(id => String(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleClearAvailableCoupons = () => {
|
||||||
|
formState.availableCoupons = []
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await loadUserPermissions()
|
await loadUserPermissions()
|
||||||
fetchShops()
|
fetchShops()
|
||||||
fetchTags()
|
fetchTags()
|
||||||
fetchCoupons()
|
|
||||||
fetchProductList()
|
fetchProductList()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1110,9 +1171,12 @@ watch(() => formState.shopId, (newShopId) => {
|
|||||||
if (newShopId) {
|
if (newShopId) {
|
||||||
fetchTags(newShopId)
|
fetchTags(newShopId)
|
||||||
fetchArtists(newShopId)
|
fetchArtists(newShopId)
|
||||||
|
fetchCoupons(newShopId)
|
||||||
} else {
|
} else {
|
||||||
tags.value = []
|
tags.value = []
|
||||||
artists.value = []
|
artists.value = []
|
||||||
|
coupons.value = []
|
||||||
|
formState.availableCoupons = []
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
@@ -1146,4 +1210,8 @@ watch(() => formState.shopId, (newShopId) => {
|
|||||||
line-height: 1.6;
|
line-height: 1.6;
|
||||||
margin-top: 6px;
|
margin-top: 6px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.coupon-actions {
|
||||||
|
margin-top: 6px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -42,7 +42,7 @@
|
|||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
||||||
<template v-else-if="column.key === 'shop'">
|
<template v-else-if="column.key === 'shop'">
|
||||||
<span>{{ getShopName(record.shopId) }}</span>
|
<span>{{ record.shopName || getShopName(record.shopId) }}</span>
|
||||||
</template>
|
</template>
|
||||||
<template v-else-if="column.key === 'actions'">
|
<template v-else-if="column.key === 'actions'">
|
||||||
<a-space>
|
<a-space>
|
||||||
@@ -333,8 +333,16 @@ const fetchShops = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getShopName = (shopId: number) => {
|
const normalizeShopIdValue = (value: string | number | undefined | null) => {
|
||||||
const shop = shops.value.find(s => s.id === shopId)
|
if (value === undefined || value === null || value === '') {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
return String(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
const getShopName = (shopId: number | string | undefined) => {
|
||||||
|
const normalizedShopId = normalizeShopIdValue(shopId)
|
||||||
|
const shop = shops.value.find(s => normalizeShopIdValue(s.id) === normalizedShopId)
|
||||||
return shop ? shop.shopName : '未知店铺'
|
return shop ? shop.shopName : '未知店铺'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -419,8 +427,9 @@ const handleModalOk = async () => {
|
|||||||
handleClearDraft(false)
|
handleClearDraft(false)
|
||||||
modalVisible.value = false
|
modalVisible.value = false
|
||||||
fetchTags()
|
fetchTags()
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error('Error:', error)
|
console.error('Error:', error)
|
||||||
|
message.error(error?.message || (isEdit.value ? '更新失败' : '创建失败'))
|
||||||
} finally {
|
} finally {
|
||||||
modalLoading.value = false
|
modalLoading.value = false
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-4
@@ -107,13 +107,14 @@ public class AdminCouponController {
|
|||||||
*/
|
*/
|
||||||
@GetMapping("/list/all")
|
@GetMapping("/list/all")
|
||||||
@RequirePermission("coupon:view")
|
@RequirePermission("coupon:view")
|
||||||
public Result<List<CouponVO>> listAllCoupons(HttpServletRequest request) {
|
public Result<List<CouponVO>> listAllCoupons(HttpServletRequest request, @RequestParam(required = false) Long shopId) {
|
||||||
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
|
||||||
Long shopId = adminScopeHelper.resolveShopScope(currentUser, null);
|
shopId = adminScopeHelper.resolveShopScope(currentUser, shopId);
|
||||||
List<CouponVO> coupons = couponService.listAllCoupons();
|
List<CouponVO> coupons = couponService.listAllCoupons();
|
||||||
if (shopId != null) {
|
Long resolvedShopId = shopId;
|
||||||
|
if (resolvedShopId != null) {
|
||||||
coupons = coupons.stream()
|
coupons = coupons.stream()
|
||||||
.filter(coupon -> shopId.equals(coupon.getShopId()))
|
.filter(coupon -> resolvedShopId.equals(coupon.getShopId()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
return Result.success(coupons);
|
return Result.success(coupons);
|
||||||
|
|||||||
+80
-15
@@ -5,9 +5,15 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.xiaowang.yuminailcore.dto.ProductDTO;
|
import com.xiaowang.yuminailcore.dto.ProductDTO;
|
||||||
|
import com.xiaowang.yuminailcore.entity.CouponDO;
|
||||||
import com.xiaowang.yuminailcore.entity.ProductDO;
|
import com.xiaowang.yuminailcore.entity.ProductDO;
|
||||||
|
import com.xiaowang.yuminailcore.entity.ShopDO;
|
||||||
|
import com.xiaowang.yuminailcore.entity.TagDO;
|
||||||
import com.xiaowang.yuminailcore.entity.UserDO;
|
import com.xiaowang.yuminailcore.entity.UserDO;
|
||||||
|
import com.xiaowang.yuminailcore.mapper.CouponMapper;
|
||||||
import com.xiaowang.yuminailcore.mapper.ProductMapper;
|
import com.xiaowang.yuminailcore.mapper.ProductMapper;
|
||||||
|
import com.xiaowang.yuminailcore.mapper.ShopMapper;
|
||||||
|
import com.xiaowang.yuminailcore.mapper.TagMapper;
|
||||||
import com.xiaowang.yuminailcore.mapper.UserMapper;
|
import com.xiaowang.yuminailcore.mapper.UserMapper;
|
||||||
import com.xiaowang.yuminailcore.service.ObjectStorageFileService;
|
import com.xiaowang.yuminailcore.service.ObjectStorageFileService;
|
||||||
import com.xiaowang.yuminailcore.service.ProductService;
|
import com.xiaowang.yuminailcore.service.ProductService;
|
||||||
@@ -23,7 +29,9 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,11 +49,15 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
|
|||||||
private final TagService tagService;
|
private final TagService tagService;
|
||||||
private final ObjectStorageFileService objectStorageFileService;
|
private final ObjectStorageFileService objectStorageFileService;
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
|
private final CouponMapper couponMapper;
|
||||||
|
private final ShopMapper shopMapper;
|
||||||
|
private final TagMapper tagMapper;
|
||||||
private final UserShopBindingSupport userShopBindingSupport;
|
private final UserShopBindingSupport userShopBindingSupport;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long createProduct(ProductDTO productDTO) {
|
public Long createProduct(ProductDTO productDTO) {
|
||||||
validateBookableArtists(productDTO.getShopId(), productDTO.getBookableArtists());
|
validateBookableArtists(productDTO.getShopId(), productDTO.getBookableArtists());
|
||||||
|
validateAvailableCoupons(productDTO.getShopId(), productDTO.getAvailableCoupons());
|
||||||
ProductDO productDO = new ProductDO();
|
ProductDO productDO = new ProductDO();
|
||||||
productDO.setShopId(productDTO.getShopId());
|
productDO.setShopId(productDTO.getShopId());
|
||||||
productDO.setTitle(productDTO.getTitle());
|
productDO.setTitle(productDTO.getTitle());
|
||||||
@@ -68,6 +80,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
validateBookableArtists(productDTO.getShopId(), productDTO.getBookableArtists());
|
validateBookableArtists(productDTO.getShopId(), productDTO.getBookableArtists());
|
||||||
|
validateAvailableCoupons(productDTO.getShopId(), productDTO.getAvailableCoupons());
|
||||||
productDO.setShopId(productDTO.getShopId());
|
productDO.setShopId(productDTO.getShopId());
|
||||||
productDO.setTitle(productDTO.getTitle());
|
productDO.setTitle(productDTO.getTitle());
|
||||||
productDO.setDescription(productDTO.getDescription());
|
productDO.setDescription(productDTO.getDescription());
|
||||||
@@ -134,28 +147,25 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ProductVO> listProductsByTag(String tag) {
|
public List<ProductVO> listProductsByTag(String tag) {
|
||||||
// 先根据标签名称查找标签ID
|
List<TagDO> matchedTags = tagMapper.selectList(
|
||||||
TagVO tagVO = tagService.getTagByTagName(tag);
|
Wrappers.lambdaQuery(TagDO.class)
|
||||||
if (tagVO == null) {
|
.eq(TagDO::getIsDelete, 0)
|
||||||
|
.eq(TagDO::getTagName, tag)
|
||||||
|
);
|
||||||
|
if (matchedTags.isEmpty()) {
|
||||||
return new java.util.ArrayList<>();
|
return new java.util.ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用标签ID查询商品,只完整匹配标签ID
|
Set<String> matchedTagIds = matchedTags.stream()
|
||||||
String tagIdStr = String.valueOf(tagVO.getId());
|
.map(TagDO::getId)
|
||||||
|
.map(String::valueOf)
|
||||||
|
.collect(Collectors.toCollection(HashSet::new));
|
||||||
List<ProductDO> productDOs = productMapper.selectList(
|
List<ProductDO> productDOs = productMapper.selectList(
|
||||||
Wrappers.lambdaQuery(ProductDO.class)
|
Wrappers.lambdaQuery(ProductDO.class)
|
||||||
.eq(ProductDO::getIsDelete, 0)
|
.eq(ProductDO::getIsDelete, 0)
|
||||||
.and(wrapper -> wrapper
|
|
||||||
.eq(ProductDO::getTags, tagIdStr)
|
|
||||||
.or()
|
|
||||||
.likeRight(ProductDO::getTags, tagIdStr + ";")
|
|
||||||
.or()
|
|
||||||
.likeLeft(ProductDO::getTags, ";" + tagIdStr)
|
|
||||||
.or()
|
|
||||||
.like(ProductDO::getTags, ";" + tagIdStr + ";")
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
return productDOs.stream()
|
return productDOs.stream()
|
||||||
|
.filter(productDO -> containsAnyTagId(productDO.getTags(), matchedTagIds))
|
||||||
.map(this::convertToVO)
|
.map(this::convertToVO)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
@@ -170,6 +180,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
|
|||||||
ProductVO productVO = new ProductVO();
|
ProductVO productVO = new ProductVO();
|
||||||
productVO.setId(productDO.getId());
|
productVO.setId(productDO.getId());
|
||||||
productVO.setShopId(productDO.getShopId());
|
productVO.setShopId(productDO.getShopId());
|
||||||
|
productVO.setShopName(resolveShopName(productDO.getShopId()));
|
||||||
productVO.setTitle(productDO.getTitle());
|
productVO.setTitle(productDO.getTitle());
|
||||||
productVO.setDescription(productDO.getDescription());
|
productVO.setDescription(productDO.getDescription());
|
||||||
productVO.setImageFileIds(productDO.getImageFileIds());
|
productVO.setImageFileIds(productDO.getImageFileIds());
|
||||||
@@ -291,4 +302,58 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateAvailableCoupons(Long shopId, String availableCoupons) {
|
||||||
|
if (!StringUtils.hasText(availableCoupons)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shopId == null || shopId <= 0) {
|
||||||
|
throw new RuntimeException("配置可用优惠券前必须先选择店铺");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String couponIdStr : availableCoupons.split(",")) {
|
||||||
|
if (!StringUtils.hasText(couponIdStr)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Long couponId;
|
||||||
|
try {
|
||||||
|
couponId = Long.parseLong(couponIdStr.trim());
|
||||||
|
} catch (NumberFormatException exception) {
|
||||||
|
throw new RuntimeException("可用优惠券配置格式不正确");
|
||||||
|
}
|
||||||
|
|
||||||
|
CouponDO couponDO = couponMapper.selectById(couponId);
|
||||||
|
if (couponDO == null || Integer.valueOf(1).equals(couponDO.getIsDelete())) {
|
||||||
|
throw new RuntimeException("存在无效的优惠券配置");
|
||||||
|
}
|
||||||
|
if (!Integer.valueOf(1).equals(couponDO.getStatus())) {
|
||||||
|
throw new RuntimeException("存在已失效的优惠券配置");
|
||||||
|
}
|
||||||
|
if (couponDO.getShopId() == null || !shopId.equals(couponDO.getShopId())) {
|
||||||
|
throw new RuntimeException("不可跨店铺配置可用优惠券");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveShopName(Long shopId) {
|
||||||
|
if (shopId == null || shopId <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
ShopDO shopDO = shopMapper.selectById(shopId);
|
||||||
|
return shopDO == null ? null : shopDO.getShopName();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean containsAnyTagId(String tags, Set<String> targetTagIds) {
|
||||||
|
if (!StringUtils.hasText(tags) || targetTagIds == null || targetTagIds.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (String tagId : tags.split(";")) {
|
||||||
|
if (targetTagIds.contains(tagId.trim())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+75
-6
@@ -3,12 +3,16 @@ package com.xiaowang.yuminailcore.service.impl;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.xiaowang.yuminailcore.dto.TagDTO;
|
import com.xiaowang.yuminailcore.dto.TagDTO;
|
||||||
|
import com.xiaowang.yuminailcore.entity.ShopDO;
|
||||||
import com.xiaowang.yuminailcore.entity.TagDO;
|
import com.xiaowang.yuminailcore.entity.TagDO;
|
||||||
|
import com.xiaowang.yuminailcore.mapper.ShopMapper;
|
||||||
import com.xiaowang.yuminailcore.mapper.TagMapper;
|
import com.xiaowang.yuminailcore.mapper.TagMapper;
|
||||||
import com.xiaowang.yuminailcore.service.TagService;
|
import com.xiaowang.yuminailcore.service.TagService;
|
||||||
import com.xiaowang.yuminailcore.vo.TagVO;
|
import com.xiaowang.yuminailcore.vo.TagVO;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.dao.DuplicateKeyException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -25,17 +29,26 @@ import java.util.stream.Collectors;
|
|||||||
public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements TagService {
|
public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements TagService {
|
||||||
|
|
||||||
private final TagMapper tagMapper;
|
private final TagMapper tagMapper;
|
||||||
|
private final ShopMapper shopMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long createTag(TagDTO tagDTO) {
|
public Long createTag(TagDTO tagDTO) {
|
||||||
|
String tagName = normalizeTagName(tagDTO.getTagName());
|
||||||
|
Long shopId = requireShopId(tagDTO.getShopId());
|
||||||
|
ensureTagNameUniqueInShop(tagName, shopId, null);
|
||||||
|
|
||||||
TagDO tagDO = new TagDO();
|
TagDO tagDO = new TagDO();
|
||||||
tagDO.setTagName(tagDTO.getTagName());
|
tagDO.setTagName(tagName);
|
||||||
tagDO.setTextColor(tagDTO.getTextColor());
|
tagDO.setTextColor(tagDTO.getTextColor());
|
||||||
tagDO.setBgColor(tagDTO.getBgColor());
|
tagDO.setBgColor(tagDTO.getBgColor());
|
||||||
tagDO.setBorderColor(tagDTO.getBorderColor());
|
tagDO.setBorderColor(tagDTO.getBorderColor());
|
||||||
tagDO.setShopId(tagDTO.getShopId());
|
tagDO.setShopId(shopId);
|
||||||
tagDO.setIsDelete(0);
|
tagDO.setIsDelete(0);
|
||||||
tagMapper.insert(tagDO);
|
try {
|
||||||
|
tagMapper.insert(tagDO);
|
||||||
|
} catch (DuplicateKeyException exception) {
|
||||||
|
throw buildDuplicateTagException(tagName, shopId, null);
|
||||||
|
}
|
||||||
return tagDO.getId();
|
return tagDO.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,12 +58,20 @@ public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements Tag
|
|||||||
if (tagDO == null) {
|
if (tagDO == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
tagDO.setTagName(tagDTO.getTagName());
|
String tagName = normalizeTagName(tagDTO.getTagName());
|
||||||
|
Long shopId = requireShopId(tagDTO.getShopId());
|
||||||
|
ensureTagNameUniqueInShop(tagName, shopId, id);
|
||||||
|
|
||||||
|
tagDO.setTagName(tagName);
|
||||||
tagDO.setTextColor(tagDTO.getTextColor());
|
tagDO.setTextColor(tagDTO.getTextColor());
|
||||||
tagDO.setBgColor(tagDTO.getBgColor());
|
tagDO.setBgColor(tagDTO.getBgColor());
|
||||||
tagDO.setBorderColor(tagDTO.getBorderColor());
|
tagDO.setBorderColor(tagDTO.getBorderColor());
|
||||||
tagDO.setShopId(tagDTO.getShopId());
|
tagDO.setShopId(shopId);
|
||||||
return tagMapper.updateById(tagDO) > 0;
|
try {
|
||||||
|
return tagMapper.updateById(tagDO) > 0;
|
||||||
|
} catch (DuplicateKeyException exception) {
|
||||||
|
throw buildDuplicateTagException(tagName, shopId, id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -99,6 +120,7 @@ public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements Tag
|
|||||||
public TagVO getTagByTagName(String tagName) {
|
public TagVO getTagByTagName(String tagName) {
|
||||||
TagDO tagDO = tagMapper.selectOne(
|
TagDO tagDO = tagMapper.selectOne(
|
||||||
Wrappers.lambdaQuery(TagDO.class)
|
Wrappers.lambdaQuery(TagDO.class)
|
||||||
|
.eq(TagDO::getIsDelete, 0)
|
||||||
.eq(TagDO::getTagName, tagName)
|
.eq(TagDO::getTagName, tagName)
|
||||||
);
|
);
|
||||||
if (tagDO == null) {
|
if (tagDO == null) {
|
||||||
@@ -121,8 +143,55 @@ public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements Tag
|
|||||||
tagVO.setBgColor(tagDO.getBgColor());
|
tagVO.setBgColor(tagDO.getBgColor());
|
||||||
tagVO.setBorderColor(tagDO.getBorderColor());
|
tagVO.setBorderColor(tagDO.getBorderColor());
|
||||||
tagVO.setShopId(tagDO.getShopId());
|
tagVO.setShopId(tagDO.getShopId());
|
||||||
|
tagVO.setShopName(resolveShopName(tagDO.getShopId()));
|
||||||
tagVO.setCreateTime(tagDO.getCreateTime());
|
tagVO.setCreateTime(tagDO.getCreateTime());
|
||||||
tagVO.setUpdateTime(tagDO.getUpdateTime());
|
tagVO.setUpdateTime(tagDO.getUpdateTime());
|
||||||
return tagVO;
|
return tagVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String normalizeTagName(String tagName) {
|
||||||
|
if (!StringUtils.hasText(tagName)) {
|
||||||
|
throw new RuntimeException("标签名称不能为空");
|
||||||
|
}
|
||||||
|
return tagName.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long requireShopId(Long shopId) {
|
||||||
|
if (shopId == null || shopId <= 0) {
|
||||||
|
throw new RuntimeException("请选择所属店铺");
|
||||||
|
}
|
||||||
|
return shopId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureTagNameUniqueInShop(String tagName, Long shopId, Long excludeId) {
|
||||||
|
if (findActiveTagByNameAndShopId(tagName, shopId, excludeId) != null) {
|
||||||
|
throw new RuntimeException("当前店铺已存在同名标签");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private TagDO findActiveTagByNameAndShopId(String tagName, Long shopId, Long excludeId) {
|
||||||
|
return tagMapper.selectOne(
|
||||||
|
Wrappers.lambdaQuery(TagDO.class)
|
||||||
|
.eq(TagDO::getIsDelete, 0)
|
||||||
|
.eq(TagDO::getShopId, shopId)
|
||||||
|
.eq(TagDO::getTagName, tagName)
|
||||||
|
.ne(excludeId != null, TagDO::getId, excludeId)
|
||||||
|
.last("LIMIT 1")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RuntimeException buildDuplicateTagException(String tagName, Long shopId, Long excludeId) {
|
||||||
|
if (findActiveTagByNameAndShopId(tagName, shopId, excludeId) != null) {
|
||||||
|
return new RuntimeException("当前店铺已存在同名标签");
|
||||||
|
}
|
||||||
|
return new RuntimeException("数据库仍使用旧的标签全局唯一约束,请先执行本次提供的 SQL 补丁");
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveShopName(Long shopId) {
|
||||||
|
if (shopId == null || shopId <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
ShopDO shopDO = shopMapper.selectById(shopId);
|
||||||
|
return shopDO == null ? null : shopDO.getShopName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,4 +53,9 @@ public class TagVO {
|
|||||||
* 店铺ID
|
* 店铺ID
|
||||||
*/
|
*/
|
||||||
private Long shopId;
|
private Long shopId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 店铺名称
|
||||||
|
*/
|
||||||
|
private String shopName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
# 2026-03-27 作品优惠券全选与标签店铺修复
|
||||||
|
|
||||||
|
## 本次处理
|
||||||
|
|
||||||
|
- 作品新增/编辑时,“可用优惠券”支持一键全选与清空。
|
||||||
|
- 作品可用优惠券改为按当前所选店铺加载,避免跨店优惠券被错误配置到作品上。
|
||||||
|
- 标签新增/编辑重复时报错改为业务提示,不再直接把数据库异常栈透给前端。
|
||||||
|
- 标签改为按“店铺 + 标签名”维度唯一,允许不同店铺使用相同标签名。
|
||||||
|
- 标签管理、作品管理中的所属店铺显示改为优先使用后端回传 `shopName`,并兼容前端本地店铺映射,避免出现“未知店铺”。
|
||||||
|
|
||||||
|
## 根因说明
|
||||||
|
|
||||||
|
### 1. 标签重复提示不友好
|
||||||
|
|
||||||
|
- 之前标签创建/更新未做店铺内重名校验,也没有拦截唯一索引异常。
|
||||||
|
- 当命中数据库唯一索引时,前端直接显示了底层 SQL 异常文本。
|
||||||
|
|
||||||
|
### 2. 标签无法按店铺重名
|
||||||
|
|
||||||
|
- 当前数据库 `t_tag` 仍是全局唯一索引 `uk_tag_name(tag_name)`。
|
||||||
|
- 这会导致 A 店和 B 店不能各自创建一个同名标签。
|
||||||
|
|
||||||
|
### 3. 标签/作品所属店铺显示成“未知店铺”
|
||||||
|
|
||||||
|
- 前端表格主要依赖本地 `shops` 列表通过 `shopId` 反查店名。
|
||||||
|
- 一旦店铺列表未及时拿到、类型不一致,或者当前列表未包含该记录对应店铺,就会退化成“未知店铺”。
|
||||||
|
- 后端 `TagVO` / `ProductVO` 转换时也没有稳定补上 `shopName`。
|
||||||
|
|
||||||
|
### 4. 作品可用优惠券存在跨店误配风险
|
||||||
|
|
||||||
|
- 作品页之前取优惠券列表时没有跟随当前所选店铺过滤。
|
||||||
|
- 在管理员场景下,会把其他店铺的优惠券也带出来。
|
||||||
|
|
||||||
|
## 实际修改
|
||||||
|
|
||||||
|
### 前端
|
||||||
|
|
||||||
|
- 文件:`Frontend/src/views/admin/ProductsView.vue`
|
||||||
|
- “可用优惠券”增加:
|
||||||
|
- `一键全选`
|
||||||
|
- `清空`
|
||||||
|
- 按所选店铺拉取优惠券列表。
|
||||||
|
- 店铺切换时自动剔除当前店铺不可用的旧优惠券选择。
|
||||||
|
- 作品列表店铺名称优先显示后端返回的 `shopName`。
|
||||||
|
|
||||||
|
- 文件:`Frontend/src/views/admin/TagsView.vue`
|
||||||
|
- 标签列表店铺名称优先显示后端返回的 `shopName`。
|
||||||
|
- 新增/编辑标签失败时显示友好错误消息。
|
||||||
|
|
||||||
|
- 文件:`Frontend/src/api/admin.ts`
|
||||||
|
- `Tag` 类型补充 `shopName` 字段。
|
||||||
|
|
||||||
|
### 后端
|
||||||
|
|
||||||
|
- 文件:`YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/vo/TagVO.java`
|
||||||
|
- 增加 `shopName` 字段。
|
||||||
|
|
||||||
|
- 文件:`YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/TagServiceImpl.java`
|
||||||
|
- 创建/更新标签前增加“当前店铺内重名校验”。
|
||||||
|
- 拦截唯一键冲突并改为友好提示:
|
||||||
|
- 同店重名:`当前店铺已存在同名标签`
|
||||||
|
- 若数据库仍未执行本轮 SQL:`数据库仍使用旧的标签全局唯一约束,请先执行本次提供的 SQL 补丁`
|
||||||
|
- 转换 `TagVO` 时补充 `shopName`。
|
||||||
|
|
||||||
|
- 文件:`YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/ProductServiceImpl.java`
|
||||||
|
- 新增作品可用优惠券校验:
|
||||||
|
- 格式必须正确
|
||||||
|
- 优惠券必须存在且未删除
|
||||||
|
- 优惠券必须处于可用状态
|
||||||
|
- 不允许跨店铺配置
|
||||||
|
- 转换 `ProductVO` 时补充 `shopName`。
|
||||||
|
|
||||||
|
- 文件:`YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/controller/AdminCouponController.java`
|
||||||
|
- 后台优惠券列表接口 `GET /admin/coupon/list/all` 支持可选 `shopId` 参数,便于作品页按店铺加载优惠券。
|
||||||
|
|
||||||
|
## SQL 变更
|
||||||
|
|
||||||
|
- 需要手动执行:
|
||||||
|
- `docs/changes/2026-03-27_tag_shop_unique_patch.sql`
|
||||||
|
|
||||||
|
- 作用:
|
||||||
|
- 移除旧的全局唯一索引 `uk_tag_name`
|
||||||
|
- 改为按 `(shop_id, tag_name)` 唯一
|
||||||
|
|
||||||
|
## 验证
|
||||||
|
|
||||||
|
- 后端执行 `mvn -q -DskipTests compile`
|
||||||
|
- 前端执行 `npm run build`
|
||||||
|
|
||||||
|
## 说明
|
||||||
|
|
||||||
|
- 这次没有直接改 `YumiNail.sql` 主文件。
|
||||||
|
- SQL 已单独拆出,按你当前规则由你手工执行即可。
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
-- 2026-03-27 标签按店铺唯一补丁
|
||||||
|
-- 说明:
|
||||||
|
-- 1. 允许不同店铺使用相同标签名
|
||||||
|
-- 2. 当前店铺内标签名仍保持唯一
|
||||||
|
|
||||||
|
SET @drop_old_tag_unique_sql = (
|
||||||
|
SELECT IF(
|
||||||
|
EXISTS(
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.statistics
|
||||||
|
WHERE table_schema = DATABASE()
|
||||||
|
AND table_name = 't_tag'
|
||||||
|
AND index_name = 'uk_tag_name'
|
||||||
|
),
|
||||||
|
'ALTER TABLE t_tag DROP INDEX uk_tag_name',
|
||||||
|
'SELECT ''skip drop uk_tag_name'''
|
||||||
|
)
|
||||||
|
);
|
||||||
|
PREPARE stmt_drop_old_tag_unique FROM @drop_old_tag_unique_sql;
|
||||||
|
EXECUTE stmt_drop_old_tag_unique;
|
||||||
|
DEALLOCATE PREPARE stmt_drop_old_tag_unique;
|
||||||
|
|
||||||
|
SET @add_shop_tag_unique_sql = (
|
||||||
|
SELECT IF(
|
||||||
|
EXISTS(
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.statistics
|
||||||
|
WHERE table_schema = DATABASE()
|
||||||
|
AND table_name = 't_tag'
|
||||||
|
AND index_name = 'uk_tag_shop_name'
|
||||||
|
),
|
||||||
|
'SELECT ''skip add uk_tag_shop_name''',
|
||||||
|
'ALTER TABLE t_tag ADD UNIQUE INDEX uk_tag_shop_name (shop_id, tag_name)'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
PREPARE stmt_add_shop_tag_unique FROM @add_shop_tag_unique_sql;
|
||||||
|
EXECUTE stmt_add_shop_tag_unique;
|
||||||
|
DEALLOCATE PREPARE stmt_add_shop_tag_unique;
|
||||||
Reference in New Issue
Block a user