<?php


namespace App\Models;


use App\Facade\TableName;
use App\Util;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class RecordPlatformData extends Model
{
    public $connection = 'read';
    protected $table = 'QPRecordDB.dbo.RecordPlatformData';
    public $timestamps = false;

    // 按天获取注册，充值，活跃等
    public function dayAccountInfo($startDateID, $endDateID)
    {
        return self::query()
            ->where('Channel', -1)
            ->whereBetween('DateID', [$startDateID, $endDateID])
            ->selectRaw('IsNull(sum(RegPeple),0) RegPeple,IsNull(sum(ActivePeple),0) ActivePeple,IsNull(sum(PayPeple),0) PayPeple,IsNull(sum(PayTotal),0) PayTotal,IsNull(sum(DrawTotal),0) DrawTotal')
            ->first();
    }

    // 按天获取注册用户
    public function registerDay($startDateID, $endDateID)
    {
        return self::query()
            ->where('Channel', -1)
            ->whereBetween('DateID', [$startDateID, $endDateID])
            ->sum('RegPeple');
    }

    // 按天获取活跃用户
    public function ActiveDay($startDateID, $endDateID)
    {
        return self::query()
            ->where('Channel', -1)
            ->whereBetween('DateID', [$startDateID, $endDateID])
            ->sum('ActivePeple');
    }

    // 按天获取充值用户
    public function WithdrawDay($startDateID, $endDateID)
    {
        return self::query()
            ->where('Channel', -1)
            ->whereBetween('DateID', [$startDateID, $endDateID])
            ->sum('PayPeple');
    }

    /**
     * 增加提现成功手续费记录
     * @param $UserID
     * @param $Free
     */
    public function WithdrawFree($UserID, $Free)
    {
        $DateID = Carbon::now()->format('Ymd');
        $Channel = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
            ->where('UserID', $UserID)
            ->value('Channel');


        if (DB::table(TableName::QPRecordDB() . 'RecordPlatformData')->where(['DateID' => $DateID, 'Channel' => -1])->value('DateID')) {
            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->where(['DateID' => $DateID, 'Channel' => -1])
                ->update(['WithdrawFree' => DB::raw("WithdrawFree+$Free")]);
        } else {
            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->insert(['DateID' => $DateID, 'Channel' => -1, 'WithdrawFree' => $Free]);
        }


        if (DB::table(TableName::QPRecordDB() . 'RecordPlatformData')->where(['DateID' => $DateID, 'Channel' => $Channel])->value('DateID')) {
            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->where(['DateID' => $DateID, 'Channel' => $Channel])
                ->update(['WithdrawFree' => DB::raw("WithdrawFree+$Free")]);
        } else {
            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->insert(['DateID' => $DateID, 'Channel' => $Channel, 'WithdrawFree' => $Free]);
        }

    }


    // 复购用户人数，充值金额
    public function OldUser($UserID, $Channel, $money, $Extra,$type = 3)
    {
        Util::WriteLog('RecordPlatformDataOld',[$UserID, $Channel, $money, $Extra,$type]);
        $dateID = Carbon::now()->format('Ymd');
        $queryUserID = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')->where('UserID', $UserID)
            ->whereDate('RegisterDate', '<', date('Y-m-d'))
            ->value('UserID');

        if (empty($queryUserID)) {  // 不是老用户直接返回
            return true;
        }

        // 判断以前有没有充值
//        if (DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')->where('UserID', $UserID)->first()) {
//            if ($Extra == 1) {
        $AllFormData = DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
            ->where(['Channel' => -1, 'DateID' => $dateID])
            ->select('DateID')
            ->first();

        // 当日单用户，第一次充值增加人数
        $addPayPeple = 0;
        if ($Extra == 1) {
            $addPayPeple = 1;
        }

        if ($AllFormData) {
            if($type == 3){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->updateOrInsert(['Channel' => -1, 'DateID' => $dateID], ['OldUserPayCount' => DB::raw("OldUserPayCount+$addPayPeple"), 'OldUserPayBi' => DB::raw('OldUserPayBi+1'), 'OldUserPaySum' => DB::raw("OldUserPaySum+$money")]);
            }elseif($type == 4){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->updateOrInsert(['Channel' => -1, 'DateID' => $dateID], ['OldUserWithdrawCount' => DB::raw("OldUserWithdrawCount+$addPayPeple"), 'OldUserWithdrawBi' => DB::raw('OldUserWithdrawBi+1'),'OldUserWithdrawSum' => DB::raw("OldUserWithdrawSum+$money")]);
            }
        } else {
            if($type == 3){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->insert(['OldUserPayCount' => 1, 'OldUserPayBi' => 1, 'OldUserPaySum' => $money, 'Channel' => -1, 'DateID' => $dateID]);
            }elseif($type == 4){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->insert(['OldUserWithdrawCount' => 1, 'OldUserWithdrawBi' => 1,  'OldUserWithdrawSum' => $money, 'Channel' => -1, 'DateID' => $dateID]);
            }

        }

        $ChannelFormData = DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
            ->where(['Channel' => $Channel, 'DateID' => $dateID])
            ->select('DateID')
            ->first();
        if ($ChannelFormData) {
//            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
//                ->where(['Channel' => $Channel, 'DateID' => $dateID])
//                ->update(['Channel' => $Channel, 'DateID' => $dateID, 'OldUserPayCount' => DB::raw("OldUserPayCount+$addPayPeple"), 'OldUserPayBi' => DB::raw('OldUserPayBi+1'), 'OldUserPaySum' => DB::raw("OldUserPaySum+$money")]);

            if($type == 3){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->where(['Channel' => $Channel, 'DateID' => $dateID])
                    ->update(['Channel' => $Channel, 'DateID' => $dateID, 'OldUserPayCount' => DB::raw("OldUserPayCount+$addPayPeple"), 'OldUserPayBi' => DB::raw('OldUserPayBi+1'), 'OldUserPaySum' => DB::raw("OldUserPaySum+$money")]);
            }elseif($type == 4){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->where(['Channel' => $Channel, 'DateID' => $dateID])
                    ->update(['Channel' => $Channel, 'DateID' => $dateID, 'OldUserWithdrawCount' => DB::raw("OldUserWithdrawCount+$addPayPeple"), 'OldUserWithdrawBi' => DB::raw('OldUserWithdrawBi+1'),  'OldUserWithdrawSum' => DB::raw("OldUserWithdrawSum+$money")]);
            }

        } else {
//            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
//                ->insert(['Channel' => $Channel, 'DateID' => $dateID, 'OldUserPayCount' => 1, 'OldUserPayBi' => 1, 'OldUserPaySum' => $money]);

            if($type == 3){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->insert(['Channel' => $Channel, 'DateID' => $dateID, 'OldUserPayCount' => 1, 'OldUserPayBi' => 1, 'OldUserPaySum' => $money]);
            }elseif($type == 4){
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->insert(['Channel' => $Channel, 'DateID' => $dateID, 'OldUserWithdrawCount' => 1,'OldUserWithdrawBi' => 1, 'OldUserWithdrawSum' => $money]);

            }
        }
//            }
//        }
    }

    // 当日注册-当日充值
    public function Today($Channel, $money, $RegisterDate, $Extra,$type = 3)
    {
        Util::WriteLog('RecordPlatformData',[$Channel, $money, $RegisterDate, $Extra,$type]);
        $dateID = Carbon::now()->format('Ymd');
//        if ($dateID == date('Ymd', strtotime($RegisterDate)) && $Extra == 1) {
        if ($dateID == date('Ymd', strtotime($RegisterDate))) {
            $AllFormData = DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->where(['Channel' => -1, 'DateID' => $dateID])
                ->select('DateID')
                ->first();

            // 当日单用户，第一次充值，加充值人数
            $addPayPeple = 0;
            if ($Extra == 1) {
                $addPayPeple = 1;
            }

            if ($AllFormData) {

                if($type == 3){
                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->updateOrInsert(['Channel' => -1, 'DateID' => $dateID], ['NewPayPeple' => DB::raw("NewPayPeple+$addPayPeple"), 'NewPayPepleBi' => DB::raw('NewPayPepleBi+1'), 'NewPayTotal' => DB::raw("NewPayTotal+$money")]);

                }elseif($type == 4){
                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->updateOrInsert(['Channel' => -1, 'DateID' => $dateID], ['NewDrawPeple' => DB::raw("NewDrawPeple+$addPayPeple"),'NewDrawBi' => DB::raw('NewDrawBi+1'),'NewDrawTotal' => DB::raw("NewDrawTotal+$money")]);

                }
            } else {
                if($type == 3){

                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->insert(['NewPayPeple' => 1, 'NewPayTotal' => $money, 'Channel' => -1, 'DateID' => $dateID, 'NewPayPepleBi' => 1]);

                }elseif($type == 4){
                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->insert(['NewDrawPeple' => 1, 'NewDrawTotal' => $money, 'Channel' => -1, 'DateID' => $dateID, 'NewDrawBi' => 1 ]);
                }

            }

            $ChannelFormData = DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->where(['Channel' => $Channel, 'DateID' => $dateID])
                ->select('DateID')
                ->first();
            if ($ChannelFormData) {
                if($type == 3){
                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->where(['Channel' => $Channel, 'DateID' => $dateID])
                        ->update(['Channel' => $Channel, 'DateID' => $dateID, 'NewPayPeple' => DB::raw("NewPayPeple+$addPayPeple"), 'NewPayPepleBi' => DB::raw('NewPayPepleBi+1'), 'NewPayTotal' => DB::raw("NewPayTotal+$money")]);

                }elseif($type == 4){
                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->where(['Channel' => $Channel, 'DateID' => $dateID])
                        ->update(['Channel' => $Channel, 'DateID' => $dateID, 'NewDrawPeple' => DB::raw("NewDrawPeple+$addPayPeple") , 'NewDrawBi' => DB::raw('NewDrawBi+1'), 'NewDrawTotal' => DB::raw("NewDrawTotal+$money")]);

                }

            } else {

                if($type == 3){
                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->insert(['Channel' => $Channel, 'DateID' => $dateID, 'NewPayPeple' => 1, 'NewPayTotal' => $money, 'NewPayPepleBi' => 1]);
                }elseif($type == 4){
                    DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                        ->insert(['Channel' => $Channel, 'DateID' => $dateID, 'NewDrawPeple' => 1, 'NewDrawTotal' => $money, 'NewDrawBi' => 1]);
                }
            }
        }
    }

    // 添加赠送金额
    public static function addGiveGold($money, $Channel, $dateID = '')
    {
        if (empty($dateID)) {
            $dateID = Carbon::now()->format('Ymd');
        }


        $AllFormData = DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
            ->where(['Channel' => -1, 'DateID' => $dateID])
            ->select('DateID')
            ->first();


        if ($AllFormData) {
            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->updateOrInsert(['Channel' => -1, 'DateID' => $dateID], ['GiveGold' => DB::raw("GiveGold+$money")]);
        } else {
            DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->insert(['GiveGold' => $money, 'Channel' => -1, 'DateID' => $dateID]);
        }

        if ($Channel != -1) {
            $ChannelFormData = DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                ->where(['Channel' => $Channel, 'DateID' => $dateID])
                ->select('DateID')
                ->first();
            if ($ChannelFormData) {
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->where(['Channel' => $Channel, 'DateID' => $dateID])
                    ->update(['Channel' => $Channel, 'DateID' => $dateID, 'GiveGold' => DB::raw("GiveGold+$money")]);
            } else {
                DB::table(TableName::QPRecordDB() . 'RecordPlatformData')
                    ->insert(['Channel' => $Channel, 'DateID' => $dateID, 'GiveGold' => $money]);
            }
        }


    }
}
