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