|
|
|
|
@ -29,7 +29,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import com.yami.shop.common.response.ServerResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import javax.validation.Valid;
|
|
|
|
|
@ -64,7 +64,7 @@ public class ShopCartController {
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping("/info")
|
|
|
|
|
@Operation(summary = "获取用户购物车信息" , description = "获取用户购物车信息,参数为用户选中的活动项数组,以购物车id为key")
|
|
|
|
|
public ResponseEntity<List<ShopCartDto>> info(@RequestBody Map<Long, ShopCartParam> basketIdShopCartParamMap) {
|
|
|
|
|
public ServerResponseEntity<List<ShopCartDto>> info(@RequestBody Map<Long, ShopCartParam> basketIdShopCartParamMap) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
|
|
|
|
|
// 更新购物车信息,
|
|
|
|
|
@ -74,33 +74,33 @@ public class ShopCartController {
|
|
|
|
|
|
|
|
|
|
// 拿到购物车的所有item
|
|
|
|
|
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId);
|
|
|
|
|
return ResponseEntity.ok(basketService.getShopCarts(shopCartItems));
|
|
|
|
|
return ServerResponseEntity.success(basketService.getShopCarts(shopCartItems));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/deleteItem")
|
|
|
|
|
@Operation(summary = "删除用户购物车物品" , description = "通过购物车id删除用户购物车物品")
|
|
|
|
|
public ResponseEntity<Void> deleteItem(@RequestBody List<Long> basketIds) {
|
|
|
|
|
public ServerResponseEntity<Void> deleteItem(@RequestBody List<Long> basketIds) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
basketService.deleteShopCartItemsByBasketIds(userId, basketIds);
|
|
|
|
|
return ResponseEntity.ok().build();
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/deleteAll")
|
|
|
|
|
@Operation(summary = "清空用户购物车所有物品" , description = "清空用户购物车所有物品")
|
|
|
|
|
public ResponseEntity<String> deleteAll() {
|
|
|
|
|
public ServerResponseEntity<String> deleteAll() {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
basketService.deleteAllShopCartItems(userId);
|
|
|
|
|
return ResponseEntity.ok("删除成功");
|
|
|
|
|
return ServerResponseEntity.success("删除成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/changeItem")
|
|
|
|
|
@Operation(summary = "添加、修改用户购物车物品", description = "通过商品id(prodId)、skuId、店铺Id(shopId),添加/修改用户购物车商品,并传入改变的商品个数(count)," +
|
|
|
|
|
"当count为正值时,增加商品数量,当count为负值时,将减去商品的数量,当最终count值小于0时,会将商品从购物车里面删除")
|
|
|
|
|
public ResponseEntity<String> addItem(@Valid @RequestBody ChangeShopCartParam param) {
|
|
|
|
|
public ServerResponseEntity<String> addItem(@Valid @RequestBody ChangeShopCartParam param) {
|
|
|
|
|
|
|
|
|
|
if (param.getCount() == 0) {
|
|
|
|
|
return ResponseEntity.badRequest().body("输入更改数量");
|
|
|
|
|
return ServerResponseEntity.showFailMsg("输入更改数量");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
@ -110,7 +110,7 @@ public class ShopCartController {
|
|
|
|
|
|
|
|
|
|
// 当商品状态不正常时,不能添加到购物车
|
|
|
|
|
if (prodParam.getStatus() != 1 || skuParam.getStatus() != 1) {
|
|
|
|
|
return ResponseEntity.badRequest().body("当前商品已下架");
|
|
|
|
|
return ServerResponseEntity.showFailMsg("当前商品已下架");
|
|
|
|
|
}
|
|
|
|
|
for (ShopCartItemDto shopCartItemDto : shopCartItems) {
|
|
|
|
|
if (Objects.equals(param.getSkuId(), shopCartItemDto.getSkuId())) {
|
|
|
|
|
@ -122,46 +122,46 @@ public class ShopCartController {
|
|
|
|
|
// 防止购物车变成负数
|
|
|
|
|
if (basket.getBasketCount() <= 0) {
|
|
|
|
|
basketService.deleteShopCartItemsByBasketIds(userId, Collections.singletonList(basket.getBasketId()));
|
|
|
|
|
return ResponseEntity.ok().build();
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当sku实际库存不足时,不能添加到购物车
|
|
|
|
|
if (skuParam.getStocks() < basket.getBasketCount() && shopCartItemDto.getProdCount() > 0) {
|
|
|
|
|
return ResponseEntity.badRequest().body("库存不足");
|
|
|
|
|
return ServerResponseEntity.showFailMsg("库存不足");
|
|
|
|
|
}
|
|
|
|
|
basketService.updateShopCartItem(basket);
|
|
|
|
|
return ResponseEntity.ok().build();
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 防止购物车已被删除的情况下,添加了负数的商品
|
|
|
|
|
if (param.getCount() < 0) {
|
|
|
|
|
return ResponseEntity.badRequest().body("商品已从购物车移除");
|
|
|
|
|
return ServerResponseEntity.showFailMsg("商品已从购物车移除");
|
|
|
|
|
}
|
|
|
|
|
// 当sku实际库存不足时,不能添加到购物车
|
|
|
|
|
if (skuParam.getStocks() < param.getCount()) {
|
|
|
|
|
return ResponseEntity.badRequest().body("库存不足");
|
|
|
|
|
return ServerResponseEntity.showFailMsg("库存不足");
|
|
|
|
|
}
|
|
|
|
|
// 所有都正常时
|
|
|
|
|
basketService.addShopCartItem(param,userId);
|
|
|
|
|
return ResponseEntity.ok("添加成功");
|
|
|
|
|
return ServerResponseEntity.success("添加成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/prodCount")
|
|
|
|
|
@Operation(summary = "获取购物车商品数量" , description = "获取所有购物车商品数量")
|
|
|
|
|
public ResponseEntity<Integer> prodCount() {
|
|
|
|
|
public ServerResponseEntity<Integer> prodCount() {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId);
|
|
|
|
|
if (CollectionUtil.isEmpty(shopCartItems)) {
|
|
|
|
|
return ResponseEntity.ok(0);
|
|
|
|
|
return ServerResponseEntity.success(0);
|
|
|
|
|
}
|
|
|
|
|
Integer totalCount = shopCartItems.stream().map(ShopCartItemDto::getProdCount).reduce(0, Integer::sum);
|
|
|
|
|
return ResponseEntity.ok(totalCount);
|
|
|
|
|
return ServerResponseEntity.success(totalCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/expiryProdList")
|
|
|
|
|
@Operation(summary = "获取购物车失效商品信息" , description = "获取购物车失效商品列表")
|
|
|
|
|
public ResponseEntity<List<ShopCartExpiryItemDto>> expiryProdList() {
|
|
|
|
|
public ServerResponseEntity<List<ShopCartExpiryItemDto>> expiryProdList() {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
List<ShopCartItemDto> shopCartItems = basketService.getShopCartExpiryItems(userId);
|
|
|
|
|
//根据店铺ID划分item
|
|
|
|
|
@ -182,20 +182,20 @@ public class ShopCartController {
|
|
|
|
|
shopcartExpiryitems.add(shopCartExpiryItemDto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(shopcartExpiryitems);
|
|
|
|
|
return ServerResponseEntity.success(shopcartExpiryitems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/cleanExpiryProdList")
|
|
|
|
|
@Operation(summary = "清空用户失效商品" , description = "清空用户失效商品")
|
|
|
|
|
public ResponseEntity<Void> cleanExpiryProdList() {
|
|
|
|
|
public ServerResponseEntity<Void> cleanExpiryProdList() {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
basketService.cleanExpiryProdList(userId);
|
|
|
|
|
return ResponseEntity.ok().build();
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/totalPay")
|
|
|
|
|
@Operation(summary = "获取选中购物项总计、选中的商品数量" , description = "获取选中购物项总计、选中的商品数量,参数为购物车id数组")
|
|
|
|
|
public ResponseEntity<ShopCartAmountDto> getTotalPay(@RequestBody List<Long> basketIds) {
|
|
|
|
|
public ServerResponseEntity<ShopCartAmountDto> getTotalPay(@RequestBody List<Long> basketIds) {
|
|
|
|
|
|
|
|
|
|
// 拿到购物车的所有item
|
|
|
|
|
List<ShopCartItemDto> dbShopCartItems = basketService.getShopCartItems(SecurityUtils.getUser().getUserId());
|
|
|
|
|
@ -244,7 +244,7 @@ public class ShopCartController {
|
|
|
|
|
shopCartAmountDto.setSubtractMoney(reduce);
|
|
|
|
|
shopCartAmountDto.setFinalMoney(Arith.sub(shopCartAmountDto.getTotalMoney(), shopCartAmountDto.getSubtractMoney()));
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(shopCartAmountDto);
|
|
|
|
|
return ServerResponseEntity.success(shopCartAmountDto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|