Files
YwxAppThink/addon/wxchat/model/WxchatUnreadCount.php
T

58 lines
1.8 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\wxchat\model;
use think\Model;
class WxchatUnreadCount extends Model
{
protected $name = 'wxchat_unread_counts';
protected $autoWriteTimestamp = 'int';
protected $createTime = 'create_at';
protected $updateTime = 'update_at';
/**
* 增加未读计数(存在则累加,不存在则新建)
*/
public static function increment($userId, $contactType, $contactId, $step = 1)
{
$row = self::where('user_id', $userId)
->where('contact_type', $contactType)
->where('contact_id', $contactId)
->find();
if ($row) {
$row->unread_count = (int) $row->unread_count + $step;
$row->save();
} else {
$m = new self();
$m->user_id = $userId;
$m->contact_type = $contactType;
$m->contact_id = $contactId;
$m->unread_count = $step;
$m->save();
}
return true;
}
/**
* 清零未读计数
*/
public static function reset($userId, $contactType, $contactId)
{
return self::where('user_id', $userId)
->where('contact_type', $contactType)
->where('contact_id', $contactId)
->update(['unread_count' => 0]);
}
}