MemberCartCollection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2021/4/12
  6. * Time: 15:45
  7. */
  8. namespace app\frontend\modules\cart\manager;
  9. use app\common\models\Member;
  10. use app\common\services\Plugin;
  11. use app\framework\Http\Request;
  12. use app\common\exceptions\AppException;
  13. use app\framework\Database\Eloquent\Collection;
  14. use app\frontend\modules\cart\models\CartGoods;
  15. use app\frontend\modules\cart\models\MemberCart;
  16. use app\frontend\modules\cart\models\ShopCart;
  17. class MemberCartCollection extends Collection
  18. {
  19. /**
  20. * 将购物车集合按groupId分组
  21. * @return static
  22. */
  23. public function groupByGroupId()
  24. {
  25. $groups = $this->groupBy(function (MemberCart $memberCart) {
  26. return $memberCart->getGroupId();
  27. });
  28. return $groups;
  29. }
  30. /**
  31. * 根据自身创建plugin_id对应类型的订单,当member已经实例化时传入member避免重复查询
  32. * @param Member|null $member
  33. * @param Plugin|null $plugin
  34. * @param Request $request
  35. * @throws AppException
  36. * @throws \Exception
  37. */
  38. public function getGroup(Plugin $plugin = null, Member $member = null, $request = null)
  39. {
  40. $request = $request ?: request();
  41. if ($this->isEmpty()) {
  42. return false;
  43. }
  44. if (!isset($member)) {
  45. $member = $this->getMember();
  46. }
  47. $app = $plugin && $plugin->app()->bound('CartContainer') ? $plugin->app() : app();
  48. $cartGoods = $this->getCartGoods($this);
  49. /**
  50. * @var ShopCart $shopCart
  51. */
  52. $shopCart = $app->make('CartContainer')->make('ShopCart');
  53. $shopCart->init($cartGoods,$member, $request);
  54. return $shopCart;
  55. }
  56. /**
  57. * 获取购物车商品对象组
  58. * @param Collection $memberCarts
  59. * @throws \Exception
  60. */
  61. public static function getCartGoods(Collection $memberCarts)
  62. {
  63. if ($memberCarts->isEmpty()) {
  64. throw new AppException("购物车记录为空");
  65. }
  66. $result = $memberCarts->map(function ($memberCart) {
  67. /**
  68. * @var $memberCart MemberCart
  69. */
  70. $data = [
  71. 'cart_id' => $memberCart->id,
  72. 'goods_id' => (int)$memberCart->goods_id,
  73. 'goods_option_id' => (int)$memberCart->option_id,
  74. 'total' => (int)$memberCart->total,
  75. ];
  76. $cartGoodsManager = app('CartContainer')->make('CartGoodsManager');
  77. /**
  78. * 根据商品获取不同的 CartGoods模型
  79. * @var $cartGoods CartGoods
  80. */
  81. $cartGoods = $cartGoodsManager->getCartGoods($memberCart->goods);
  82. $cartGoods->setRelation('goodsOption', $memberCart->goodsOption);
  83. $cartGoods->initialAttributes($data);
  84. $cartGoods->setRelation('memberCart', $memberCart);
  85. // $cartGoods->setRelation('goods', $memberCart->goods);
  86. return $cartGoods;
  87. });
  88. return new CartGoodsCollection($result);
  89. }
  90. /**
  91. * 所属用户对象
  92. * @return Member
  93. */
  94. public function getMember()
  95. {
  96. return $this->first()->member;
  97. }
  98. /**
  99. * @return mixed
  100. */
  101. public function getPlugin()
  102. {
  103. return $this->first()->getPluginApp();
  104. }
  105. }