<?php

namespace App\Modules\Book\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class BookGiftsSend extends Model
{
    protected $table = 'book_gifts_send';
    protected $fillable = ['id','name_desc','icon','cost','uid','bid','gift_id','cost_recharge','cost_reward'];

    /**
     * @param $param
     * @return mixed
     */
    public static function addGift($param) {
        return self::create($param);
    }

    /**
     * @param $uid
     * @param $bid
     * @return mixed
     */
    public static function getSendRecords($bid){
        return self::join('users','users.id','=','book_gifts_send.uid')
            ->select(DB::raw('users.nickname,book_gifts_send.*'))
            ->where([
                //['uid','=',$uid],
                ['bid','=',$bid]
            ])
            ->orderBy('book_gifts_send.created_at','desc')
            ->paginate();
    }

    /**
     * @param $uid
     * @param $bid
     * @return mixed
     */
    public static function getSendRecordsV2($bid){
        $rank = DB::select("SELECT t.*,users.nickname,users.head_img FROM (SELECT uid,bid,sum(cost) as cost_sum FROM book_gifts_send WHERE bid= $bid GROUP BY uid ORDER BY cost_sum desc LIMIT 3) t LEFT JOIN users on users.id = t.uid");
        $record = self::join('users','users.id','=','book_gifts_send.uid')
            ->select(DB::raw('users.nickname,book_gifts_send.*'))
            ->where([
                //['uid','=',$uid],
                ['bid','=',$bid]
            ])
            ->orderBy('book_gifts_send.created_at','desc')
            ->limit(8)
            ->get();
        return compact('rank','record');
    }

    /**
     * @param $uid
     * @return mixed
     */
    public static function getConsumeSendRecords($uid) {
        return self::leftjoin('book_configs','book_configs.bid','=','book_gifts_send.bid')
            ->leftjoin('users','users.id','=','book_gifts_send.uid')
            ->where('book_gifts_send.uid',$uid)
            ->select(['book_gifts_send.id','book_gifts_send.name_desc',
                'book_gifts_send.icon','book_gifts_send.cost',
                'book_gifts_send.gift_id','book_configs.book_name',
                'book_configs.bid','book_gifts_send.created_at',
                'users.nickname','book_gifts_send.uid'])
            ->orderBy('book_gifts_send.created_at','desc')
            ->paginate();
    }

    /**
     * 获取送礼按书统计数据
     * @param $start
     * @param $end
     * @return mixed
     */
    public static function getGiftsSendStatisticByBook($start,$end) {
        return self::select(DB::raw("bid,count(book_gifts_send.id) as send_times,COUNT(DISTINCT book_gifts_send.uid) as send_user_num,sum(book_gifts_send.cost) as cost_sum"))
            ->whereBetween('created_at',[$start,$end])
            ->groupBy('bid')
            ->paginate();
    }

    public static function getGiftsSendStatisticByGift($start,$end) {
        return self::select(DB::raw("gift_id,name_desc as gift_name,count(book_gifts_send.id) as send_times,COUNT(DISTINCT book_gifts_send.uid) as send_user_num,sum(book_gifts_send.cost) as cost_sum"))
            ->whereBetween('created_at',[$start,$end])
            ->groupBy('gift_id')
            ->paginate();
    }
}
