<?php

namespace app\models\user;

use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;

/**
 * 用户搜索历史
 */
class UserSearch extends BaseModel 
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user_search';

    use ModelTrait;

    /**
     * 记录搜索词汇
     */
    public static function InsertHistory($uid, $text)
    {
        return self::create([
            'uid' => $uid,
            'searched' => $text,
            'add_time' => time(),
        ]);
    }

    public static function getList($page, $limit)
    {
        return self::alias('s')->join('user u', 'u.uid=s.uid', 'left')
            ->field('s.uid, u.nickname,s.searched, FROM_UNIXTIME(s.add_time,"%Y-%m-%d %H") as add_time')
            ->order('add_time desc')
            ->page(intval($page), intval($limit))
            ->select()->toArray();
    }
}