|
@@ -1,23 +1,29 @@
|
|
|
package cn.iocoder.yudao.module.product.service.favorite;
|
|
|
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
|
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoritePageReqVO;
|
|
|
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteReqVO;
|
|
|
import cn.iocoder.yudao.module.product.convert.favorite.ProductFavoriteConvert;
|
|
|
import cn.iocoder.yudao.module.product.dal.dataobject.favorite.ProductFavoriteDO;
|
|
|
+import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
|
|
|
import cn.iocoder.yudao.module.product.dal.mysql.favorite.ProductFavoriteMapper;
|
|
|
import cn.iocoder.yudao.module.product.controller.app.favorite.vo.AppFavoriteRespVO;
|
|
|
+import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.validation.Valid;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
-import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
|
|
-import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTION_EXISTS;
|
|
|
-import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTION_NOT_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_FAVORITE_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
|
* 商品收藏 Service 实现类
|
|
@@ -27,43 +33,61 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.COLLECTIO
|
|
|
@Service
|
|
|
@Validated
|
|
|
public class ProductFavoriteServiceImpl implements ProductFavoriteService {
|
|
|
-
|
|
|
+ @Resource
|
|
|
+ private ProductSpuService productSpuService;
|
|
|
@Resource
|
|
|
private ProductFavoriteMapper mapper;
|
|
|
|
|
|
@Override
|
|
|
- public Boolean collect(@Valid AppFavoriteReqVO reqVO) {
|
|
|
- // TODO @jason:userId 要从 Controller 传递过来,Service 不能有转台
|
|
|
- Long userId = getLoginUserId();
|
|
|
- // TODO @jason:代码缩进不对;
|
|
|
+ public Boolean create(Long userId, @Valid AppFavoriteReqVO reqVO) {
|
|
|
+ Assert.notNull(userId, "the userId must not be null");
|
|
|
ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
|
|
|
if (Objects.nonNull(favoriteDO)) {
|
|
|
- throw exception(COLLECTION_EXISTS);
|
|
|
+ throw exception(PRODUCT_FAVORITE_EXISTS);
|
|
|
}
|
|
|
-
|
|
|
- // TODO @jason:插入只有成功,不用判断 1
|
|
|
ProductFavoriteDO entity = ProductFavoriteConvert.INSTANCE.convert(userId, reqVO);
|
|
|
- int count = mapper.insert(entity);
|
|
|
- return count == 1;
|
|
|
+ mapper.insert(entity);
|
|
|
+ return Boolean.TRUE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Boolean cancelCollect(@Valid AppFavoriteReqVO reqVO) {
|
|
|
- // TODO @jason:代码缩进不对;
|
|
|
- Long loginUserId = getLoginUserId();
|
|
|
- ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(loginUserId, reqVO.getSpuId(), reqVO.getType());
|
|
|
+ public Boolean delete(Long userId, @Valid AppFavoriteReqVO reqVO) {
|
|
|
+ Assert.notNull(userId, "the userId must not be null ");
|
|
|
+ ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
|
|
|
if (Objects.isNull(favoriteDO)) {
|
|
|
- throw exception(COLLECTION_NOT_EXISTS);
|
|
|
+ throw exception(PRODUCT_FAVORITE_NOT_EXISTS);
|
|
|
}
|
|
|
- // TODO @jason:插入只有成功,不用判断 1
|
|
|
- int count = mapper.deleteById(favoriteDO.getId());
|
|
|
- return count == 1;
|
|
|
+ mapper.deleteById(favoriteDO.getId());
|
|
|
+ return Boolean.TRUE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public PageResult<AppFavoriteRespVO> pageCollectList(@Valid AppFavoritePageReqVO reqVO) {
|
|
|
- Long userId = getLoginUserId();
|
|
|
- return mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO);
|
|
|
+ public PageResult<AppFavoriteRespVO> page(Long userId, @Valid AppFavoritePageReqVO reqVO) {
|
|
|
+ Assert.notNull(userId, "the userId must not be null ");
|
|
|
+ PageResult<ProductFavoriteDO> favorites = mapper.selectPageByUserAndType(userId, reqVO.getType(), reqVO);
|
|
|
+ if (favorites.getTotal() > 0) {
|
|
|
+ PageResult<AppFavoriteRespVO> pageResult = new PageResult<>(favorites.getTotal());
|
|
|
+ List<ProductFavoriteDO> list = favorites.getList();
|
|
|
+ //得到商品spu 信息
|
|
|
+ List<Long> spuIds = CollectionUtils.convertList(list, ProductFavoriteDO::getSpuId);
|
|
|
+ Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(productSpuService.getSpuList(spuIds), ProductSpuDO::getId, val -> val);
|
|
|
+ List<AppFavoriteRespVO> resultList = new ArrayList<>(list.size());
|
|
|
+ for (ProductFavoriteDO item : list) {
|
|
|
+ ProductSpuDO spuDO = spuMap.get(item.getSpuId());
|
|
|
+ resultList.add(ProductFavoriteConvert.INSTANCE.convert(spuDO, item));
|
|
|
+ }
|
|
|
+ pageResult.setList(resultList);
|
|
|
+ return pageResult;
|
|
|
+ }else {
|
|
|
+ return PageResult.empty();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean checkFavorite(Long userId, @Valid AppFavoriteReqVO reqVO) {
|
|
|
+ Assert.notNull(userId, "the userId must not be null ");
|
|
|
+ ProductFavoriteDO favoriteDO = mapper.selectByUserAndSpuAndType(userId, reqVO.getSpuId(), reqVO.getType());
|
|
|
+ return Objects.nonNull(favoriteDO);
|
|
|
}
|
|
|
|
|
|
}
|