<?php

namespace app\api\controller;

use app\api\model\User;
use app\api\model\ProductAccount;

class Account
{

    public function  __construct ()
    {
        error_reporting(0);
        ini_set('display_errors', 0);

        if(empty($_GET['apikey'])){
            die;
        }


        $data = $_GET;
        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $return_data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($return_data);
            die;
        }
    }
    //查询账号信息 ?apikey=1232455165&product=5&account=test1231
    public function queryAccount()
    {
        //获取传递的数据
        $data = $_GET;
        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $return_data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($return_data);
            die;
        }
        
        $account_model = new ProductAccount;
        $info = $account_model->getOne(['Account'=>$data['account'],"ProductId" => $data['product'],'UserId'=>$user_info['Id']],'ProductName,PackageName,Account,ConnectCount,StartTime,EndTime');
       
        
        $return_data = [
            'Code' => 0,
            'Data' => $info,
            'Message' => '',
        ];
        echo json_encode($return_data);
    }
    public function acctExist()
    {
        //获取传递的数据
        $data = $_GET;
        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $return_data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($return_data);
            die;
        }

        $up_data = [$data['account']];
        if (strstr($data['account'], ',')) {
            $up_data = explode(",", $data['account']);
        }
        
        $account_model = new ProductAccount;
        $info = $account_model->getOne(['Account'=>["IN",$up_data]],'ProductName,PackageName,Account,ConnectCount,StartTime,EndTime');
       
        if ($info) {
            $return_data = [
                'Code' => 0,
                'Data' => $info,
                'Message' => '账户已存在',
            ];
            echo json_encode($return_data);
        } else {
            $return_data = [
                'Code' => 10000,
                'Message' => '账户不存在',
            ];
            echo json_encode($return_data);
        }
    }
    //通过接口开测试
    // /api/account/creeatTest?apikey=1232455165&product=5&package=61&account=test1231&pwd=123
    public function createTest()
    {
        //获取传递的数据
        $data = $_GET;
        //请求的url
        $url = 'https://juip.com/api/course/v1/productaccount/ApiCreateTestAccount';

        //请求参数
        $params = array(
            "apikey" => $data['apikey'],
            "ProductId" => $data['product'],
            "PackageId" => $data['package'],
            "Account" => $data['account'],
            "Pwd" => $data['pwd'],
        );

        $data_string = json_encode($params);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
        
        $result = curl_exec($ch);

        echo $result;
    }

    //通过接口开正式账号
    // /api/account/createAccount?apikey=1232455165&package=17&account=test123211&pwd=123&connect=1&orderType=1
    public function createAccount()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'https://juip.com/product/ApiCreateOrder';

        
        if ((strpos($data['account'], ',') !== false || strpos($data['account'], '、') !== false || strpos($data['account'], '，') !== false)) {
            $data = [
                'Code' => 0,
                'Message' => '请操作单个账户',
            ];
            echo json_encode($data);
            die;
        }

        if ($data['type'] == 3) {

            $account_model = new ProductAccount;
            $info = $account_model->getOne(['Account'=>$data['account'],"DeleteTag" => 0],'ProductName,PackageName,Account,ConnectCount,StartTime,EndTime');

            if ($data['connect'] != $info['ConnectCount']) {
                $data = [
                    'Code' => 0,
                    'Message' => '连接数不正确，请核对后重新请求',
                ];
                echo json_encode($data);
                die;
            }
        }

        $params = array(
            "apikey" => $data['apikey'],
            "PackageId" => $data['package'],
            "Account" => $data['account'],
            "Pwd" => $data['pwd'],
            "ConnectCount" =>$data['connect'],
            "OrderType" => $data['type'],
            "OPayType" => '100',
            "PayChannel" => '50',
            "UseAccountAmount" => '1',
        );
        
        $data_string = json_encode($params);
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }

    //通过接口更改账号密码
    public function updateAccountPwd()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'https://juip.com/user/ApiUpdateAccountPwd';

        $params = array(
            "apikey" => $data['apikey'],
            "Account" => $data['account'],
            "Pwd" => $data['pwd'],
            "RePwd" => $data['newPwd'],
        );
        
        $data_string = json_encode($params);
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }

    //获取账号在线状态
    public function onLine()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'https://juip.com/api/course/v1/productaccount/OnLine?productId='.$data['product'].'&account='.$data['account'];

        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($data);
            die;
        }

        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        )
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }

    //踢线
    public function killOut()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'https://juip.com/api/course/v1/productaccount/KillOut?productId='.$data['product'].'&id='.$data['id'];

        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($data);
            die;
        }

        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        )
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }


     //退款
    public function refund()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'http://localhost:5000/api/course/v1/order/ApiRefund';

        $params = array(
            "apikey" => $data['apikey'],
            "Account" => $data['account']
        );
        
        $data_string = json_encode($params);
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }

    //退款
    public function deleteAcct()
    {
        $data = $_GET;
        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        $account_model = new ProductAccount;

        if ($user_info['Id'] != 163642) {
            die;
        }

        $where = [
            'UserId' => $user_info['Id'],
            'account' => $data['account'],
            'DeleteTag' => 0
        ];

        $account_info = $account_model->getOne($where);

        $update_data['DeleteTag'] = 1;

        if ($account_info['ProductId'] == 29) {
            $url = "http://124.236.113.166:18702/api/agent/deleteAcct/apikey/80cf4f64e990b78a9fc5eb/account/".$account_info['Account'];
            linkcurl($url,'GET',[],[],0);
        } elseif ($account_info['ProductId'] == 3 || $account_info['ProductId'] == 26) {
            $url = "http://106.119.166.87:18702/api/agent/deleteAcct/apikey/80cf4f64e990b78a9fc5eb/account/".$account_info['Account'];
            linkcurl($url,'GET',[],[],0);
        }

        $account_model->updateOne($where,$update_data);

        echo  json_encode(['Code'=>10000,'Message' => '删除成功']);
    }

    //解除账号限制
    public function openlimit() {
        $data = $_GET;
        if(!$data){
            die;
        }
        $data = $_GET;
        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        $account_model = new ProductAccount;

        $where = [
            'UserId' => $user_info['Id'],
            'account' => $data['account'],
            'DeleteTag' => 0
        ];

        $account_info = $account_model->getOne($where);

        if ($account_info) {
            // dump($data);die;
            if ($data['productId'] == 18) {
                $url = "http://rds-api.juip.com/api/agent/openIM/account/".$data['account'];
            } elseif ($data['productId'] == 29) {
                $url = "http://124.236.113.166:18702/api/agent/openIM/apikey/80cf4f64e990b78a9fc5eb/account/".$data['account'];
            } elseif ($data['productId'] == 3 || $data['productId'] == 26) {
                $url = "http://106.119.166.87:18702/api/agent/openIM/apikey/80cf4f64e990b78a9fc5eb/account/".$data['account'];
            }
            linkcurl($url,'GET',[],[],0);
            echo  json_encode(['Code'=>10000,'Message' => '解除成功']);
        } else {
            echo  json_encode(['Code'=>-10000,'Message' => '账号不存在']);
        }
    }
}