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

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

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

	//lists页面数据
	public static function article(){
		$tdata=Request::param();
		$id=$tdata['id'];
		
		$get_id_array=get_index_cate_id_son($id);//获取所有下级栏目
		$model_id=tq_value($id,'model_id','cate');
		$get_id_array=get_index_id_array_filter($get_id_array,$model_id);//去除属于其他模型的栏目,获得真实pid_array
		
		$table=tq_value($model_id,'table','cate_type');
		$order=tq_value($id,'is_sort','cate');

		$map = [];
		$map[] = ['is_effect','=',1];
		$map[] = ['is_delete','=',1];
		if($tdata['title']!=''){
			$map[] = ['title','like','%'.$tdata['title'].'%'];
		}
		$map[] = ['pid','in',$get_id_array];

		$list =Db::name('article')->alias('a')->join('article_'.$table.' c', 'a.id=c.aid', 'left')->field('a.*,c.*')->where($map)
			  ->order('sort '.$order.',create_time desc')->select()->toArray();

		foreach($list as $k=>$c){
			$list[$k]['create_time']=date("Y-m-d",$c['create_time']);
			if($c['link']!=''){
				$list[$k]['url']=$c['link'];
			}else{
				$list[$k]['url']=info_seo_url($c['pid'],$c['id']);
			}
		}

		return $list;
	}


	//lists页面数据
	public static function article_teshu(){
		$tdata=Request::param();
		$id=$tdata['id'];
		
		$get_id_array=get_index_cate_id_son($id);//获取所有下级栏目
		$model_id=tq_value($id,'model_id','cate');
		$get_id_array=get_index_id_array_filter($get_id_array,$model_id);//去除属于其他模型的栏目,获得真实pid_array
		
		$table=tq_value($model_id,'table','cate_type');
		$order=tq_value($id,'is_sort','cate');

		$map = [];
		$map[] = ['is_effect','=',1];
		$map[] = ['is_delete','=',1];
		
		if($tdata['gangwei']!=''){
			$map[] = ['gangwei','=',$tdata['gangwei']];
		}
		if($tdata['zhineng']!=''){
			$map[] = ['zhineng','=',$tdata['zhineng']];
		}
		if($tdata['gzdd']!=''){
			$map[] = ['gzdd','=',$tdata['gzdd']];
		}

		if($tdata['title']!=''){
			$map[] = ['title','like','%'.$tdata['title'].'%'];
		}

		$map[] = ['pid','in',$get_id_array];

		$list =Db::name('article')->alias('a')->join('article_'.$table.' c', 'a.id=c.aid', 'left')->field('a.*,c.*')->where($map)
			  ->order('sort '.$order.',create_time desc')->select()->toArray();

		foreach($list as $k=>$c){
			$list[$k]['gangwei']=tq_value($c['gangwei'],'name','label');
			$list[$k]['zhineng']=tq_value($c['zhineng'],'name','label');
			$list[$k]['gzdd']=tq_value($c['gzdd'],'name','label');
			$list[$k]['gzzz']=hang($c['gzzz']);
			$list[$k]['zwyq']=hang($c['zwyq']);
		}

		return $list;
	}


	public static function address(){
		$tdata=Request::param();
		

		$map[] = ['pid','in',$get_id_array];

		$list =Db::name('article')->alias('a')->join('article_'.$table.' c', 'a.id=c.aid', 'left')->field('a.*,c.*')->where($map)
			  ->order('sort '.$order.',create_time desc')->select()->toArray();

		foreach($list as $k=>$c){
			$list[$k]['gangwei']=tq_value($c['gangwei'],'name','label');
			$list[$k]['zhineng']=tq_value($c['zhineng'],'name','label');
			$list[$k]['gzdd']=tq_value($c['gzdd'],'name','label');
			$list[$k]['gzzz']=hang($c['gzzz']);
			$list[$k]['zwyq']=hang($c['zwyq']);
		}

		return $list;
	}
	
}