cqpCow 1 year ago
parent
commit
f0fdfa3ef5
3 changed files with 123 additions and 23 deletions
  1. 4 2
      app/Model/Customer.php
  2. 10 0
      app/Model/CustomerInfo.php
  3. 109 21
      app/Service/CustomerService.php

+ 4 - 2
app/Model/Customer.php

@@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Model;
 
 
 class Customer extends Model
 class Customer extends Model
 {
 {
-    protected $fillable = ['userData'];
+    protected $fillable = ['userData','search'];
     protected $table = "customer"; //指定表
     protected $table = "customer"; //指定表
     const CREATED_AT = 'crt_time';
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     const UPDATED_AT = 'upd_time';
@@ -21,19 +21,21 @@ class Customer extends Model
 
 
     public static $user = [];
     public static $user = [];
     public static $is_search = false;
     public static $is_search = false;
+    public static $is_pond_state = true;
 
 
     public function __construct(array $attributes = [])
     public function __construct(array $attributes = [])
     {
     {
         if(! empty($attributes['userData'])) {
         if(! empty($attributes['userData'])) {
             self::$user = $attributes['userData'];
             self::$user = $attributes['userData'];
             self::$is_search = true;
             self::$is_search = true;
+            if(! empty($attributes['search']['pond_state'])) self::$is_pond_state = false;
         }
         }
         parent::__construct($attributes);
         parent::__construct($attributes);
     }
     }
 
 
     protected static function boot(){
     protected static function boot(){
         parent::boot();
         parent::boot();
-        if(self::$is_search){
+        if(self::$is_search && self::$is_pond_state){
             static::addGlobalScope(new DepartmentScope(self::$user));
             static::addGlobalScope(new DepartmentScope(self::$user));
         }
         }
     }
     }

+ 10 - 0
app/Model/CustomerInfo.php

@@ -16,6 +16,8 @@ class CustomerInfo extends Model
     const type_four = 4; // 联系人
     const type_four = 4; // 联系人
     const type_five = 5; // 图片
     const type_five = 5; // 图片
     const type_six = 6; // 文件
     const type_six = 6; // 文件
+    const type_seven = 7; // 可见部门
+    const type_eight = 8; // 可见人
     public static $type = [
     public static $type = [
         self::type_one,
         self::type_one,
         self::type_two,
         self::type_two,
@@ -23,5 +25,13 @@ class CustomerInfo extends Model
         self::type_four,
         self::type_four,
         self::type_five,
         self::type_five,
         self::type_six,
         self::type_six,
+        self::type_seven,
+        self::type_eight,
+    ];
+    public static $man = [
+        self::type_two,
+        self::type_three,
+        self::type_four,
+        self::type_eight,
     ];
     ];
 }
 }

+ 109 - 21
app/Service/CustomerService.php

