整合es、rabbitmq、redis完成上下架修改es
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.atguigu.ssyx;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* ClassName: ServiceSearchApplication
|
||||
* Package: com.atguigu.ssyx
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 17:20
|
||||
*/
|
||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
public class ServiceSearchApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServiceSearchApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package com.atguigu.ssyx.search.controller;
|
||||
|
||||
import com.atguigu.ssyx.common.result.Result;
|
||||
import com.atguigu.ssyx.search.service.SkuService;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* ClassName: SkuApiController
|
||||
* Package: com.atguigu.ssyx.search.controller
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 17:22
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("api/search/sku")
|
||||
public class SkuApiController {
|
||||
|
||||
@Autowired
|
||||
private SkuService skuService;
|
||||
|
||||
@ApiOperation(value = "上架商品")
|
||||
@GetMapping("inner/upperSku/{skuId}")
|
||||
public Result upperGoods(@PathVariable("skuId") Long skuId) {
|
||||
skuService.upperSku(skuId);
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "下架商品")
|
||||
@GetMapping("inner/lowerSku/{skuId}")
|
||||
public Result lowerGoods(@PathVariable("skuId") Long skuId) {
|
||||
skuService.lowerSku(skuId);
|
||||
return Result.ok(null);
|
||||
}
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
package com.atguigu.ssyx.search.receiver;
|
||||
|
||||
import com.atguigu.ssyx.mq.constant.MqConst;
|
||||
import com.atguigu.ssyx.search.service.SkuService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* ClassName: SkuReceiver
|
||||
* Package: com.atguigu.ssyx.search.receiver
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 23:57
|
||||
*/
|
||||
@Component
|
||||
public class SkuReceiver {
|
||||
|
||||
@Autowired
|
||||
private SkuService skuService;
|
||||
|
||||
/**
|
||||
* 商品上架
|
||||
*
|
||||
* @param skuId
|
||||
* @param message
|
||||
* @param channel
|
||||
* @throws IOException
|
||||
*/
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
value = @Queue(value = MqConst.QUEUE_GOODS_UPPER, durable = "true"),
|
||||
exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
|
||||
key = {MqConst.ROUTING_GOODS_UPPER}
|
||||
))
|
||||
public void upperSku(Long skuId, Message message, Channel channel) throws IOException {
|
||||
if (null != skuId) {
|
||||
skuService.upperSku(skuId);
|
||||
}
|
||||
//第一个参数:表示收到的消息的标号
|
||||
//第二个参数:如果为true表示可以签收多个消息
|
||||
channel.basicAck(message
|
||||
.getMessageProperties()
|
||||
.getDeliveryTag(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品下架
|
||||
*
|
||||
* @param skuId
|
||||
*/
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
value = @Queue(value = MqConst.QUEUE_GOODS_LOWER, durable = "true"),
|
||||
exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
|
||||
key = {MqConst.ROUTING_GOODS_LOWER}
|
||||
))
|
||||
public void lowerSku(Long skuId, Message message, Channel channel) throws IOException {
|
||||
if (null != skuId) {
|
||||
skuService.lowerSku(skuId);
|
||||
}
|
||||
//第一个参数:表示收到的消息的标号
|
||||
//第二个参数:如果为true表示可以签收多个消息
|
||||
channel.basicAck(message
|
||||
.getMessageProperties()
|
||||
.getDeliveryTag(), false);
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.atguigu.ssyx.search.repository;
|
||||
|
||||
import com.atguigu.ssyx.model.search.SkuEs;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
/**
|
||||
* ClassName: SkuRepository
|
||||
* Package: com.atguigu.ssyx.search.repository
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 17:24
|
||||
*/
|
||||
public interface SkuRepository extends ElasticsearchRepository<SkuEs, Long> {
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package com.atguigu.ssyx.search.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* ClassName: SkuService
|
||||
* Package: com.atguigu.ssyx.search.service
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 17:23
|
||||
*/
|
||||
|
||||
@Service
|
||||
public interface SkuService {
|
||||
/**
|
||||
* 上架商品列表
|
||||
*
|
||||
* @param skuId
|
||||
*/
|
||||
void upperSku(Long skuId);
|
||||
|
||||
/**
|
||||
* 下架商品列表
|
||||
*
|
||||
* @param skuId
|
||||
*/
|
||||
|
||||
void lowerSku(Long skuId);
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
package com.atguigu.ssyx.search.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.atguigu.ssyx.client.product.ProductFeignClient;
|
||||
import com.atguigu.ssyx.enums.SkuType;
|
||||
import com.atguigu.ssyx.model.product.Category;
|
||||
import com.atguigu.ssyx.model.product.SkuInfo;
|
||||
import com.atguigu.ssyx.model.search.SkuEs;
|
||||
import com.atguigu.ssyx.search.repository.SkuRepository;
|
||||
import com.atguigu.ssyx.search.service.SkuService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* ClassName: SkuServiceImpl
|
||||
* Package: com.atguigu.ssyx.search.service.impl
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/9/16 17:23
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SkuServiceImpl implements SkuService {
|
||||
|
||||
@Autowired
|
||||
private ProductFeignClient productFeignClient;
|
||||
|
||||
@Autowired
|
||||
private SkuRepository skuEsRepository;
|
||||
|
||||
/**
|
||||
* 上架商品列表
|
||||
*
|
||||
* @param skuId
|
||||
*/
|
||||
@Override
|
||||
public void upperSku(Long skuId) {
|
||||
log.info("upperSku:" + skuId);
|
||||
//查询sku信息
|
||||
SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
|
||||
if (null == skuInfo) return;
|
||||
|
||||
// 查询分类
|
||||
SkuEs skuEs = new SkuEs();
|
||||
Category category = productFeignClient.getCategory(skuInfo.getCategoryId());
|
||||
if (category != null) {
|
||||
skuEs.setCategoryId(category.getId());
|
||||
skuEs.setCategoryName(category.getName());
|
||||
}
|
||||
skuEs.setId(skuInfo.getId());
|
||||
skuEs.setKeyword(skuInfo.getSkuName() + "," + skuEs.getCategoryName());
|
||||
skuEs.setWareId(skuInfo.getWareId());
|
||||
skuEs.setIsNewPerson(skuInfo.getIsNewPerson());
|
||||
skuEs.setImgUrl(skuInfo.getImgUrl());
|
||||
skuEs.setTitle(skuInfo.getSkuName());
|
||||
|
||||
|
||||
if (Objects.equals(skuInfo.getSkuType(), SkuType.COMMON.getCode())) {
|
||||
skuEs.setSkuType(0);
|
||||
skuEs.setPrice(skuInfo
|
||||
.getPrice()
|
||||
.doubleValue());
|
||||
skuEs.setStock(skuInfo.getStock());
|
||||
skuEs.setSale(skuInfo.getSale());
|
||||
skuEs.setPerLimit(skuInfo.getPerLimit());
|
||||
} else {
|
||||
//TODO 待完善-秒杀商品
|
||||
|
||||
}
|
||||
SkuEs save = skuEsRepository.save(skuEs);
|
||||
log.info("upperSku:" + JSON.toJSONString(save));
|
||||
}
|
||||
|
||||
/**
|
||||
* a下架商品列表
|
||||
*
|
||||
* @param skuId
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void lowerSku(Long skuId) {
|
||||
skuEsRepository.deleteById(skuId);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user