1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repository\Front; |
4
|
|
|
|
5
|
|
|
use App\Model\Admin\Comment; |
6
|
|
|
use App\Model\Admin\CommentOperateLog; |
7
|
|
|
use App\Repository\Searchable; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Illuminate\Support\Facades\Cache; |
10
|
|
|
|
11
|
|
|
class CommentRepository |
12
|
|
|
{ |
13
|
|
|
public static function list($perPage = 10, $condition = []) |
14
|
|
|
{ |
15
|
|
|
$data = Comment::query() |
16
|
|
|
->where(function ($query) use ($condition) { |
17
|
|
|
Searchable::buildQuery($query, $condition); |
18
|
|
|
}) |
19
|
|
|
->orderBy('id', 'desc') |
20
|
|
|
->paginate($perPage); |
21
|
|
|
|
22
|
|
|
if ($condition['rid'][1] > 0) { |
23
|
|
|
// 接口直接请求评论的回复数据,则直接返回 |
24
|
|
|
return $data; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$data->transform(function ($item) use ($perPage) { |
28
|
|
|
// 获取评论回复 |
29
|
|
|
$reply = self::reply($item->id, $perPage); |
30
|
|
|
$item->reply = $reply; |
31
|
|
|
return $item; |
32
|
|
|
}); |
33
|
|
|
return $data; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function reply($id, $perPage = 10) |
37
|
|
|
{ |
38
|
|
|
return Cache::rememberForever('comment_replay:' . $id, function () use ($id, $perPage) { |
39
|
|
|
return Comment::query()->where('rid', $id)->orderBy('id', 'desc')->paginate($perPage); |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function like(int $id, int $uid) |
44
|
|
|
{ |
45
|
|
|
self::operate($id, $uid, 'like'); |
46
|
|
|
return self::info($id, $uid); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function dislike(int $id, int $uid) |
50
|
|
|
{ |
51
|
|
|
self::operate($id, $uid, 'dislike'); |
52
|
|
|
return self::info($id, $uid); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function neutral(int $id, int $uid) |
56
|
|
|
{ |
57
|
|
|
DB::transaction(function () use ($id, $uid) { |
58
|
|
|
$logs = CommentOperateLog::query()->select('operate')-> |
59
|
|
|
where('user_id', $uid)->where('comment_id', $id)->get(); |
60
|
|
|
if ($logs->isEmpty()) { |
61
|
|
|
// 未操作则直接返回 |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
foreach ($logs as $log) { |
66
|
|
|
if ($log->operate === 'like') { |
67
|
|
|
Comment::query()->where('id', $id)->decrement('like'); |
68
|
|
|
} elseif ($log->operate === 'dislike') { |
69
|
|
|
Comment::query()->where('id', $id)->decrement('dislike'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return CommentOperateLog::query()->where('user_id', $uid)->where('comment_id', $id)->delete(); |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
return self::info($id, $uid); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function operate(int $id, int $uid, string $operate) |
80
|
|
|
{ |
81
|
|
|
return DB::transaction(function () use ($id, $uid, $operate) { |
82
|
|
|
$log = CommentOperateLog::query()->where('user_id', $uid)->where('comment_id', $id) |
83
|
|
|
->where('operate', $operate)->first(); |
84
|
|
|
if ($log) { |
85
|
|
|
// 已操作则直接返回 |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$oppositeOperate = $operate === 'like' ? 'dislike' : 'like'; |
90
|
|
|
$log = CommentOperateLog::query()->where('user_id', $uid)->where('comment_id', $id) |
91
|
|
|
->where('operate', $oppositeOperate)->first(); |
92
|
|
|
if ($log) { |
93
|
|
|
// 存在反操作则删除,并递减数量 |
94
|
|
|
$log->delete(); |
95
|
|
|
Comment::query()->where('id', $id)->decrement($oppositeOperate); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
CommentOperateLog::query()->create(['user_id' => $uid, 'comment_id' => $id, 'operate' => $operate]); |
99
|
|
|
return Comment::query()->where('id', $id)->increment($operate); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* 返回指定用户对评论的操作情况及评论操作数据 |
105
|
|
|
* |
106
|
|
|
* @param int $id |
107
|
|
|
* @param int $uid |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public static function info(int $id, int $uid = 0) |
111
|
|
|
{ |
112
|
|
|
$data = [ |
113
|
|
|
'like' => 0, |
114
|
|
|
'dislike' => 0, |
115
|
|
|
]; |
116
|
|
|
$comment = Comment::query()->select(['like', 'dislike'])->where('id', $id)->first(); |
117
|
|
|
if (!$comment) { |
118
|
|
|
return $data; |
119
|
|
|
} |
120
|
|
|
$data['like'] = $comment->like; |
121
|
|
|
$data['dislike'] = $comment->dislike; |
122
|
|
|
|
123
|
|
|
if ($uid > 0) { |
124
|
|
|
$data['operate'] = [ |
125
|
|
|
'like' => false, |
126
|
|
|
'dislike' => false, |
127
|
|
|
]; |
128
|
|
|
$operates = CommentOperateLog::query()->select('operate') |
129
|
|
|
->where('user_id', $uid)->where('comment_id', $id)->get(); |
130
|
|
|
foreach ($operates as $operate) { |
131
|
|
|
$data['operate'][$operate->operate] = true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $data; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function addReplyCount($id) |
139
|
|
|
{ |
140
|
|
|
return Comment::query()->where('id', $id)->increment('reply_count'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|