后端:增加商品修改时,发送 MQ 消息

后端:增加搜索服务,监听 MQ 消息,建立商品索引
This commit is contained in:
YunaiV
2019-04-25 20:12:01 +08:00
parent cddffabeba
commit f529985c40
41 changed files with 642 additions and 251 deletions

View File

@@ -42,6 +42,7 @@ public class UsersProductSpuController {
@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"),
})
@PermitAll
@Deprecated // 使用商品搜索接口
public CommonResult<UsersProductSpuPageVO> page(@RequestParam(value = "cid", required = false) Integer cid,
@RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

View File

@@ -5,6 +5,7 @@ import cn.iocoder.mall.product.api.bo.ProductCategoryBO;
import cn.iocoder.mall.product.api.dto.ProductCategoryAddDTO;
import cn.iocoder.mall.product.api.dto.ProductCategoryUpdateDTO;
import java.util.Collection;
import java.util.List;
public interface ProductCategoryService {
@@ -15,6 +16,14 @@ public interface ProductCategoryService {
*/
List<ProductCategoryBO> getListByPid(Integer pid);
/**
* 获得商品分类数组
*
* @param ids 商品分类编号
* @return 数组
*/
List<ProductCategoryBO> getListByIds(Collection<Integer> ids);
/**
* @return 返回所有产品分类们
*/
@@ -28,4 +37,4 @@ public interface ProductCategoryService {
CommonResult<Boolean> deleteProductCategory(Integer admin, Integer productCategoryId);
}
}

View File

@@ -0,0 +1,20 @@
package cn.iocoder.mall.product.api.message;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 商品更新(包括创建)消息
*/
@Data
@Accessors(chain = true)
public class ProductUpdateMessage {
public static final String TOPIC = "ProductUpdate";
/**
* 商品编号
*/
private Integer id;
}

View File

@@ -42,6 +42,11 @@
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -4,6 +4,7 @@ import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
@Repository
@@ -16,8 +17,10 @@ public interface ProductCategoryMapper {
ProductCategoryDO selectById(@Param("id") Integer id);
List<ProductCategoryDO> selectByIds(@Param("ids") Collection<Integer> ids);
void insert(ProductCategoryDO productCategoryDO);
int update(ProductCategoryDO productCategoryDO);
}
}

View File

@@ -16,6 +16,7 @@ import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@@ -32,6 +33,12 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
return ProductCategoryConvert.INSTANCE.convertToBO(categoryList);
}
@Override
public List<ProductCategoryBO> getListByIds(Collection<Integer> ids) {
List<ProductCategoryDO> categoryList = productCategoryMapper.selectByIds(ids);
return ProductCategoryConvert.INSTANCE.convertToBO(categoryList);
}
@Override
public CommonResult<List<ProductCategoryBO>> getAll() {
List<ProductCategoryDO> categoryList = productCategoryMapper.selectList();

View File

@@ -13,17 +13,20 @@ import cn.iocoder.mall.product.api.dto.ProductSkuAddOrUpdateDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuPageDTO;
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
import cn.iocoder.mall.product.api.message.ProductUpdateMessage;
import cn.iocoder.mall.product.convert.ProductSpuConvert;
import cn.iocoder.mall.product.dao.ProductSkuMapper;
import cn.iocoder.mall.product.dao.ProductSpuMapper;
import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
import cn.iocoder.mall.product.dataobject.ProductSkuDO;
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 org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@@ -41,6 +44,9 @@ public class ProductSpuServiceImpl implements ProductSpuService {
@Autowired
private ProductAttrServiceImpl productAttrService;
@Resource
private RocketMQTemplate rocketMQTemplate;
// @Override
// public ProductSpuBO getProductSpuDetail(Integer id) {
// ProductSpuDO productSpuDO = productSpuMapper.selectById(id);
@@ -82,10 +88,20 @@ public class ProductSpuServiceImpl implements ProductSpuService {
return CommonResult.success(spus);
}
@SuppressWarnings("Duplicates")
@Override
@Transactional
public CommonResult<ProductSpuDetailBO> addProductSpu(Integer adminId, ProductSpuAddDTO productSpuAddDTO) {
CommonResult<ProductSpuDetailBO> result = addProductSpu0(adminId, productSpuAddDTO);
// 如果新增生成,发送创建商品 Topic 消息
if (result.isSuccess()) {
// TODO 芋艿,先不考虑事务的问题。等后面的 fescar 一起搞
sendProductUpdateMessage(result.getData().getId());
}
return result;
}
@SuppressWarnings("Duplicates")
@Transactional
public CommonResult<ProductSpuDetailBO> addProductSpu0(Integer adminId, ProductSpuAddDTO productSpuAddDTO) {
// 校验商品分类分类存在
CommonResult<ProductCategoryDO> validCategoryResult = productCategoryService.validProductCategory(productSpuAddDTO.getCid());
if (validCategoryResult.isError()) {
@@ -129,10 +145,19 @@ public class ProductSpuServiceImpl implements ProductSpuService {
validCategoryResult.getData()));
}
@SuppressWarnings("Duplicates")
@Override
@Transactional
public CommonResult<Boolean> updateProductSpu(Integer adminId, ProductSpuUpdateDTO productSpuUpdateDTO) {
CommonResult<Boolean> result = updateProductSpu0(adminId, productSpuUpdateDTO);
if (result.isSuccess()) {
// TODO 芋艿,先不考虑事务的问题。等后面的 fescar 一起搞
sendProductUpdateMessage(productSpuUpdateDTO.getId());
}
return result;
}
@SuppressWarnings("Duplicates")
@Transactional
public CommonResult<Boolean> updateProductSpu0(Integer adminId, ProductSpuUpdateDTO productSpuUpdateDTO) {
// 校验 Spu 是否存在
if (productSpuMapper.selectById(productSpuUpdateDTO.getId()) == null) {
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_SPU_NOT_EXISTS.getCode());
@@ -208,6 +233,8 @@ public class ProductSpuServiceImpl implements ProductSpuService {
// 更新排序
ProductSpuDO updateSpu = new ProductSpuDO().setId(spuId).setSort(sort);
productSpuMapper.update(updateSpu);
// 修改成功,发送商品 Topic 消息
sendProductUpdateMessage(spuId);
// 返回成功
return CommonResult.success(true);
}
@@ -329,4 +356,8 @@ public class ProductSpuServiceImpl implements ProductSpuService {
spu.setQuantity(skus.stream().mapToInt(ProductSkuAddOrUpdateDTO::getQuantity).sum()); // 求库存之和
}
private void sendProductUpdateMessage(Integer id) {
rocketMQTemplate.convertAndSend(ProductUpdateMessage.TOPIC, new ProductUpdateMessage().setId(id));
}
}

View File

@@ -22,4 +22,11 @@ dubbo:
port: -1
name: dubbo
scan:
base-packages: cn.iocoder.mall.product.service
base-packages: cn.iocoder.mall.product.service
# rocketmq
rocketmq:
name-server: 127.0.0.1:9876
producer:
group: product-producer-group

View File

@@ -32,6 +32,17 @@
AND deleted = 0
</select>
<select id="selectByIds" resultType="ProductCategoryDO">
SELECT
<include refid="FIELDS" />
FROM product_category
WHERE id IN
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
AND deleted = 0
</select>
<insert id="insert" parameterType="ProductCategoryDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO product_category (
pid, name, description, pic_url, sort,
@@ -70,4 +81,4 @@
WHERE id = #{id}
</update>
</mapper>
</mapper>