<?php if( !defined( 'ELikj')){ exit( 'Error ELikj'); }
$shiba = false;
$plusfunction = 'memcached';
$hash = 'safetoken/'.$_SESSION['adminid'];
$SESSIONtokenx = $SESSIONtoken = $ELiMem ->g($hash);
if( trim($CANSHU['0']) != $SESSIONtokenx){
    $shiba = true;
}
$SESSIONtoken =  (uuid());
$ELiMem ->s($hash,$SESSIONtoken);
if( $shiba ){
    return echoapptoken([],-1,"安全验证失败",$SESSIONtoken);
}

$kongzhi = isset($CANSHU['1'])?$CANSHU['1']:'get';
$db = db('memcached');
$where  = [];
if($kongzhi == 'get'){

    $page = (int)(isset($_GET['page'])?$_GET['page']:1);
    $limitx = (int)(isset($_GET['limit'])?$_GET['limit']:10);
    if($limitx < 10 ){
        $limitx = 10;
    }else if($limitx > 100 ){
        $limitx = 100;
    }
    $limit = Limit($limitx,$page);
    $chushi = true;

  
    if(isset($_GET['name']) && $_GET['name'] != ""){
        $chushi = false; 
        $where['name LIKE'] = '%'.ELixss($_GET['name']).'%';
    }

 
   
    $data = $db ->where($where)->limit($limit)->order('id desc')->select();
    $total = $db ->where($where)-> total();
    if(!$data){
        $data = [];
    }
    $kuozhan = [ 'count'=> (int)$total ];

    if($page <=1 && $chushi){
        $kuozhan = ['count'=> (int)$total];
       
    }
 
    return echoapptoken($data,0,'',$SESSIONtoken,$kuozhan );

}else if($kongzhi == 'put'){

    $id = (int)(isset($_POST['id'])?$_POST['id']:0);
    $data = $db ->where(['id' => $id])->find();
    if(!$data){
        return echoapptoken([],-1,'id不存在',$SESSIONtoken);
    }
    if($_POST['atime'] < time() && $_POST['atime'] > 0){
        $_POST['atime'] +=time();
    }
    $UPdata = [
        'keval'=>$_POST['keval'],
        'atime'=>$_POST['atime'],
    ];
  
    $fan = $db ->where(['id' => $id])->update($UPdata);
    if($fan){
    
        ELilog('adminlog',$_SESSION['adminid'],3,[ 'yuan'=>$data,'data'=> $UPdata],$plusfunction);
        return echoapptoken($UPdata,1,'修改成功',$SESSIONtoken);
    }else{
        return echoapptoken([],-1,'修改失败',$SESSIONtoken);
    }

}else if($kongzhi == 'add'){
    $fan = $db ->where(['mark' => $_POST['mark'] ])->find();
    if($fan){
        return echoapptoken([],-1,'应用标识重复',$SESSIONtoken);
    }

    $_POST['atime'] = time();
    $_POST['adminid'] = $_SESSION['adminid'];
    $fan = $db ->insert($_POST);
    if($fan){

        ELilog('adminlog',$_SESSION['adminid'],4,$_POST,$plusfunction);
        return echoapptoken([],1,'新增成功',$SESSIONtoken);
    }else{
        return echoapptoken([],-1,'新增失败',$SESSIONtoken);
    }



}else if($kongzhi == 'del'){


    if(isset($_POST['shuaxin']) && $_POST['shuaxin'] == $_POST['shuaxin']){

        if(isset($_POST['name']) && $_POST['name'] != ""){

            $name = ELixss($_POST['name']);
            $fan = $db ->where(['name LIKE' => '%'.$name.'%'])->delete();
            if($fan){
        
                ELilog('adminlog',$_SESSION['adminid'],5,$name,$plusfunction);
                return echoapptoken([],1,'删除成功',$SESSIONtoken);
            }

        }



        return echoapptoken([],1,'',$SESSIONtoken);
        
    }

    $id = (int)(isset($_POST['id'])?$_POST['id']:0);
    $data = $db ->where(['id' => $id])->find();
    if(!$data){
        return echoapptoken([],-1,'id不存在',$SESSIONtoken);
    }
  
    $fan = $db ->where(['id' => $id])->delete();
    if($fan){
        
        ELilog('adminlog',$_SESSION['adminid'],5,$data,$plusfunction);
        return echoapptoken([],1,'删除成功',$SESSIONtoken);
    }else{
        return echoapptoken([],-1,'删除失败',$SESSIONtoken);
    }

}
return echoapptoken([],1,'ok',$SESSIONtoken);