cqpCow 1 tahun lalu
induk
melakukan
3812d6f3cd

+ 2 - 2
app/Model/BasicType.php

@@ -2,7 +2,7 @@
 
 namespace App\Model;
 
-use App\Scopes\DepartmentScope;
+use App\Scopes\TopDepartmentScope;
 use Illuminate\Database\Eloquent\Model;
 
 class BasicType extends Model
@@ -55,7 +55,7 @@ class BasicType extends Model
     protected static function boot(){
         parent::boot();
         if(self::$is_search){
-            static::addGlobalScope(new DepartmentScope(self::$user, self::$search));
+            static::addGlobalScope(new TopDepartmentScope(self::$user, self::$search));
         }
     }
 }

+ 2 - 2
app/Model/ProductCategory.php

@@ -2,7 +2,7 @@
 
 namespace App\Model;
 
-use App\Scopes\DepartmentScope;
+use App\Scopes\TopDepartmentScope;
 use Illuminate\Database\Eloquent\Model;
 
 class ProductCategory extends Model
@@ -30,7 +30,7 @@ class ProductCategory extends Model
     protected static function boot(){
         parent::boot();
         if(self::$is_search){
-            static::addGlobalScope(new DepartmentScope(self::$user, self::$search));
+            static::addGlobalScope(new TopDepartmentScope(self::$user, self::$search));
         }
     }
 }

+ 1 - 0
app/Scopes/DepartmentScope.php

@@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Scope;
 
+//判断所属部门和顶级部门
 class DepartmentScope implements Scope
 {
     public $user = [];

+ 32 - 0
app/Scopes/TopDepartmentScope.php

@@ -0,0 +1,32 @@
+<?php
+namespace App\Scopes;
+
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Scope;
+
+//只判断顶级id
+class TopDepartmentScope implements Scope
+{
+    public $user = [];
+    public $search = [];
+
+    public function __construct($user = [], $search = [])
+    {
+        $this->user = $user;
+        $this->search = $search;
+    }
+
+    public function apply(Builder $builder, Model $model)
+    {
+        if(empty($this->search['top_depart_id'])){
+            //默认进来 只显示自己公司下的 自己权限范围下的部门数据
+            $top_depart_id = $this->user['depart_top'][0] ?? [];
+            $top_depart_id = $top_depart_id['depart_id'] ?? 0;
+        }else{
+            //查询给的公司下的 自己权限范围下的部门数据
+            $top_depart_id = $this->search['top_depart_id'];
+        }
+        $builder->where('top_depart_id', $top_depart_id);
+    }
+}

+ 8 - 4
app/Service/EmployeeService.php

@@ -286,14 +286,18 @@ class EmployeeService extends Service
             $bool = Employee::where('del_time',0)
                 ->where('id','<>',$data['id'])
                 ->where(function ($query) use ($mobile, $number){
-                    $query->where('mobile', $mobile)
-                        ->orWhere('number', $number);
+                    $query->where('number', $number);
+                    $query->when(! empty($mobile), function ($query) use ($mobile) {
+                        return $query->orWhere('mobile', $mobile);
+                    });
                 })->exists();
         }else{
             $bool = Employee::where('del_time',0)
                 ->where(function ($query) use ($mobile, $number){
-                    $query->where('mobile', $mobile)
-                        ->orWhere('number', $number);
+                    $query->where('number', $number);
+                    $query->when(! empty($mobile), function ($query) use ($mobile) {
+                        return $query->orWhere('mobile', $mobile);
+                    });
                 })->exists();
         }
         if($bool) return [false,'工号或手机号码已存在!'];