修复:修复后台表格折叠与购物车美甲师选择

This commit is contained in:
xiaowang
2026-03-27 12:29:36 +08:00
parent d2c693e6a1
commit 6b07b2d538
13 changed files with 329 additions and 62 deletions
@@ -4,19 +4,28 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaowang.yuminailcore.dto.CartDTO;
import com.xiaowang.yuminailcore.entity.CartDO;
import com.xiaowang.yuminailcore.entity.ProductDO;
import com.xiaowang.yuminailcore.entity.UserDO;
import com.xiaowang.yuminailcore.mapper.CartMapper;
import com.xiaowang.yuminailcore.mapper.ProductMapper;
import com.xiaowang.yuminailcore.mapper.UserMapper;
import com.xiaowang.yuminailcore.service.CartService;
import com.xiaowang.yuminailcore.service.TagService;
import com.xiaowang.yuminailcore.support.BookableArtistSupport;
import com.xiaowang.yuminailcore.support.UserShopBindingSupport;
import com.xiaowang.yuminailcore.vo.CartArtistVO;
import com.xiaowang.yuminailcore.vo.CartVO;
import com.xiaowang.yuminailcore.vo.TagVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
@@ -31,32 +40,38 @@ public class CartServiceImpl implements CartService {
@Resource
private TagService tagService;
@Resource
private UserMapper userMapper;
@Resource
private UserShopBindingSupport userShopBindingSupport;
@Resource
private BookableArtistSupport bookableArtistSupport;
@Override
@Transactional
public void addToCart(Long userId, CartDTO cartDTO) {
validateQuantity(cartDTO.getQuantity());
// 检查商品是否存在
ProductDO product = productMapper.selectById(cartDTO.getProductId());
if (product == null || product.getIsDelete() == 1) {
if (product == null || Integer.valueOf(1).equals(product.getIsDelete())) {
throw new RuntimeException("商品不存在");
}
// 检查购物车中是否已有该商品
CartDO existingCart = cartMapper.selectByUserIdAndProductId(userId, cartDTO.getProductId());
if (existingCart != null) {
// 更新数量
existingCart.setQuantity(existingCart.getQuantity() + cartDTO.getQuantity());
cartMapper.updateById(existingCart);
} else {
// 添加新商品到购物车
CartDO newCart = new CartDO();
newCart.setUserId(userId);
newCart.setProductId(cartDTO.getProductId());
newCart.setQuantity(cartDTO.getQuantity());
newCart.setIsDelete(0);
cartMapper.insert(newCart);
return;
}
CartDO newCart = new CartDO();
newCart.setUserId(userId);
newCart.setProductId(cartDTO.getProductId());
newCart.setQuantity(cartDTO.getQuantity());
newCart.setIsDelete(0);
cartMapper.insert(newCart);
}
@Override
@@ -102,30 +117,42 @@ public class CartServiceImpl implements CartService {
List<CartVO> cartVOList = new ArrayList<>();
for (CartDO cart : cartList) {
if (cart.getIsDelete() == 0) {
ProductDO product = productMapper.selectById(cart.getProductId());
if (product != null && product.getIsDelete() == 0) {
CartVO cartVO = new CartVO();
cartVO.setId(cart.getId());
cartVO.setProductId(product.getId());
cartVO.setShopId(product.getShopId());
cartVO.setProductTitle(product.getTitle());
// 简单处理,取第一张图片
String imageFileIds = product.getImageFileIds();
if (imageFileIds != null && !imageFileIds.isEmpty()) {
String firstImageId = imageFileIds.split(",")[0];
cartVO.setProductImage("/api/file/get/" + firstImageId);
}
cartVO.setPrice(product.getPrice());
cartVO.setQuantity(cart.getQuantity());
cartVO.setTotalPrice(product.getPrice().multiply(new BigDecimal(cart.getQuantity())));
cartVO.setTags(product.getTags());
cartVO.setTagNames(resolveTagNames(product.getTags()));
cartVO.setApproximateDuration(product.getApproximateDuration());
cartVO.setBookableArtists(product.getBookableArtists());
cartVOList.add(cartVO);
}
if (!Integer.valueOf(0).equals(cart.getIsDelete())) {
continue;
}
ProductDO product = productMapper.selectById(cart.getProductId());
if (product == null || Integer.valueOf(1).equals(product.getIsDelete())) {
continue;
}
CartVO cartVO = new CartVO();
cartVO.setId(cart.getId());
cartVO.setProductId(product.getId());
cartVO.setShopId(product.getShopId());
cartVO.setProductTitle(product.getTitle());
String imageFileIds = product.getImageFileIds();
if (StringUtils.hasText(imageFileIds)) {
String firstImageId = imageFileIds.split(",")[0];
cartVO.setProductImage("/api/file/get/" + firstImageId);
}
cartVO.setPrice(product.getPrice());
cartVO.setQuantity(cart.getQuantity());
cartVO.setTotalPrice(product.getPrice().multiply(new BigDecimal(cart.getQuantity())));
cartVO.setTags(product.getTags());
cartVO.setTagNames(resolveTagNames(product.getTags()));
cartVO.setApproximateDuration(product.getApproximateDuration());
String validBookableArtists = bookableArtistSupport.filterValidBookableArtists(product.getShopId(), product.getBookableArtists());
List<CartArtistVO> availableArtists = resolveAvailableArtists(product.getShopId(), product.getBookableArtists(), validBookableArtists);
cartVO.setBookableArtists(validBookableArtists);
cartVO.setAvailableBookableArtistCount(availableArtists.size());
cartVO.setBookableArtistRestricted(StringUtils.hasText(product.getBookableArtists()));
cartVO.setAvailableArtists(availableArtists);
cartVOList.add(cartVO);
}
return cartVOList;
@@ -142,12 +169,12 @@ public class CartServiceImpl implements CartService {
private void validateQuantity(Integer quantity) {
if (quantity == null || quantity < 1 || quantity > 99) {
throw new RuntimeException("购物车数量必须在1到99之间");
throw new RuntimeException("购物车数量必须在 1 到 99 之间");
}
}
private String resolveTagNames(String tagIds) {
if (tagIds == null || tagIds.isBlank()) {
if (!StringUtils.hasText(tagIds)) {
return "";
}
@@ -165,4 +192,54 @@ public class CartServiceImpl implements CartService {
.filter(tagName -> tagName != null && !tagName.isBlank())
.collect(Collectors.joining(","));
}
private List<CartArtistVO> resolveAvailableArtists(Long shopId, String rawBookableArtists, String validBookableArtists) {
LinkedHashSet<Long> candidateArtistIds = new LinkedHashSet<>();
if (StringUtils.hasText(validBookableArtists)) {
for (String artistIdText : validBookableArtists.split(",")) {
if (!StringUtils.hasText(artistIdText)) {
continue;
}
try {
candidateArtistIds.add(Long.parseLong(artistIdText.trim()));
} catch (NumberFormatException ignored) {
// Ignore invalid ids and keep valid artists only.
}
}
} else if (!StringUtils.hasText(rawBookableArtists) && shopId != null && shopId > 0) {
candidateArtistIds.addAll(userShopBindingSupport.listUserIdsByShopId(shopId, true));
}
if (candidateArtistIds.isEmpty()) {
return List.of();
}
List<UserDO> artistUsers = userMapper.selectList(
new QueryWrapper<UserDO>()
.in("id", candidateArtistIds)
.eq("type", 3)
.eq("status", 1)
.eq("is_delete", 0)
);
if (artistUsers.isEmpty()) {
return List.of();
}
Map<Long, UserDO> artistMap = artistUsers.stream()
.collect(Collectors.toMap(UserDO::getId, artist -> artist, (left, right) -> left));
return candidateArtistIds.stream()
.map(artistMap::get)
.filter(Objects::nonNull)
.map(this::buildCartArtist)
.collect(Collectors.toList());
}
private CartArtistVO buildCartArtist(UserDO artist) {
CartArtistVO cartArtistVO = new CartArtistVO();
cartArtistVO.setId(artist.getId());
cartArtistVO.setName(artist.getNickname());
cartArtistVO.setAvatar(artist.getAvatar());
return cartArtistVO;
}
}
@@ -65,7 +65,7 @@ public class NailArtistInfoServiceImpl extends ServiceImpl<NailArtistInfoMapper,
@Override
public List<NailArtistInfoDTO> getByShopId(Long shopId) {
List<Long> artistUserIds = userShopBindingSupport.listUserIdsByShopId(shopId, false);
List<Long> artistUserIds = userShopBindingSupport.listUserIdsByShopId(shopId, true);
if (artistUserIds.isEmpty()) {
return new ArrayList<>();
}
@@ -125,7 +125,7 @@ public class NailArtistInfoServiceImpl extends ServiceImpl<NailArtistInfoMapper,
nailArtistInfoDTO.setNickname(userDO.getNickname());
nailArtistInfoDTO.setSignature(userDO.getSignature()); // 添加用户签名
List<ShopSimpleVO> shopOptions = userShopBindingSupport.listShopOptionsByUserId(userDO.getId(), false, false);
List<ShopSimpleVO> shopOptions = userShopBindingSupport.listShopOptionsByUserId(userDO.getId(), true, true);
if (!shopOptions.isEmpty()) {
ShopSimpleVO primaryShop = shopOptions.get(0);
nailArtistInfoDTO.setShopId(primaryShop.getId());
@@ -0,0 +1,10 @@
package com.xiaowang.yuminailcore.vo;
import lombok.Data;
@Data
public class CartArtistVO {
private Long id;
private String name;
private String avatar;
}
@@ -3,6 +3,7 @@ package com.xiaowang.yuminailcore.vo;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
@Data
public class CartVO {
@@ -18,4 +19,7 @@ public class CartVO {
private String tagNames;
private String approximateDuration;
private String bookableArtists;
private Integer availableBookableArtistCount;
private Boolean bookableArtistRestricted;
private List<CartArtistVO> availableArtists;
}