<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\model;

use think\Model;
use think\facade\Config;
use think\facade\Db;

class BaseModel extends Model{
	
	public function scopeUserauth($query, $where = []){
		$auth = request()->auth();
		$uid = request()->user['uid'];
		$map = [];
		if(!in_array($uid, Config::get('auth.admin_root'))){
			$subMap = [];
			if($auth['data_range'] == 2){
				$subMap[] = ['department_id', 'IN', getDepartmentChild($auth['department_id'])];
			}elseif($auth['data_range'] == 3){
				$subMap[] = ['department_id', 'IN', $auth['department_id']];
			}elseif($auth['data_range'] == 4){
				$map[] = ['uid', '=', $uid];
			}
			if(!empty($subMap)){
				$subsql = Db::name('users')->where($subMap)->field('uid')->buildSql();
				$subsql = str_replace(" AS thinkphp) AS T1 )", "", $subsql);
				$subsql = str_replace("SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER ( ORDER BY rand()) AS ROW_NUMBER FROM (", "", $subsql);
				$map[] = ['uid', 'IN', Db::raw($subsql)];
			}
		}
		$query->where($where)->where($map);
	}
}