<?php
/**
 * Created by PhpStorm.
 * User: ikeke
 * Date: 2018/12/4
 * Time: 15:08
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;
use DB;

class MoveDataController extends Controller
{
    public function __construct()
    {
        
    }

    /*//连接数据库
    public function connecDb($host,$port,$dbname,$user,$password){
        $host = "host={$host}";
        $port = "port={$port}";
        $dbname = "dbname={$dbname}";
        $credentials = "user={$user} password={$password}";
        $db = pg_connect("$host $port $dbname $credentials");
        if(!$db)return -4020000202;
        return $db;
    }

    //查询
    public function select($db,$sql,$type=1){
        $data = @pg_query($db,$sql);
        print_r($data);
        if($type!=1){
            $res = pg_num_rows($data);
            return $res;
        }
        $resdata = array();
        $i=0;
        while ($row = pg_fetch_array($data,null,PGSQL_ASSOC)){
            $resdata[$i] = $row;
            $i++;
        }
        if($resdata)return $resdata;
        return array();
    }*/

    //迁移数据
    /*public function MovedataView(Req $req){
        $limit = Request::has ('limit') ? Request::get ('limit') : 10;
        $page = Request::has ('page') ? Request::get ('page') : 1;
        $ids=isset($req->id)?trim($req->id):'all';
        $db = $this->connecDb('192.168.2.200','5432','kaiyou2','postgres','qq123456');
        $sql = 'select * from account';
        $data = $this->select($db,$sql);
        
        $clslog = '\App\Model\Account_list';
        $cls = '\App\Model\Account';
        $bank = '\App\Model\Account_bank';
        $group = '\App\Model\Account_group';
        $userpwd = '\App\Model\Account_password';
        $usertoken = '\App\Model\Account_Token';
        $uservip = '\App\Model\Uservip';
        if (!$req->isMethod('post')) {
            if($ids=='all'){
                $unum = $this->select($db,$sql,2);
                $data['unum'] = empty($unum)?0:$unum;
            }else{
                $data['unum'] = 1;
            }
            
            return view('admin.MoveData/index', array('data' => $data));
        } else {
            echo '<pre>';
            $password = Request::has('rpassword') ? Request::get('rpassword') : '';
            $allnum = Request::has('allnum') ? Request::get('allnum') : '0';
            if (empty($password)) {
                return responseToJson(-7010123622); //密码不能为空
            }
            //验证二级密码
            //验证密码
            $checkpwdsql = 'select * from system_user where id='.session('adminInfo.admin_id');
            $admin_info = $this->select($db,$checkpwdsql);
            $admin_info = current($admin_info);
            if (!$admin_info) {
                return responseToJson(-7010100202);
            }
            if (!VerPassword($password, $admin_info['encryption_2'], $admin_info['password_2'])) {
                return responseToJson(-7010001622);
            }

            $log = array(
                session('adminInfo.admin_name'),
            );
            //$res = OperationLog(session('adminInfo.admin_id'), 'GameKjcodes', $log);
            $identitys = array();
            $accounts = array();
            if($ids=='all'){
                $offset = ($page-1) * $limit;
                $uinfosql = 'select * from account_list orderBy id asc limit ' . $limit . ' offset ' . $offset;
            }else{
                $uinfosql = 'select * from account_list where id=' . $ids;
                $allnum = 1;
            }
            $uinfo = $this->select($db,$uinfosql);
            print_r($uinfo);exit;
            $curnum = count($uinfo);
            if(empty($uinfo) || $curnum<1)return responseToJson(-4020000102);
            foreach ($uinfo as $k => $v) {
                $identitys[] = $v['account_identity'];
                $accounts[] = $v['account'];
                $accounts[] = 'vip' . $v['account'];
            }
            $bankinfo = $bank::whereIn('account_identity',$identitys)->get();
            $pwdinfo = $userpwd::whereIn('account_identity',$identitys)->get();
            $tokeninfo = $usertoken::whereIn('account_identity',$identitys)->get();
            $vipinfo = $uservip::whereIn('account_identity',$identitys)->get();
            $data = array(
                'identitys' => $identitys,
                'accounts' => $accounts,
                'uinfo' => $uinfo,
                'bankinfo' => $bankinfo?$bankinfo->toArray() : array(),
                'pwdinfo' => $pwdinfo?$pwdinfo->toArray() : array(),
                'tokeninfo' => $tokeninfo?$tokeninfo->toArray() : array(),
                'vipinfo' => $vipinfo?$vipinfo->toArray() : array(),
            );
            echo "<pre>";
            print_r($data);exit;
            return responseToJson(1,'',array('allnum'=>$allnum,'curnum'=>$curnum,'data'=>$data));
        }
    }*/

    //迁移数据
    public function MovedataView(Req $req){
        $limit = Request::has ('limit') ? Request::get ('limit') : 10;
        $page = Request::has ('page') ? Request::get ('page') : 1;
        $ids=isset($req->id)?trim($req->id):'all';
        $oldDB = DB::connection('pgsql');

        $oldcls = $oldDB->table('account');
        if (!$req->isMethod('post')) {
            if($ids=='all'){
                $unum = $oldcls->count('id');
                $data['unum'] = empty($unum)?0:$unum;
            }else{
                $data['unum'] = 1;
            }
            
            return view('admin.MoveData/index', array('data' => $data));
        } else {
            $oldclslog = $oldDB->table('account_list');
            $oldbank = $oldDB->table('account_bank');
            $oldgroup = $oldDB->table('account_group');
            $olduserpwd = $oldDB->table('account_password');
            $oldusertoken = $oldDB->table('account_token');
            $olduservip = $oldDB->table('user_vip');
            $oldadmin = $oldDB->table('system_user');
            $oldpay = $oldDB->table('pay_password');
            $password = Request::has('rpassword') ? Request::get('rpassword') : '';
            $allnum = Request::has('allnum') ? Request::get('allnum') : '0';
            if (empty($password)) {
                return responseToJson(-7010123622); //密码不能为空
            }
            //验证二级密码
            //验证密码
            $admin_info = $oldadmin->where('id', session('adminInfo.admin_id'))->first();
            if (!$admin_info) {
                return responseToJson(-7010100202);
            }
            $admin_info = $this->toArray($admin_info);
            if (!VerPassword($password, $admin_info['encryption_2'], $admin_info['password_2'])) {
                return responseToJson(-7010001622);
            }
            
            $log = array(
                session('adminInfo.admin_name'),
            );
            //$res = OperationLog(session('adminInfo.admin_id'), 'GameKjcodes', $log);
            $identitys = array();
            $accounts = array();
            if($ids=='all'){
                $offset = ($page-1) * $limit;
                $uinfo = $oldclslog->limit($limit)->offset($offset)->orderBy('identity','asc')->get();
            }else{
                $uinfo = $oldclslog->where('id',$ids)->get();
                $allnum = 1;
            }
            $uinfo = $this->toArray($uinfo);
            $curnum = count($uinfo);
            if(!$uinfo || $curnum<1)return responseToJson(-4020000102);
            foreach ($uinfo as $k => $v) {
                $identitys[] = $v['account_identity'];
                $accounts[] = $v['account'];
                $accounts[] = 'vip' . $v['account'];
            }
            $bankinfo = $oldbank->whereIn('account_identity',$identitys)->get();
            $pwdinfo = $olduserpwd->whereIn('account_identity',$identitys)->get();
            $tokeninfo = $oldusertoken->whereIn('account_identity',$identitys)->get();
            $vipinfo = $olduservip->whereIn('account_identity',$identitys)->get();
            $payinfo = $oldpay->whereIn('account_identity',$identitys)->get();
            $data = array(
                'identitys' => $identitys,
                'accounts' => $accounts,
                'uinfo' => $uinfo,
                'bankinfo' => $this->toArray($bankinfo),
                'pwdinfo' => $this->toArray($pwdinfo),
                'tokeninfo' => $this->toArray($tokeninfo),
                'vipinfo' => $this->toArray($vipinfo),
                'payinfo' => $this->toArray($payinfo),
            );
            $this->AddData($data);
            return responseToJson(1,'',array('allnum'=>$allnum,'curnum'=>$curnum,'data'=>$data));
        }
    }

    //迁移数据到新数据库
    function AddData($data){
        $newDB = DB::connection('pgsql1');

        $newcls = $newDB->table('account');
        $newclslog = $newDB->table('change_record');
        $newdetail = $newDB->table('account_detailed');
        $newbank = $newDB->table('account_bank');
        $newgroup = $newDB->table('account_group');
        $newuserpwd = $newDB->table('account_password');
        $newusertoken = $newDB->table('account_token');
        $newuservip = $newDB->table('user_vip');
        $newpay = $newDB->table('pay_password');
        //获取已迁移数据信息，用于避免重复迁移
        $hsinfo = $newclslog->select('old_identity')->whereIn('old_identity',$data['identitys'])->get();
        $hsids = array();
        $hsinfo = $this->toArray($hsinfo);
        if($hsinfo && count($hsinfo)>0){
            foreach ($hsinfo as $k => $v) {
                $hsids[] = $v['old_identity'];
            }
        }

        //获取分组信息
        $groupinfo = $this->getGroupinfo($newgroup);
        //查询重复用户名
        $reaccount = $newcls->select('account')->whereIn('account',$data['accounts'])->get();
        $unames = array();
        $reaccount = $this->toArray($reaccount);
        if($reaccount && count($reaccount)>0){
            foreach ($reaccount as $k => $v) {
                $unames[] = $v['account'];
            }
        }
        //不迁移数据
        $nouser = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/nouser.txt');
        $nouser = explode(PHP_EOL, $nouser);

        //组装数据
        $cashinfo = array();
        $account_data = array();    //会员信息
        $accountdetail_data = array();  //会员详情数据
        $record_data = array(); //迁移记录信息
        foreach ($data['uinfo'] as $k => $v) {
            if(in_array($v['account_identity'], $hsids))continue;
            if(in_array($v['account'], $nouser)){
                $hsids[] = $v['account_identity'];
                continue;
            }

            $tuname = !in_array($v['account'], $unames)?$v['account'] : (!in_array('vip'.$v['account'], $unames)?'vip'.$v['account'] : $v['account'].'vip');
            $account_data[] = array(
                'identity' => $v['identity'],
                'account' => $tuname,
                'status' => $v['account_status'],
                'fanshui' => $v['fanshui'],
                'remark' => $v['remark'],
            );
            $record_data[] = array(
                'old_user' => $v['account'],
                'old_identity' => $v['account_identity'],
                'new_user' => $tuname,
                'new_identity' => $v['account_identity'],
                'update_time' => date('Y-m-d H:i:s'),
                'old_id' => $v['id'],
            );
            $accountdetail_data[] = array(
                'identity' => $v['detail_identity'],
                'account_identity' => $v['account_identity'],
                'register_time' => $v['register_time'],
                'register_ip' => $v['register_ip'],
                'last_time' => $v['last_time'],
                'last_ip' => $v['last_ip'],
                'grade' => $v['grade'],
                'email' => $v['email'],
                'phone' => $v['phone'],
                'invitation' => $v['invitation'],
                'available_cash' => 0,
                'cash' => 0,
                'frozen_cash' => $v['frozen_cash'],
                'open_invitation' => $v['open_invitation'],
                'spread' => $v['spread'],
                'placing' => $v['placing'],
                'token' => $v['token'],
                'statuss' => $v['statuss'],
                'ranking' => $v['ranking'],
                'card' => $v['card'],
                'name' => $v['name'],
                'show' => $v['show'],
                'img_url' => $v['img_url'],
                'qq' => $v['qq'],
                'register_url' => $v['register_url'],
                'last_url' => $v['last_url'],
                'group_code' => $this->getGroupcode($groupinfo,$v['group_code'],$v['group_name']),
                'group_name' => $v['group_name'],
                'parent_id' => $v['parent_id'],
                'parent_path' => $v['parent_path'],
                'level' => $v['level'],
                //'finance_cash' => $v['finance_cash'],
            );
            if($v['cash']<=0)continue;
            $cashinfo[$v['account_identity']] = array(
                'identity' => $v['account_identity'],
                'account' => $v['account'],
                'money' => $v['cash'],
                'remark' => '用户活动，账号资金迁移',
            );
        }

        //会员相关数据组装
        $bank_data = array();   //会员银行卡信息
        $pwd_data = array();    //会员密码数据
        $token_data = array();  //会员token信息
        $uservip_data = array();  //会员vip信息
        $pay_data = array();    //支付密码信息
        if(!empty($data['bankinfo']) && count($data['bankinfo'])>0){
            foreach ($data['bankinfo'] as $k => $v) {
                if(in_array($v['account_identity'], $hsids))continue;
                $v['account_name'] = !in_array($v['account_name'], $unames)?$v['account_name'] : (!in_array('vip'.$v['account_name'], $unames)?'vip'.$v['account_name'] : $v['account_name'].'vip');
                unset($v['id']);
                unset($v['bank_code']);
                $bank_data[] = $v;
            }
        }
        if(!empty($data['pwdinfo']) && count($data['pwdinfo'])>0){
            foreach ($data['pwdinfo'] as $k => $v) {
                if(in_array($v['account_identity'], $hsids))continue;
                unset($v['id']);
                $pwd_data[] = $v;
            }
        }
        if(!empty($data['tokeninfo']) && count($data['tokeninfo'])>0){
            foreach ($data['tokeninfo'] as $k => $v) {
                if(in_array($v['account_identity'], $hsids))continue;
                unset($v['id']);
                $token_data[] = $v;
            }
        }
        if(!empty($data['vipinfo']) && count($data['vipinfo'])>0){
            foreach ($data['vipinfo'] as $k => $v) {
                if(in_array($v['account_identity'], $hsids))continue;
                $v['account_name'] = !in_array($v['account_name'], $unames)?$v['account_name'] : (!in_array('vip'.$v['account_name'], $unames)?'vip'.$v['account_name'] : $v['account_name'].'vip');
                unset($v['id']);
                $uservip_data[] = $v;
            }
        }
        if(!empty($data['payinfo']) && count($data['payinfo'])>0){
            foreach ($data['payinfo'] as $k => $v) {
                if(in_array($v['account_identity'], $hsids))continue;
                unset($v['id']);
                $pay_data[] = $v;
            }
        }

        DB::beginTransaction ();
        //添加数据到对应表中
        $res = $this->addDataIntable($newcls,$account_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
        $res = $this->addDataIntable($newdetail,$accountdetail_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
        $res = $this->addDataIntable($newclslog,$record_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
        $res = $this->addDataIntable($newbank,$bank_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
        $res = $this->addDataIntable($newuserpwd,$pwd_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
        $res = $this->addDataIntable($newusertoken,$token_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
        $res = $this->addDataIntable($newuservip,$uservip_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
        $res = $this->addDataIntable($newpay,$pay_data);
        if($res<0){
            DB::rollback ();
            return $res;
        }
//print_r($cashinfo);
        //会员加款
        //$url = $_SERVER['HTTP_ORIGIN'] . '/admin/Regulation/Edit';
        if(count($cashinfo)>0){
            foreach ($cashinfo as $k => $v) {
                //$res = $this->addMoney($url,$v);
                $res = $this->addMoneys($v,$newDB);
                if($res<0){
                    DB::rollback ();
                    return $res;
                }
            }
        }
        DB::commit ();
        return 1;
    }

    //通过用户分组名获取用户分组代码
    function getGroupcode($groupinfo,$group_code,$group_name){
        if(empty($groupinfo))return $group_code;
        $group_name = explode('|', $group_name);
        if(count($group_name)<1)return $group_code;
        $group_code = '0,';
        foreach ($group_name as $k => $v) {
            $v = trim($v);
            if(isset($groupinfo[$v])){
                $group_code .= $groupinfo[$v] . ',';
            }
        }
        return $group_code;
    }

    //获取本站用户组信息
    function getGroupinfo($newgroup){
        $data = $newgroup->select('group_name','group_code')->get();
        $data = $this->toArray($data);
        $resdata = array();
        if(count($data)>0){
            foreach ($data as $k => $v) {
                $resdata[$v['group_name']] = $v['group_code'];
            }
        }
        return $resdata;
    }

    //加款
    function addMoneys($getdata,$newDB)
    {
        $id = $getdata['identity'];
        $money = $getdata['money'];
        $remark = $getdata['remark'];

        $newcls = $newDB->table('account');
        $newrecharge = $newDB->table('money_recharge');
        $newwith = $newDB->table('withdraw_rule');
        $newmc = $newDB->table('money_count');
        $newman = $newDB->table('message');
        $newdetail = $newDB->table('account_detailed');
        $newmdetail = $newDB->table('money_details');
        $newlog = $newDB->table('system_log');

        $admin_name = session ('adminInfo.admin_name') ? session ('adminInfo.admin_name') : '来自接口';

        //检测提交数据
        $res_data = $this->CheckaddMoney ($id, $money,$newrecharge);
        if ($res_data < 0)return $res_data;
        
        $data = array(
            'account_name' => $getdata['account'],
            'account_identity' => $id,
            'money' => $money,
            'reason' => $remark,
            'sysetem_user' => $admin_name,
        );

        //充值处理
        //资金统计记录
        $cdata = $data;
        $cdata['pass_time'] = date ('Y-m-d', time ());
        $res = $this->AddCountinfo ($cdata, $newmc);
        if ($res < 0)return $res;
        
        //加钱
        $data['money_cash'] = $money;
        $data['rate'] = 1;
        $trade_type = 12;
        /***********活动加款**************/
        $reason = str_replace ('用于活动', '!@#$', $data['reason']);
        $order_id = $this->addMoney ($data, 1, $trade_type,$newdetail,$newman,$newrecharge,$newmdetail);//加钱
        if ($order_id < 0)return $order_id;

        $prize_money = 0;
        $log = array(
            session ('adminInfo.admin_name'),
            $data['account_name'],
            $money,
            $prize_money,
            $data['reason'],
            $order_id,
            1,
        );
        $this->OperationLog (session ('adminInfo.admin_id'), 'add_money', $log,$newlog);
        return 1;
    }

    function OperationLog($id, $opt, $_data,$newlog)
    {
        $data = array(
            'identity' => UUID(),
            'status' => 1,
            'ip' => GETIP(),
            'operation_time' => date('Y-m-d H:i:s', time()),
            'system_identity' => $id,
            'operation' => vsprintf(trans('operation.' . $opt), $_data),
        );
        return $newlog->insert($data);
    }

    //添加最新金额到统计表
    private function AddCountinfo($data,$newmc){
        $mtype = 'save';
        $ts = GetpartTime($data['pass_time']);  //获取统计周期信息
        $type = $this->GetcountType();  //获取周期类型
        $indata = array(
            'account' => $data['account_name'],
            'account_identity' => $data['account_identity'],
            'ut_time' => $data['pass_time'],
            $mtype => $data['money'],
        );
        for($i=1;$i<6;$i++){
            $indata['count_time'] = $ts[$type[$i]];
            switch ($i) {
                case '1':
                    $indata['count_alltime'] = $indata['count_time'];
                    break;
                case '3':
                    $indata['count_alltime'] = $ts['year'] . '-' . $ts['month'] . '-' . $indata['count_time'];
                    break;
                case '4':
                    $indata['count_alltime'] = $ts['wyear'] . '-' . $indata['count_time'];
                    break;
                
                default:
                    $indata['count_alltime'] = $ts['year'] . '-' . $indata['count_time'];
                    break;
            }
            $indata['type'] = $i;
            $indata['identity'] =$data['account_identity'] . '_' . $i . '_' . $indata['count_alltime'];
            $res=$newmc->where('identity',$indata['identity'])->first();
            if(!$res){
                $res = $newmc->insert($indata);
            }else{
                $res = $newmc->where('identity',$indata['identity'])->increment($mtype,$data['money']);
            }
            if($res<1)return -300326;
        }
        return 1;
    }

    //周期类型
    private function GetcountType(){
        return $type = array(
            '1' => 'year',
            '2' => 'month',
            '3' => 'day',
            '4' => 'week',
            '5' => 'season',
        );
    }

    /**
     * 加钱函数
     * @param array $data
     * @param int $type
     * @param int $trade_type
     * @param obj $newudetail
     * @param obj $newman
     * @param obj $newrecharge
     * @param obj $newmdetail
     * @return int|string
     */
    private function addMoney(array $data, $type, $trade_type,$newudetail,$newman,$newrecharge,$newmdetail)
    {
        $order_id = $this->rechargeLog ($data,$newrecharge);//充值记录
        if ($order_id < 0)return $order_id;
        
        $detail=$data;
        $detail['rate']=1;
        $res = $this->dataLog ($detail, $order_id, $type, $trade_type,$newmdetail);
        if ($res < 0)return $res;

        //添加用户余额
        $res1=$newudetail->where('account_identity',$data['account_identity'])->increment('available_cash',$data['money']);
        if(!$res1)return -5040022022;//增加约失败
        
        $res2=$newudetail->where('account_identity',$data['account_identity'])->increment('cash',$data['money']);
        if(!$res2)return -5040022022;//增加约失败

        $this->updateMessage ('admin', $data['account_name'], '加款提示', '加款成功，加款金额:' . $data['money'], $newman);
        return $order_id;
    }

    //添加公告数据
    function updateMessage($sender, $reciver, $title, $content, $newman)
    {
        $data['add_time']=date('Y-m-d H:i:s');
        $data['formname']=$sender;
        $data['toname']=$reciver;
        $data['title'] = $title;
        $data['type'] =2;
        $data['content'] = $content;
        //普通消息
        $res = $newman->insertGetId ($data);
        if(!$res)return -6030001222;
        return 1;
    }

    /**
     * 添加充值/扣款详情记录函数
     * @param array $data 详情数据数组
     * @param mixed $order_id 充值/扣款订单号
     * @param int $type 订单类别[1=>'充值',2=>'扣款'];
     * @param int $trade_type 交易类型
     * @return int|string 成功返回当前详单号，失败返回错误号
     */
    private function dataLog(array $data, $order_id, $type, $trade_type,$money_db)
    {
        $rate = 1;
        unset($data['rate']);
        $deal_data = $data;
        $deal_data['money_type'] = $type;
        $deal_data['money_time'] = date ('Y-m-d H:i:s', time ());
        $deal_data['trade_id'] = $order_id;
        $deal_data['trade_type'] = $trade_type;
        $admin_name = session ('adminInfo.admin_name');
        $admin_name = isset($admin_name) ? $admin_name : '来自接口' . GETIP ();
        $deal_data['sysetem_user'] = $admin_name;
        $deal_data['trade_desc'] = sprintf (trans ('trade.admin_active_money'), $admin_name, $data['account_name'], $data['money'], $order_id, $rate);

        $deal_data['status'] = 1;
        $deal_data['info_identity'] = UUID();
        $res = $money_db->insert($deal_data);
        if (!$res) {
            return -2020072022; //插入资金详情失败
        }
        return $order_id;
    }


    /**
     * 充值记录
     * @param array $data 充值记录数据
     * @return int|string 充值成功返回充值订单号
     */
    private function rechargeLog(array $data,$recharge)
    {
        $data['recharge_type'] = '活动充值';
        $data['status'] = 1;
        $data['apply_time'] = date ('Y-m-d H:i:s', time ());
        $data['complete_time'] = date ('Y-m-d H:i:s', time ());
        $data['rate'] = 1;
        $data['order_id'] = OrderID (); 
        $data['info_identity'] = UUID ();
        $data['remark'] = '后台充值';
        $res = $recharge->insert($data);
        if (!$res)return -3020011322; //加款失败
        
        return $data['order_id'];exit;
    }

    //调用加款接口
    /*function addMoney($url,$data){
        $data = json_encode($data);
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);  //设置http验证方法
        curl_setopt($ch, CURLOPT_TIMEOUT,30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'X-USER-TOKEN: {{ csrf_token() }}'
        ));

        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        $cinfo = curl_getinfo($ch);
        //释放curl句柄
        curl_close($ch);
        //返回数据
        print_r($cinfo);
        print_r($output);
        return $output;
    }*/

    //添加数据到对应表中
    function addDataIntable($obj,$data){
        if(!empty($data) && count($data)>0){
            $res = $obj->insert($data);
            if($res>0)return 1;
            return -1;
        }
        return 1;
    }

    //验证金额
    function CheckaddMoney($account_id, $money,$newrecharge)
    {
        $time = date ('Y-m-d H:i:s', time () - 10);
        $res = $newrecharge->where ('account_identity', $account_id)->where ('money', $money)->where ('apply_time', '>', $time)->first ();
        if (!$res) {
            return 1;
        }
        return -5023809122;
    }

    //对象转数组
    function toArray($data){
        $data = json_encode($data);
        $data = json_decode($data,1);
        return $data;
    }
}

