<?php
namespace app\admin\model;
use think\Model;

// 引入框架内置类
use think\facade\Db;
use think\facade\Event;
use think\facade\Session;
use think\facade\Request;

class Common extends Model{
	
    // 定义时间戳字段名
    protected $createTime = 'createTime';
    protected $updateTime = 'updateTime';

	//列表页状态修改 如:is_effect等
    public static function state_edit(){
		$tdata=Request::param();
		$table=$tdata['dbname'];
		unset($tdata['dbname']);
		Db::name($table)->save($tdata);
        return $table;
    }

	//列表删除信息-单个删除-真删除
    public static function delete_id(){
		$tdata=Request::param();
		$table=$tdata['table'];
		
		//判断cate是否有下级栏目
		if($table=='cate'){
			$info=Db::name($table)->where('pid',$tdata['id'])->limit(1)->select();
			$dmap[] = ['pid','=',$tdata['id']];
			$dmap[] = ['is_delete','=',1];
			$article=Db::name('article')->where($dmap)->limit(1)->select();
			if($info->isEmpty() and $article->isEmpty()){
				Db::name($table)->delete($tdata['id']);
				$return=1;
			}else{
				$return=2;
			}
		}else{
			Db::name($table)->delete($tdata['id']);
			$return=1;
		}
				
        return $return;
    }
	
	//批量假删除
    public static function list_delete(){
		$tdata=Request::param();
		$table=$tdata['table'];

		foreach($tdata['id'] as $k=>$v){
			$data['id']=$v;
			$data['is_delete']=0;
			$info=Db::name($table)->update($data);
		}
        return 1;
    }

	//批量恢复
    public static function list_back(){
		$tdata=Request::param();

		foreach($tdata['id'] as $k=>$v){
			$data['id']=$v;
			$data['is_delete']=1;
			$info=Db::name('article')->update($data);
		}
        return 1;
    }

	//批量删除
    public static function list_delete_true(){
		$tdata=Request::param();
		$table=$tdata['table'];
		
		$info=Db::name($table)->delete($tdata['id']);
        return 1;
    }



	//获取详情
    public static function info($id,$table,$json=''){
		$info=Db::name($table)->json([$json])->find($id);
        return $info;
    }	

	//公用排序
    public static function is_sort(){
		$tdata=Request::param();
		$table=$tdata['dbname'];
		
		if(isset($tdata['order'])){
			$order=$tdata['order'];
		}else{
			$order='';
		}
		
		if(isset($tdata['aid'])){
			$tdata['pid']=get_id_array($tdata['aid'],1);
		}
		
		//置顶
		if($tdata['type']=='t'){
			if($order=='desc'){
				$k='>';
				$p='desc';
			}else{
				$k='<';	
				$p='asc';	
			}

			$map = [];
			$map[] = ['pid','in',$tdata['pid']];
			$map[] = ['sort',$k,$tdata['sort']];
			$sort=Db::name($table)->where([$map])->order('sort '.$p.'')->limit(2)->select();
			if($sort->isEmpty()==false and $sort[0]['sort']!=$tdata['sort']){
				$sdata['id']=$tdata['id'];
				if($order=='desc'){
				$sdata['sort']=$sort[0]['sort']+1;
				}else{
				$sdata['sort']=$sort[0]['sort']-1;
				}
				Db::name($table)->save($sdata);
				$return=1;
			}else{
				$return=0;
			}
		}
		
		if($tdata['type']=='p'){
			if($order=='desc'){
				$k='>';
				$p='asc';
			}else{
				$k='<';	
				$p='desc';	
			}

			$map = [];
			$map[] = ['pid','in',$tdata['pid']];
			$map[] = ['sort',$k,$tdata['sort']];
			$sort=Db::name($table)->where([$map])->order('sort '.$p.'')->limit(2)->select();
			if($sort->isEmpty()==false and $sort[0]['sort']!=$tdata['sort']){
				$data1['id']=$tdata['id'];
				$data1['sort']=$sort[0]['sort'];
				Db::name($table)->save($data1);
				$data2['id']=$sort[0]['id'];
				$data2['sort']=$tdata['sort'];
				Db::name($table)->save($data2);
				$return=1;
			}else{
				$return=0;
			}
		}

		if($tdata['type']=='n'){
			if($order=='desc'){
				$k='<';
				$p='desc';
			}else{
				$k='>';	
				$p='asc';	
			}
			
			$map = [];
			$map[] = ['pid','in',$tdata['pid']];
			$map[] = ['sort',$k,$tdata['sort']];
			$sort=Db::name($table)->where([$map])->order('sort '.$p.'')->limit(2)->select();
			if($sort->isEmpty()==false and $sort[0]['sort']!=$tdata['sort']){
				$data1['id']=$tdata['id'];
				$data1['sort']=$sort[0]['sort'];
				Db::name($table)->save($data1);
				$data2['id']=$sort[0]['id'];
				$data2['sort']=$tdata['sort'];
				Db::name($table)->save($data2);
				$return=1;
			}else{
				$return=0;
			}
		}
		
        return $return;
    }

	//公用排序-input
    public static function sort_all(){
		$tdata=Request::param();
		$table=$tdata['dbname'];
		foreach($tdata['id'] as $k=>$l){
			$data['id']=$l;
			$data['sort']=$tdata['sort'][$k];
			Db::name($table)->save($data);
		}
        return 1;
	}
	
	//获取单个字段值 $id|id $field|获取字段 $table|表名
    public static function tq_value($id,$field,$table){
		$value=Db::name($table)->where('id',$id)->value($field);
		return $value;
	}
	
}