<?php
namespace app\api\controller\board;

use crmeb\services\UtilService;
use app\models\board\UserBoard;
use \think\facade\Config;
use crmeb\utils\Redis;
/**
 * 协议见文档 docs/board.md, 协议可能变更
 *
 * 使用 UserBoard Model, 数据表为 store_order_cart_info
 *
 *
 * Class UserBoardController
 * @package app\api\controller\board
 */
class UserBoardController {
    protected static $DAILY_WIN_MONEY = 1;
    public static $RDS_BOARD_KEY = 'tmp:board';

    public function boards() {
       [$type,] = UtilService::getMore([
           ['type', 1],
       ], null, true);
       $s = $this->get_cached_board();
       $board = json_decode($s, true);
       return app('json')->successful('ok', $board);
    }

    protected function daily_win_money() {
        $board = new UserBoard();
        $res = $board->getDailyWinMoney();
        foreach ($res as &$row) {
            $row['value'] = floatval($row['value']);
            $row['border'] = 0;
            $row['vip'] = 0;
        }
        return array(
            'banner' => Config::get('app.leader_board_banner'),
            'name' => "日榜",
            'board' => $res,
        );
    }

    public function cache_board() {
        $redis = Redis::instance();
        $redis->set(self::$RDS_BOARD_KEY, json_encode($this->daily_win_money()));
    }

    protected function get_cached_board() {
        $redis = Redis::instance();
        return $redis->get(self::$RDS_BOARD_KEY);
    }
}