<?php


namespace App\dao\Pay;


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

class PayError
{
    public function create($UserID, $PayConfigValue)
    {

        return true;
        $PollSwitch = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
            ->where('StatusName', 'PollSwitch')
            ->value('StatusValue');
        if (!$PollSwitch) {
            return true;
        }

        $data = [
            'UserID' => $UserID,
            'PayConfigValue' => $PayConfigValue,
            'CreateTime' => now()
        ];
        DB::table(TableName::agent() . 'pay_error')->insert($data);

        $where[] = ['PayConfigValue', $PayConfigValue];
        $map[] = ['type', 'pay'];
        // 查近四个小时出现用户有多少 【大于2，切换通道】
        $date = Carbon::now()->subHours(4)->format('Y-m-d H:i:s');
        if (DB::table(TableName::agent() . 'pay_error')->where($where)->where('CreateTime', '>=', $date)->distinct()->count('UserID') >= 2) {

            DB::table(TableName::agent() . 'admin_configs')->where($map)->where('config_key', $PayConfigValue)->update(['pay_error' => 1]);
        }


        $date = Carbon::now()->subHours(2)->format('Y-m-d H:i:s');
        if (DB::table(TableName::agent() . 'pay_error')->where($where)->where('CreateTime', '>=', $date)->distinct()->count('UserID') < 2) {

            DB::table(TableName::agent() . 'admin_configs')->where($map)->update(['pay_error' => 0]);
        }

        return true;
    }
}
