优惠券小程序

UserInterestGoods.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class UserInterestGoods extends Model
  6. {
  7. use HasFactory;
  8. protected $table = 'user_interest_goods';
  9. public $timestamps = false;
  10. protected static $unguarded = true;
  11. /**
  12. * 获取商品收藏/浏览历史列表
  13. * */
  14. public static function interestListOfUser($userId, $type, $page, $pageSize, $interestIds=[])
  15. {
  16. $collectModel = self::where('is_del', 0)->where('enable', 1)->where('user_id', $userId)->where('type', $type);
  17. # 根据主键id搜索
  18. if($interestIds) {
  19. $collectModel->whereIn('id', $interestIds);
  20. }
  21. $count = $collectModel->count();
  22. $list = $collectModel->select([
  23. 'id', 'item_id', 'item_short_title', 'item_title', 'item_price', 'item_end_price',
  24. 'shop_type', 'coupon_money', 'item_pic', 'pid', 'updated_at'
  25. ])->orderBy('updated_at', 'desc')->offset(($page-1)*$pageSize)->limit($pageSize)->get();
  26. return [$list, $count];
  27. }
  28. /**
  29. * 批量移除商品收藏/浏览历史
  30. * */
  31. public static function removeInterest($userId, $ids, $type)
  32. {
  33. $result = self::where('user_id', $userId)->where('enable', 1)->where('is_del', 0)
  34. ->where('type', $type)->whereIn('item_id', $ids)->update(['is_del' => 1]);
  35. return $result ? 0 : 1005;
  36. }
  37. /**
  38. * 判断是否已收藏该商品
  39. * */
  40. public static function hadInterest($userId, $itemId)
  41. {
  42. return self::where('user_id', $userId)
  43. ->where('item_id', $itemId)
  44. ->where('enable', 1)->where('is_del', 0)
  45. ->where('type', 1)
  46. ->exists();
  47. }
  48. }