@ -20,19 +20,19 @@ import com.yami.shop.bean.event.ShopCartEvent;
import com.yami.shop.bean.model.Basket ;
import com.yami.shop.bean.model.Basket ;
import com.yami.shop.bean.model.Product ;
import com.yami.shop.bean.model.Product ;
import com.yami.shop.bean.model.Sku ;
import com.yami.shop.bean.model.Sku ;
import com.yami.shop.common.response.ServerResponseEntity ;
import com.yami.shop.common.util.Arith ;
import com.yami.shop.common.util.Arith ;
import com.yami.shop.security.api.util.SecurityUtils ;
import com.yami.shop.security.api.util.SecurityUtils ;
import com.yami.shop.service.BasketService ;
import com.yami.shop.service.BasketService ;
import com.yami.shop.service.ProductService ;
import com.yami.shop.service.ProductService ;
import com.yami.shop.service.SkuService ;
import com.yami.shop.service.SkuService ;
import io.swagger.v3.oas.annotations.tags.Tag ;
import io.swagger.v3.oas.annotations.Operation ;
import io.swagger.v3.oas.annotations.Operation ;
import io.swagger.v3.oas.annotations.tags.Tag ;
import jakarta.validation.Valid ;
import lombok.AllArgsConstructor ;
import lombok.AllArgsConstructor ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.ApplicationContext ;
import com.yami.shop.common.response.ServerResponseEntity ;
import org.springframework.web.bind.annotation.* ;
import org.springframework.web.bind.annotation.* ;
import jakarta.validation.Valid ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
@ -63,7 +63,7 @@ public class ShopCartController {
* @return
* @return
* /
* /
@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 ) {
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
@ -79,7 +79,7 @@ public class ShopCartController {
}
}
@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 ) {
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
basketService . deleteShopCartItemsByBasketIds ( userId , basketIds ) ;
basketService . deleteShopCartItemsByBasketIds ( userId , basketIds ) ;
@ -87,7 +87,7 @@ public class ShopCartController {
}
}
@DeleteMapping ( "/deleteAll" )
@DeleteMapping ( "/deleteAll" )
@Operation ( summary = "清空用户购物车所有物品" , description = "清空用户购物车所有物品" )
@Operation ( summary = "清空用户购物车所有物品" , description = "清空用户购物车所有物品" )
public ServerResponseEntity < String > deleteAll ( ) {
public ServerResponseEntity < String > deleteAll ( ) {
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
basketService . deleteAllShopCartItems ( userId ) ;
basketService . deleteAllShopCartItems ( userId ) ;
@ -143,12 +143,12 @@ public class ShopCartController {
return ServerResponseEntity . showFailMsg ( "库存不足" ) ;
return ServerResponseEntity . showFailMsg ( "库存不足" ) ;
}
}
// 所有都正常时
// 所有都正常时
basketService . addShopCartItem ( param , userId ) ;
basketService . addShopCartItem ( param , userId ) ;
return ServerResponseEntity . success ( "添加成功" ) ;
return ServerResponseEntity . success ( "添加成功" ) ;
}
}
@GetMapping ( "/prodCount" )
@GetMapping ( "/prodCount" )
@Operation ( summary = "获取购物车商品数量" , description = "获取所有购物车商品数量" )
@Operation ( summary = "获取购物车商品数量" , description = "获取所有购物车商品数量" )
public ServerResponseEntity < Integer > prodCount ( ) {
public ServerResponseEntity < Integer > prodCount ( ) {
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
List < ShopCartItemDto > shopCartItems = basketService . getShopCartItems ( userId ) ;
List < ShopCartItemDto > shopCartItems = basketService . getShopCartItems ( userId ) ;
@ -160,7 +160,7 @@ public class ShopCartController {
}
}
@GetMapping ( "/expiryProdList" )
@GetMapping ( "/expiryProdList" )
@Operation ( summary = "获取购物车失效商品信息" , description = "获取购物车失效商品列表" )
@Operation ( summary = "获取购物车失效商品信息" , description = "获取购物车失效商品列表" )
public ServerResponseEntity < List < ShopCartExpiryItemDto > > expiryProdList ( ) {
public ServerResponseEntity < List < ShopCartExpiryItemDto > > expiryProdList ( ) {
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
List < ShopCartItemDto > shopCartItems = basketService . getShopCartExpiryItems ( userId ) ;
List < ShopCartItemDto > shopCartItems = basketService . getShopCartExpiryItems ( userId ) ;
@ -186,7 +186,7 @@ public class ShopCartController {
}
}
@DeleteMapping ( "/cleanExpiryProdList" )
@DeleteMapping ( "/cleanExpiryProdList" )
@Operation ( summary = "清空用户失效商品" , description = "清空用户失效商品" )
@Operation ( summary = "清空用户失效商品" , description = "清空用户失效商品" )
public ServerResponseEntity < Void > cleanExpiryProdList ( ) {
public ServerResponseEntity < Void > cleanExpiryProdList ( ) {
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
String userId = SecurityUtils . getUser ( ) . getUserId ( ) ;
basketService . cleanExpiryProdList ( userId ) ;
basketService . cleanExpiryProdList ( userId ) ;
@ -194,30 +194,30 @@ public class ShopCartController {
}
}
@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 ( ) ) ;
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 ;
} )
} )
. collect ( Collectors . toList ( ) ) ;
. collect ( Collectors . toList ( ) ) ;
// 根据店铺ID划分item
// 根据店铺ID划分item
Map < Long , List < ShopCartItemDto > > shopCartMap = chooseShopCartItems . stream ( ) . collect ( Collectors . groupingBy ( ShopCartItemDto : : getShopId ) ) ;
Map < Long , List < ShopCartItemDto > > shopCartMap = chooseShopCartItems . stream ( ) . collect ( Collectors . groupingBy ( ShopCartItemDto : : getShopId ) ) ;
double total = 0.0 ;
double total = 0.0 ;
int count = 0 ;
int count = 0 ;
double reduce = 0.0 ;
double reduce = 0.0 ;
int totalScore = 0 ;
for ( Long shopId : shopCartMap . keySet ( ) ) {
for ( Long shopId : shopCartMap . keySet ( ) ) {
//获取店铺的所有商品项
//获取店铺的所有商品项
List < ShopCartItemDto > shopCartItemDtoList = shopCartMap . get ( shopId ) ;
List < ShopCartItemDto > shopCartItemDtoList = shopCartMap . get ( shopId ) ;
@ -234,7 +234,12 @@ public class ShopCartController {
for ( ShopCartItemDto shopCartItem : shopCartItems ) {
for ( ShopCartItemDto shopCartItem : shopCartItems ) {
count = shopCartItem . getProdCount ( ) + count ;
count = shopCartItem . getProdCount ( ) + count ;
total = Arith . add ( shopCartItem . getProductTotalAmount ( ) , total ) ;
if ( shopCartItem . getProductType ( ) ! = 2 ) {
total = Arith . add ( shopCartItem . getProductTotalAmount ( ) , total ) ;
} else {
totalScore + = ( shopCartItem . getPayScore ( ) * shopCartItem . getProdCount ( ) ) ;
}
}
}
}
}
}
}
@ -242,6 +247,7 @@ public class ShopCartController {
shopCartAmountDto . setCount ( count ) ;
shopCartAmountDto . setCount ( count ) ;
shopCartAmountDto . setTotalMoney ( total ) ;
shopCartAmountDto . setTotalMoney ( total ) ;
shopCartAmountDto . setSubtractMoney ( reduce ) ;
shopCartAmountDto . setSubtractMoney ( reduce ) ;
shopCartAmountDto . setTotalScore ( totalScore ) ;
shopCartAmountDto . setFinalMoney ( Arith . sub ( shopCartAmountDto . getTotalMoney ( ) , shopCartAmountDto . getSubtractMoney ( ) ) ) ;
shopCartAmountDto . setFinalMoney ( Arith . sub ( shopCartAmountDto . getTotalMoney ( ) , shopCartAmountDto . getSubtractMoney ( ) ) ) ;
return ServerResponseEntity . success ( shopCartAmountDto ) ;
return ServerResponseEntity . success ( shopCartAmountDto ) ;