<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Index extends ApiCommon
{


    //获取用户的函数   (根据条件筛选出来)
    public function getuser(Request $request){
        if ($request->isPost()){
            $data = input();
            $group = isset($data['group'])?$data['group']:'';          //组
            $user  = isset($data['user'])?$data['user']:'';            //用户
            $order  = isset($data['order'])?$data['order']:'';         //用户
             
            $group = explode(",", $group);                             //使用字符串切割
            $user  = explode(",", $user);                              //使用字符串切割

          
            $where1=[];              //后面一个字符的选择
            $where2=[];              //后面多个字符的选择
          

            foreach ($group as $key => $value) {
                // 对其中的通配符和单字符进行匹配
                $pos = strpos($value,"*");   //是否全字匹配
                if($pos===false){
                    
                    $pos_one = strpos($value,"?");
                    if($pos_one===false){
                        continue;
                    }else{

                        $value = str_replace("?","",$value);
                        if($pos_one==0){
                            $where1['GROUP'] = ['like','_'.$value];         //前面一个字符的选择 
                        }else{
                            $where1['GROUP'] = ['like',$value.'_'];         //后面一个字符的选择
                        }

                        unset($group[$key]);
                    }
                }else{
                    
                    $value = str_replace("*","",$value);
                    if($pos==0){
                        $where2['GROUP'] = ['like','%'.$value];         //前面多个字符的选择
                    }else{
                        $where2['GROUP'] = ['like',$value.'%'];         //后面多个字符的选择
                    } 
                    unset($group[$key]);
                }

            }
            
            $where['GROUP'] = ['in',$group];       //精准选择(组的成员)
            $whereuser['LOGIN']=['in',$user];      //精准选择(用户成员)

            $result = Db::name('mt4_users')->where($where)->whereOr($where1)->whereOr($where2)->whereOr($whereuser)->distinct(true)->select();  //筛选函数

            $result1 = Db::name('order_progress')->where(['orig_order'=>$order])->select();  //筛选函数(当前跟踪的订单)
            
           

            //外层筛选出来之前做过的订单
            foreach ($result as $key => $value) {
                
                //自己选择的用户
                foreach ($result1 as $k => $v) {
                    if($value['LOGIN']==$v['dest_login']){
                        unset($result[$key]);
                    }
                }

            }
            $result = array_values($result);          //重新组织数组的键值对
            if($result){
                
                return json(['code'=>'00','user'=>$result,'msg'=>'获得数据成功']);

            }else{

                return json(['code'=>'01','msg'=>'没有对应的数据']);
            }               

        }else{

            return json(['code'=>'01','msg'=>'请使用post方式访问']);

        }


    }


    //点击订单后获取到的信息
    public function getorder(Request $request){

        if ($request->isPost()){

            $data = input(); //获取订单的信息

            $result = Db::name('mt4_trades')->where(['TICKET'=>$data['orderid']])->find();  //从数据库获取到订单信息
            if($result){

                return json(['code'=>'00','data'=>$result,'msg'=>'获得数据成功']);

            }else{

                return json(['code'=>'01','msg'=>'没有找到匹配的订单']);
            }
        }else{

            return json(['code'=>'01','msg'=>'请使用post方式访问']);

        }

    }

    // 点击订单后查询函数(rollback)
    public function rollback_order(Request $request){
        if ($request->isPost()){

            $data = input(); //获取订单的信息

            $result = Db::name('order_progress')->where(['type'=>"insert","orig_order"=>$data['orderid'],'error_code'=>0])->select();                        //insert成功后插入的订单
            $result1 = Db::name('order_progress')->where(['type'=>"rollback","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select();        //rollback失败后的订单
            $result2 = Db::name('order_progress')->where(['type'=>"retry","orig_order"=>$data['orderid']])->where('error_code','eq',0)->select();            //retry成功的订单
            $result = array_merge($result,$result1,$result2);               //对数据进行合并
            if($result){
                
                return json(['code'=>'00','data'=>$result,'msg'=>'获得数据成功']);

            }else{

                return json(['code'=>'01','msg'=>'暂无数据']);
            }
        }else{

            return json(['code'=>'01','msg'=>'请使用post方式访问']);

        }
    }



    // 点击订单后查询函数(retry)
    public function retry_order(Request $request){
        if ($request->isPost()){

            $data = input(); //获取订单的信息

            $result = Db::name('order_progress')->where(['type'=>"insert","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select();               //insert失败的订单
            $result1 = Db::name('order_progress')->where(['type'=>"retry","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select();               //retry失败的订单
            $result2 = Db::name('order_progress')->where(['type'=>"except_rollback","orig_order"=>$data['orderid']])->where('error_code','neq',0)->select();     //except_rollback失败的订单
            $result = array_merge($result,$result1,$result2);               //对数据进行合并
            if($result){

                return json(['code'=>'00','data'=>$result,'msg'=>'获得数据成功']);

            }else{

                return json(['code'=>'01','msg'=>'暂无数据']);
            }
        }else{

            return json(['code'=>'01','msg'=>'请使用post方式访问']);

        }
    }

    


    //确认密码函数(批量操作的时候需要)
    public function  querypassword(Request $request){
        if ($request->isPost()){


            $data = input();                                              //获取传递的信息
            $password = md5($data['password']);                           //获取加密后的密码




            //根据用户来选择数据
            if($data['nickname']=="普通管理员"){
                $result = Db::name('admin')->where(['id'=>1])->find();        //找到用户(获取原始的密码)
            }else{
                $result = Db::name('admin')->where(['id'=>2])->find();        //找到用户(获取原始的密码)
            }


            // 筛选出来的数据进行密码的匹配
            if($password==$result['password']){
                return json(['code'=>'00','msg'=>'请求数据成功']);
            }else{
                return json(['code'=>'01','msg'=>'密码填写有误']); 
            }
        }else{

            return json(['code'=>'01','msg'=>'请使用post方式访问']);

        }
    }



    //更新梯度函数(retry的时候进行查询)

    public function update_percentage(Request $request){
        if ($request->isPost()){
            
            $data = input();                                              //获取传递的信息
            $update_array =[];
            foreach ($data as $key => $value) {
                $result =  Db::name('mt4_users')->where(['LOGIN'=>$value])->find();
                $update_array[$value]= $result['BALANCE'];
            }
            if($update_array){
                return json(['code'=>'00','data'=>$update_array,'msg'=>'请求数据成功']);
            }else{
                return json(['code'=>'01','data'=>$update_array,'msg'=>'请求数据有误']); 
            }
        }else{

            return json(['code'=>'01','msg'=>'请使用post方式访问']);
        }
    }


}
