点赞收藏功能细化,问题修复

This commit is contained in:
xiaowang
2026-03-05 15:15:59 +08:00
parent bea4b63e3e
commit 560921c0e9
13 changed files with 560 additions and 131 deletions
@@ -2,6 +2,8 @@ package com.xiaowang.yuminailcore.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiaowang.yuminailcore.entity.ProductDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
/**
* 商品信息表 Mapper
@@ -11,4 +13,40 @@ import com.xiaowang.yuminailcore.entity.ProductDO;
* @date 2026-02-13
*/
public interface ProductMapper extends BaseMapper<ProductDO> {
/**
* 增加商品收藏数
*
* @param productId 商品ID
* @return 影响行数
*/
@Update("UPDATE t_product SET collect_count = collect_count + 1 WHERE id = #{productId}")
int incrementCollectCount(@Param("productId") Long productId);
/**
* 减少商品收藏数
*
* @param productId 商品ID
* @return 影响行数
*/
@Update("UPDATE t_product SET collect_count = CASE WHEN collect_count > 0 THEN collect_count - 1 ELSE 0 END WHERE id = #{productId}")
int decrementCollectCount(@Param("productId") Long productId);
/**
* 增加商品点赞数
*
* @param productId 商品ID
* @return 影响行数
*/
@Update("UPDATE t_product SET like_count = like_count + 1 WHERE id = #{productId}")
int incrementLikeCount(@Param("productId") Long productId);
/**
* 减少商品点赞数
*
* @param productId 商品ID
* @return 影响行数
*/
@Update("UPDATE t_product SET like_count = CASE WHEN like_count > 0 THEN like_count - 1 ELSE 0 END WHERE id = #{productId}")
int decrementLikeCount(@Param("productId") Long productId);
}
@@ -34,6 +34,22 @@ public interface ProductService extends IService<ProductDO> {
*/
boolean updateProduct(Long id, ProductDTO productDTO);
/**
* 增加商品点赞数
*
* @param productId 商品ID
* @return 影响行数
*/
int incrementLikeCount(Long productId);
/**
* 减少商品点赞数
*
* @param productId 商品ID
* @return 影响行数
*/
int decrementLikeCount(Long productId);
/**
* 删除商品
*
@@ -78,6 +78,16 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
return removeById(id);
}
@Override
public int incrementLikeCount(Long productId) {
return productMapper.incrementLikeCount(productId);
}
@Override
public int decrementLikeCount(Long productId) {
return productMapper.decrementLikeCount(productId);
}
@Override
public ProductVO getProductById(Long id) {
ProductDO productDO = productMapper.selectById(id);
@@ -6,11 +6,13 @@ import com.xiaowang.yuminailcore.dto.WishlistDTO;
import com.xiaowang.yuminailcore.vo.ProductVO;
import com.xiaowang.yuminailcore.entity.WishlistDO;
import com.xiaowang.yuminailcore.mapper.WishlistMapper;
import com.xiaowang.yuminailcore.mapper.ProductMapper;
import com.xiaowang.yuminailcore.service.WishlistService;
import com.xiaowang.yuminailcore.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@@ -21,7 +23,11 @@ public class WishlistServiceImpl extends ServiceImpl<WishlistMapper, WishlistDO>
@Autowired
private ProductService productService;
@Autowired
private ProductMapper productMapper;
@Override
@Transactional
public void addToWishlist(Long userId, Long productId) {
LambdaQueryWrapper<WishlistDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(WishlistDO::getUserId, userId);
@@ -31,15 +37,22 @@ public class WishlistServiceImpl extends ServiceImpl<WishlistMapper, WishlistDO>
wishlistDO.setUserId(userId);
wishlistDO.setProductId(productId);
save(wishlistDO);
// 增加商品收藏数
productMapper.incrementCollectCount(productId);
}
}
@Override
@Transactional
public void removeFromWishlist(Long userId, Long productId) {
LambdaQueryWrapper<WishlistDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(WishlistDO::getUserId, userId);
wrapper.eq(WishlistDO::getProductId, productId);
remove(wrapper);
if (count(wrapper) > 0) {
remove(wrapper);
// 减少商品收藏数
productMapper.decrementCollectCount(productId);
}
}
@Override
@@ -641,3 +641,18 @@ WHERE p.is_delete = 0
'nail_artist:view',
'nail_artist:edit'
);
-- 商品点赞记录表
CREATE TABLE t_product_like (
id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '点赞记录唯一主键ID'
PRIMARY KEY COMMENT '商品点赞主键索引',
user_id BIGINT UNSIGNED NOT NULL COMMENT '关联用户表主键',
product_id BIGINT UNSIGNED NOT NULL COMMENT '关联商品表主键',
create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间',
update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
is_delete TINYINT UNSIGNED DEFAULT 0 NULL COMMENT '逻辑删除:0-未删除,1-已删除',
CONSTRAINT uk_user_product_like
UNIQUE (user_id, product_id) COMMENT '用户-商品唯一索引'
) COMMENT '商品点赞记录表';
CREATE INDEX idx_user_id_like ON t_product_like (user_id) COMMENT '按用户查询点赞的索引';
CREATE INDEX idx_product_id_like ON t_product_like (product_id) COMMENT '按商品查询点赞的索引';