<?php


namespace App\Http\logic\api;


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

class OrderLogic extends BaseApiLogic
{

    // 创建订单
    public function create_order($data_arr, $UserID, $type = 0, $payment_code = 'SerPayMent')
    {
        $body = [
            'app_id' => 1,
            'user_id' => $UserID,
            'order_sn' => $data_arr['custOrderNo'],
            'amount' => $data_arr['payAmt'],
            'order_title' => $data_arr['goodsName'],
            'payment_code' => $payment_code,
            'payment_name' => '创建收款单',
            'remarks' => 'pay_order',
            'created_at' => date('Y-m-d H:i:s'),
            'type' => $type,
            'GameID' => 0,
            'UserID' => $UserID
        ];

        $order = DB::table('agent.dbo.order')->insert($body);


        if (!$order) {
            $this->error = '订单创建失败';
            return false;
        }
        return true;
    }


    public function create($data, $userID, $type = 0)
    {
        $body = [
            'app_id' => 1,
            'user_id' => $userID,
            'order_sn' => $data['pay_orderid'],
            'amount' => $data['pay_amount'],
            'order_title' => $data['pay_productname'],
            'payment_code' => 'pay_order',
            'payment_name' => '创建收款单',
            'remarks' => 'pay_order',
            'created_at' => date('Y-m-d H:i:s'),
            'type' => $type,
            'GameID' => 0,
            'UserID' => $userID
        ];

        $order = DB::table('agent.dbo.order')->insert($body);


        if (!$order) {
            $this->error = '订单创建失败';
            return false;
        }
        return true;
    }

    // 创建订单
    public function orderCreate($order_sn, $amount, $payment_code, $userID, $order_title = '',$GiftsID = 0,$AdId = '',$eventType = 2)
    {
        if (empty($AdId) || $AdId == 'undefined') {
//            $AdId = DB::connection('write')->table(TableName::QPAccountsDB() . 'AccountsInfo')
//                ->where('UserID', $userID)
//                ->value('LastLogonMachine');
//            if(!$AdId){
                $redis = Redis::connection();
                $AdId = $redis->get('user_ad_'.$userID);
//            }
//            $AdId = 'efa7d051-2f29-429b-b26e-d73b64604673';
        }

        $Channel = DB::connection('write')->table(TableName::QPAccountsDB() . 'AccountsInfo')
            ->where('UserID', $userID)->select('Channel')->first()->Channel;

        if($userID == 4293017 ){
//            $Channel = 101;
        }

        $body = [
            'app_id' => 1,
            'user_id' => $userID,
            'order_sn' => $order_sn,
            'amount' => $amount,
            'order_title' => empty($order_title) ? '用户充值' : $order_title,
            'payment_code' => $payment_code,
            'payment_name' => '创建收款单',
            'remarks' => 'pay_order',
            'created_at' => date('Y-m-d H:i:s'),
            'type' => 0,
            'GameID' => 0,
            'UserID' => $userID,
            'GiftsID' => $GiftsID,
            'AdId' => $AdId,
            'eventType' => $eventType,
            'Channel'=>$Channel
        ];

        $order = DB::connection('write')->table('agent.dbo.order')->insert($body);


        if (!$order) {
            $this->error = 'Payment error_2';
            return false;
        }
        return true;
    }
}
