<?php
namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Http\Models\DiepollingpaymentModel;
use App\Http\Models\LivepollingpaymentModel;
use Illuminate\Support\Facades\Redis;
class Order_Redis extends Controller {

    /****订单取码****/
    public function dom($T,$type,$code_type,$data = array()){
        try {
            if($T == 2){
                $code_type = $code_type == 1 ? 'die' : 'live';
                $list = Redis::smembers('random_'.$type.'_'.$code_type);
                if(!empty($list)){
                    foreach ($list as $v) {
                       $info = json_decode($v,true); 
                       //判断是否有指定区域的码
                       if($info['region_id'] == $data['region_id'] && $info['city_id'] == $data['city_id']){
                            if($info['merchant']['price'] >= $data['amount']){
                              if(isset($data['member_id'])){
                                 if(is_array($data['member_id'])){
                                    if(in_array($info['member_id'],$data['member_id'])){
                                        if(isset($data['polling_id']) || isset($data['passageway_id'])){
                                            if(isset($data['polling_id'])){
                                                if($info['polling_id'] == $data['polling_id']){
                                                    if(isset($data['passageway_id'])){
                                                        if(is_array($data['passageway_id'])){
                                                            if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                               $listinfo[] = $info;
                                                            }
                                                        }else{
                                                            if($info['passageway_id'] == $data['passageway_id']){
                                                                $listinfo[] = $info;
                                                            }
                                                        }
                                                    }else{
                                                        $listinfo[] = $info;
                                                    }
                                                }
                                            }else{
                                              if(is_array($data['passageway_id'])){
                                                  if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                     $listinfo[] = $info;
                                                  }
                                              }else{
                                                  if($info['passageway_id'] == $data['passageway_id']){
                                                      $listinfo[] = $info;
                                                  }
                                              }
                                            }
                                        }else{
                                            $listinfo[] = $info;
                                        }
                                    }
                                 }else{
                                    if($info['member_id'] == $data['member_id']){
                                      if(isset($data['polling_id']) || isset($data['passageway_id'])){
                                              if(isset($data['polling_id'])){
                                                  if($info['polling_id'] == $data['polling_id']){
                                                      if(isset($data['passageway_id'])){
                                                          if(is_array($data['passageway_id'])){
                                                              if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                                 $listinfo[] = $info;
                                                              }
                                                          }else{
                                                              if($info['passageway_id'] == $data['passageway_id']){
                                                                  $listinfo[] = $info;
                                                              }
                                                          }
                                                      }else{
                                                          $listinfo[] = $info;
                                                      }
                                                  }
                                              }else{
                                                if(is_array($data['passageway_id'])){
                                                    if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                       $listinfo[] = $info;
                                                    }
                                                }else{
                                                    if($info['passageway_id'] == $data['passageway_id']){
                                                        $listinfo[] = $info;
                                                    }
                                                }
                                              }
                                      }else{
                                          $listinfo[] = $info;
                                      }
                                    }
                                 }
                              }else{
                                if($info['merchant']['open'] == 1){
                                    if(isset($data['passageway_id'])){
                                        if(is_array($data['passageway_id'])){
                                            if(in_array($info['passageway_id'],$data['passageway_id'])){
                                               $listinfo[] = $info;
                                            }
                                        }else{
                                            if($info['passageway_id'] == $data['passageway_id']){
                                                $listinfo[] = $info;
                                            }
                                        }
                                    }else{
                                        $listinfo[] = $info;
                                    }
                                }
                              }
                            }
                       }
                    }
                }
                if(isset($listinfo)){
                    $count = count($listinfo);
                    $num = $count == 1 ? 0 : rand(0,$count-1);
                    //删除当前码
                    $deltype = $code_type == 'die' ? 1 : 2;
                    $this->delete($T,$type,$deltype,array('key'=>$listinfo[$num]['key']));
                    $payment = $code_type == 'die' ?  new DiepollingpaymentModel : new LivepollingpaymentModel;
                    $info  = $payment->where('Id',$listinfo[$num]['Id'])->with('user','payment', 'merchant')->first();
                    if($info->merchant->price > 1 && $info->merchant->frozen_amount >= 0 && $info->user->status == 1 && $info->user->loop == 1 && $info->gateway_state ==1){
                        Redis::sadd('random_'.$type.'_'.$code_type,json_encode($info));
                        //查询当前码是否出码过
                        $result = $this->query_record($type,$deltype,$info->key);
                        if($result){
                            Redis::lset($type.'_'.$code_type.'_record',$result['id'],json_encode(array('number'=>$result['number']+1,'form'=>$info)));
                        }else{
                        	Redis::rpush($type.'_'.$code_type.'_record',json_encode(array('number'=>1,'form'=>$info)));
                        }
                    }
                    return $listinfo[$num];
                }else{
                    return false;
                }
            }else{
            	//$code_type = 2;
                $code_type = $code_type == 1 ? 'die' : 'live';
                //echo 'sort_'.$type.'_'.$code_type;exit;
                $list = Redis::lrange('sort_'.$type.'_'.$code_type,0,-1);
                //print_r($list);exit;
                if(!empty($list)){
                    foreach ($list as $v) {
                       $info = json_decode($v,true); 
                       //判断是否有指定区域的码
                       if($info['region_id'] == $data['region_id'] && $info['city_id'] == $data['city_id']){
                            if((float)$info['merchant']['price'] >= $data['amount']){
                              if(isset($data['member_id'])){
                                if(is_array($data['member_id'])){
                                    if(in_array($info['member_id'],$data['member_id'])){
                                        if(isset($data['polling_id']) || isset($data['passageway_id'])){
                                              if(isset($data['polling_id'])){
                                                  if($info['polling_id'] == $data['polling_id']){
                                                      if(isset($data['passageway_id'])){
                                                          if(is_array($data['passageway_id'])){
                                                              if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                                 $listinfo = $info;
                                                                 break;
                                                              }
                                                          }else{
                                                              if($info['passageway_id'] == $data['passageway_id']){
                                                                  $listinfo = $info;
                                                                  break;
                                                              }
                                                          }
                                                      }else{
                                                          $listinfo = $info;
                                                          break;
                                                      }
                                                  }
                                              }else{
                                                  if(is_array($data['passageway_id'])){
                                                      if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                         $listinfo = $info;
                                                         break;
                                                      }
                                                  }else{
                                                      if($info['passageway_id'] == $data['passageway_id']){
                                                          $listinfo = $info;
                                                          break;
                                                      }
                                                  }
                                              }
                                        }else{
                                            $listinfo = $info;
                                            break;
                                        }
                                    }
                                }else{
                                  if($info['member_id'] == $data['member_id']){
                                      if(isset($data['polling_id']) || isset($data['passageway_id'])){
                                              if(isset($data['polling_id'])){
                                                  if($info['polling_id'] == $data['polling_id']){
                                                      if(isset($data['passageway_id'])){
                                                          if(is_array($data['passageway_id'])){
                                                              if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                                 $listinfo = $info;
                                                                 break;
                                                              }
                                                          }else{
                                                              if($info['passageway_id'] == $data['passageway_id']){
                                                                  $listinfo = $info;
                                                                  break;
                                                              }
                                                          }
                                                      }else{
                                                          $listinfo = $info;
                                                          break;
                                                      }
                                                  }
                                              }else{
                                                  if(is_array($data['passageway_id'])){
                                                      if(in_array($info['passageway_id'],$data['passageway_id'])){
                                                         $listinfo = $info;
                                                         break;
                                                      }
                                                  }else{
                                                      if($info['passageway_id'] == $data['passageway_id']){
                                                          $listinfo = $info;
                                                          break;
                                                      }
                                                  }
                                              }
                                      }else{
                                          $listinfo = $info;
                                          break;
                                      }
                                  }
                                }
                              }else{
                                if($info['merchant']['open'] == 1){
                                    if(isset($data['passageway_id'])){
                                        if(is_array($data['passageway_id'])){
                                            if(in_array($info['passageway_id'],$data['passageway_id'])){
                                               $listinfo = $info;
                                               break;
                                            }
                                        }else{
                                            if($info['passageway_id'] == $data['passageway_id']){
                                                $listinfo = $info;
                                                break;
                                            }
                                        }
                                    }else{
                                        $listinfo = $info;
                                        break;
                                    }
                                }
                              }
                            }
                       }
                    }
                }
                if(isset($listinfo)){
                    //删除此码再次添加
                    $deltype = $code_type == 'die' ? 1 : 2;
                    $this->delete($T,$type,$deltype,array('key'=>$listinfo['key']));
                    $payment = $code_type == 'die' ?  new DiepollingpaymentModel : new LivepollingpaymentModel;
                    $info  = $payment->where('Id',$listinfo['Id'])->with('user','payment', 'merchant')->first();
                    if($info->merchant->price > 1 && $info->merchant->frozen_amount >= 0 && $info->user->status == 1 && $info->user->loop == 1 && $info->gateway_state ==1){
                        Redis::rpush('sort_'.$type.'_'.$code_type,json_encode($info));
                        //查询当前码是否出码过
                        $result = $this->query_record($type,$deltype,$info->key);
                        if($result){
                            Redis::lset($type.'_'.$code_type.'_record',$result['id'],json_encode(array('number'=>$result['number']+1,'form'=>$info)));
                        }else{
                        	Redis::rpush($type.'_'.$code_type.'_record',json_encode(array('number'=>1,'form'=>$info)));
                        }
                    }
                    return $listinfo;
                }else{
                    return false;
                } 
            }
        } catch (\Exception $e) {
            return false;
        }
    }

    /****删除账户下所有条件码****/
    public function delete($T,$type,$code_type,$data = array()){
        try {
            if($T == 2){
                $code_type = $code_type == 1 ? 'die' : 'live';
                $list = Redis::smembers('random_'.$type.'_'.$code_type);
                if(!empty($list)){
                    foreach ($list as $v) {
                       $info = json_decode($v,true); 
                       //判断是否有指定条件的码
                       if(isset($data['member_id'])){
                           if($info['member_id'] == $data['member_id']){
                                $status = Redis::srem('random_'.$type.'_'.$code_type,json_encode($info));
                                if(!$status){
                                	Redis::srem('random_'.$type.'_'.$code_type,json_encode($info));
                                }
                           } 
                       }else if(isset($data['polling_id'])){
                           if($info['polling_id'] == $data['polling_id']){
                                $status = Redis::srem('random_'.$type.'_'.$code_type,json_encode($info));
                                if(!$status){
                                	Redis::srem('random_'.$type.'_'.$code_type,json_encode($info));
                                }
                           } 
                       }else if(isset($data['key'])){
                          if($info['key'] == $data['key']){
                            	$status = Redis::srem('random_'.$type.'_'.$code_type,json_encode($info));
                            	if(!$status){
                                	Redis::srem('random_'.$type.'_'.$code_type,json_encode($info));
                                }
                           }
                       }
                    }
                }
                return true;
            }else{
                $code_type = $code_type == 1 ? 'die' : 'live';
                $list = Redis::lrange('sort_'.$type.'_'.$code_type,0,-1);
                if(!empty($list)){
                    foreach ($list as $v) {
                       $info = json_decode($v,true); 
                       //判断是否有指定条件的码
                        if(isset($data['member_id'])){
                           if($info['member_id'] == $data['member_id']){
                                if(isset($data['passageway_id'])){
                                    if($info['passageway_id'] == $data['passageway_id']){
                                        $status = Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                                        if(!$status){
                                        	Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                                        }
                                    }
                                }else{
                                    $status = Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                                    if(!$status){
                                        Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                                    }
                                }
                           } 
                        }else if(isset($data['polling_id'])){
                           if($info['polling_id'] == $data['polling_id']){
                               $status = Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                                if(!$status){
                                   Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                                }
                           }
                        }else if(isset($data['key'])){
                           if($info['key'] == $data['key']){
                               $status = Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                               if(!$status){
                                   Redis::lrem('sort_'.$type.'_'.$code_type,1,json_encode($info));
                                }
                           }
                        }
                    }
                }
                return true;
            }
        } catch (\Exception $e) {
           return false; 
        }
    }
    
    /****查找当前成功出码二维码****/
	public function query_record($type,$code_type,$key){
		$code_type = $code_type == 1 ? 'die' : 'live';
		try{
			$list = Redis::lrange($type.'_'.$code_type.'_record',0,-1);
	        if(!empty($list)){
	        	foreach ($list as $k => $v){
	        	   $info = json_decode($v,true);
	        	   if($info['form']['key'] == $key){
		        	    $result = $info;
		        	    $result['id'] = $k;
		        	    break;
		           }
	        	}
	        	if(isset($result)){
	        		return $result;
	        	}else{
	        		return false;
	        	}
	        }else{
	        	return false;
	        }
		} catch (\Exception $e) {
			return false;
		}
	}
	
	/****删除成功出码二维码****/
	public function query_delete($type,$code_type,$key){
		$code_type = $code_type == 1 ? 'die' : 'live';
		try{
			$list = Redis::lrange($type.'_'.$code_type.'_record',0,-1);
	        if(!empty($list)){
	        	foreach ($list as $v){
	        	   $info = json_decode($v,true);
	        	   if($info['form']['key'] == $key){
		        	    $status = Redis::lrem($type.'_'.$code_type.'_record',1,json_encode($info));
		        	    if(!$status){
		        	    	Redis::lrem($type.'_'.$code_type.'_record',1,json_encode($info));
		        	    }
		           }
	        	}
	        	return true;
	        }else{
	        	return false;
	        }
		} catch (\Exception $e) {
			return false;
		}
	}
    /****移动队列*****/
    public function move($a){
        if($a == 2){
            /****从随机队列移到顺序队列*****/
            $type = array('alipay','aliwap','wechat','unionpay');
            foreach ($type as $v) {
                 $list_a = Redis::smembers('random_'.$v.'_die');
                 $list_b = Redis::smembers('random_'.$v.'_live');
                 if(!empty($list_a)){
                    foreach ($list_a as $n) {
                       Redis::rpush('sort_'.$v.'_die',$n);
                       Redis::srem('random_'.$v.'_die',$n);
                    }
                 }
                 if(!empty($list_b)){
                    foreach ($list_b as $n) {
                       Redis::rpush('sort_'.$v.'_libe',$n);
                       Redis::srem('random_'.$v.'_live',$n);
                    }
                 }
            }
        }else{
            /****从顺序队列移到随机队列*****/
            $type = array('alipay','aliwap','wechat','unionpay');
            foreach ($type as $v) {
                 $list_a = Redis::lrange('sort_'.$v.'_die',0,-1);
                 $list_b = Redis::lrange('sort_'.$v.'_live',0,-1);
                 if(!empty($list_a)){
                    foreach ($list_a as $n) {
                       Redis::sadd('random_'.$v.'_die',$n);
                       Redis::lrem('sort_'.$v.'_die',1,$n);
                    }
                 }
                 if(!empty($list_b)){
                    foreach ($list_b as $n) {
                       Redis::sadd('random_'.$v.'_live',$n);
                       Redis::lrem('sort_'.$v.'_live',1,$n);
                    }
                 }
            }
        }
    }
}