diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuCollectionController.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuCollectionController.java index 4a6af9f86..39de1d967 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuCollectionController.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuCollectionController.java @@ -30,11 +30,10 @@ public class UsersProductSpuCollectionController { @PostMapping("/collection/{spuId}/{hasCollectionType}") @ApiOperation("商品收藏") -// @RequiresLogin + @RequiresLogin public CommonResult productSpuCollection(@PathVariable("spuId") Integer spuId, @PathVariable("hasCollectionType") Integer hasCollectionType) { -// final Integer userId = UserSecurityContextHolder.getContext().getUserId(); - - return success(productSpuCollectionService.productSpuCollection(spuId, hasCollectionType,140)); + final Integer userId = UserSecurityContextHolder.getContext().getUserId(); + return success(productSpuCollectionService.productSpuCollection(spuId, hasCollectionType,userId)); } } diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserFavoriteController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserFavoriteController.java new file mode 100644 index 000000000..4869b166f --- /dev/null +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserFavoriteController.java @@ -0,0 +1,46 @@ +package cn.iocoder.mall.user.application.controller.users; + +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.user.api.UserProductSpuCollectionsService; +import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsPageBO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsPageDTO; +import cn.iocoder.mall.user.sdk.annotation.RequiresLogin; +import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.apache.dubbo.config.annotation.Reference; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +/** + * 用户收藏 + * @author xiaofeng + * @date 2019/07/07 11:06 + * @version 1.0 + */ +@RestController +@RequestMapping("users/favorite") +@Api("用户收藏") +public class UserFavoriteController { + + @Reference(validation = "true", version = "${dubbo.provider.UserProductSpuCollectionsService.version}") + private UserProductSpuCollectionsService userProductSpuCollectionsService; + + @GetMapping("page") + @RequiresLogin + @ApiOperation("用户商品收藏列表") + public CommonResult getUserProductSpuCollectionsPage( + @Validated UserProductSpuCollectionsPageDTO userProductSpuCollectionsPageDTO) { + final Integer userId = UserSecurityContextHolder.getContext().getUserId(); + userProductSpuCollectionsPageDTO.setUserId(userId); + return userProductSpuCollectionsService.getUserProductSpuCollectionsPage(userProductSpuCollectionsPageDTO); + } + + @DeleteMapping("remove") + @RequiresLogin + @ApiOperation(value = "用户商品收藏-删除") + public CommonResult removeFavorite(@RequestParam("spuId") final Integer spuId) { + final Integer userId = UserSecurityContextHolder.getContext().getUserId(); + return userProductSpuCollectionsService.deleteUserProductSpuCollections(userId, spuId); + } +} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserProductSpuCollectionsService.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserProductSpuCollectionsService.java index 95693ea23..ff500c084 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserProductSpuCollectionsService.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserProductSpuCollectionsService.java @@ -1,7 +1,10 @@ package cn.iocoder.mall.user.api; +import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO; +import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsPageBO; import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsPageDTO; import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO; /** @@ -33,4 +36,19 @@ public interface UserProductSpuCollectionsService { */ int updateUserProductSpuCollections(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO); + /** + * 获取用户收藏列表数据 + * @param userProductSpuCollectionsPageDTO + * @return + */ + CommonResult getUserProductSpuCollectionsPage( + UserProductSpuCollectionsPageDTO userProductSpuCollectionsPageDTO); + + /** + * 删除收藏数据 + * @param userId + * @param spuId + * @return + */ + CommonResult deleteUserProductSpuCollections(Integer userId, Integer spuId); } diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserProductSpuCollectionsPageBO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserProductSpuCollectionsPageBO.java new file mode 100644 index 000000000..ccfeddf4e --- /dev/null +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserProductSpuCollectionsPageBO.java @@ -0,0 +1,30 @@ +package cn.iocoder.mall.user.api.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.List; + +/** + * 商品收藏分页 + * @author xiaofeng + * @date 2019/07/06 18:37 + * @version 1.0 + */ +@Data +@Accessors(chain = true) +public class UserProductSpuCollectionsPageBO implements Serializable { + + /** + * 返回的数据列表 + */ + private List list; + + /** + * 总量 + */ + private Integer total; + + +} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsPageDTO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsPageDTO.java new file mode 100644 index 000000000..484234a88 --- /dev/null +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsPageDTO.java @@ -0,0 +1,35 @@ +package cn.iocoder.mall.user.api.dto; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * 商品收藏分页参数 + * @author xiaofeng + * @date 2019/07/06 18:40 + * @version 1.0 + */ +@Data +@Accessors(chain = true) +public class UserProductSpuCollectionsPageDTO implements Serializable { + + /** + * 用户ID + */ + private Integer userId; + + /** + * 当前页 + */ + @NotNull(message = "页码不能为空") + private Integer pageNo; + + /** + * 每页显示的条数 + */ + @NotNull(message = "每页条数不能为空") + private Integer pageSize; +} diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserProductSpuCollectionsMapper.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserProductSpuCollectionsMapper.java index c94a28d4a..411a01bc6 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserProductSpuCollectionsMapper.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserProductSpuCollectionsMapper.java @@ -3,6 +3,9 @@ package cn.iocoder.mall.user.biz.dao; import cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; /** * 用户_商品_收藏记录表 @@ -25,4 +28,22 @@ public interface UserProductSpuCollectionsMapper extends BaseMapper selectListByUser(@Param("userId") Integer userId, @Param("offset") Integer offset, + @Param("limit") Integer limit); + + /** + * 根据用户ID 查找总数 + * @param userId + * @return + */ + Integer selectCountByUser(Integer userId); + } diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserProductSpuCollectionsServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserProductSpuCollectionsServiceImpl.java index 8722bea34..b2dd16519 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserProductSpuCollectionsServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserProductSpuCollectionsServiceImpl.java @@ -1,14 +1,23 @@ package cn.iocoder.mall.user.biz.service; +import cn.iocoder.common.framework.constant.DeletedStatusEnum; +import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.user.api.UserProductSpuCollectionsService; import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO; +import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsPageBO; import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsPageDTO; import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO; import cn.iocoder.mall.user.biz.convert.UserProductSpuCollectionsConvert; import cn.iocoder.mall.user.biz.dao.UserProductSpuCollectionsMapper; import cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.Collections; +import java.util.Date; +import java.util.List; /** * UserSkuCollectionsServiceImpl @@ -44,5 +53,54 @@ public class UserProductSpuCollectionsServiceImpl implements UserProductSpuColle .updateById(UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsUpdateDTO)); } + @Override + public CommonResult getUserProductSpuCollectionsPage( + UserProductSpuCollectionsPageDTO userProductSpuCollectionsPageDTO) { + final int offset = + (userProductSpuCollectionsPageDTO.getPageNo() - 1) * userProductSpuCollectionsPageDTO.getPageSize(); + final int totalCount = this.userProductSpuCollectionsMapper + .selectCountByUser(userProductSpuCollectionsPageDTO.getUserId()); + if (totalCount == 0) { + return CommonResult + .success(new UserProductSpuCollectionsPageBO().setList(Collections.emptyList()).setTotal(0)); + } + List list = userProductSpuCollectionsMapper + .selectListByUser(userProductSpuCollectionsPageDTO.getUserId(), offset, + userProductSpuCollectionsPageDTO.getPageSize()); + if (CollectionUtils.isEmpty(list)) { + return CommonResult.success( + new UserProductSpuCollectionsPageBO().setList(Collections.emptyList()).setTotal(totalCount)); + } + + UserProductSpuCollectionsPageBO userProductSpuCollectionsPageBO = new UserProductSpuCollectionsPageBO(); + userProductSpuCollectionsPageBO.setList(UserProductSpuCollectionsConvert.INSTANCE.convert(list)); + // 查询分页总数 + userProductSpuCollectionsPageBO.setTotal(totalCount); + // 返回结果 + return CommonResult.success(userProductSpuCollectionsPageBO); + } + + + /** + * 取消收藏 + * @param userId + * @param spuId + * @return + */ + @Override + public CommonResult deleteUserProductSpuCollections(final Integer userId, final Integer spuId) { + UserProductSpuCollectionsBO userProductSpuCollectionsBO = this + .getUserSpuCollectionsByUserIdAndSpuId(userId, spuId); + int result = 0; + if (userProductSpuCollectionsBO != null) { + // 未取消收藏的数据 + if (userProductSpuCollectionsBO.getDeleted().equals(DeletedStatusEnum.DELETED_NO.getValue())) { + UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO = new UserProductSpuCollectionsUpdateDTO().setId(userProductSpuCollectionsBO.getId()) + .setUpdateTime(new Date()).setDeleted(DeletedStatusEnum.DELETED_YES.getValue()); + result = this.updateUserProductSpuCollections(userProductSpuCollectionsUpdateDTO); + } + } + return CommonResult.success(result > 0 ? Boolean.TRUE : Boolean.FALSE); + } } diff --git a/user/user-service-impl/src/main/resources/mapper/UserProductSpuCollectionsMapper.xml b/user/user-service-impl/src/main/resources/mapper/UserProductSpuCollectionsMapper.xml new file mode 100644 index 000000000..fb0287887 --- /dev/null +++ b/user/user-service-impl/src/main/resources/mapper/UserProductSpuCollectionsMapper.xml @@ -0,0 +1,42 @@ + + + + + + id, user_id, nickname, spu_id, spu_name, + spu_image, create_time, update_time, + deleted + + + + + + + + + +