Update ShopCartController.java

cyj
pbvfus8to 8 months ago
parent ced98f2db7
commit b49569532d

@ -40,136 +40,195 @@ import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
*
* /
* `Swagger`便使
*
* @author lanhai * @author lanhai
*/ */
@RestController @RestController
@RequestMapping("/p/shopCart") @RequestMapping("/p/shopCart")
@Tag(name = "购物车接口") @Tag(name = "购物车接口")
// 使用 @AllArgsConstructor 注解,由 lombok 自动生成包含所有成员变量的构造函数,用于依赖注入
@AllArgsConstructor @AllArgsConstructor
public class ShopCartController { public class ShopCartController {
// 用于处理购物车相关业务逻辑的服务层对象,比如更新购物车、获取购物车商品项等操作
private final BasketService basketService; private final BasketService basketService;
// 用于处理商品相关业务逻辑的服务层对象,比如获取商品信息等操作
private final ProductService productService; private final ProductService productService;
// 用于处理商品库存单元SKU相关业务逻辑的服务层对象比如获取SKU信息等操作
private final SkuService skuService; private final SkuService skuService;
// Spring应用上下文对象用于发布事件等操作例如发布购物车相关事件
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
/** /**
* *
* ID
* *
* @param basketIdShopCartParamMap * @param basketIdShopCartParamMap ID
* @return * @return ServerResponseEntity<List<ShopCartDto>>DTO
*/ */
@PostMapping("/info") @PostMapping("/info")
@Operation(summary = "获取用户购物车信息" , description = "获取用户购物车信息,参数为用户选中的活动项数组,以购物车id为key") @Operation(summary = "获取用户购物车信息", description = "获取用户购物车信息,参数为用户选中的活动项数组,以购物车id为key")
public ServerResponseEntity<List<ShopCartDto>> info(@RequestBody Map<Long, ShopCartParam> basketIdShopCartParamMap) { public ServerResponseEntity<List<ShopCartDto>> info(@RequestBody Map<Long, ShopCartParam> basketIdShopCartParamMap) {
// 获取当前登录用户的ID用于关联购物车与用户
String userId = SecurityUtils.getUser().getUserId(); String userId = SecurityUtils.getUser().getUserId();
// 更新购物车信息 // 如果传入的购物车参数映射表不为空,调用购物车服务层方法根据参数更新购物车信息
if (MapUtil.isNotEmpty(basketIdShopCartParamMap)) { if (MapUtil.isNotEmpty(basketIdShopCartParamMap)) {
basketService.updateBasketByShopCartParam(userId, basketIdShopCartParamMap); basketService.updateBasketByShopCartParam(userId, basketIdShopCartParamMap);
} }
// 拿到购物车的所有item // 调用购物车服务层方法获取该用户的购物车商品项列表
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId); List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId);
return ServerResponseEntity.success(basketService.getShopCarts(shopCartItems)); return ServerResponseEntity.success(basketService.getShopCarts(shopCartItems));
} }
/**
*
* ID
*
* @param basketIds ID
* @return ServerResponseEntity<Void>
*/
@DeleteMapping("/deleteItem") @DeleteMapping("/deleteItem")
@Operation(summary = "删除用户购物车物品" , description = "通过购物车id删除用户购物车物品") @Operation(summary = "删除用户购物车物品", description = "通过购物车id删除用户购物车物品")
public ServerResponseEntity<Void> deleteItem(@RequestBody List<Long> basketIds) { public ServerResponseEntity<Void> deleteItem(@RequestBody List<Long> basketIds) {
// 获取当前登录用户的ID用于关联购物车与用户
String userId = SecurityUtils.getUser().getUserId(); String userId = SecurityUtils.getUser().getUserId();
basketService.deleteShopCartItemsByBasketIds(userId, basketIds); basketService.deleteShopCartItemsByBasketIds(userId, basketIds);
return ServerResponseEntity.success(); return ServerResponseEntity.success();
} }
/**
*
*
*
* @return ServerResponseEntity<String>"删除成功"
*/
@DeleteMapping("/deleteAll") @DeleteMapping("/deleteAll")
@Operation(summary = "清空用户购物车所有物品" , description = "清空用户购物车所有物品") @Operation(summary = "清空用户购物车所有物品", description = "清空用户购物车所有物品")
public ServerResponseEntity<String> deleteAll() { public ServerResponseEntity<String> deleteAll() {
// 获取当前登录用户的ID用于关联购物车与用户
String userId = SecurityUtils.getUser().getUserId(); String userId = SecurityUtils.getUser().getUserId();
basketService.deleteAllShopCartItems(userId); basketService.deleteAllShopCartItems(userId);
return ServerResponseEntity.success("删除成功"); return ServerResponseEntity.success("删除成功");
} }
/**
*
* 0
*
*
* @param param IDSKU IDID
* @return ServerResponseEntity<String>
*/
@PostMapping("/changeItem") @PostMapping("/changeItem")
@Operation(summary = "添加、修改用户购物车物品", description = "通过商品id(prodId)、skuId、店铺Id(shopId),添加/修改用户购物车商品,并传入改变的商品个数(count)" + @Operation(summary = "添加、修改用户购物车物品", description = "通过商品id(prodId)、skuId、店铺Id(shopId),添加/修改用户购物车商品,并传入改变的商品个数(count)" +
"当count为正值时增加商品数量当count为负值时将减去商品的数量当最终count值小于0时会将商品从购物车里面删除") "当count为正值时增加商品数量当count为负值时将减去商品的数量当最终count值小于0时会将商品从购物车里面删除")
public ServerResponseEntity<String> addItem(@Valid @RequestBody ChangeShopCartParam param) { public ServerResponseEntity<String> addItem(@Valid @RequestBody ChangeShopCartParam param) {
// 如果更改的商品数量为0则返回提示输入更改数量的失败响应
if (param.getCount() == 0) { if (param.getCount() == 0) {
return ServerResponseEntity.showFailMsg("输入更改数量"); return ServerResponseEntity.showFailMsg("输入更改数量");
} }
// 获取当前登录用户的ID用于关联购物车与用户
String userId = SecurityUtils.getUser().getUserId(); String userId = SecurityUtils.getUser().getUserId();
// 获取当前用户的购物车商品项列表,用于后续判断商品是否已在购物车中等操作
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId); List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId);
// 根据传入的商品ID获取商品信息
Product prodParam = productService.getProductByProdId(param.getProdId()); Product prodParam = productService.getProductByProdId(param.getProdId());
// 根据传入的SKU ID获取SKU信息
Sku skuParam = skuService.getSkuBySkuId(param.getSkuId()); Sku skuParam = skuService.getSkuBySkuId(param.getSkuId());
// 当商品状态不正常时,不能添加到购物车 // 当商品状态不正常下架状态状态值不为1返回提示商品已下架的失败响应
if (prodParam.getStatus() != 1 || skuParam.getStatus() != 1) { if (prodParam.getStatus()!= 1 || skuParam.getStatus()!= 1) {
return ServerResponseEntity.showFailMsg("当前商品已下架"); return ServerResponseEntity.showFailMsg("当前商品已下架");
} }
// 遍历购物车商品项列表,判断要操作的商品是否已在购物车中
for (ShopCartItemDto shopCartItemDto : shopCartItems) { for (ShopCartItemDto shopCartItemDto : shopCartItems) {
if (Objects.equals(param.getSkuId(), shopCartItemDto.getSkuId())) { if (Objects.equals(param.getSkuId(), shopCartItemDto.getSkuId())) {
Basket basket = new Basket(); Basket basket = new Basket();
basket.setUserId(userId); basket.setUserId(userId);
// 更新购物车中该商品的数量,为原数量加上传入的更改数量
basket.setBasketCount(param.getCount() + shopCartItemDto.getProdCount()); basket.setBasketCount(param.getCount() + shopCartItemDto.getProdCount());
basket.setBasketId(shopCartItemDto.getBasketId()); basket.setBasketId(shopCartItemDto.getBasketId());
// 防止购物车变成负数 // 防止购物车商品数量变成负数如果数量小于等于0则删除该购物车商品项
if (basket.getBasketCount() <= 0) { if (basket.getBasketCount() <= 0) {
basketService.deleteShopCartItemsByBasketIds(userId, Collections.singletonList(basket.getBasketId())); basketService.deleteShopCartItemsByBasketIds(userId, Collections.singletonList(basket.getBasketId()));
return ServerResponseEntity.success(); return ServerResponseEntity.success();
} }
// 当sku实际库存不足时不能添加到购物车 // 当SKU实际库存不足当前库存小于购物车中要设置的数量且原购物车商品数量大于0说明是修改操作返回提示库存不足的失败响应
if (skuParam.getStocks() < basket.getBasketCount() && shopCartItemDto.getProdCount() > 0) { if (skuParam.getStocks() < basket.getBasketCount() && shopCartItemDto.getProdCount() > 0) {
return ServerResponseEntity.showFailMsg("库存不足"); return ServerResponseEntity.showFailMsg("库存不足");
} }
// 调用购物车服务层方法更新购物车商品项信息
basketService.updateShopCartItem(basket); basketService.updateShopCartItem(basket);
return ServerResponseEntity.success(); return ServerResponseEntity.success();
} }
} }
// 防止购物车已被删除的情况下,添加了负数的商品 // 如果商品不在购物车中,且传入的更改数量为负数,说明商品已被删除,返回相应提示的失败响应
if (param.getCount() < 0) { if (param.getCount() < 0) {
return ServerResponseEntity.showFailMsg("商品已从购物车移除"); return ServerResponseEntity.showFailMsg("商品已从购物车移除");
} }
// 当sku实际库存不足时不能添加到购物车
// 当SKU实际库存不足当前库存小于要添加的数量说明是添加操作返回提示库存不足的失败响应
if (skuParam.getStocks() < param.getCount()) { if (skuParam.getStocks() < param.getCount()) {
return ServerResponseEntity.showFailMsg("库存不足"); return ServerResponseEntity.showFailMsg("库存不足");
} }
// 所有都正常时
basketService.addShopCartItem(param,userId); // 所有验证都通过时,调用购物车服务层方法添加购物车商品项,并返回添加成功的响应
basketService.addShopCartItem(param, userId);
return ServerResponseEntity.success("添加成功"); return ServerResponseEntity.success("添加成功");
} }
/**
*
* 0
*
* @return ServerResponseEntity<Integer>
*/
@GetMapping("/prodCount") @GetMapping("/prodCount")
@Operation(summary = "获取购物车商品数量" , description = "获取所有购物车商品数量") @Operation(summary = "获取购物车商品数量", description = "获取所有购物车商品数量")
public ServerResponseEntity<Integer> prodCount() { public ServerResponseEntity<Integer> prodCount() {
// 获取当前登录用户的ID用于关联购物车与用户
String userId = SecurityUtils.getUser().getUserId(); String userId = SecurityUtils.getUser().getUserId();
// 获取当前用户的购物车商品项列表
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId); List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId);
if (CollectionUtil.isEmpty(shopCartItems)) { if (CollectionUtil.isEmpty(shopCartItems)) {
return ServerResponseEntity.success(0); return ServerResponseEntity.success(0);
} }
// 使用流操作计算购物车商品项列表中所有商品数量的总和
Integer totalCount = shopCartItems.stream().map(ShopCartItemDto::getProdCount).reduce(0, Integer::sum); Integer totalCount = shopCartItems.stream().map(ShopCartItemDto::getProdCount).reduce(0, Integer::sum);
return ServerResponseEntity.success(totalCount); return ServerResponseEntity.success(totalCount);
} }
/**
*
* ID
*
* @return ServerResponseEntity<List<ShopCartExpiryItemDto>>DTO
*/
@GetMapping("/expiryProdList") @GetMapping("/expiryProdList")
@Operation(summary = "获取购物车失效商品信息" , description = "获取购物车失效商品列表") @Operation(summary = "获取购物车失效商品信息", description = "获取购物车失效商品列表")
public ServerResponseEntity<List<ShopCartExpiryItemDto>> expiryProdList() { public ServerResponseEntity<List<ShopCartExpiryItemDto>> expiryProdList() {
// 获取当前登录用户的ID用于关联购物车与用户
String userId = SecurityUtils.getUser().getUserId(); String userId = SecurityUtils.getUser().getUserId();
// 获取当前用户购物车中的失效商品项列表
List<ShopCartItemDto> shopCartItems = basketService.getShopCartExpiryItems(userId); List<ShopCartItemDto> shopCartItems = basketService.getShopCartExpiryItems(userId);
//根据店铺ID划分item // 根据店铺ID对失效商品项列表进行分组得到以店铺ID为键对应商品项列表为值的映射表
Map<Long, List<ShopCartItemDto>> shopCartItemDtoMap = shopCartItems.stream().collect(Collectors.groupingBy(ShopCartItemDto::getShopId)); Map<Long, List<ShopCartItemDto>> shopCartItemDtoMap = shopCartItems.stream().collect(Collectors.groupingBy(ShopCartItemDto::getShopId));
// 返回一个店铺对应的所有信息 // 用于存储构建好的每个店铺对应的购物车失效商品信息对象列表
List<ShopCartExpiryItemDto> shopcartExpiryitems = Lists.newArrayList(); List<ShopCartExpiryItemDto> shopcartExpiryitems = Lists.newArrayList();
// 遍历分组后的店铺ID集合构建每个店铺对应的购物车失效商品信息对象并添加到列表中
for (Long key : shopCartItemDtoMap.keySet()) { for (Long key : shopCartItemDtoMap.keySet()) {
ShopCartExpiryItemDto shopCartExpiryItemDto = new ShopCartExpiryItemDto(); ShopCartExpiryItemDto shopCartExpiryItemDto = new ShopCartExpiryItemDto();
shopCartExpiryItemDto.setShopId(key); shopCartExpiryItemDto.setShopId(key);
@ -185,66 +244,44 @@ public class ShopCartController {
return ServerResponseEntity.success(shopcartExpiryitems); return ServerResponseEntity.success(shopcartExpiryitems);
} }
/**
*
*
*
* @return ServerResponseEntity<Void>
*/
@DeleteMapping("/cleanExpiryProdList") @DeleteMapping("/cleanExpiryProdList")
@Operation(summary = "清空用户失效商品" , description = "清空用户失效商品") @Operation(summary = "清空用户失效商品", description = "清空用户失效商品")
public ServerResponseEntity<Void> cleanExpiryProdList() { public ServerResponseEntity<Void> cleanExpiryProdList() {
// 获取当前登录用户的ID用于关联购物车与用户
String userId = SecurityUtils.getUser().getUserId(); String userId = SecurityUtils.getUser().getUserId();
basketService.cleanExpiryProdList(userId); basketService.cleanExpiryProdList(userId);
return ServerResponseEntity.success(); return ServerResponseEntity.success();
} }
/**
*
* IDIDDTO
*
* @param basketIds ID
* @return ServerResponseEntity<ShopCartAmountDto>DTO
*/
@PostMapping("/totalPay") @PostMapping("/totalPay")
@Operation(summary = "获取选中购物项总计、选中的商品数量" , description = "获取选中购物项总计、选中的商品数量,参数为购物车id数组") @Operation(summary = "获取选中购物项总计、选中的商品数量", description = "获取选中购物项总计、选中的商品数量,参数为购物车id数组")
public ServerResponseEntity<ShopCartAmountDto> getTotalPay(@RequestBody List<Long> basketIds) { public ServerResponseEntity<ShopCartAmountDto> getTotalPay(@RequestBody List<Long> basketIds) {
// 拿到购物车的所有item // 拿到购物车的所有item
List<ShopCartItemDto> dbShopCartItems = basketService.getShopCartItems(SecurityUtils.getUser().getUserId()); List<ShopCartItemDto> dbShopCartItems = basketService.getShopCartItems(SecurityUtils.getUser().getUserId());
// 从所有购物车商品项中筛选出传入的购物车ID列表对应的商品项
List<ShopCartItemDto> chooseShopCartItems = dbShopCartItems List<ShopCartItemDto> chooseShopCartItems = dbShopCartItems
.stream() .stream()
.filter(shopCartItemDto -> { .filter(shopCartItemDto -> {
for (Long basketId : basketIds) { for (Long basketId : basketIds) {
if (Objects.equals(basketId,shopCartItemDto.getBasketId())) { if (Objects.equals(basketId, shopCartItemDto.getBasketId())) {
return true; return true;
} }
} }
return false; return false;
}) })
.toList(); .toList();
// 根据店铺ID划分item
Map<Long, List<ShopCartItemDto>> shopCartMap = chooseShopCartItems.stream().collect(Collectors.groupingBy(ShopCartItemDto::getShopId));
double total = 0.0;
int count = 0;
double reduce = 0.0;
for (Long shopId : shopCartMap.keySet()) {
//获取店铺的所有商品项
List<ShopCartItemDto> shopCartItemDtoList = shopCartMap.get(shopId);
// 构建每个店铺的购物车信息
ShopCartDto shopCart = new ShopCartDto();
shopCart.setShopId(shopId);
applicationContext.publishEvent(new ShopCartEvent(shopCart, shopCartItemDtoList));
List<ShopCartItemDiscountDto> shopCartItemDiscounts = shopCart.getShopCartItemDiscounts();
for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartItemDiscounts) {
List<ShopCartItemDto> shopCartItems = shopCartItemDiscount.getShopCartItems();
for (ShopCartItemDto shopCartItem : shopCartItems) {
count = shopCartItem.getProdCount() + count;
total = Arith.add(shopCartItem.getProductTotalAmount(), total);
}
}
}
ShopCartAmountDto shopCartAmountDto = new ShopCartAmountDto();
shopCartAmountDto.setCount(count);
shopCartAmountDto.setTotalMoney(total);
shopCartAmountDto.setSubtractMoney(reduce);
shopCartAmountDto.setFinalMoney(Arith.sub(shopCartAmountDto.getTotalMoney(), shopCartAmountDto.getSubtractMoney()));
return ServerResponseEntity.success(shopCartAmountDto);
}
}

Loading…
Cancel
Save