|
@@ -0,0 +1,159 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Model;
|
|
|
+
|
|
|
+use App\Scopes\TopDepartmentScope;
|
|
|
+use App\Service\RangeService;
|
|
|
+use Illuminate\Database\Eloquent\Model;
|
|
|
+
|
|
|
+class UseScopeBaseModel extends Model
|
|
|
+{
|
|
|
+ //可见范围
|
|
|
+ const range_function = '';
|
|
|
+
|
|
|
+ public function __construct(array $attributes = [])
|
|
|
+ {
|
|
|
+ parent::__construct($attributes);
|
|
|
+ }
|
|
|
+
|
|
|
+ //顶级部门过滤
|
|
|
+ public function scopeTopClear($query, $user, $search)
|
|
|
+ {
|
|
|
+ //是否所有部门
|
|
|
+ $is_all_depart = $user['is_all_depart'] ?? 0;
|
|
|
+ //权限范围内的部门
|
|
|
+ $depart_range = $user['depart_range'] ?? [];
|
|
|
+
|
|
|
+ //顶级部门
|
|
|
+ $search_depart_id = $search['top_depart_id'] ?? 0;
|
|
|
+ if(empty($search_depart_id)){
|
|
|
+ //默认进来 自身顶级公司
|
|
|
+ $top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
+ $top_depart_id = $top_depart_id['depart_id'] ?? 0;
|
|
|
+ }else{
|
|
|
+ //查询 顶级公司
|
|
|
+ $top_depart_id = $search_depart_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($is_all_depart){
|
|
|
+ //所有部门
|
|
|
+ if(empty($search_depart_id)){
|
|
|
+ //全部
|
|
|
+ $query->whereIn('top_depart_id', $depart_range);
|
|
|
+ }else{
|
|
|
+ //查看某个分社
|
|
|
+ $query->where('top_depart_id', $top_depart_id);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ //某个分社全部
|
|
|
+ $query->where('top_depart_id', $top_depart_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query;
|
|
|
+ }
|
|
|
+
|
|
|
+ //部门和顶级部门(公司)过滤
|
|
|
+ public function scopeClear($query, $user, $search)
|
|
|
+ {
|
|
|
+ //是否所有部门
|
|
|
+ $is_all_depart = $user['is_all_depart'] ?? 0;
|
|
|
+ //权限范围内的部门
|
|
|
+ $depart_range = $user['depart_range'] ?? [];
|
|
|
+ //我可见的
|
|
|
+ $is_see = $search['is_see'] ?? 0;
|
|
|
+
|
|
|
+ //可见范围方法
|
|
|
+ $model = $query->getModel(); // 获取模型的实例
|
|
|
+ $range_function = $model::range_function ?? ""; // 访问静态属性
|
|
|
+ $is_function_range = $this->hasMethod(new RangeService(),$range_function);
|
|
|
+
|
|
|
+ //顶级部门
|
|
|
+ $search_depart_id = $search['top_depart_id'] ?? 0; //顶级公司
|
|
|
+ if(empty($search_depart_id)){
|
|
|
+ //默认进来 自身顶级公司
|
|
|
+ $top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
+ $top_depart_id = $top_depart_id['depart_id'] ?? 0;
|
|
|
+ }else{
|
|
|
+ //查询 顶级公司
|
|
|
+ $top_depart_id = $search_depart_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ $id = [];
|
|
|
+ //可见范围 以及单据里面填写人员
|
|
|
+ if($is_function_range) $id = RangeService::$range_function($user,$search);
|
|
|
+
|
|
|
+ if($is_all_depart){
|
|
|
+ //所有部门
|
|
|
+ if(empty($search_depart_id)){
|
|
|
+ if(! $is_see){
|
|
|
+ //全部
|
|
|
+ $query->whereIn('depart_id', $depart_range);
|
|
|
+ }else{
|
|
|
+ //可见
|
|
|
+ $query->whereIn('id', $id);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ if(! $is_see){
|
|
|
+ //查看某个分社
|
|
|
+ $query->where('top_depart_id', $top_depart_id);
|
|
|
+ }else{
|
|
|
+ //查看某个分社可见
|
|
|
+ $query->whereIn('id', $id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ //分社
|
|
|
+ if(! $is_see){
|
|
|
+ //某个分社全部
|
|
|
+ $query->where('top_depart_id', $top_depart_id)
|
|
|
+ ->whereIn('depart_id', $depart_range)
|
|
|
+ ->orWhereIn('id', $id);
|
|
|
+ }else{
|
|
|
+ //某个分社可见
|
|
|
+ $query->whereIn('id', $id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function hasMethod($class, $methodName)
|
|
|
+ {
|
|
|
+ $reflection = new \ReflectionClass($class);
|
|
|
+ return $reflection->hasMethod($methodName);
|
|
|
+ }
|
|
|
+
|
|
|
+ //顶级部门过滤 取别名a
|
|
|
+ public function scopeATopClear($query, $user, $search)
|
|
|
+ {
|
|
|
+ //是否所有部门
|
|
|
+ $is_all_depart = $user['is_all_depart'] ?? 0;
|
|
|
+ //权限范围内的部门
|
|
|
+ $depart_range = $user['depart_range'] ?? [];
|
|
|
+
|
|
|
+ //顶级部门
|
|
|
+ $search_depart_id = $search['top_depart_id'] ?? 0;
|
|
|
+ if(empty($search_depart_id)){
|
|
|
+ //默认进来 自身顶级公司
|
|
|
+ $top_depart_id = $user['depart_top'][0] ?? [];
|
|
|
+ $top_depart_id = $top_depart_id['depart_id'] ?? 0;
|
|
|
+ }else{
|
|
|
+ //查询 顶级公司
|
|
|
+ $top_depart_id = $search_depart_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($is_all_depart){
|
|
|
+ //所有部门
|
|
|
+ if(empty($search_depart_id)){
|
|
|
+ //全部
|
|
|
+ $query->whereIn('a.top_depart_id', $depart_range);
|
|
|
+ }else{
|
|
|
+ //查看某个分社
|
|
|
+ $query->where('a.top_depart_id', $top_depart_id);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ //某个分社全部
|
|
|
+ $query->where('a.top_depart_id', $top_depart_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query;
|
|
|
+ }
|
|
|
+}
|