<?php


namespace App\dao\RecordPlatformData;


use App\Facade\TableName;
use Illuminate\Support\Facades\DB;

class RecordPlatformData
{

    // 申请提现金额,申请人数，申请笔数【分渠道】
    public function ApplyWithDraw($date)
    {
        return DB::connection('sqlsrv')->table(DB::raw(TableName::QPAccountsDB() . 'OrderWithDraw as od WITH (NOLOCK)'))
            ->join(DB::raw(TableName::QPAccountsDB() . 'AccountsInfo as ai WITH (NOLOCK)'), 'ai.UserID', '=', 'od.UserID')
            ->where('CreateDate', '>=', $date->format('Y-m-d'))
            ->where('CreateDate', '<', $date->copy()->addDay()->format('Y-m-d'))
            ->selectRaw('IsNull(sum(WithDraw),0) + IsNull(sum(ServiceFee),0) WithDraw,count(distinct(od.UserID)) UserCount,count(RecordID) CountBi,Channel')
            ->groupBy('Channel')
            ->get();
    }

    // 总申请提现金额【分渠道】
    public function TotalOrderWithDraw($date)
    {
        return DB::connection('sqlsrv')->table(DB::raw(TableName::QPAccountsDB() . 'OrderWithDraw WITH (NOLOCK)'))
            ->where('CreateDate', '>=', $date->format('Y-m-d'))
            ->where('CreateDate', '<', $date->copy()->addDay()->format('Y-m-d'))
            ->selectRaw('IsNull(sum(WithDraw),0) + IsNull(sum(ServiceFee),0) WithDraw,count(distinct(UserID)) UserCount,count(RecordID) CountBi')
            ->first();
    }

    // 提现成功的手续费【分渠道】
    public function WithDrawOkFree($DateID)
    {
        return DB::connection('sqlsrv')->table(DB::raw(TableName::QPRecordDB() . 'RecordUserDataStatisticsNew as rn WITH (NOLOCK)'))
            ->join(DB::raw(TableName::QPAccountsDB() . 'AccountsInfo as ai WITH (NOLOCK)'), 'ai.UserID', '=', 'rn.UserID')
            ->where('DateID', $DateID)
            ->selectRaw('Channel,sum(ServiceFee) ServiceFee')
            ->groupBy('Channel')
            ->get();
    }

    // 游戏人数【分渠道】
    public function GameUserCount($DateID)
    {
        return DB::connection('sqlsrv')->table(DB::raw(TableName::QPRecordDB() . 'RecordUserGameDayCount as rc WITH (NOLOCK)'))
            ->join(DB::raw(TableName::QPAccountsDB() . 'AccountsInfo as ai WITH (NOLOCK)'), 'ai.UserID', '=', 'rc.UserID')
            ->where('DateID', $DateID)
            ->selectRaw('count(distinct(rc.UserID)) UserCount,Channel')
            ->groupBy('Channel')
            ->get();
    }

    // 新增游戏人数【分渠道】
    public function NewGameUserCount($date)
    {
        return DB::connection('sqlsrv')->table(DB::raw(TableName::QPAccountsDB() . 'AccountsInfo as ai WITH (NOLOCK)'))
            ->where('ai.RegisterDate', '>=', $date->format('Y-m-d'))
            ->where('ai.RegisterDate', '<', $date->copy()->addDay()->format('Y-m-d'))
            ->join(DB::raw(TableName::QPRecordDB() . 'RecordUserGameDayCount as rd WITH (NOLOCK)'), 'ai.UserID', '=', 'rd.UserID')
            ->where('DateID', $date->format('Ymd'))
            ->selectRaw('count(distinct(rd.UserID)) UserCount,Channel')
            ->groupBy('Channel')
            ->get();
    }


    // 新增游戏人数打到了提现额度【分渠道】
    public function NewGameWinTopCount($date)
    {
        return DB::connection('sqlsrv')->table(DB::raw(TableName::QPAccountsDB() . 'AccountsInfo as ai WITH (NOLOCK)'))
            ->where('ai.RegisterDate', '>=', $date->format('Y-m-d'))
            ->where('ai.RegisterDate', '<', $date->copy()->addDay()->format('Y-m-d'))
            ->join(DB::raw(TableName::QPTreasureDB() . 'GameScoreInfo as rd WITH (NOLOCK)'), 'ai.UserID', '=', 'rd.UserID')
            ->whereRaw('(ISNULL(rd.Score,0) + ISNULL(rd.InsureScore,0)) > ?', [4000])
            ->selectRaw('COUNT(ai.UserID) AS UserCount, ai.Channel')
            ->groupBy('ai.Channel')
            ->get();
    }

    // 数据修改

    /**
     * @param array $data 要修改的数据
     * @param $UpdateChannel // 要修改的渠道号
     * @param $Channel // 本身的渠道号
     * @param $DateID // 日期 Ymd
     * @param bool $isUpdate // 是否修改
     * @return bool
     */
    public function Update($data, $UpdateChannel, $Channel, $DateID, $isUpdate = false)
    {
        if ($UpdateChannel == $Channel) {
            DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordPlatformData')->where('DateID', $DateID)->where('Channel', $Channel)->update($data);
        } else {
            DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordPlatformData')->where('DateID', $DateID)->updateOrInsert(['DateID' => $DateID, 'Channel' => $Channel], $data);
        }
        return true;
    }

    // 充值彩金
    public function RechargeWinnings($dateID)
    {
        return DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordUserScoreChangeStatistics as rc')
            ->lock('with(nolock)')
            ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'rc.UserID', 'ai.UserID')
            ->where('DateID', $dateID)
            ->whereIn('ScoreType', [45, 51, 59])
            ->selectRaw('sum(Score) as Score,Channel')
            ->groupBy('Channel')
            ->get();
    }

    // 分享彩金
    public function ShareWinnings($dateID)
    {
        return DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordUserScoreChangeStatistics as rc')
            ->lock('with(nolock)')
            ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'rc.UserID', 'ai.UserID')
            ->where('DateID', $dateID)
            ->whereIn('ScoreType', [36, 53, 72])
            ->selectRaw('sum(Score) as Score,Channel')
            ->groupBy('Channel')
            ->get();
    }

    // 低保金额，人数
    public function dibao($dateID)
    {
        return DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordUserScoreChangeStatistics as rc')
            ->lock('with(nolock)')
            ->where('DateID', $dateID)
            ->where('ScoreType', 13)
            ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'rc.UserID', 'ai.UserID')
            ->selectRaw('sum(Score) as Score,count(rc.UserID) TotalDibaoCount,Channel')
            ->groupBy('Channel')
            ->get();
    }
    // 付费低保人数
    public function payDibao($dateID)
    {
        return DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordUserScoreChangeStatistics as rc')
            ->lock('with(nolock)')
            ->where('DateID', $dateID)
            ->where('ScoreType', 13)
            ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'rc.UserID', 'ai.UserID')
            ->join(TableName::QPAccountsDB().'YN_VIPAccount as ac','rc.UserID','ac.UserID')
            ->selectRaw('count(rc.UserID) PayDibao,Channel')
            ->groupBy('Channel')
            ->get();
    }
    // 签到人数，金额
    public function signIn($dateID)
    {
        return DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordUserScoreChangeStatistics as rc')
            ->lock('with(nolock)')
            ->where('DateID', $dateID)
            ->where('ScoreType', 44)
            ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'rc.UserID', 'ai.UserID')
            ->selectRaw('sum(Score) as SignInSum,count(rc.UserID) SignInCount,Channel')
            ->groupBy('Channel')
            ->get();
    }
}
