12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- class UserInterestGoods extends Model
- {
- use HasFactory;
- protected $table = 'user_interest_goods';
- public $timestamps = false;
- protected static $unguarded = true;
- /**
- * 获取商品收藏/浏览历史列表
- * */
- public static function interestListOfUser($userId, $type, $page, $pageSize, $interestIds=[])
- {
- $collectModel = self::where('is_del', 0)->where('enable', 1)->where('user_id', $userId)->where('type', $type);
- # 根据主键id搜索
- if($interestIds) {
- $collectModel->whereIn('id', $interestIds);
- }
- $count = $collectModel->count();
- $list = $collectModel->select([
- 'id', 'item_id', 'item_short_title', 'item_title', 'item_price', 'item_end_price',
- 'shop_type', 'coupon_money', 'item_pic', 'pid', 'updated_at'
- ])->orderBy('updated_at', 'desc')->offset(($page-1)*$pageSize)->limit($pageSize)->get();
- return [$list, $count];
- }
- /**
- * 批量移除商品收藏/浏览历史
- * */
- public static function removeInterest($userId, $ids, $type)
- {
- $result = self::where('user_id', $userId)->where('enable', 1)->where('is_del', 0)
- ->where('type', $type)->whereIn('item_id', $ids)->update(['is_del' => 1]);
- return $result ? 0 : 1005;
- }
- /**
- * 判断是否已收藏该商品
- * */
- public static function hadInterest($userId, $itemId)
- {
- return self::where('user_id', $userId)
- ->where('item_id', $itemId)
- ->where('enable', 1)->where('is_del', 0)
- ->where('type', 1)
- ->exists();
- }
- }
|