<?php
namespace app\models\board;

use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;
use think\facade\Db;
use think\facade\Env;

class UserBoard extends BaseModel {
    protected $pk = 'id';
    protected $name = 'store_order_cart_info';
    use ModelTrait;

    /**
     * 按照当日最高赚钱数获取记录
     *

    select u.uid, u.avatar, u.nickname, u.level, oid, sum(reparation) as value
    from tw_store_order_cart_info tsoci inner join tw_store_order tso on tsoci.oid=tso.id inner join tw_user u on tso.uid = u.uid
    where activity is not null and tso.pay_time > 0
    group by oid
    order by value desc, u.uid asc limit 10

     */
    public function getDailyWinMoney() {
        $start = ts_of_day();
        $prefix = Env::get('database.prefix', 'eb_');
        $tuser = $prefix . 'user';
        $tcart_info = $prefix . 'store_order_cart_info';
        $torder = $prefix . 'store_order';
        $sql = sprintf("
            select distinct u.uid, u.avatar, u.nickname, u.level, sum(reparation) as value 
            from %s tsoci inner join %s tso on tsoci.oid=tso.id inner join %s u on tso.uid = u.uid 
            where activity is not null and tso.pay_time > %d and reparation > 0
            group by oid 
            order by value desc, u.uid asc limit %d
        ", $tcart_info, $torder, $tuser, $start, 30);
        return Db::query($sql);
    }
}