perf: 添加消息回复量

This commit is contained in:
kuaifan 2022-06-24 21:08:53 +08:00
parent dfb6f23b60
commit f34133f63e
2 changed files with 58 additions and 0 deletions

View File

@ -23,6 +23,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property string|null $key 搜索关键词
* @property int|null $read 已阅数量
* @property int|null $send 发送数量
* @property int|null $reply_num 有多少条回复
* @property int|null $reply_id 回复ID
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
@ -44,6 +45,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereMsg($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereRead($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereReplyId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereReplyNum($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereSend($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereType($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereUpdatedAt($value)
@ -287,6 +289,10 @@ class WebSocketDialogMsg extends AbstractModel
$deleteRead = WebSocketDialogMsgRead::whereMsgId($this->id)->whereNull('read_at')->delete(); // 未阅读记录不需要软删除,直接删除即可
$this->delete();
//
if ($this->reply_id > 0) {
self::whereId($this->reply_id)->decrement('reply_num');
}
//
$last_msg = null;
if ($this->webSocketDialog) {
$last_msg = WebSocketDialogMsg::whereDialogId($this->dialog_id)->orderByDesc('id')->first();
@ -471,6 +477,9 @@ class WebSocketDialogMsg extends AbstractModel
'msg' => $msg,
'read' => 0,
]);
if ($reply_id > 0) {
self::whereId($reply_id)->increment('reply_num');
}
AbstractModel::transaction(function () use ($dialogMsg) {
$dialog = WebSocketDialog::find($dialogMsg->dialog_id);
if (empty($dialog)) {

View File

@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddWebSocketDialogMsgsReplyNum extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$isAdd = false;
Schema::table('web_socket_dialog_msgs', function (Blueprint $table) use (&$isAdd) {
if (!Schema::hasColumn('web_socket_dialog_msgs', 'reply_num')) {
$isAdd = true;
$table->bigInteger('reply_num')->nullable()->default(0)->after('send')->comment('有多少条回复');
}
});
if ($isAdd) {
\App\Models\WebSocketDialogMsg::select(['reply_id'])
->distinct()
->where('reply_id', '>', 0)
->chunk(100, function ($lists) {
/** @var \App\Models\WebSocketDialogMsg $item */
foreach ($lists as $item) {
\App\Models\WebSocketDialogMsg::whereId($item->reply_id)->update([
'reply_num' => \App\Models\WebSocketDialogMsg::whereReplyId($item->reply_id)->count()
]);
}
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('web_socket_dialog_msgs', function (Blueprint $table) {
$table->dropColumn("reply_num");
});
}
}