perf: 优化已读数据

This commit is contained in:
kuaifan 2024-12-06 17:57:38 +08:00
parent 4b89eb88bd
commit 13222fbe9a

View File

@ -112,7 +112,11 @@ class WebSocketDialogMsg extends AbstractModel
public function getPercentageAttribute() public function getPercentageAttribute()
{ {
if (!isset($this->appendattrs['percentage'])) { if (!isset($this->appendattrs['percentage'])) {
$this->generatePercentage(); if ($this->read > $this->send || empty($this->send)) {
$this->appendattrs['percentage'] = 100;
} else {
$this->appendattrs['percentage'] = intval($this->read / $this->send * 100);
}
} }
return $this->appendattrs['percentage']; return $this->appendattrs['percentage'];
} }
@ -189,22 +193,6 @@ class WebSocketDialogMsg extends AbstractModel
return $msg; return $msg;
} }
/**
* 获取占比
* @param bool|int $increment 是否新增阅读数
* @return int
*/
public function generatePercentage($increment = false) {
if ($increment) {
$this->increment('read', is_bool($increment) ? 1 : $increment);
}
if ($this->read > $this->send || empty($this->send)) {
return $this->appendattrs['percentage'] = 100;
} else {
return $this->appendattrs['percentage'] = intval($this->read / $this->send * 100);
}
}
/** /**
* 标记已送达 同时 告诉发送人已送达 * 标记已送达 同时 告诉发送人已送达
* @param $userid * @param $userid
@ -234,16 +222,17 @@ class WebSocketDialogMsg extends AbstractModel
if (!$msgRead->read_at) { if (!$msgRead->read_at) {
$msgRead->read_at = Carbon::now(); $msgRead->read_at = Carbon::now();
$msgRead->save(); $msgRead->save();
$this->generatePercentage(true); //
$row = self::incrementRead($this->id);
PushTask::push([ PushTask::push([
'userid' => $this->userid, 'userid' => $row->userid,
'msg' => [ 'msg' => [
'type' => 'dialog', 'type' => 'dialog',
'mode' => 'readed', 'mode' => 'readed',
'data' => [ 'data' => [
'id' => $this->id, 'id' => $row->id,
'read' => $this->read, 'read' => $row->read,
'percentage' => $this->percentage, 'percentage' => $row->percentage,
], ],
] ]
]); ]);
@ -252,6 +241,24 @@ class WebSocketDialogMsg extends AbstractModel
return true; return true;
} }
/**
* 增加已读数量
* @param $msgId
* @return self
*/
private static function incrementRead($msgId)
{
return self::transaction(function () use ($msgId) {
$model = WebSocketDialogMsg::lockForUpdate()->find($msgId);
if (!$model) {
throw new \Exception('记录不存在');
}
$model->increment('read');
return WebSocketDialogMsg::find($msgId);
});
}
/** /**
* emoji回复 * emoji回复
* @param $symbol * @param $symbol