<?php
namespace app\sever\controller;
use \think\Db;
use \think\Controller;
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Connection\AsyncTcpConnection;
class Words {
    public function add_order_tx($take_id, $content)
    {
        error_reporting(E_ALL^E_NOTICE^E_WARNING);
        $web_config = Db::name('config')->where('id',1)->find();
        $value = json_decode($content,true);
        for ($i = 0; $i < count($value); $i++) {
            if($value[$i]['id']!=''){
             //取账号信息
             $land_data = Db::name('land')->where('id',$value[$i]['id'])->find();
             //区分账号类型
             $game_data = Db::name('game_list')->where('id',$land_data['typec'])->find();
             if($game_data['game_type'] == '1'){
                 $openid = getSubstr($land_data['json'],'openid=','&');
                 $openkey = getSubstr($land_data['json'],'openkey=','&');
             }else{
                 $openid = getSubstr($land_data['json'],'midas_txcz_openid=','; midas_txcz_openkey=');
                 $openkey = getSubstr($land_data['json'],'; midas_txcz_openkey=','; midas_txcz_sessionid');
             }
             
             //先取未支付订单
             $where['land_id'] = $value[$i]['id'];
             $where['state'] = 1;
             $order_list = Db::name('orders')->where($where)->limit(10000)->select();
             if(count($order_list)>0){
                  $post = 'pf=mds_storeopen_qb-__mds_wxservice_menu_gmshop_v1_0_0.common4_v1-android&pfkey=pfkey&from_h5=1&openid='.$openid.'&openkey='.$openkey.'&session_id=openid&session_type=kp_accesstoken&SerialNo=&PageNum=1&PageSize=100&CmdCode=query2&SubCmdCode=default&SystemType=portal';
                   $tx_data = curl('https://api.unipay.qq.com/v1/r/1450000186/trade_record_query',$post);
                   $tx = json_decode($tx_data);
                   if($tx->ret=='0'){
                   //开始搜订单是否存在
                   for ($s = 0; $s < count($order_list); $s++) {
                       if($order_list[$s]['syorder']!=''){
                           if(strpos($tx_data,$order_list[$s]['syorder'])){
                              echo "[".$s."]".$order_list[$s]['syorder'].'-已支付<br>'."\n";
                              $paytime = time();
			      	     	$update = Db::name('orders')->where('id',$order_list[$s]['id'])->update(array('pay_time'=>$paytime,'state'=>2,'http'=>'还未请求'      ));
                           }else{
                              echo "[".$s."]".$order_list[$s]['syorder'].'-未支付<br>'."\n";
                              //置订单已支付并且进行回调
             
                           }
                       
                       }
             }
             echo "正在处理账号数据：". $land_data['username']."\n";
             echo "------------------------------------------------------------------------------------------------------\n";
             sleep(1.5);
             } 
             }
            }
        }
       
        
        $this->sendLater_tx();
       
	} 
    public function add_order($take_id, $content)
    {
        error_reporting(E_ALL^E_NOTICE^E_WARNING);
        $web_config = Db::name('config')->where('id',1)->find();
        $value = json_decode($content,true);
        
        for ($i = 0; $i < count($value); $i++) {
           
            if($value[$i]['syorder']!=''){
            
             $url[] = $web_config['pay_url']."/pay/orders?num=".$value[$i]['num']."&syorder=".$value[$i]['syorder'];
             echo "[".$i."]正在处理订单数据：".$web_config['pay_url']."/pay/orders?num=".$value[$i]['num']."&syorder=".$value[$i]['syorder']."\n";
            }
        }
        echo date('H:i:s',time())."开始处理\n";
        echo var_dump(async_get_url($url));
        echo date('H:i:s',time())."处理完成\n";
        echo "------------------------------------------------------------------------------------------------------\n";
       
	} 
	
	public function add_reback($take_id, $content)
    {
        error_reporting(E_ALL^E_NOTICE^E_WARNING);
        $web_config = Db::name('config')->where('id',1)->find();
        $value = json_decode($content,true);
        for ($i = 0; $i < count($value); $i++) {
            if($value[$i]['num']!=''){
             $url[] = $web_config['url']."/sever/sever/reback_renwu.html?order_id=".$value[$i]['num'];
             echo "[".$i."]取到未回调数据：".$web_config['url']."/sever/sever/reback_renwu.html?order_id=".$value[$i]['num']."\n";
            }
        }
        echo date('H:i:s',time())."开始处理\n";
        async_get_url($url);
        echo date('H:i:s',time())."处理完成\n";
        echo "------------------------------------------------------------------------------------------------------\n";
	} 


    public function sendLater()
    {
       
        $game_id = Db::name('game_list')->field('id')->where('game_type','in',[1,4])->select();
        for ($i = 0; $i < count($game_id); $i++) {
             $game_id_for[] = $game_id[$i]['id'] ;
        }
        
        //先取出非TX类游戏
        $where['payc'] = ['not in',$game_id_for];
        $where['state'] = 1;
        $order_list =  Db::name('orders')->where($where)->limit(10000)->select();
        $order_count = count($order_list);
        for ($i = 0; $i < count($order_list); $i++) {
            $order[$i]['num'] = $order_list[$i]['num'];
            $order[$i]['syorder'] = $order_list[$i]['syorder'];
        }
        
        if(isset($order)){
            Timer::add(1, array($this, 'add_order'), array($order_count, json_encode($order)),false);
        }

        
    }
    
    public function sendLater_tx()
    {
       
        $game_id = Db::name('game_list')->field('id')->where('game_type','in',[1,4])->select();
        for ($i = 0; $i < count($game_id); $i++) {
             $game_id_for[] = $game_id[$i]['id'] ;
        }
        
        //先取出账号
        $where['typec'] = ['in',$game_id_for];
        $where['land_lx'] = 1;
        $land_list =  Db::name('land')->where($where)->limit(10000)->select();
        $land_count = count($land_list);
        for ($i = 0; $i < count($land_list); $i++) {
            $land[$i]['id'] = $land_list[$i]['id'];
        }
        
        if(isset($land)){
            Timer::add(1, array($this, 'add_order_tx'), array($land_count, json_encode($land)),false);
        }

        
    }
    
    public function send_reback()
    {
       
        
        //先取出数据库有多少数据当前
        $where['state'] = 2;
        $where['api_state'] = 1;
        $order_list =  Db::name('orders')->where($where)->limit(10000)->select();
        $order_count = count($order_list);
        for ($i = 0; $i < count($order_list); $i++) {
            $reback[$i]['num'] = $order_list[$i]['num'];
        }
        
        if(isset($reback)){
            Timer::add(1, array($this, 'add_reback'), array($order_count, json_encode($reback)),false);
        }
        
        
    }
    
    }
    

   
    define('MAX_REQUEST', 1000);
    $task = new Worker();
    $task->onWorkerStart = function ($task) {
        //订单异步回调
        Timer::add(10, function(){
           $mail = new Words();
           $mail->send_reback();
        },array());
        //TX类
        Timer::add(1, function(){
           $mail = new Words();
           $mail->sendLater_tx();
        },array(),false);
        //其他通道订单监控
        Timer::add(60, function(){
           $mail = new Words();
           $mail->sendLater();
        },array());

    };
        
    // Run all workers
    Worker::runAll();     
    
    
    
  

