获取购物车指定商品信息
This commit is contained in:
parent
6987b5015e
commit
7885364caa
@ -0,0 +1,22 @@
|
||||
package net.jieyuu.feign;
|
||||
|
||||
import net.jieyuu.utils.JsonData;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(name = "xdclass-product-service")
|
||||
public interface ProductFeignService {
|
||||
|
||||
/**
|
||||
* 获取购物车的最新商品价格(也会清空购物车商品)
|
||||
*
|
||||
* @param productIdList
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/api/cart/v1/confirm_order_cart_items")
|
||||
public JsonData confirmOrderCartItems(@RequestBody List<Long> productIdList);
|
||||
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package net.jieyuu.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OrderItemVO {
|
||||
/**
|
||||
* 商品id
|
||||
*/
|
||||
@JsonProperty("product_id")
|
||||
private Long productId;
|
||||
|
||||
/**
|
||||
* 购买数量
|
||||
*/
|
||||
@JsonProperty("buy_num")
|
||||
private Integer buyNum;
|
||||
/**
|
||||
* 商品标题
|
||||
*/
|
||||
@JsonProperty("product_title")
|
||||
private String productTitle;
|
||||
/**
|
||||
* 商品图片
|
||||
*/
|
||||
@JsonProperty("product_img")
|
||||
private String productImg;
|
||||
|
||||
/**
|
||||
* 商品单价
|
||||
*/
|
||||
private BigDecimal amount;
|
||||
/**
|
||||
* 商品总价
|
||||
*/
|
||||
@JsonProperty("total_amount")
|
||||
private BigDecimal totalAmount;
|
||||
|
||||
|
||||
public Long getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(Long productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
|
||||
public Integer getBuyNum() {
|
||||
return buyNum;
|
||||
}
|
||||
|
||||
public void setBuyNum(Integer buyNum) {
|
||||
this.buyNum = buyNum;
|
||||
}
|
||||
|
||||
public String getProductTitle() {
|
||||
return productTitle;
|
||||
}
|
||||
|
||||
public void setProductTitle(String productTitle) {
|
||||
this.productTitle = productTitle;
|
||||
}
|
||||
|
||||
public String getProductImg() {
|
||||
return productImg;
|
||||
}
|
||||
|
||||
public void setProductImg(String productImg) {
|
||||
this.productImg = productImg;
|
||||
}
|
||||
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(BigDecimal amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品总价 * 商品数量
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public BigDecimal getTotalAmount() {
|
||||
return this.amount.multiply(new BigDecimal(this.buyNum));
|
||||
}
|
||||
|
||||
}
|
@ -5,9 +5,11 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.jieyuu.enums.BizCodeEnum;
|
||||
import net.jieyuu.exception.BizException;
|
||||
import net.jieyuu.feign.ProductFeignService;
|
||||
import net.jieyuu.feign.UserFeignService;
|
||||
import net.jieyuu.interceptor.LoginInterceptor;
|
||||
import net.jieyuu.model.LoginUser;
|
||||
import net.jieyuu.model.OrderItemVO;
|
||||
import net.jieyuu.model.ProductOrderDO;
|
||||
import net.jieyuu.mapper.ProductOrderMapper;
|
||||
import net.jieyuu.request.ConfirmOrderRequest;
|
||||
@ -19,6 +21,8 @@ import net.jieyuu.vo.ProductOrderAddressVO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务实现类
|
||||
@ -37,6 +41,9 @@ public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, Pro
|
||||
@Autowired
|
||||
private UserFeignService userFeignService;
|
||||
|
||||
@Autowired
|
||||
private ProductFeignService productFeignService;
|
||||
|
||||
/**
|
||||
* * 防重提交
|
||||
* * 用户微服务-确认收货地址
|
||||
@ -63,9 +70,16 @@ public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, Pro
|
||||
ProductOrderAddressVO addressVO = this.getUserAddress(orderRequest.getAddressId());
|
||||
log.info("收货地址信息:{}", addressVO);
|
||||
// 获取商品最新信息
|
||||
// JsonData productJsonData = productFeignService.getProductList(orderRequest.getProductList());
|
||||
//
|
||||
List<Long> productList = orderRequest.getProductList();
|
||||
|
||||
JsonData cartItemsDate = productFeignService.confirmOrderCartItems(productList);
|
||||
List<OrderItemVO> orderItemList = cartItemsDate.getData(new TypeReference<List<OrderItemVO>>() {
|
||||
});
|
||||
log.info("获取商品:{}", orderItemList);
|
||||
|
||||
if (orderItemList == null) {
|
||||
throw new BizException(BizCodeEnum.ORDER_CONFIRM_CART_ITEM_NOT_EXIST);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -8,11 +8,13 @@ import net.jieyuu.request.CartItemRequest;
|
||||
import net.jieyuu.service.CartService;
|
||||
import net.jieyuu.service.ProductService;
|
||||
import net.jieyuu.utils.JsonData;
|
||||
import net.jieyuu.vo.CartItemVO;
|
||||
import net.jieyuu.vo.CartVO;
|
||||
import net.jieyuu.vo.ProductVO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@ -41,7 +43,7 @@ public class CartController {
|
||||
return JsonData.buildSuccess();
|
||||
}
|
||||
|
||||
@ApiOperation("添加到购物车")
|
||||
@ApiOperation("修改购物车商品数量")
|
||||
@PostMapping("/change")
|
||||
public JsonData changeItemNum(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
|
||||
cartService.changeItemNum(cartItemRequest);
|
||||
@ -49,7 +51,6 @@ public class CartController {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ApiOperation("清空购物车")
|
||||
@DeleteMapping("/clear")
|
||||
public JsonData clearMyCart() {
|
||||
@ -70,4 +71,18 @@ public class CartController {
|
||||
cartService.deleteItem(productId);
|
||||
return JsonData.buildSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于订单服务,确认订单,获取对应的商品项详情信息
|
||||
* 会清空购物车商品数据
|
||||
*
|
||||
* @param productIdList
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("获取对应订单的商品信息")
|
||||
@PostMapping("/confirm_order_cart_items")
|
||||
public JsonData confirmOrderCartItems(@ApiParam("商品id列表") @RequestBody List<Long> productIdList) {
|
||||
List<CartItemVO> cartItemVOList = cartService.confirmOrderCartItems(productIdList);
|
||||
return JsonData.buildSuccess(cartItemVOList);
|
||||
}
|
||||
}
|
@ -29,6 +29,13 @@ public class ProductController {
|
||||
private ProductService productService;
|
||||
|
||||
|
||||
/**
|
||||
* 分页查询商品列表
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "分页查询商品列表")
|
||||
@GetMapping("page")
|
||||
public JsonData page(@ApiParam("当前页")
|
||||
@ -39,7 +46,13 @@ public class ProductController {
|
||||
return JsonData.buildSuccess(pageResult);
|
||||
}
|
||||
|
||||
@ApiOperation("")
|
||||
/**
|
||||
* 根据id查找商品详情
|
||||
*
|
||||
* @param productId
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("根据id查询商品详情")
|
||||
@GetMapping("detail/{product_id}")
|
||||
public JsonData detail(@ApiParam(value = "商品id", required = true) @PathVariable("product_id") long productId) {
|
||||
ProductVO productVO = productService.findDetailById(productId);
|
||||
@ -55,7 +68,7 @@ public class ProductController {
|
||||
@ApiOperation("商品库存锁定")
|
||||
@PutMapping("lock_products")
|
||||
public JsonData lockProduct(@ApiParam("商品库存锁定") @RequestBody LockProductRequest lockProductRequest) {
|
||||
JsonData jsonData = productService.lockProductStock(lockProductRequest);
|
||||
JsonData jsonData = productService.lockProductStock(lockProductRequest);
|
||||
return jsonData;
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,11 @@
|
||||
package net.jieyuu.service;
|
||||
|
||||
import net.jieyuu.request.CartItemRequest;
|
||||
import net.jieyuu.vo.CartItemVO;
|
||||
import net.jieyuu.vo.CartVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CartService {
|
||||
/**
|
||||
* 添加商品到购物车
|
||||
@ -36,4 +39,13 @@ public interface CartService {
|
||||
* @param cartItemRequest
|
||||
*/
|
||||
void changeItemNum(CartItemRequest cartItemRequest);
|
||||
|
||||
/**
|
||||
* 用于订单服务,确认订单,获取对应的商品项详情信息
|
||||
* 会清空购物车商品数据
|
||||
*
|
||||
* @param productIdList
|
||||
* @return
|
||||
*/
|
||||
List<CartItemVO> confirmOrderCartItems(List<Long> productIdList);
|
||||
}
|
||||
|
@ -120,6 +120,29 @@ public class CartServiceImpl implements CartService {
|
||||
myCartOps.put(cartItemRequest.getProductId(), JSON.toJSONString(cartItemVO));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于订单服务,确认订单,获取对应的商品项详情信息
|
||||
* 会清空购物车商品数据
|
||||
*
|
||||
* @param productIdList
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<CartItemVO> confirmOrderCartItems(List<Long> productIdList) {
|
||||
// 获取全部购物车商品
|
||||
List<CartItemVO> cartItemVOList = buildCartItem(true);
|
||||
// 更能将选择的商品id进行过滤,并且清空对应的购物项
|
||||
|
||||
List<CartItemVO> resultList = cartItemVOList.stream().filter(obj -> {
|
||||
if (productIdList.contains(obj.getProductId())) {
|
||||
this.deleteItem(obj.getProductId());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}).collect(Collectors.toList());
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最新购物项
|
||||
*
|
||||
|
@ -88,4 +88,15 @@ public class CartItemVO {
|
||||
return this.amount.multiply(new BigDecimal(this.buyNum));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CartItemVO{" +
|
||||
"productId=" + productId +
|
||||
", buyNum=" + buyNum +
|
||||
", productTitle='" + productTitle + '\'' +
|
||||
", productImg='" + productImg + '\'' +
|
||||
", amount=" + amount +
|
||||
", totalAmount=" + totalAmount +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user