feat(search): 商品添加到ES

This commit is contained in:
bunny 2024-04-04 16:34:58 +08:00
parent 0f06295fc5
commit 8207c5269e
16 changed files with 241 additions and 25 deletions

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" autoUpload="Always" remoteFilesAllowedToDisappearOnAutoupload="false" autoUploadExternalChanges="true">
<option name="myAutoUpload" value="ALWAYS" />
</component>
</project>

View File

@ -13,6 +13,9 @@
<file url="file://$PROJECT_DIR$/model/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/model/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/model/src/main/resources-filtered" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/service-client/service-product-client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/service-client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/service-client/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/service/service-acl/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/service/service-product/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/service/service-search/src/main/java" charset="UTF-8" />

View File

@ -18,6 +18,7 @@
<module>common</module>
<module>model</module>
<module>service</module>
<module>service-client</module>
</modules>
<properties>

53
service-client/pom.xml Normal file
View File

@ -0,0 +1,53 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>guigu-ssyx-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-client</artifactId>
<packaging>pom</packaging>
<name>service-client</name>
<url>https://maven.apache.org</url>
<modules>
<module>service-product-client</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服务调用feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- loadbalancer依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,23 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>service-client</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-product-client</artifactId>
<packaging>jar</packaging>
<name>service-product-client</name>
<url>https://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>

View File

@ -0,0 +1,17 @@
package com.atguigu.ssyx.client.product;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.model.product.SkuInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "service-product", path = "/api/product")
public interface ProductFeignClient {
@GetMapping("inner/getCategory/{categoryId}")
Result<Category> getCategory(@PathVariable Long categoryId);
@GetMapping("inner/getSkuInfo/{skuId}")
Result<SkuInfo> getSkuInfo(@PathVariable Long skuId);
}

View File

@ -72,12 +72,5 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--开发者工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -6,7 +6,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@ComponentScan(basePackages = {"com.atguigu.ssyx"})
@ComponentScan(basePackages = {"com.atguigu.ssyx.common"})
@EnableTransactionManagement
public class ServiceAclApplication {
public static void main(String[] args) {

View File

@ -6,7 +6,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@ComponentScan(basePackages = {"com.atguigu.ssyx"})
@ComponentScan(basePackages = {"com.atguigu.ssyx.common"})
@EnableTransactionManagement
public class ServiceProductApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,38 @@
package com.atguigu.ssyx.product.api;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.product.service.CategoryService;
import com.atguigu.ssyx.product.service.SkuInfoService;
import io.swagger.annotations.Api;
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;
@Api(value = "远程调用接口", tags = "远程调用接口")
@RestController
@RequestMapping("/api/product")
public class ProductInnerController {
@Autowired
private CategoryService categoryService;
@Autowired
private SkuInfoService skuInfoService;
@ApiOperation(value = "根据分类id获取分类信息")
@GetMapping("inner/getCategory/{categoryId}")
public Result<Category> getCategory(@PathVariable Long categoryId) {
Category category = categoryService.getById(categoryId);
return Result.success(category);
}
@ApiOperation(value = "根据skuId获取sku信息")
@GetMapping("inner/getSkuInfo/{skuId}")
public Result<SkuInfo> getSkuInfo(@PathVariable Long skuId) {
SkuInfo skuInfo = skuInfoService.getById(skuId);
return Result.success(skuInfo);
}
}

View File

@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@ -18,6 +18,12 @@
</properties>
<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-product-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
@ -27,10 +33,5 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>com.atguigu</groupId> -->
<!-- <artifactId>service-product-client</artifactId> -->
<!-- <version>1.0-SNAPSHOT</version> -->
<!-- </dependency> -->
</dependencies>
</project>

View File

@ -6,12 +6,11 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {"com.atguigu.ssyx"})
@ComponentScan(basePackages = {"com.atguigu.ssyx.common"})
@EnableFeignClients(basePackages = {"com.atguigu.ssyx.client"})
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceSearchApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSearchApplication.class, args);

View File

@ -1,9 +1,34 @@
package com.atguigu.ssyx.search.controller;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.search.service.SkuService;
import io.swagger.annotations.Api;
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;
@Api(tags = "商品添加到ES")
@RestController
@RequestMapping("/api/search/sku")
public class SkuApiController {
@Autowired
private SkuService skuService;
@ApiOperation(value = "上架商品")
@GetMapping("inner/upperSku/{skuId}")
public Result<SkuInfo> upperGoods(@PathVariable Long skuId) {
skuService.upperSku(skuId);
return Result.success();
}
@ApiOperation(value = "下架商品")
@GetMapping("inner/lowerSku/{skuId}")
public Result<SkuInfo> lowerGoods(@PathVariable Long skuId) {
skuService.lowerGoods(skuId);
return Result.success();
}
}

View File

@ -1,4 +1,17 @@
package com.atguigu.ssyx.search.service;
public interface SkuService {
/**
* 上架商品
*
* @param skuId 商品ID
*/
void upperSku(Long skuId);
/**
* 下架商品
*
* @param skuId 商品ID
*/
void lowerGoods(Long skuId);
}

View File

@ -1,8 +1,64 @@
package com.atguigu.ssyx.search.service.impl;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class SkuServiceImpl implements SkuService {
@Autowired
private SkuRepository skuRepository;
@Autowired
private ProductFeignClient productFeignClient;
/**
* 上架商品
*
* @param skuId 商品ID
*/
@Override
public void upperSku(Long skuId) {
SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId).getData();
Category category = productFeignClient.getCategory(skuId).getData();
// 获取数据封装SkuEs
SkuEs skuEs = new SkuEs();
if (category != null) {
skuEs.setCategoryId(category.getId());
skuEs.setCategoryName(category.getName());
}
if (skuInfo != null) {
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());
}
skuRepository.save(skuEs);
}
}
/**
* 下架商品
*
* @param skuId 商品ID
*/
@Override
public void lowerGoods(Long skuId) {
skuRepository.deleteById(skuId);
}
}

View File

@ -7,7 +7,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.atguigu.ssyx"})
@ComponentScan(basePackages = {"com.atguigu.ssyx.common"})
public class ServiceSysApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSysApplication.class, args);