修复:修复标签店铺绑定与作品优惠券选择

This commit is contained in:
xiaowang
2026-03-27 10:35:00 +08:00
parent f4b535cebd
commit 1220a9ebba
9 changed files with 386 additions and 37 deletions
@@ -107,13 +107,14 @@ public class AdminCouponController {
*/
@GetMapping("/list/all")
@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);
Long shopId = adminScopeHelper.resolveShopScope(currentUser, null);
shopId = adminScopeHelper.resolveShopScope(currentUser, shopId);
List<CouponVO> coupons = couponService.listAllCoupons();
if (shopId != null) {
Long resolvedShopId = shopId;
if (resolvedShopId != null) {
coupons = coupons.stream()
.filter(coupon -> shopId.equals(coupon.getShopId()))
.filter(coupon -> resolvedShopId.equals(coupon.getShopId()))
.collect(Collectors.toList());
}
return Result.success(coupons);
@@ -5,9 +5,15 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowang.yuminailcore.dto.ProductDTO;
import com.xiaowang.yuminailcore.entity.CouponDO;
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.mapper.CouponMapper;
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.service.ObjectStorageFileService;
import com.xiaowang.yuminailcore.service.ProductService;
@@ -23,7 +29,9 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -41,11 +49,15 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
private final TagService tagService;
private final ObjectStorageFileService objectStorageFileService;
private final UserMapper userMapper;
private final CouponMapper couponMapper;
private final ShopMapper shopMapper;
private final TagMapper tagMapper;
private final UserShopBindingSupport userShopBindingSupport;
@Override
public Long createProduct(ProductDTO productDTO) {
validateBookableArtists(productDTO.getShopId(), productDTO.getBookableArtists());
validateAvailableCoupons(productDTO.getShopId(), productDTO.getAvailableCoupons());
ProductDO productDO = new ProductDO();
productDO.setShopId(productDTO.getShopId());
productDO.setTitle(productDTO.getTitle());
@@ -68,6 +80,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
return false;
}
validateBookableArtists(productDTO.getShopId(), productDTO.getBookableArtists());
validateAvailableCoupons(productDTO.getShopId(), productDTO.getAvailableCoupons());
productDO.setShopId(productDTO.getShopId());
productDO.setTitle(productDTO.getTitle());
productDO.setDescription(productDTO.getDescription());
@@ -134,28 +147,25 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
@Override
public List<ProductVO> listProductsByTag(String tag) {
// 先根据标签名称查找标签ID
TagVO tagVO = tagService.getTagByTagName(tag);
if (tagVO == null) {
List<TagDO> matchedTags = tagMapper.selectList(
Wrappers.lambdaQuery(TagDO.class)
.eq(TagDO::getIsDelete, 0)
.eq(TagDO::getTagName, tag)
);
if (matchedTags.isEmpty()) {
return new java.util.ArrayList<>();
}
// 用标签ID查询商品,只完整匹配标签ID
String tagIdStr = String.valueOf(tagVO.getId());
Set<String> matchedTagIds = matchedTags.stream()
.map(TagDO::getId)
.map(String::valueOf)
.collect(Collectors.toCollection(HashSet::new));
List<ProductDO> productDOs = productMapper.selectList(
Wrappers.lambdaQuery(ProductDO.class)
.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()
.filter(productDO -> containsAnyTagId(productDO.getTags(), matchedTagIds))
.map(this::convertToVO)
.collect(Collectors.toList());
}
@@ -170,6 +180,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
ProductVO productVO = new ProductVO();
productVO.setId(productDO.getId());
productVO.setShopId(productDO.getShopId());
productVO.setShopName(resolveShopName(productDO.getShopId()));
productVO.setTitle(productDO.getTitle());
productVO.setDescription(productDO.getDescription());
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;
}
}
@@ -3,12 +3,16 @@ package com.xiaowang.yuminailcore.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowang.yuminailcore.dto.TagDTO;
import com.xiaowang.yuminailcore.entity.ShopDO;
import com.xiaowang.yuminailcore.entity.TagDO;
import com.xiaowang.yuminailcore.mapper.ShopMapper;
import com.xiaowang.yuminailcore.mapper.TagMapper;
import com.xiaowang.yuminailcore.service.TagService;
import com.xiaowang.yuminailcore.vo.TagVO;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.stream.Collectors;
@@ -25,17 +29,26 @@ import java.util.stream.Collectors;
public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements TagService {
private final TagMapper tagMapper;
private final ShopMapper shopMapper;
@Override
public Long createTag(TagDTO tagDTO) {
String tagName = normalizeTagName(tagDTO.getTagName());
Long shopId = requireShopId(tagDTO.getShopId());
ensureTagNameUniqueInShop(tagName, shopId, null);
TagDO tagDO = new TagDO();
tagDO.setTagName(tagDTO.getTagName());
tagDO.setTagName(tagName);
tagDO.setTextColor(tagDTO.getTextColor());
tagDO.setBgColor(tagDTO.getBgColor());
tagDO.setBorderColor(tagDTO.getBorderColor());
tagDO.setShopId(tagDTO.getShopId());
tagDO.setShopId(shopId);
tagDO.setIsDelete(0);
tagMapper.insert(tagDO);
try {
tagMapper.insert(tagDO);
} catch (DuplicateKeyException exception) {
throw buildDuplicateTagException(tagName, shopId, null);
}
return tagDO.getId();
}
@@ -45,12 +58,20 @@ public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements Tag
if (tagDO == null) {
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.setBgColor(tagDTO.getBgColor());
tagDO.setBorderColor(tagDTO.getBorderColor());
tagDO.setShopId(tagDTO.getShopId());
return tagMapper.updateById(tagDO) > 0;
tagDO.setShopId(shopId);
try {
return tagMapper.updateById(tagDO) > 0;
} catch (DuplicateKeyException exception) {
throw buildDuplicateTagException(tagName, shopId, id);
}
}
@Override
@@ -99,6 +120,7 @@ public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements Tag
public TagVO getTagByTagName(String tagName) {
TagDO tagDO = tagMapper.selectOne(
Wrappers.lambdaQuery(TagDO.class)
.eq(TagDO::getIsDelete, 0)
.eq(TagDO::getTagName, tagName)
);
if (tagDO == null) {
@@ -121,8 +143,55 @@ public class TagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements Tag
tagVO.setBgColor(tagDO.getBgColor());
tagVO.setBorderColor(tagDO.getBorderColor());
tagVO.setShopId(tagDO.getShopId());
tagVO.setShopName(resolveShopName(tagDO.getShopId()));
tagVO.setCreateTime(tagDO.getCreateTime());
tagVO.setUpdateTime(tagDO.getUpdateTime());
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
*/
private Long shopId;
/**
* 店铺名称
*/
private String shopName;
}