From ab0438285206643adf7e21802024ff0c14cdd0f8 Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Wed, 3 Jul 2019 00:01:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E6=94=B6?= =?UTF-8?q?=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: