CartGroupCollection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/4/30
  8. * Time: 18:09
  9. */
  10. namespace app\frontend\modules\cart\manager;
  11. use app\frontend\modules\cart\models\ShopCart;
  12. use Illuminate\Support\Collection;
  13. class CartGroupCollection extends Collection
  14. {
  15. /**
  16. * 第一个选中的购物车店铺类型
  17. * 设置商品是否可选择
  18. */
  19. public function firstCheckedCart()
  20. {
  21. $firstShop = $this->first(function (ShopCart $shop) {
  22. return $shop->isCheckedCartGoods();
  23. });
  24. //根据商铺类型判断是否能其他商品是否能选择
  25. $this->map(function (ShopCart $shop) use($firstShop) {
  26. $shop->setDisable($firstShop);
  27. return $shop;
  28. });
  29. }
  30. public function getCartInvalidGoods()
  31. {
  32. $invalidCart = $this->map(function (ShopCart $shop) {
  33. return $shop->getFailureCart();
  34. })->collapse()->map(function ($cartGoods) {
  35. return $cartGoods->memberCart;
  36. })->values();
  37. return $invalidCart;
  38. }
  39. /**
  40. * 获取指定购物车折扣
  41. * @param null $code
  42. * @return mixed
  43. */
  44. public function getSingleCartDiscount($code = null)
  45. {
  46. return $this->sum(function (ShopCart $shop) use ($code) {
  47. if (is_null($code)) {
  48. return $shop->cartDiscounts()->sum('amount');
  49. }
  50. return $shop->cartDiscounts()->where('code', $code)->sum('amount');
  51. });
  52. }
  53. public function getSingleCartDeductions($code = null)
  54. {
  55. return $this->sum(function (ShopCart $shop) use ($code) {
  56. if (is_null($code)) {
  57. return $shop->cartDeductions()->sum('amount');
  58. }
  59. return $shop->cartDeductions()->where('code', $code)->sum('amount');
  60. });
  61. }
  62. public function getSingleCartExtraCharges($code = null)
  63. {
  64. return $this->sum(function (ShopCart $shop) use ($code) {
  65. if (is_null($code)) {
  66. return $shop->cartExtraCharges()->sum('amount');
  67. }
  68. return $shop->cartExtraCharges()->where('code', $code)->sum('amount');
  69. });
  70. }
  71. public function getCartDiscounts()
  72. {
  73. // 将所有订单商品的优惠
  74. return $this->reduce(function (Collection $result, ShopCart $cart) {
  75. return $result->merge($cart->cartDiscounts());
  76. },collect());
  77. }
  78. public function getCartDeductions()
  79. {
  80. // 将所有订单商品的优惠
  81. return $this->reduce(function (Collection $result, ShopCart $cart) {
  82. return $result->merge($cart->cartDeductions());
  83. },collect());
  84. }
  85. public function getCartExtraCharges()
  86. {
  87. // 将所有订单商品的优惠
  88. return $this->reduce(function (Collection $result, ShopCart $cart) {
  89. return $result->merge($cart->cartExtraCharges());
  90. },collect());
  91. }
  92. }