@@ -75,7 +75,7 @@ class CustomerService extends Service
                 foreach ($data['employee_one'] as $value){
                 foreach ($data['employee_one'] as $value){
                     $insert[] = [
                     $insert[] = [
                         'customer_id' => $model->id,
                         'customer_id' => $model->id,
-                        'employee_id' => $value,
+                        'data_id' => $value,
                         'type' => CustomerInfo::type_two,
                         'type' => CustomerInfo::type_two,
                         'crt_time' => $time,
                         'crt_time' => $time,
                     ];
                     ];
@@ -88,7 +88,7 @@ class CustomerService extends Service
                 foreach ($data['employee_two'] as $value){
                 foreach ($data['employee_two'] as $value){
                     $insert[] = [
                     $insert[] = [
                         'customer_id' => $model->id,
                         'customer_id' => $model->id,
-                        'employee_id' => $value,
+                        'data_id' => $value,
                         'type' => CustomerInfo::type_three,
                         'type' => CustomerInfo::type_three,
                         'crt_time' => $time,
                         'crt_time' => $time,
                     ];
                     ];
@@ -101,7 +101,7 @@ class CustomerService extends Service
                 foreach ($data['employee_three'] as $value){
                 foreach ($data['employee_three'] as $value){
                     $insert[] = [
                     $insert[] = [
                         'customer_id' => $model->id,
                         'customer_id' => $model->id,
-                        'employee_id' => $value,
+                        'data_id' => $value,
                         'type' => CustomerInfo::type_four,
                         'type' => CustomerInfo::type_four,
                         'crt_time' => $time,
                         'crt_time' => $time,
                     ];
                     ];
@@ -137,6 +137,32 @@ class CustomerService extends Service
                 CustomerInfo::insert($insert);
                 CustomerInfo::insert($insert);
             }
             }
 
 
+            if(! empty($data['employee'])){
+                $insert = [];
+                foreach ($data['employee'] as $value){
+                    $insert[] = [
+                        'customer_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => CustomerInfo::type_eight,
+                        'crt_time' => $time,
+                    ];
+                }
+                CustomerInfo::insert($insert);
+            }
+
+            if(! empty($data['depart'])){
+                $insert = [];
+                foreach ($data['depart'] as $value){
+                    $insert[] = [
+                        'customer_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => CustomerInfo::type_seven,
+                        'crt_time' => $time,
+                    ];
+                }
+                CustomerInfo::insert($insert);
+            }
+
             DB::commit();
             DB::commit();
         }catch (\Exception $exception){
         }catch (\Exception $exception){
             DB::rollBack();
             DB::rollBack();
@@ -203,7 +229,7 @@ class CustomerService extends Service
                 foreach ($data['employee_one'] as $value){
                 foreach ($data['employee_one'] as $value){
                     $insert[] = [
                     $insert[] = [
                         'customer_id' => $model->id,
                         'customer_id' => $model->id,
-                        'employee_id' => $value,
+                        'data_id' => $value,
                         'type' => CustomerInfo::type_two,
                         'type' => CustomerInfo::type_two,
                         'crt_time' => $time,
                         'crt_time' => $time,
                     ];
                     ];
@@ -216,7 +242,7 @@ class CustomerService extends Service
                 foreach ($data['employee_two'] as $value){
                 foreach ($data['employee_two'] as $value){
                     $insert[] = [
                     $insert[] = [
                         'customer_id' => $model->id,
                         'customer_id' => $model->id,
-                        'employee_id' => $value,
+                        'data_id' => $value,
                         'type' => CustomerInfo::type_three,
                         'type' => CustomerInfo::type_three,
                         'crt_time' => $time,
                         'crt_time' => $time,
                     ];
                     ];
@@ -229,7 +255,7 @@ class CustomerService extends Service
                 foreach ($data['employee_three'] as $value){
                 foreach ($data['employee_three'] as $value){
                     $insert[] = [
                     $insert[] = [
                         'customer_id' => $model->id,
                         'customer_id' => $model->id,
-                        'employee_id' => $value,
+                        'data_id' => $value,
                         'type' => CustomerInfo::type_four,
                         'type' => CustomerInfo::type_four,
                         'crt_time' => $time,
                         'crt_time' => $time,
                     ];
                     ];
@@ -265,6 +291,32 @@ class CustomerService extends Service
                 CustomerInfo::insert($insert);
                 CustomerInfo::insert($insert);
             }
             }
 
 
+            if(! empty($data['employee'])){
+                $insert = [];
+                foreach ($data['employee'] as $value){
+                    $insert[] = [
+                        'customer_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => CustomerInfo::type_eight,
+                        'crt_time' => $time,
+                    ];
+                }
+                CustomerInfo::insert($insert);
+            }
+
+            if(! empty($data['depart'])){
+                $insert = [];
+                foreach ($data['depart'] as $value){
+                    $insert[] = [
+                        'customer_id' => $model->id,
+                        'data_id' => $value,
+                        'type' => CustomerInfo::type_seven,
+                        'crt_time' => $time,
+                    ];
+                }
+                CustomerInfo::insert($insert);
+            }
+
             DB::commit();
             DB::commit();
         }catch (\Exception $exception){
         }catch (\Exception $exception){
             DB::rollBack();
             DB::rollBack();
@@ -325,7 +377,7 @@ class CustomerService extends Service
             $address = $tmp;
             $address = $tmp;
         }
         }
         $customer['address'] = $address;
         $customer['address'] = $address;
-        $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = [];
+        $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['employee'] = $customer['depart'] = [];
         $array = [
         $array = [
             $customer['customer_intention'],
             $customer['customer_intention'],
             $customer['customer_from'],
             $customer['customer_from'],
@@ -356,14 +408,29 @@ class CustomerService extends Service
 
 
         $customer_info = CustomerInfo::where('del_time',0)
         $customer_info = CustomerInfo::where('del_time',0)
             ->where('customer_id',$customer['id'])
             ->where('customer_id',$customer['id'])
-            ->select('id','customer_id','contact_type','contact_info','employee_id','file','type','name')
+            ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
             ->get()->toArray();
             ->get()->toArray();
-        $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_info,'employee_id'))))
+        $emp_id = [];
+        $emp_id[] = $customer['crt_id'];
+        $depart = [];
+        foreach ($customer_info as $value){
+            if(in_array($value['type'], CustomerInfo::$man)){
+                $emp_id[] = $value['data_id'];
+            }else{
+                if($value['type'] == CustomerInfo::type_seven){
+                    $depart[] = $value['data_id'];
+                }
+            }
+        }
+        $emp_map = Employee::whereIn('id',array_unique($emp_id))
             ->pluck('emp_name','id')
             ->pluck('emp_name','id')
             ->toArray();
             ->toArray();
         $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
         $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
             ->pluck('title','id')
             ->pluck('title','id')
             ->toArray();
             ->toArray();
+        $depart_map = Depart::whereIn('id',array_unique($depart))
+            ->pluck('title','id')
+            ->toArray();
         foreach ($customer_info as $value){
         foreach ($customer_info as $value){
             if($value['type'] == CustomerInfo::type_one){
             if($value['type'] == CustomerInfo::type_one){
                 $tmp = [
                 $tmp = [
@@ -402,6 +469,18 @@ class CustomerService extends Service
                     'name' => $value['name'],
                     'name' => $value['name'],
                 ];
                 ];
                 $customer['file'][] = $tmp;
                 $customer['file'][] = $tmp;
+            }elseif ($value['type'] == CustomerInfo::type_seven){
+                $tmp = [
+                    'id' => $value['depart_id'],
+                    'name' => $depart_map[$value['depart_id']],
+                ];
+                $customer['depart'][] = $tmp;
+            }elseif ($value['type'] == CustomerInfo::type_eight){
+                $tmp = [
+                    'id' => $value['employee_id'],
+                    'name' => $emp_map[$value['employee_id']] ?? '',
+                ];
+                $customer['employee'][] = $tmp;
             }
             }
         }
         }
         $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
         $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
@@ -417,22 +496,31 @@ class CustomerService extends Service
      * @return array
      * @return array
      */
      */
     public function customerList($data,$user){
     public function customerList($data,$user){
-        $model = new Customer(['userData' => $user]);
+        $model = new Customer(['userData' => $user,'search'=> $data]);
         $model = $model->where('del_time',0)
         $model = $model->where('del_time',0)
             ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','customer_grade','pond_state')
             ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','customer_grade','pond_state')
             ->orderby('id', 'desc');
             ->orderby('id', 'desc');
 
 
-        //getALL传入后无视设置范围
-        if(empty($data['getAll']) && $user['id'] != Employee::SPECIAL_ADMIN) {
-            // 销售人员/负责人 3协同人 可以看见
-            $user_id = $user['id'];
-            $customer_id = CustomerInfo::where('del_time',0)
-                ->where(function ($query) use($user_id) {
-                    $query->where('employee_id',$user_id);
-                })->select('customer_id')->get()
-                ->toArray();
-            $emp_id = array_unique(array_column($customer_id,'customer_id'));
-            $model->WhereIn('id', $emp_id);
+        if(empty($data['pond_state'])) { // 未进入公海池的
+            //getALL传入后无视设置范围
+            if(empty($data['getAll']) && $user['id'] != Employee::SPECIAL_ADMIN) {
+                $user_id = $user['id'];
+                $depart_id = $user['depart_range'];
+                $type = implode(',',CustomerInfo::$man);
+                $type2 = CustomerInfo::type_eight;
+                $depart_str = implode(',',$depart_id);
+                $str = "(data_id = $user_id AND type IN({$type})) OR (data_id IN({$depart_str}) AND type = {$type2})";
+
+                // 销售人员/负责人 3协同人  可见部门 可见人 可以看见
+                $customer_id = CustomerInfo::where('del_time',0)
+                    ->where(function ($query) use($str) {
+                        $query->whereRaw($str);
+                    })->select('customer_id')->get()->toArray();
+                $emp_id = array_unique(array_column($customer_id,'customer_id'));
+                $model->whereIn('id', $emp_id);
+            }
+        }else{
+            $model->where('pond_state', '>',0);
         }
         }
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
         if(! empty($data['time_type'])) {
         if(! empty($data['time_type'])) {