// +---------------------------------------------------------------------- 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]); } }