MemberFavorite.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/2/27
  6. * Time: 下午6:35
  7. */
  8. namespace app\frontend\modules\member\models;
  9. class MemberFavorite extends \app\common\models\MemberFavorite
  10. {
  11. public function goods()
  12. {
  13. return $this->hasOne(\app\common\modules\shop\ShopConfig::current()->get('goods.models.commodity_classification'),'id','goods_id');
  14. }
  15. /*
  16. * 通过主键ID查找
  17. *
  18. * @params int $favoriteId
  19. *
  20. * @return object*/
  21. public static function getFavoriteById($favoriteId)
  22. {
  23. return static::uniacid()->where('id', $favoriteId)->first();
  24. }
  25. /*
  26. * 通过商品ID、会员ID查找
  27. *
  28. * @params int $goodsId
  29. * @params int $memberId
  30. *
  31. * @return object */
  32. public static function getFavoriteByGoodsId($goodsId, $memberId)
  33. {
  34. return static::uniacid()->where('goods_id', $goodsId)->where('member_id', $memberId)->first();
  35. }
  36. /*
  37. * 获取会员收藏列表
  38. *
  39. * @params int $goodsId
  40. * @params int $memberId
  41. *
  42. * @return object */
  43. public static function getFavoriteList($memberId)
  44. {
  45. // return static::select('id', 'goods_id', 'created_at')->uniacid()->where('member_id', $memberId)
  46. // ->with(['goods' => function($query) {
  47. // return $query->select('id', 'thumb', 'price', 'market_price', 'title');
  48. // }])
  49. // ->orderBy('created_at', 'desc')->get()->toArray();
  50. $data = static::select('id', 'goods_id', 'created_at')->uniacid()->where('member_id', $memberId)
  51. ->with(['goods' => function($query) {
  52. return $query->select('id', 'thumb', 'price', 'market_price', 'title')->whereNull('deleted_at');
  53. }])
  54. ->has('goods')
  55. ->orderBy('created_at', 'desc')->get();
  56. foreach ($data as &$itme){
  57. $itme['vip_level_status'] = $itme->goods->vip_level_status;
  58. }
  59. return $data->toArray();
  60. }
  61. /**
  62. * 会员收藏数量
  63. * @param $memberId
  64. * @return int
  65. */
  66. public static function getFavoriteCount($memberId = null)
  67. {
  68. if ($memberId) {
  69. return static::uniacid()->where('member_id', $memberId)->count();
  70. }
  71. return 0;
  72. }
  73. /**
  74. * remove collection
  75. *
  76. * @param array $data
  77. *
  78. * @return 1 or 0
  79. * */
  80. public static function destroyFavorite($favoriteId)
  81. {
  82. return static::uniacid()->where('id', $favoriteId)->delete();
  83. }
  84. /**
  85. * 定义字段名
  86. *
  87. * @return array */
  88. public function atributeNames() {
  89. return [
  90. 'goods_id' => '商品ID不能为空',
  91. ];
  92. }
  93. /**
  94. * 字段规则
  95. *
  96. * @return array */
  97. public function rules()
  98. {
  99. return [
  100. 'goods_id' => 'required|integer',
  101. ];
  102. }
  103. }