diff --git a/README.md b/README.md index 30cbff66e..721f44f7b 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ 6. 会员资料 @nengjie 7. 拼团购买 @大太阳 8. 部门管理 @Tprotect曦 +9. 商品收藏 @笑笑生 # 演示 diff --git a/docs/guides/功能列表/功能列表-H5 商城.md b/docs/guides/功能列表/功能列表-H5 商城.md index 0134b293d..160c5d984 100644 --- a/docs/guides/功能列表/功能列表-H5 商城.md +++ b/docs/guides/功能列表/功能列表-H5 商城.md @@ -12,7 +12,7 @@ - [x] 商品列表(基于分类) - [ ] 商品列表(基于促销活动) - [x] 商品详情 - - [ ] 商品收藏【待认领】 + - [ ] 商品收藏 @笑笑生 - 订单相关 - [x] 下单(直接购买) - [x] 下单(购物车购买) diff --git a/docs/sql/mall_user.sql b/docs/sql/mall_user.sql index 9d1a99528..7500b7410 100644 --- a/docs/sql/mall_user.sql +++ b/docs/sql/mall_user.sql @@ -128,4 +128,23 @@ CREATE TABLE `users` ( UNIQUE KEY `idx_uid` (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=108 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +-- ---------------------------- +-- Table structure for user_spu_collections +-- ---------------------------- +DROP TABLE IF EXISTS `user_spu_collections`; +CREATE TABLE `user_spu_collections` ( + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id自增长', + `user_id` int(11) NOT NULL COMMENT '用户id', + `nickname` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名称', + `spu_id` int(11) NOT NULL COMMENT '商品id', + `spu_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '商品名字', + `spu_image` varchar(250) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '图片名字', + `create_time` datetime(0) NOT NULL COMMENT '创建时间', + `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间', + `deleted` smallint(2) NOT NULL COMMENT '删除状态', + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户_商品_收藏记录表' ROW_FORMAT = Dynamic; + + + SET FOREIGN_KEY_CHECKS = 1; 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 new file mode 100644 index 000000000..4a6af9f86 --- /dev/null +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuCollectionController.java @@ -0,0 +1,40 @@ +package cn.iocoder.mall.product.application.controller.users; + +import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.product.api.ProductSpuCollectionService; +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.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static cn.iocoder.common.framework.vo.CommonResult.success; + +/** + * 商品收藏接口 + * @author xiaofeng + * @date 2019/07/01 23:21 + * @version 1.0 + */ +@RestController +@RequestMapping("users/spu") +@Api("商品收藏") +public class UsersProductSpuCollectionController { + + @Reference(validation = "true", version = "${dubbo.provider.ProductSpuCollectionService.version}") + private ProductSpuCollectionService productSpuCollectionService; + + @PostMapping("/collection/{spuId}/{hasCollectionType}") + @ApiOperation("商品收藏") +// @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)); + } +} diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuCollectionService.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuCollectionService.java new file mode 100644 index 000000000..130a0bf6b --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuCollectionService.java @@ -0,0 +1,19 @@ +package cn.iocoder.mall.product.api; + +/** + * 商品收藏 + * @author xiaofeng + * @date 2019/07/01 23:13 + * @version 1.0 + */ +public interface ProductSpuCollectionService { + + /** + * 商品收藏 + * @param spuId + * @param hasCollectionType 1 商品收藏 2 取消收藏 + * @param userId + * @return + */ + boolean productSpuCollection(Integer spuId,Integer hasCollectionType,Integer userId); +} diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/message/ProductSpuCollectionMessage.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/message/ProductSpuCollectionMessage.java new file mode 100644 index 000000000..685a2cc45 --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/message/ProductSpuCollectionMessage.java @@ -0,0 +1,46 @@ +package cn.iocoder.mall.product.api.message; + +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 商品收藏或取消收藏时发送消息 + * @author xiaofeng + * @date 2019/07/01 22:33 + * @version 1.0 + */ +@Data +@Accessors(chain = true) +public class ProductSpuCollectionMessage { + + public static final String TOPIC = "ProductSpuCollection"; + + /** + * SPU 编号 + */ + private Integer spuId; + + /** + * 用户ID + */ + private Integer userId; + + // ========== 基本信息 ========= + /** + * SPU 名字 + */ + private String spuName; + + + /** + * 商品图片 + */ + private String spuImage; + + /** + * 1 收藏 2 取消 + */ + private Integer hasCollectionType; + + +} diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuCollectionServiceImpl.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuCollectionServiceImpl.java new file mode 100644 index 000000000..dae127325 --- /dev/null +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuCollectionServiceImpl.java @@ -0,0 +1,56 @@ +package cn.iocoder.mall.product.service; + +import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.mall.product.api.ProductSpuCollectionService; +import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum; +import cn.iocoder.mall.product.api.message.ProductSpuCollectionMessage; +import cn.iocoder.mall.product.dao.ProductSpuMapper; +import cn.iocoder.mall.product.dataobject.ProductSpuDO; +import org.apache.rocketmq.spring.core.RocketMQTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * ProductSpuCollectionServiceImpl + * @author xiaofeng + * @date 2019/07/01 23:14 + * @version 1.0 + */ +@Service // 实际上不用添加。添加的原因是,必须 Spring 报错提示 +@org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.ProductSpuCollectionService.version}") +public class ProductSpuCollectionServiceImpl implements ProductSpuCollectionService { + + @Autowired + private ProductSpuMapper productSpuMapper; + + @Resource + private RocketMQTemplate rocketMQTemplate; + + + @Override + public boolean productSpuCollection(Integer spuId, Integer hasCollectionType, Integer userId) { + ProductSpuDO productSpuDO = this.productSpuMapper.selectById(spuId); + // 校验 Spu 是否存在 + if (productSpuDO == null) { + throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_SPU_NOT_EXISTS.getCode()); + } + this.sendProductSpuCollectionMessage(productSpuDO, hasCollectionType, userId); + return Boolean.TRUE; + } + + /** + * 发送商品收藏或取消消息 + * @param productSpuDO + * @param hasCollectionType + */ + private void sendProductSpuCollectionMessage(final ProductSpuDO productSpuDO, final Integer hasCollectionType, + final Integer userId) { + ProductSpuCollectionMessage productSpuCollectionMessage = new ProductSpuCollectionMessage() + .setSpuId(productSpuDO.getId()).setSpuName(productSpuDO.getName()) + .setSpuImage(productSpuDO.getPicUrls()).setHasCollectionType(hasCollectionType) + .setUserId(userId); + rocketMQTemplate.convertAndSend(ProductSpuCollectionMessage.TOPIC, productSpuCollectionMessage); + } +} diff --git a/product/product-service-impl/src/main/resources/config/application.yaml b/product/product-service-impl/src/main/resources/config/application.yaml index 65c2ec48a..33e49f0f5 100644 --- a/product/product-service-impl/src/main/resources/config/application.yaml +++ b/product/product-service-impl/src/main/resources/config/application.yaml @@ -35,6 +35,8 @@ dubbo: version: 1.0.0 OAuth2Service: version: 1.0.0 + ProductSpuCollectionService: + version: 1.0.0 # rocketmq rocketmq: 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 new file mode 100644 index 000000000..95693ea23 --- /dev/null +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserProductSpuCollectionsService.java @@ -0,0 +1,36 @@ +package cn.iocoder.mall.user.api; + +import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO; + +/** + * UserProductSpuCollectionsService + * @author xiaofeng + * @date 2019/07/01 20:27 + * @version 1.0 + */ +public interface UserProductSpuCollectionsService { + + /** + * 添加商品收藏 + * @return + */ + int addUserSkuCollections(UserProductSpuCollectionsAddDTO userProductSpuCollectionsAddDTO); + + /** + * 获取用户商品收藏 + * @param userId 用户ID + * @param spuId 商品ID + * @return + */ + UserProductSpuCollectionsBO getUserSpuCollectionsByUserIdAndSpuId(Integer userId, Integer spuId); + + /** + * 取消商品收藏 + * @param userProductSpuCollectionsUpdateDTO + * @return + */ + int updateUserProductSpuCollections(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO); + +} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserProductSpuCollectionsBO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserProductSpuCollectionsBO.java new file mode 100644 index 000000000..694cd73a0 --- /dev/null +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserProductSpuCollectionsBO.java @@ -0,0 +1,64 @@ +package cn.iocoder.mall.user.api.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + +/** + * 用户_商品_收藏记录表 + * @author xiaofeng + * @date 2019-07-01 20:23:30 + */ +@Data +@Accessors(chain = true) +public class UserProductSpuCollectionsBO implements Serializable { + + /** + * id自增长 + */ + private Integer id; + + /** + * 用户id + */ + private Integer userId; + + /** + * 用户名称 + */ + private String nickname; + + /** + * 商品id + */ + private Integer spuId; + + /** + * 商品名字 + */ + private String spuName; + + /** + * 图片名字 + */ + private String spuImage; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新时间 + */ + private Date updateTime; + + /** + * 删除状态 + */ + private Integer deleted; + + +} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsAddDTO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsAddDTO.java new file mode 100644 index 000000000..e949c3d1b --- /dev/null +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsAddDTO.java @@ -0,0 +1,65 @@ +package cn.iocoder.mall.user.api.dto; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + +/** + * 添加商品收藏参数 + * @author xiaofeng + * @date 2019/07/01 20:38 + * @version 1.0 + */ +@Data +@Accessors(chain = true) +public class UserProductSpuCollectionsAddDTO implements Serializable { + + /** + * id自增长 + */ + private Integer id; + + /** + * 用户id + */ + private Integer userId; + + /** + * 用户名称 + */ + private String nickname; + + /** + * 商品id + */ + private Integer spuId; + + /** + * 商品名字 + */ + private String spuName; + + /** + * 图片名字 + */ + private String spuImage; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新时间 + */ + private Date updateTime; + + /** + * 删除状态 + */ + private Integer deleted; + + +} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsUpdateDTO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsUpdateDTO.java new file mode 100644 index 000000000..7dfd501a3 --- /dev/null +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsUpdateDTO.java @@ -0,0 +1,35 @@ +package cn.iocoder.mall.user.api.dto; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + +/** + * 更新商品收藏参数 + * @author xiaofeng + * @date 2019/07/01 20:38 + * @version 1.0 + */ +@Data +@Accessors(chain = true) +public class UserProductSpuCollectionsUpdateDTO implements Serializable { + + /** + * id自增长 + */ + private Integer id; + + /** + * 更新时间 + */ + private Date updateTime; + + /** + * 删除状态 + */ + private Integer deleted; + + +} diff --git a/user/user-service-impl/pom.xml b/user/user-service-impl/pom.xml index 7f0f6123e..c3c92503d 100644 --- a/user/user-service-impl/pom.xml +++ b/user/user-service-impl/pom.xml @@ -17,6 +17,12 @@ 1.0-SNAPSHOT + + cn.iocoder.mall + product-service-api + 1.0-SNAPSHOT + + mysql @@ -67,6 +73,12 @@ curator-framework + + + org.apache.rocketmq + rocketmq-spring-boot-starter + + diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/convert/UserProductSpuCollectionsConvert.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/convert/UserProductSpuCollectionsConvert.java new file mode 100644 index 000000000..fbbafbd89 --- /dev/null +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/convert/UserProductSpuCollectionsConvert.java @@ -0,0 +1,66 @@ +package cn.iocoder.mall.user.biz.convert; + +import cn.iocoder.mall.product.api.message.ProductSpuCollectionMessage; +import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO; +import cn.iocoder.mall.user.biz.dataobject.UserProductSpuCollectionsDO; +import org.mapstruct.Mapper; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +/** + * 用户_商品_收藏记录表 + * + * @author xiaofeng + * @date 2019-07-01 20:23:30 + */ +@Mapper +public interface UserProductSpuCollectionsConvert { + + UserProductSpuCollectionsConvert INSTANCE = Mappers.getMapper(UserProductSpuCollectionsConvert.class); + + /** + * DTO convert DO + * @param userSkuCollectionsAddDTO + * @return + */ + @Mappings({}) + UserProductSpuCollectionsDO convert(UserProductSpuCollectionsAddDTO userSkuCollectionsAddDTO); + + /** + * update DTO convert DO + * @param userProductSpuCollectionsUpdateDTO + * @return + */ + @Mappings({}) + UserProductSpuCollectionsDO convert(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO); + + /** + * DO Convert BO + * @param userSkuCollectionsDO + * @return + */ + @Mappings({}) + UserProductSpuCollectionsBO convert(UserProductSpuCollectionsDO userSkuCollectionsDO); + + /** + * DO List convert BO LIST + * @param userSkuCollectionsDOS + * @return + */ + @Mappings({}) + List convert(List userSkuCollectionsDOS); + + /** + * 消处数据转换 + * @param productSpuCollectionMessage + * @return + */ + @Mappings({}) + UserProductSpuCollectionsAddDTO convert(ProductSpuCollectionMessage productSpuCollectionMessage); + + +} 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 new file mode 100644 index 000000000..c94a28d4a --- /dev/null +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserProductSpuCollectionsMapper.java @@ -0,0 +1,28 @@ +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; + +/** + * 用户_商品_收藏记录表 + * + * @author xiaofeng + * @date 2019-07-01 20:23:30 + */ +public interface UserProductSpuCollectionsMapper extends BaseMapper { + + /** + * 根据用户id 和 spuId 查找用户商品收藏 + * @param userId + * @param spuId + * @return + */ + default UserProductSpuCollectionsDO getUserSpuCollectionsByUserIdAndSpuId(final Integer userId, + final Integer spuId) { + QueryWrapper query = new QueryWrapper() + .eq("user_id", userId).eq("spu_id", spuId); + return selectOne(query); + } + +} diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserProductSpuCollectionsDO.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserProductSpuCollectionsDO.java new file mode 100644 index 000000000..bda45a973 --- /dev/null +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserProductSpuCollectionsDO.java @@ -0,0 +1,69 @@ +package cn.iocoder.mall.user.biz.dataobject; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + + +/** + * 用户_商品_收藏记录表 + * + * @author xiaofeng + * @date 2019-07-01 20:23:30 + */ +@TableName("user_spu_collections") +@Data +@Accessors(chain = true) +public class UserProductSpuCollectionsDO implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * id自增长 + */ + private Integer id; + + /** + * 用户id + */ + private Integer userId; + + /** + * 用户名称 + */ + private String nickname; + + /** + * 商品id + */ + private Integer spuId; + + /** + * 商品名字 + */ + private String spuName; + + /** + * 图片名字 + */ + private String spuImage; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新时间 + */ + private Date updateTime; + + /** + * 删除状态 + */ + private Integer deleted; + + +} diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/mq/UserProductSpuCollectionsConsumer.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/mq/UserProductSpuCollectionsConsumer.java new file mode 100644 index 000000000..967bae562 --- /dev/null +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/mq/UserProductSpuCollectionsConsumer.java @@ -0,0 +1,124 @@ +package cn.iocoder.mall.user.biz.mq; + +import cn.iocoder.common.framework.constant.DeletedStatusEnum; +import cn.iocoder.common.framework.util.ServiceExceptionUtil; +import cn.iocoder.mall.product.api.message.ProductSpuCollectionMessage; +import cn.iocoder.mall.user.api.UserProductSpuCollectionsService; +import cn.iocoder.mall.user.api.UserService; +import cn.iocoder.mall.user.api.bo.UserBO; +import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO; +import cn.iocoder.mall.user.api.constant.UserErrorCodeEnum; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsUpdateDTO; +import cn.iocoder.mall.user.biz.convert.UserProductSpuCollectionsConvert; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQListener; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; + +/** + * 商品收藏 消费者 + * @author xiaofeng + * @date 2019/07/02 19:57 + * @version 1.0 + */ +@Service +@RocketMQMessageListener(topic = ProductSpuCollectionMessage.TOPIC, consumerGroup = "product-spu-consumer-group-" + + ProductSpuCollectionMessage.TOPIC) +public class UserProductSpuCollectionsConsumer implements RocketMQListener { + + @Autowired + private UserProductSpuCollectionsService userProductSpuCollectionsService; + + @Autowired + private UserService userService; + + @Override + public void onMessage(ProductSpuCollectionMessage productSpuCollectionMessage) { + UserBO userBO = userService.getUser(productSpuCollectionMessage.getUserId()); + if (userBO == null) { + throw ServiceExceptionUtil.exception(UserErrorCodeEnum.USER_NOT_EXISTS.getCode()); + } + // 收藏 + if (productSpuCollectionMessage.getHasCollectionType().equals(1)) { + this.saveUserProductSpuCollections(productSpuCollectionMessage, userBO.getNickname()); + } else if (productSpuCollectionMessage.getHasCollectionType().equals(2)) { + // 取消收藏 + this.deleteUserProductSpuCollections(productSpuCollectionMessage.getUserId(), + productSpuCollectionMessage.getSpuId()); + } + + } + + /** + * 保存商品收藏 + * @param productSpuCollectionMessage + * @param nickname + * @return + */ + private int saveUserProductSpuCollections(final ProductSpuCollectionMessage productSpuCollectionMessage, + final String nickname) { + int result = 0; + UserProductSpuCollectionsBO userProductSpuCollectionsBO = this.userProductSpuCollectionsService + .getUserSpuCollectionsByUserIdAndSpuId(productSpuCollectionMessage.getUserId(), + productSpuCollectionMessage.getSpuId()); + if (userProductSpuCollectionsBO == null) { + UserProductSpuCollectionsAddDTO userProductSpuCollectionsAddDTO = UserProductSpuCollectionsConvert.INSTANCE + .convert(productSpuCollectionMessage); + userProductSpuCollectionsAddDTO.setNickname(StringUtils.isEmpty(nickname) ? "" : nickname); + userProductSpuCollectionsAddDTO.setCreateTime(new Date()); + userProductSpuCollectionsAddDTO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); + result = userProductSpuCollectionsService.addUserSkuCollections(userProductSpuCollectionsAddDTO); + } else { + // 存在重新收藏 + if (userProductSpuCollectionsBO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) { + UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO = this + .setUserProductSpuCollectionsUpdateDTO(userProductSpuCollectionsBO.getId(), + DeletedStatusEnum.DELETED_NO); + result = this.userProductSpuCollectionsService + .updateUserProductSpuCollections(userProductSpuCollectionsUpdateDTO); + } + } + return result; + } + + /** + * 取消收藏 + * @param userId + * @param spuId + * @return + */ + private int deleteUserProductSpuCollections(final Integer userId, final Integer spuId) { + UserProductSpuCollectionsBO userProductSpuCollectionsBO = this.userProductSpuCollectionsService + .getUserSpuCollectionsByUserIdAndSpuId(userId, spuId); + int result = 0; + if (userProductSpuCollectionsBO != null) { + // 未取消收藏的数据 + if (userProductSpuCollectionsBO.getDeleted().equals(DeletedStatusEnum.DELETED_NO.getValue())) { + UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO = this + .setUserProductSpuCollectionsUpdateDTO(userProductSpuCollectionsBO.getId(), + DeletedStatusEnum.DELETED_YES); + result = this.userProductSpuCollectionsService + .updateUserProductSpuCollections(userProductSpuCollectionsUpdateDTO); + } + } + return result; + } + + /** + * 设置更新值 + * @param id + * @param deletedStatusEnum + * @return + */ + private UserProductSpuCollectionsUpdateDTO setUserProductSpuCollectionsUpdateDTO(final Integer id, + final DeletedStatusEnum deletedStatusEnum) { + return new UserProductSpuCollectionsUpdateDTO().setId(id).setUpdateTime(new Date()) + .setDeleted(deletedStatusEnum.getValue()); + } + + +} 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 new file mode 100644 index 000000000..8722bea34 --- /dev/null +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserProductSpuCollectionsServiceImpl.java @@ -0,0 +1,48 @@ +package cn.iocoder.mall.user.biz.service; + +import cn.iocoder.mall.user.api.UserProductSpuCollectionsService; +import cn.iocoder.mall.user.api.bo.UserProductSpuCollectionsBO; +import cn.iocoder.mall.user.api.dto.UserProductSpuCollectionsAddDTO; +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; + +/** + * UserSkuCollectionsServiceImpl + * @author xiaofeng + * @date 2019/07/01 21:02 + * @version 1.0 + */ +@Service +@org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.UserProductSpuCollectionsService.version}") +public class UserProductSpuCollectionsServiceImpl implements UserProductSpuCollectionsService { + + + @Autowired + private UserProductSpuCollectionsMapper userProductSpuCollectionsMapper; + + + @Override + public int addUserSkuCollections(UserProductSpuCollectionsAddDTO userProductSpuCollectionsAddDTO) { + return userProductSpuCollectionsMapper + .insert(UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsAddDTO)); + } + + @Override + public UserProductSpuCollectionsBO getUserSpuCollectionsByUserIdAndSpuId(Integer userId, Integer spuId) { + UserProductSpuCollectionsDO userProductSpuCollectionsDO = userProductSpuCollectionsMapper + .getUserSpuCollectionsByUserIdAndSpuId(userId, spuId); + return UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsDO); + } + + @Override + public int updateUserProductSpuCollections(UserProductSpuCollectionsUpdateDTO userProductSpuCollectionsUpdateDTO) { + return userProductSpuCollectionsMapper + .updateById(UserProductSpuCollectionsConvert.INSTANCE.convert(userProductSpuCollectionsUpdateDTO)); + } + + +} diff --git a/user/user-service-impl/src/main/resources/config/application.yaml b/user/user-service-impl/src/main/resources/config/application.yaml index 9d394cb66..aa93e91a6 100644 --- a/user/user-service-impl/src/main/resources/config/application.yaml +++ b/user/user-service-impl/src/main/resources/config/application.yaml @@ -39,6 +39,14 @@ dubbo: version: 1.0.0 UserService: version: 1.0.0 + UserProductSpuCollectionsService: + version: 1.0.0 consumer: OAuth2Service: version: 1.0.0 + +# rocketmq +rocketmq: + name-server: 127.0.0.1:9876 + producer: + group: user-producer-spu-collection-group