UserFavorite.php
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace addons\shopro\model;
use think\Model;
use traits\model\SoftDelete;
/**
* 用户收藏模型
*/
class UserFavorite extends Model
{
use SoftDelete;
protected $name = 'shopro_user_favorite';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $hidden = ['createtime', 'updatetime'];
// 追加属性
protected $append = [
];
public static function edit($params)
{
extract($params);
$user = User::info();
//批量删除模式
if (!empty($goods_ids)) {
foreach ($goods_ids as $g) {
self::get(['goods_id' => $g, 'user_id' => $user->id])->delete();
// 减少收藏量
Goods::where('id',$g)->setInc('likes');
}
return false;
}
//单商品默认反向增删
$favorite = self::get(['goods_id' => $goods_id, 'user_id' => $user->id]);
if ($favorite) {
$favorite->delete();
// 减少收藏量
$likes = Goods::where('id',$goods_id)->value('likes');
if($likes <= 0){
Goods::where('id',$goods_id)->update(['likes'=>0]);
}else{
Goods::where('id',$goods_id)->setDec('likes');
}
return false;
}else{
self::create([
'user_id' => $user->id,
'goods_id' => $goods_id
]);
Goods::where('id',$goods_id)->setInc('likes');
return true;
}
}
public static function getGoodsList()
{
$user = User::info();
return self::with(['goods'])->where(['user_id' => $user->id, 'deletetime' => null])->order('createtime', 'DESC')->paginate(10);
}
public function goods()
{
return $this->belongsTo(Goods::class, 'goods_id', 'id');
}
}