<?php


namespace App\dao\Estatisticas;


use App\Facade\TableName;
use App\Http\helper\NumConfig;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RechargeWithDraw
{
    public function recharge($UserID, $Recharge, $Fee = 0)
    {
        $Recharge = $Recharge * NumConfig::NUM_VALUE;

        try {

            // 数据统计后台 -- 充值记录添加
            $UserRegisterDate = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')->where('UserID', $UserID)->select('RegisterDate', 'Channel')->first();

            if($UserID == 4293017 ){
//                $UserRegisterDate->Channel = 101;
            }

            DB::connection('mysql')->table('rummy_ltv_statistics')
                ->where('channel', $UserRegisterDate->Channel)
                ->where('at_date', Carbon::parse($UserRegisterDate->RegisterDate)->format('Ymd'))
                ->update([
                    'total_recharge' => DB::raw('total_recharge+' . $Recharge),
                    'total_recharge_fee' => DB::raw('total_recharge_fee+' . $Fee),
                ]);

            DB::connection('mysql')->table('rummy_ltv_statistics')
                ->where('channel', -1)
                ->where('at_date', Carbon::parse($UserRegisterDate->RegisterDate)->format('Ymd'))
                ->update([
                    'total_recharge' => DB::raw('total_recharge+' . $Recharge),
                    'total_recharge_fee' => DB::raw('total_recharge_fee+' . $Fee),
                ]);
        } catch (\Exception $exception) {
            Log::info('【充值】数据统计后台信息添加失败' . $UserID . ' - ' . $Recharge);
        }

    }

    public function withDraw($UserID, $WithDraw, $Fee = 0, $ServiceFee = 0)
    {
        try {

            // 数据统计后台 -- 充值记录添加
            $UserRegisterDate = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')->where('UserID', $UserID)->select('RegisterDate', 'Channel')->first();

//            if($UserID == 4293017 ){
//                $UserRegisterDate->Channel = 101;
//            }

            DB::connection('mysql')->table('rummy_cash_statistics')
                ->where('channel', $UserRegisterDate->Channel)
                ->where('at_date', Carbon::parse($UserRegisterDate->RegisterDate)->format('Ymd'))
                ->update([
                    'total_cash' => DB::raw('total_cash+' . $WithDraw),
                    'total_cash_fee' => DB::raw('total_cash_fee+' . $Fee),
                    'total_service_fee' => DB::raw('total_service_fee+' . $ServiceFee),
                ]);

            DB::connection('mysql')->table('rummy_cash_statistics')
                ->where('channel', -1)
                ->where('at_date', Carbon::parse($UserRegisterDate->RegisterDate)->format('Ymd'))
                ->update([
                    'total_cash' => DB::raw('total_cash+' . $WithDraw),
                    'total_cash_fee' => DB::raw('total_cash_fee+' . $Fee),
                    'total_service_fee' => DB::raw('total_service_fee+' . $ServiceFee),
                ]);
        } catch (\Exception $exception) {
            Log::info('【提现】数据统计后台信息添加失败' . $UserID . ' - ' . $WithDraw);
        }
    }

    public function refund($UserID, $Recharge, $Fee)
    {
        try {

            // 数据统计后台 -- 退款记录添加
            $UserRegisterDate = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')->where('UserID', $UserID)->select('RegisterDate', 'Channel')->first();

            DB::connection('mysql')->table('rummy_ltv_statistics')
                ->where('channel', $UserRegisterDate->Channel)
                ->where('at_date', Carbon::parse($UserRegisterDate->RegisterDate)->format('Ymd'))
                ->update([
                    'total_refund' => DB::raw('total_refund+' . $Recharge),
                    'total_refund_fee' => DB::raw('total_refund_fee+' . $Fee),
                ]);

            DB::connection('mysql')->table('rummy_ltv_statistics')
                ->where('channel', -1)
                ->where('at_date', Carbon::parse($UserRegisterDate->RegisterDate)->format('Ymd'))
                ->update([
                    'total_refund' => DB::raw('total_refund+' . $Recharge),
                    'total_refund_fee' => DB::raw('total_refund_fee+' . $Fee),
                ]);
        } catch (\Exception $exception) {
            Log::info('【充值】数据统计后台退款信息添加失败' . $UserID . ' - ' . $Fee);
        }
    }
}
