dev-v2 #3
|
@ -23,5 +23,29 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.atguigu</groupId>
|
||||||
|
<artifactId>common-util</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.atguigu</groupId>
|
||||||
|
<artifactId>spzx-model</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- openfeign依赖 -->
|
||||||
|
<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>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -16,8 +16,4 @@
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.atguigu.feign.product;
|
||||||
|
|
||||||
|
import com.atguigu.spzx.model.entity.product.ProductSku;
|
||||||
|
import com.atguigu.spzx.model.vo.result.Result;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@FeignClient(value = "service-product")
|
||||||
|
public interface ProductFeignClient {
|
||||||
|
@GetMapping("getBySkuId/{skuId}")
|
||||||
|
Result<ProductSku> getBySkuId(@PathVariable("skuId") Long skuId);
|
||||||
|
}
|
|
@ -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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
|
@ -19,10 +19,9 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>com.atguigu</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>service-product-client</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -3,9 +3,13 @@ package com.atguigu.cart;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
// 排除数据库的自动化配置,Cart微服务不需要访问数据库
|
// 排除数据库的自动化配置,Cart微服务不需要访问数据库
|
||||||
|
@ComponentScan("com.atguigu")
|
||||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
||||||
|
@EnableFeignClients(basePackages = {"com.atguigu"})
|
||||||
public class CartApplication {
|
public class CartApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(CartApplication.class, args);
|
SpringApplication.run(CartApplication.class, args);
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.atguigu.cart.controller;
|
||||||
|
|
||||||
|
import com.atguigu.cart.service.CartService;
|
||||||
|
import com.atguigu.spzx.model.vo.result.Result;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Tag(name = "购物车接口")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("api/order/cart")
|
||||||
|
public class CartController {
|
||||||
|
@Autowired
|
||||||
|
private CartService cartService;
|
||||||
|
|
||||||
|
@Operation(summary = "添加购物车")
|
||||||
|
@GetMapping("auth/addToCart/{skuId}/{skuNum}")
|
||||||
|
public Result<String> addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
|
||||||
|
@Parameter(name = "skuNum", description = "数量", required = true) @PathVariable("skuNum") Integer skuNum) {
|
||||||
|
cartService.addToCart(skuId, skuNum);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.atguigu.cart.service;
|
||||||
|
|
||||||
|
public interface CartService {
|
||||||
|
/**
|
||||||
|
* 添加购物车
|
||||||
|
*
|
||||||
|
* @param skuId 商品的ID值
|
||||||
|
* @param skuNum 商品数量
|
||||||
|
*/
|
||||||
|
void addToCart(Long skuId, Integer skuNum);
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package com.atguigu.cart.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.atguigu.cart.service.CartService;
|
||||||
|
import com.atguigu.context.BaseContext;
|
||||||
|
import com.atguigu.feign.product.ProductFeignClient;
|
||||||
|
import com.atguigu.spzx.model.entity.h5.CartInfo;
|
||||||
|
import com.atguigu.spzx.model.entity.product.ProductSku;
|
||||||
|
import com.atguigu.utils.EmptyUtil;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CartServiceImpl implements CartService {
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
@Autowired
|
||||||
|
private EmptyUtil emptyUtil;
|
||||||
|
@Autowired
|
||||||
|
private ProductFeignClient productFeignClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加购物车
|
||||||
|
*
|
||||||
|
* @param skuId 商品的ID值
|
||||||
|
* @param skuNum 商品数量
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addToCart(Long skuId, Integer skuNum) {
|
||||||
|
Long userId = BaseContext.getUserInfo().getId();
|
||||||
|
// 获取key名称
|
||||||
|
String cartKey = userId.toString();
|
||||||
|
Object cartInfoJson = redisTemplate.opsForHash().get(cartKey, String.valueOf(skuId));
|
||||||
|
// 如果不为空
|
||||||
|
CartInfo cartInfo = null;
|
||||||
|
if (cartInfoJson != null) {
|
||||||
|
cartInfo = JSON.parseObject(cartInfoJson.toString(), CartInfo.class);
|
||||||
|
// 数量相加
|
||||||
|
cartInfo.setSkuNum(cartInfo.getSkuNum() + skuNum);
|
||||||
|
// 购物车状态为选中
|
||||||
|
cartInfo.setIsChecked(1);
|
||||||
|
cartInfo.setUpdateTime(new Date());
|
||||||
|
} else {
|
||||||
|
cartInfo = new CartInfo();
|
||||||
|
// 远程调用,根据skuId获取商品
|
||||||
|
// 购物车数据是从商品详情得到 {skuInfo}
|
||||||
|
ProductSku productSku = productFeignClient.getBySkuId(skuId).getData();
|
||||||
|
cartInfo.setCartPrice(productSku.getSalePrice());
|
||||||
|
cartInfo.setSkuNum(skuNum);
|
||||||
|
cartInfo.setSkuId(skuId);
|
||||||
|
cartInfo.setUserId(userId);
|
||||||
|
cartInfo.setImgUrl(productSku.getThumbImg());
|
||||||
|
cartInfo.setSkuName(productSku.getSkuName());
|
||||||
|
cartInfo.setIsChecked(1);
|
||||||
|
cartInfo.setCreateTime(new Date());
|
||||||
|
cartInfo.setUpdateTime(new Date());
|
||||||
|
}
|
||||||
|
// 将商品数据存储到购物车中
|
||||||
|
redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo));
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,16 +20,5 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<source>8</source>
|
|
||||||
<target>8</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -37,4 +37,11 @@ public class ProductController {
|
||||||
|
|
||||||
return Result.success(vo);
|
return Result.success(vo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "获取ProductSku")
|
||||||
|
@GetMapping("getBySkuId/{skuId}")
|
||||||
|
public Result<ProductSku> getBySkuId(@PathVariable Long skuId) {
|
||||||
|
ProductSku productSku = productService.getBySkuId(skuId);
|
||||||
|
return Result.success(productSku);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -32,4 +32,12 @@ public interface ProductService {
|
||||||
* @return ProductItemVo
|
* @return ProductItemVo
|
||||||
*/
|
*/
|
||||||
ProductItemVo item(Long skuId);
|
ProductItemVo item(Long skuId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取ProductSku
|
||||||
|
*
|
||||||
|
* @param skuId Id
|
||||||
|
* @return 获取ProductSku
|
||||||
|
*/
|
||||||
|
ProductSku getBySkuId(Long skuId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,4 +89,15 @@ public class ProductServiceImpl implements ProductService {
|
||||||
productItemVo.setSkuSpecValueMap(skuSpecValueMap);
|
productItemVo.setSkuSpecValueMap(skuSpecValueMap);
|
||||||
return productItemVo;
|
return productItemVo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取ProductSku
|
||||||
|
*
|
||||||
|
* @param skuId Id
|
||||||
|
* @return 获取ProductSku
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ProductSku getBySkuId(Long skuId) {
|
||||||
|
return productSkuMapper.getById(skuId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
server:
|
server:
|
||||||
port: 8511
|
port: 8511
|
||||||
spring:
|
spring:
|
||||||
|
application:
|
||||||
|
name: service-product
|
||||||
profiles:
|
profiles:
|
||||||
active: dev
|
active: dev
|
||||||
main:
|
main:
|
||||||
allow-bean-definition-overriding: true
|
allow-bean-definition-overriding: true
|
||||||
application:
|
|
||||||
name: service-product
|
|
||||||
cloud:
|
cloud:
|
||||||
nacos:
|
nacos:
|
||||||
discovery:
|
discovery:
|
||||||
|
|
Loading…
Reference in New Issue