<?php

namespace App\Models;

use App\Facade\TableName;
use App\Http\helper\NumConfig;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Yansongda\Pay\Log;

class RecordScoreInfo extends Model
{
    const TABLE = 'QPRecordDB.dbo.YN_RecordScoreInfo';
    protected $table = self::TABLE;

    public $timestamps = false;
    protected $guarded = [];

    // 彩金----21：绑定手机赠送--33：注册赠送--44：签到--45：充值--49：月卡--42：邮件附件(彩金) 51：首充彩金 36:推广充值彩金
    protected static $Reason = [21, 33, 36, 37, 42, 44, 45, 49, 51, 52, 72, 73];
    /** @var int vip商城充值额外赠送 */
    const REASON_VIP_SEND_CHIPS = 73;

    public static function addScore($user_id, $ChangeScore, $Reason,$currentScore = 0)
    {

        // 增加用户金币变化记录
        if ($currentScore > 0) {
            $AfterScore = $currentScore;
        }else{
            $AfterScore = DB::connection('read')->table('QPTreasureDB.dbo.GameScoreInfo')
                    ->where('UserID', $user_id)
                    ->select('Score')
                    ->lockForUpdate()
                    ->first()->Score ?? 0;
        }

        if ($ChangeScore > 0 && $Reason > 0) {
            $RecordScoreInfo = [
                'BeforeScore' => $AfterScore,
                'AfterScore' => $AfterScore + $ChangeScore,
                'ChangeScore' => $ChangeScore,
                'UserID' => $user_id,
                'Reason' => $Reason,
                'UpdateTime' => date('Y-m-d H:i:s'),
            ];

            DB::connection('write')->table(TableName::QPRecordDB() . 'RecordUserScoreChange')->lockForUpdate()->insertGetId($RecordScoreInfo);


            // 写分总数
            $first = DB::connection('write')->table('QPRecordDB.dbo.RecordUserScoreStatisticsNew')->where('ScoreType', $Reason)
                ->where('UserID', $user_id)
                ->where('DateID', date('Ymd'))
                ->first();

            if ($first) {
                $data = [
                    'ScoreType' => $Reason,
                    'Score' => $first->Score + $ChangeScore,
                    'ScoreCount' => $first->ScoreCount + 1
                ];
                DB::connection('write')->table('QPRecordDB.dbo.RecordUserScoreStatisticsNew')->where('ScoreType', $Reason)
                    ->where('UserID', $user_id)
                    ->where('DateID', date('Ymd'))
                    ->update($data);

            } else {
                $data = [
                    'ScoreType' => $Reason,
                    'UserID' => $user_id,
                    'Score' => $ChangeScore,
                    'ScoreCount' => 1,
                    'DateID' => date('Ymd')
                ];
                DB::connection('write')->table('QPRecordDB.dbo.RecordUserScoreStatisticsNew')->insert($data);
            }
            if (in_array($Reason, self::$Reason)) {
                RecordUserDataStatistics::updateOrAdd($user_id, 0, 0, 0, $ChangeScore);
            }
        }

        return $AfterScore + $ChangeScore;
    }

    // 全部彩金
    public static function totalLottery($UserID = '')
    {
        $sql = DB::connection('read')->table(TableName::QPRecordDB() . 'RecordUserScoreChange')->whereIn('Reason', self::$Reason)
            ->selectRaw('Isnull(SUM(ChangeScore),0) as ChangeScore');

        !empty($UserID) && $sql->where('UserID', $UserID);

        return $sql->first()->ChangeScore;
    }

    // 今日彩金 -- 分渠道
    public static function todayLottery($date, $UserID = '')
    {

        $ChannelLottery = DB::connection('read')->table(TableName::QPRecordDB() . 'RecordUserScoreChange as rc')
            ->whereDate('UpdateTime', $date)
            ->whereIn('Reason', self::$Reason)
            ->join(TableName::QPAccountsDB() . 'AccountsInfo as ai', 'ai.UserID', 'rc.UserID')
            ->selectRaw('Isnull(SUM(ChangeScore),0) as ChangeScore,Channel')
            ->groupBy('Channel')
            ->get();
        return $ChannelLottery;
    }
}
