<?php
namespace app\models\redis;

use crmeb\utils\Redis;

/**
 * Class SystemCarousel
 * @package app\models\redis
 */
class SystemCarousel {

    protected static $KEY = 'sys:carousel';

    /*
     格式:
            {
                 "text": "<span style=\"color:2343;\"></span>",
                 "uri": "page/boards",
            }
     uri 为客户端路由, 用于客户端跳转
     */
    public static function add($text, $uri='') {
        $val = json_encode([
            'text'=> $text,
            'uri' => $uri,
        ]);

        return Redis::lPush(self::$KEY, $val);
    }

    public static function getFirst($n) {
        $arrStr = Redis::lRange(self::$KEY, 0, $n) ?? '[]';
        $rv = [];
        foreach ($arrStr as $str) {
            $rv[] = json_decode($str);
        }
        return $rv;
    }

    public static function removeTrash($start) {
        return Redis::lTrim(self::$KEY, 0, $start);
    }
}