manghe/app/common/model/FFFavorites.php
2025-06-25 18:26:26 +08:00

145 lines
3.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace app\common\model;
use think\Model;
/**
* 用户商品收藏模型
* Class FFFavorites
* @package app\common\model
*/
class FFFavorites extends Model
{
// 设置表名
protected $name = 'ff_favorites';
// 设置主键
protected $pk = 'favorite_id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
// 关联商品
public function product()
{
return $this->belongsTo('FFProducts', 'product_id', 'id');
}
/**
* 查询用户是否收藏了指定商品
* @param int $userId 用户ID
* @param int $productId 商品ID
* @return bool|array false表示未收藏array表示收藏信息
*/
public static function isFavorite($userId, $productId)
{
$where = [
'user_id' => $userId,
'product_id' => $productId,
];
$favorite = self::where($where)->find();
return $favorite ? $favorite->toArray() : false;
}
/**
* 添加收藏
* @param int $userId 用户ID
* @param int $productId 商品ID
* @return bool|array 收藏结果
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function addFavorite($userId, $productId)
{
// 检查是否已收藏
$favorite = self::where([
'user_id' => $userId,
'product_id' => $productId,
])->find();
if ($favorite) {
// 已收藏,返回已存在
return $favorite->toArray();
}
// 新增收藏
$model = new self();
$model->user_id = $userId;
$model->product_id = $productId;
$model->save();
return $model->toArray();
}
/**
* 取消收藏
* @param int $userId 用户ID
* @param int $productId 商品ID
* @return bool 取消结果
*/
public static function cancelFavorite($userId, $productId)
{
return self::where([
'user_id' => $userId,
'product_id' => $productId,
])->delete();
}
/**
* 获取用户收藏列表
* @param int $userId 用户ID
* @param int $page 页码
* @param int $limit 每页条数
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function getUserFavoriteList($userId, $page = 1, $limit = 10)
{
$list = self::where('user_id', $userId)
->with(['product'])
->page($page, $limit)
->order('create_time', 'desc')
->select();
return $list ? $list->toArray() : [];
}
/**
* 获取商品收藏数量
* @param int $productId 商品ID
* @return int 收藏数量
*/
public static function getProductFavoriteCount($productId)
{
return self::where('product_id', $productId)->count();
}
/**
* 批量取消收藏
* @param int $userId 用户ID
* @param array $productIds 商品ID数组
* @return int 影响行数
*/
public static function batchCancelFavorite($userId, $productIds)
{
return self::where('user_id', $userId)
->whereIn('product_id', $productIds)
->delete();
}
/**
* 关联用户表
* @return \think\model\relation\HasOne
*/
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
}