MemberCart.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/2
  6. * Time: 下午5:09
  7. */
  8. namespace app\frontend\models;
  9. use app\common\exceptions\AppException;
  10. use app\frontend\modules\member\models\MemberAddress;
  11. use app\frontend\modules\member\models\YzMemberAddress;
  12. use app\frontend\modules\member\services\MemberService;
  13. use app\frontend\modules\memberCart\MemberCartCollection;
  14. use Illuminate\Database\Eloquent\Builder;
  15. /**
  16. * Class MemberCart
  17. * @package app\frontend\models
  18. * @property Goods goods
  19. * @property GoodsOption goodsOption
  20. */
  21. class MemberCart extends \app\common\models\MemberCart
  22. {
  23. protected $fillable = [];
  24. protected $guarded = ['id'];
  25. protected $hidden = ['member_id', 'uniacid','updated_at','deleted_at'];
  26. public static function getCartNum($member_id)
  27. {
  28. if (!$member_id) {
  29. return 0;
  30. }
  31. return static::uniacid()->where('member_id', $member_id)->count();
  32. }
  33. /**
  34. * 根据购物车id数组,获取购物车记录数组
  35. * @param $cartIds
  36. * @return mixed
  37. */
  38. public static function getCartsByIds($cartIds)
  39. {
  40. if (!is_array($cartIds)) {
  41. $cartIds = explode(',', $cartIds);
  42. }
  43. $result = static::whereIn('id', $cartIds)
  44. ->get();
  45. return $result;
  46. }
  47. public function scopeFilterFailureGoods(Builder $query)
  48. {
  49. return $query->join('yz_goods', 'yz_goods.id', '=', 'yz_member_cart.goods_id')
  50. ->where('yz_goods.status',1)
  51. ->whereNull('yz_goods.deleted_at');
  52. }
  53. public function scopeCarts(Builder $query)
  54. {
  55. return $query->select($this->getTable().'.*')->uniacid()
  56. ->with(['goods' => function ($query) {
  57. return $query->withTrashed()->select('id', 'thumb', 'price', 'market_price', 'title', 'deleted_at','plugin_id','stock','status','has_option');
  58. }])
  59. ->with(['goodsOption' => function ($query) {
  60. return $query->whereHas('goods')->select('id', 'goods_id','title', 'thumb', 'product_price', 'market_price','stock');
  61. }]);
  62. }
  63. public function goodsOption()
  64. {
  65. return $this->belongsTo(app('GoodsManager')->make('GoodsOption'), 'option_id');
  66. }
  67. /**
  68. * Get a list of members shopping cart through cart IDs
  69. *
  70. * @param array $cartIds
  71. *
  72. * @return array
  73. * */
  74. public static function getMemberCartByIds($cartIds)
  75. {
  76. return static::uniacid()->whereIn('id', $cartIds)->get()->toArray();
  77. }
  78. /**
  79. * Add merchandise to shopping cart
  80. *
  81. * @param array $data
  82. *
  83. * @return 1 or 0
  84. * */
  85. public static function storeGoodsToMemberCart($data)
  86. {
  87. //需要监听事件,购物车存在的处理方式
  88. return static::insert($data);
  89. }
  90. /**
  91. * 检测商品是否存在购物车
  92. *
  93. * @param array $data ['member_id', 'goods_id', 'option_id']
  94. *
  95. * @return self | false
  96. * */
  97. public static function hasGoodsToMemberCart($data)
  98. {
  99. $hasGoods = self::uniacid()
  100. ->where([
  101. 'member_id' => $data['member_id'],
  102. 'goods_id' => $data['goods_id'],
  103. 'option_id' => $data['option_id']
  104. ])
  105. ->first();
  106. return $hasGoods ? $hasGoods : false;
  107. }
  108. /**
  109. * 定义字段名
  110. *
  111. * @return array
  112. */
  113. public function atributeNames()
  114. {
  115. return [
  116. 'goods_id' => '未获取到商品',
  117. 'total' => '商品数量不能为空',
  118. ];
  119. }
  120. /**
  121. * 字段规则
  122. *
  123. * @return array
  124. */
  125. public function rules()
  126. {
  127. return [
  128. 'goods_id' => 'required',
  129. 'total' => 'required',
  130. ];
  131. }
  132. /**
  133. * @return MemberCartCollection
  134. * @throws AppException
  135. */
  136. protected function getAllMemberCarts(){
  137. return (new MemberCartCollection(Member::current()->memberCarts));
  138. }
  139. public function goods()
  140. {
  141. return $this->belongsTo(app('GoodsManager')->make('Goods'));
  142. }
  143. public function hasManyAddress()
  144. {
  145. return $this->hasMany(MemberAddress::class,"uid","member_id");
  146. }
  147. public function hasManyMemberAddress()
  148. {
  149. return $this->hasMany(YzMemberAddress::class,"uid","member_id");
  150. }
  151. }