From 9b2d96d247185c5149164710330e129ad89d36a9 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 29 Jun 2019 16:23:36 +0800 Subject: [PATCH 1/6] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3=20?= =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0=E4=BB=BB=E5=8A=A1=E5=88=97=E8=A1=A8?= =?UTF-8?q?=EF=BC=8C=E6=A0=87=E8=AE=B0=E8=B0=81=E5=9C=A8=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E4=BB=80=E4=B9=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + docs/guides/功能列表/功能列表-H5 商城.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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] 下单(购物车购买) From d1de3aaaa1f6521f415404e65e1e546d1f2d667e Mon Sep 17 00:00:00 2001 From: "190601564@qq.com" <190601564@qq.com> Date: Sun, 30 Jun 2019 10:57:55 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=94=A8=E5=95=86?= =?UTF-8?q?=E5=93=81=E6=94=B6=E8=97=8F=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/sql/mall_user.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/sql/mall_user.sql b/docs/sql/mall_user.sql index 9d1a99528..59899ecd7 100644 --- a/docs/sql/mall_user.sql +++ b/docs/sql/mall_user.sql @@ -128,4 +128,21 @@ 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_sku_collections +-- ---------------------------- +DROP TABLE IF EXISTS `user_sku_collections`; +CREATE TABLE `user_sku_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 '用户名称', + `sku_id` int(11) NOT NULL COMMENT '商品id', + `sku_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '商品名字', + `sku_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 = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户_商品_收藏记录表' ROW_FORMAT = Dynamic; + SET FOREIGN_KEY_CHECKS = 1; From 8a4115e8eabae8ff89f4c32611a1f81736970ae9 Mon Sep 17 00:00:00 2001 From: "190601564@qq.com" <190601564@qq.com> Date: Sun, 30 Jun 2019 11:03:57 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/sql/mall_user.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/sql/mall_user.sql b/docs/sql/mall_user.sql index 59899ecd7..cdebac444 100644 --- a/docs/sql/mall_user.sql +++ b/docs/sql/mall_user.sql @@ -145,4 +145,6 @@ CREATE TABLE `user_sku_collections` ( PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户_商品_收藏记录表' ROW_FORMAT = Dynamic; + + SET FOREIGN_KEY_CHECKS = 1; From 41cd57df2dc57839f5987489fbf08005f2570095 Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Tue, 2 Jul 2019 23:47:52 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/sql/mall_user.sql | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/sql/mall_user.sql b/docs/sql/mall_user.sql index cdebac444..7500b7410 100644 --- a/docs/sql/mall_user.sql +++ b/docs/sql/mall_user.sql @@ -129,21 +129,21 @@ CREATE TABLE `users` ( ) ENGINE=InnoDB AUTO_INCREMENT=108 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; -- ---------------------------- --- Table structure for user_sku_collections +-- Table structure for user_spu_collections -- ---------------------------- -DROP TABLE IF EXISTS `user_sku_collections`; -CREATE TABLE `user_sku_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 '用户名称', - `sku_id` int(11) NOT NULL COMMENT '商品id', - `sku_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '商品名字', - `sku_image` varchar(250) 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 = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户_商品_收藏记录表' ROW_FORMAT = Dynamic; +) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户_商品_收藏记录表' ROW_FORMAT = Dynamic; From 5c2c4da6fa1b8fedf9a12deeccba1101d93bc1eb Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Tue, 2 Jul 2019 23:57:52 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81?= =?UTF-8?q?=E6=94=B6=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/UserProductSpuCollectionsService.java | 36 +++++ .../api/bo/UserProductSpuCollectionsBO.java | 64 +++++++++ .../dto/UserProductSpuCollectionsAddDTO.java | 65 +++++++++ .../UserProductSpuCollectionsUpdateDTO.java | 35 +++++ user/user-service-impl/pom.xml | 12 ++ .../UserProductSpuCollectionsConvert.java | 66 ++++++++++ .../dao/UserProductSpuCollectionsMapper.java | 28 ++++ .../UserProductSpuCollectionsDO.java | 69 ++++++++++ .../mq/UserProductSpuCollectionsConsumer.java | 124 ++++++++++++++++++ .../UserProductSpuCollectionsServiceImpl.java | 48 +++++++ .../main/resources/config/application.yaml | 8 ++ 11 files changed, 555 insertions(+) create mode 100644 user/user-service-api/src/main/java/cn/iocoder/mall/user/api/UserProductSpuCollectionsService.java create mode 100644 user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserProductSpuCollectionsBO.java create mode 100644 user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsAddDTO.java create mode 100644 user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserProductSpuCollectionsUpdateDTO.java create mode 100644 user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/convert/UserProductSpuCollectionsConvert.java create mode 100644 user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserProductSpuCollectionsMapper.java create mode 100644 user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserProductSpuCollectionsDO.java create mode 100644 user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/mq/UserProductSpuCollectionsConsumer.java create mode 100644 user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserProductSpuCollectionsServiceImpl.java 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 From ab0438285206643adf7e21802024ff0c14cdd0f8 Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Wed, 3 Jul 2019 00:01:24 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81?= =?UTF-8?q?=E6=94=B6=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UsersProductSpuCollectionController.java | 40 +++++++++++++ .../api/ProductSpuCollectionService.java | 19 +++++++ .../message/ProductSpuCollectionMessage.java | 46 +++++++++++++++ .../ProductSpuCollectionServiceImpl.java | 56 +++++++++++++++++++ .../main/resources/config/application.yaml | 2 + 5 files changed, 163 insertions(+) create mode 100644 product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuCollectionController.java create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuCollectionService.java create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/message/ProductSpuCollectionMessage.java create mode 100644 product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuCollectionServiceImpl.java 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: