<?php

namespace App\Game\Services;

use App\dao\Estatisticas\RechargeWithDraw;
use App\Models\PrivateMail;
use App\Models\RecordUserDataStatistics;
use App\Services\LogDayStatisticalByDayAndChannel;
use App\Services\StoredProcedure;
use App\Util;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;

class AgentSystemService
{
    public static function callAgentBackofficeApi($postData,$path='/agent_notify')
    {
        $apiurl = 'http://agent.24680.org';


        $client = new Client();

        $response = $client->post($apiurl . $path, [
            'verify'=>false,
            'form_params' => $postData, // 传递 POST 数据//                'query' => $getData, // 传递 GET 数据
        ]);
        $res = json_decode($response->getBody(), true);
//            Util::WriteLog('subserver',$res);
        return $res;
    }
    public static function FailWithdraw($orderWithDraw){
        self::callAgentBackofficeApi(['agentID'=>$orderWithDraw->BankNO,'sn'=>$orderWithDraw->BranchBank,'status'=>4],'/agent_notify');
    }
    public static function FinishWithdraw($orderWithDraw)
    {
        $query=$orderWithDraw;
        $UserID=$orderWithDraw->UserID;
        Util::WriteLog('AgentSystem','AgentSystem提现成功');
        $now = now();
        $withdraw_data = [
            'State' => 2,
            'agent' => 6666,
            'finishDate' => $now
        ];

        $TakeMoney = $orderWithDraw->WithDraw + $orderWithDraw->ServiceFee;
        $OrderId=$orderWithDraw->OrderId;
        // 增加提现记录
        $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->first();
        if ($first) {
            DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->increment('TakeMoney', $TakeMoney);
        } else {
            DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
            try {
                PrivateMail::praiseSendMail($UserID);
            }catch (\Exception $e){

            }
        }


        // 免审的时候，修改免审状态
        $withdrawal_position_log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $OrderId)->first();
        if ($withdrawal_position_log) {
            DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $OrderId)->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
        }

        try {
            StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
        }catch (\Exception $exception){
            Util::WriteLog('StoredProcedure',$exception);
        }


        $ServiceFee = $orderWithDraw->ServiceFee;
        // 增加用户提现值
        RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);

        // 给用户发邮件
        //PrivateMail::successMail($UserID, $OrderId, $TakeMoney);

        //StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);

        // 数据统计后台 -- 提现记录添加
        (new RechargeWithDraw())->withDraw($UserID, $TakeMoney);
        $RecordData = [
            'before_state' => $query->State,
            'after_state' => $withdraw_data['State'] ?? 0,
            'RecordID' => $query->RecordID,
            'update_at' => date('Y-m-d H:i:s')
        ];

        // 添加用户提现操作记录
        DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
//        DB::connection('write')->table('QPAccountsDB.dbo.withdraw_notify')->updateOrInsert(['order_sn' => $OrderId], $notify_data);
        DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $query->OrderId)->update($withdraw_data);

        if (isset($withdraw_data['State']) && $withdraw_data['State'] == 2) {
            // 单控标签
//            StoredProcedure::user_label($UserID, 2, $TakeMoney);
            // 渠道后台埋点
            (new LogDayStatisticalByDayAndChannel())->updateData($UserID, 2);
        }

        self::callAgentBackofficeApi(['agentID'=>$orderWithDraw->BankNO,'sn'=>$orderWithDraw->BranchBank,'status'=>2],'/agent_notify');

    }


}
