www.maiwd.cn // +---------------------------------------------------------------------- namespace app\admin\model\wdsxh\message; use think\Model; /** * 会员消息通知模型 */ class MemberNotification extends Model { // 表名 protected $name = 'wdsxh_member_notification'; // 自动写入时间戳字段 protected $autoWriteTimestamp = 'int'; // 定义时间戳字段名 protected $createTime = 'createtime'; protected $updateTime = false; // 追加属性 protected $append = [ 'is_read_text', 'createtime_text' ]; // 已读状态列表 public static $isReadList = [ 0 => '未读', 1 => '已读' ]; /** * 获取已读状态文本 */ public function getIsReadTextAttr($value, $data) { return isset(self::$isReadList[$data['is_read']]) ? self::$isReadList[$data['is_read']] : ''; } /** * 获取创建时间文本 */ public function getCreatetimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } /** * 关联会员 */ public function member() { return $this->belongsTo('app\admin\model\wdsxh\member\Member', 'member_id', 'id', [], 'LEFT')->setEagerlyType(0); } /** * 标记为已读 */ public function markAsRead() { if ($this->is_read == 0) { $this->is_read = 1; $this->read_time = time(); return $this->save(); } return true; } /** * 获取未读数量 * @param int $member_id 会员ID * @return int */ public static function getUnreadCount($member_id) { return self::where('member_id', $member_id) ->where('is_read', 0) ->count(); } /** * 批量标记为已读 * @param array $ids 消息ID数组 * @param int $member_id 会员ID * @return bool */ public static function markBatchAsRead($ids, $member_id) { return self::where('id', 'in', $ids) ->where('member_id', $member_id) ->update([ 'is_read' => 1, 'read_time' => time() ]); } }