<?php

namespace app\admin\model;

use support\Model;

class UserNews extends Model
{
    const LOG_TYPE_NOTICE = 0;//消息
    const LOG_TYPE_REMIND = 1;//提醒
    const LOG_TYPE_SERVICE = 2;//服务
    const LOG_TYPE_ACTIVITY = 3;//活动
    const AUTH_TYPE = [
        self::LOG_TYPE_NOTICE => '消息',
        self::LOG_TYPE_REMIND => '提醒',
        self::LOG_TYPE_SERVICE => '服务',
        self::LOG_TYPE_ACTIVITY => '活动'
    ];
    /**
     * 会员消息表
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_news';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'log_id';

    public $timestamps = false;
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */

    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'uid');
    }

    /**
     * Notes:插入数据
     * @param array $data
     * @return int
     * User: yym
     * Date: 2022/12/28
     */
    public static function insertData(array $data)
    {
        return static::insertGetId($data);
    }

    /**
     * Notes:获取消息列表
     * @param int $uid
     * @param int $type
     * @param int $page
     * @param int $limit
     * @return array
     * User: yym
     * Date: 2022/12/28
     */
    public static function getUserNews(int $uid, int $type, int $page, int $limit)
    {
        return static::where(['log_type' => $type])
            ->when($uid > 0, function ($query) use ($uid){
                $query->where(function ($query2) use ($uid){
                    $query2->where('log_uid', '=', $uid)
                        ->orWhere('log_uid', '=', 0);
                });
            })
            ->forPage($page, $limit)
            ->orderBy('log_create_time', 'desc')
            ->get()
            ->toArray();
    }
}
