Parcourir la source

修改增加客户的关系

cqp il y a 4 semaines
Parent
commit
6e8db5b127

+ 13 - 5
app/Console/Commands/CustomerFromThreePlatForm.php

@@ -4,6 +4,7 @@ namespace App\Console\Commands;
 
 use App\Model\BasicType;
 use App\Model\Customer;
+use App\Model\CustomerAccessFlat;
 use App\Model\CustomerInfo;
 use App\Model\CustomerRemain;
 use App\Model\DepartWithDHF;
@@ -43,8 +44,6 @@ class CustomerFromThreePlatForm extends Command
      */
     public function handle()
     {
-        echo '任务--------start---------------';
-
         $basic = BasicType::where('del_time',0)
             ->where('type',BasicType::type_10)
             ->select('id','title')
@@ -96,7 +95,18 @@ class CustomerFromThreePlatForm extends Command
                                     }
                                 }
                             }
-                            if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
+                            if(! empty($insert_detail_2)) {
+                                CustomerInfo::insert($insert_detail_2);
+
+                                // 新增
+                                $insertArray = CustomerInfo::whereIn('customer_id', array_values($map))
+                                    ->where('crt_time', $nowStamp)
+                                    ->whereIn('type', [CustomerInfo::type_two])
+                                    ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_one .' as source_type'), 'customer_id','data_id as target_id',DB::raw(CustomerAccessFlat::type_one .' as type'),DB::raw('2 as top_depart_id'))
+                                    ->get()
+                                    ->toArray();
+                                if(! empty($insertArray)) CustomerAccessFlat::insert($insertArray);
+                            }
                         }
 
                         if(! empty($main_detail3)){
@@ -124,8 +134,6 @@ class CustomerFromThreePlatForm extends Command
                     }
                 });
             });
-
-        echo '任务结束--------end---------------';
     }
 
     private function processingData($data, $time,$basic,$dhf_map)

+ 163 - 0
app/Model/UseScopeBaseNewVModel.php

@@ -0,0 +1,163 @@
+<?php
+
+namespace App\Model;
+
+use App\Service\RangeService;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+
+class UseScopeBaseNewVModel extends Model
+{
+    const range_function = '';
+    const is_check_function = '';
+
+    public function scopeClear($query, $user, $search)
+    {
+        $is_all_depart = $user['is_all_depart'] ?? 0;
+        $depart_range = $user['depart_range'] ?? [];
+        $is_see = ! empty($search['is_see']);
+        $is_check = ! empty($search['is_check']);
+        $auth_type = $this->getQx($search, $user);
+
+        $model = $query->getModel();
+        $className = get_class($model);
+
+        // 可见范围方法
+        $range_function = "";
+        if (defined($className . '::range_function')) $range_function = $className::range_function;
+        $function_range_bool = $this->hasMethod(new RangeService(), $range_function);
+
+        // 状态过滤方法
+        $is_check_function = "";
+        if (defined($className . '::is_check_function')) $is_check_function = $className::is_check_function;
+        $is_check_function_bool = $this->hasMethod(new RangeService(), $is_check_function);
+
+        $search_depart_id = $search['top_depart_id'] ?? 0;
+        $my_top_depart_id = $user['depart_top'][0]['depart_id'] ?? 0;
+
+        if(empty($search_depart_id)){
+            $top_depart_id = $my_top_depart_id;
+        }else{
+            $top_depart_id = $is_all_depart ? $search_depart_id : $my_top_depart_id;
+        }
+
+        // $id 现在是 Query Builder 对象(子查询)
+        $id = null;
+        if($function_range_bool) {
+            $id = RangeService::customerRange2($user, $search);
+        }
+
+        $check_search = "";
+        if($is_check_function_bool && $is_check) {
+            $check_search = RangeService::$is_check_function($user, $search);
+        }
+
+        if($is_see){
+            // 直接使用子查询
+            $query->whereIn('id', $id);
+        }elseif($is_all_depart){
+            $this->allDepart($query, $is_check, $auth_type, $user, $depart_range, $search_depart_id, $top_depart_id, $id, $check_search);
+        }else{
+            $this->notAllDepart($query, $is_check, $auth_type, $user, $depart_range, $top_depart_id, $id, $check_search);
+        }
+
+        if(! empty($search['get_my_top_depart_data'])) $query->where('top_depart_id', $my_top_depart_id);
+    }
+
+    private function allDepart(&$query, $is_check, $auth_type, $user, $depart_range, $search_depart_id, $top_depart_id, $id, $check_search)
+    {
+        if(empty($search_depart_id)){
+            if ($is_check){
+                if(! $auth_type){
+                    $query->when(! empty($check_search), function ($query) use ($check_search) {
+                        return $query->whereRaw($check_search);
+                    });
+                }elseif($auth_type == 1){
+                    $query->where('crt_id', $user['id'])
+                        ->when(! empty($check_search), function ($query) use ($check_search) {
+                            return $query->whereRaw($check_search);
+                        })
+                        ->when($id, function ($query) use ($id) { // 这里 $id 是对象
+                            return $query->orWhereIn('id', $id);
+                        });
+                }elseif ($auth_type == 2 || $auth_type == 3){
+                    $query->whereIn('depart_id', $depart_range)
+                        ->when(! empty($check_search), function ($query) use ($check_search) {
+                            return $query->whereRaw($check_search);
+                        });
+                }
+            }else{
+                if($auth_type == 1) {
+                    $query->where('crt_id', $user['id'])
+                        ->when($id, function ($query) use ($id) {
+                            return $query->orWhereIn('id', $id);
+                        });
+                }elseif ($auth_type == 2 || $auth_type == 3){
+                    $query->whereIn('depart_id', $depart_range);
+                }
+            }
+        }else{
+            // 指定公司逻辑
+            $query->where('top_depart_id', $top_depart_id);
+
+            if ($is_check){
+                $query->when(! empty($check_search), function ($query) use ($check_search) {
+                    return $query->whereRaw($check_search);
+                });
+            }
+
+            if($auth_type == 1) {
+                $query->where('crt_id', $user['id'])
+                    ->when($id, function ($query) use ($id) {
+                        return $query->orWhereIn('id', $id);
+                    });
+            }elseif ($auth_type == 2 || $auth_type == 3){
+                $query->whereIn('depart_id', $depart_range)
+                    ->when($id, function ($query) use ($id) {
+                        return $query->orWhereIn('id', $id);
+                    });
+            }
+        }
+    }
+
+    private function notAllDepart(&$query, $is_check, $auth_type, $user, $depart_range, $top_depart_id, $id, $check_search)
+    {
+        $query->where('top_depart_id', $top_depart_id);
+
+        if ($is_check){
+            $query->when(! empty($check_search), function ($query) use ($check_search) {
+                return $query->whereRaw($check_search);
+            });
+        }
+
+        if(! $auth_type || $auth_type == 3){
+            $query->when($id, function ($query) use ($id) {
+                return $query->orWhereIn('id', $id);
+            });
+        }else if($auth_type == 1) {
+            $query->where('crt_id', $user['id'])
+                ->when($id, function ($query) use ($id) {
+                    return $query->orWhereIn('id', $id);
+                });
+        }elseif ($auth_type == 2) {
+            $query->whereIn('depart_id', $depart_range)
+                ->when($id, function ($query) use ($id) {
+                    return $query->orWhereIn('id', $id);
+                });
+        }
+    }
+
+    public function getQx($data, $user)
+    {
+        if (empty($data['menu_id'])) return 0;
+        // 假设 Employee 常量在此可用,若不可用请自行调整
+        if ($user['id'] == 1) return 0; // 这里的1代表超级管理员ID
+        return $user['role_authority'][$data['menu_id']] ?? 0;
+    }
+
+    function hasMethod($class, $methodName)
+    {
+        if (empty($methodName)) return false;
+        return method_exists($class, $methodName);
+    }
+}

+ 54 - 8
app/Service/CustomerService.php

@@ -5,6 +5,7 @@ namespace App\Service;
 use App\Model\BasicType;
 use App\Model\BasicTypeAllUse;
 use App\Model\Customer;
+use App\Model\CustomerAccessFlat;
 use App\Model\CustomerInfo;
 use App\Model\CustomerRepeat;
 use App\Model\Depart;
@@ -79,9 +80,14 @@ class CustomerService extends Service
             $old = CustomerInfo::where('del_time',0)
                 ->where('customer_id',$data['id'])
                 ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])
-                ->select('file')
-                ->get()->toArray();
-            $old = array_column($old,'file');
+                ->pluck('file')
+                ->all();
+
+            $source_id = CustomerInfo::where('del_time',0)
+                ->where('customer_id',$data['id'])
+                ->where('type',CustomerInfo::type_three)
+                ->pluck('id')
+                ->all();
             CustomerInfo::where('del_time',0)
                 ->where('customer_id',$data['id'])
                 ->whereNotIn('type',CustomerInfo::$no_edit)
@@ -183,6 +189,14 @@ class CustomerService extends Service
                 CustomerInfo::insert($insert);
             }
 
+            //维护客户关系表
+            $this->changeCustomerAccessFlat([
+                'customer_id' => $data['id'],
+                'time' => $time,
+                'top_depart_id' => $data['top_depart_id'],
+                'id' => $source_id
+            ]);
+
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();
@@ -269,11 +283,6 @@ class CustomerService extends Service
                     ];
                 }
                 CustomerInfo::insert($insert);
-
-//                Customer::where('id',$model->id)->update([
-//                    'fp_top_depart_id' => $data['fp_top_depart_id'] ?? 0,
-//                    'fp_time' => $time,
-//                ]);
             }
 
             if(! empty($data['employee_two'])){
@@ -333,6 +342,13 @@ class CustomerService extends Service
                 CustomerInfo::insert($insert);
             }
 
+            //维护客户关系表
+            $this->changeCustomerAccessFlat([
+                'customer_id' => $model->id,
+                'time' => $time,
+                'top_depart_id' => $data['top_depart_id']
+            ]);
+
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();
@@ -345,6 +361,32 @@ class CustomerService extends Service
         return [true, ['file' => ['new' => $new]]];
     }
 
+    public function changeCustomerAccessFlat($data = []){
+        $customer_id = $data['customer_id'];
+        $time = $data['time'];
+        $top_depart_id = $data['top_depart_id'];
+        $source_id = $data['id'] ?? [];
+
+        // 新增
+        $insertArray = CustomerInfo::where('customer_id', $customer_id)
+            ->where('crt_time', $time)
+            ->whereIn('type', [CustomerInfo::type_two,CustomerInfo::type_three])
+            ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_one .' as source_type'), 'customer_id','data_id as target_id',DB::raw(CustomerAccessFlat::source_type_one .' as type'))
+            ->get()
+            ->toArray();
+        foreach ($insertArray as $key => $value){
+            $insertArray[$key]['top_depart_id'] = $top_depart_id;
+        }
+        if(! empty($insertArray)) CustomerAccessFlat::insert($insertArray);
+
+        //删除
+        if(! empty($source_id)){
+            CustomerAccessFlat::where('source_type', CustomerAccessFlat::source_type_one)
+                ->whereIn('source_id', $source_id)
+                ->delete();
+        }
+    }
+
     /**
      * 客户删除
      * @param $data
@@ -373,6 +415,10 @@ class CustomerService extends Service
 
             (new RangeService())->RangeDelete($data['id'],SeeRange::type_one);
 
+            CustomerAccessFlat::where('source_type', CustomerAccessFlat::source_type_one)
+                ->where('customer_id', $data['id'])
+                ->delete();
+
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();

+ 128 - 14
app/Service/DeleteService.php

@@ -5,6 +5,7 @@ namespace App\Service;
 use App\Model\Construction;
 use App\Model\ConstructionInfo;
 use App\Model\Customer;
+use App\Model\CustomerAccessFlat;
 use App\Model\CustomerInfo;
 use App\Model\Depart;
 use App\Model\Employee;
@@ -147,6 +148,8 @@ class DeleteService extends Service
         if(! is_array($data['id'])) $data_id = [$data['id']];
         else $data_id = $data['id'];
 
+        $top_depart_id_map = Customer::whereIn('id', $data_id)->pluck('top_depart_id','id')->all();
+
         $time = time();
         if(! empty($data['man'])){
             $insert = [];
@@ -156,21 +159,23 @@ class DeleteService extends Service
                 ->whereIn('customer_id',$data_id)
                 ->where('type',CustomerInfo::type_three)
                 ->where('data_id',$user['id'])
-                ->select('customer_id')->get()->toArray();
-            $xt = array_column($xt,'customer_id');
+                ->pluck('customer_id')->all();
 
-            $customer_map = Customer::whereIn('id', $data_id)
-                ->pluck('title','id')
-                ->toArray();
             $emp_map = Employee::whereIn('id',$data['man'])
                 ->pluck('emp_name','id')
                 ->toArray();
+            //获取删除id
+            $source_id = CustomerInfo::where('del_time',0)
+                ->whereIn('customer_id',$data_id)
+                ->where('type',CustomerInfo::type_two)
+                ->pluck('id')
+                ->all();
             //负责人清除
             CustomerInfo::where('del_time',0)
                 ->whereIn('customer_id',$data_id)
                 ->where('type', CustomerInfo::type_two)
                 ->update(['del_time' => $time]);
-            $send_data = [];
+            $send_data = $added_xt = [];
             foreach ($data['man'] as $value){
                 $emp_tmp = $emp_map[$value] ?? "";
                 if($emp_tmp) $emp_tmp .= "(负责人)";
@@ -182,18 +187,19 @@ class DeleteService extends Service
                         'type' => CustomerInfo::type_two,
                         'crt_time' => $time,
                     ];
-                    if(! in_array($c, $xt)){
-                        //协同人累加
+
+                    if(!in_array($c, $xt) && !in_array($c, $added_xt)){
+                        // 协同人累加
                         $insert[] = [
                             'customer_id' => $c,
                             'data_id' => $user['id'],
                             'type' => CustomerInfo::type_three,
                             'crt_time' => $time,
                         ];
+                        // 记录一下,防止下一个负责人循环时再次进入
+                        $added_xt[] = $c;
                     }
 
-//                    $customer_tmp = $customer_map[$c] ?? "";
-
                     $send_data[] = [
                         'employee_id' => $value,
                         'type' => 2,
@@ -211,7 +217,28 @@ class DeleteService extends Service
                 }
             }
 
-            CustomerInfo::insert($insert);
+            if(! empty($insert)){
+                CustomerInfo::insert($insert);
+
+                // 新增
+                $insertArray = CustomerInfo::whereIn('customer_id', $data_id)
+                    ->where('crt_time', $time)
+                    ->whereIn('type', [CustomerInfo::type_two,CustomerInfo::type_three])
+                    ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_one .' as source_type'), 'customer_id','data_id as target_id',DB::raw(CustomerAccessFlat::source_type_one .' as type'))
+                    ->get()
+                    ->toArray();
+                foreach ($insertArray as $key => $value){
+                    $insertArray[$key]['top_depart_id'] = $top_depart_id_map[$value['customer_id']];
+                }
+
+                if(! empty($insertArray)) CustomerAccessFlat::insert($insertArray);
+            }
+
+            if(! empty($source_id)) {
+                CustomerAccessFlat::where('source_type', CustomerAccessFlat::source_type_one)
+                    ->whereIn('source_id', $source_id)
+                    ->delete();
+            }
 
             if(! is_array($data['id'])){
                 $title = Customer::where('id',$data['id'])->value('title') ?? "";
@@ -237,6 +264,18 @@ class DeleteService extends Service
             (new OaService())->sendWxOaCheckMessage($send_data);
         }
 
+        $see_id = SeeRange::where('del_time',0)
+            ->whereIn('data_id', $data_id)
+            ->where('data_type',SeeRange::type_one)
+            ->where('type',SeeRange::data_three)
+            ->pluck('id')
+            ->all();
+        if(! empty($see_id)) {
+            CustomerAccessFlat::where('source_type', CustomerAccessFlat::source_type_two)
+                ->whereIn('source_id', $see_id)
+                ->delete();
+        }
+
         //客户被指派的门店 先清空
         SeeRange::where('del_time',0)
             ->whereIn('data_id', $data_id)
@@ -265,7 +304,23 @@ class DeleteService extends Service
                     ];
                 }
             }
-            if(! empty($insert)) SeeRange::insert($insert);
+            if(! empty($insert)) {
+                SeeRange::insert($insert);
+
+                // 新增
+                $insertArray2 = SeeRange::whereIn('data_id', $data_id)
+                    ->where('crt_time', $time)
+                    ->where('type', SeeRange::data_three)
+                    ->where('data_type', SeeRange::type_one)
+                    ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_two .' as source_type'), 'data_id as customer_id','param_id as target_id',DB::raw(CustomerAccessFlat::type_three .' as type'))
+                    ->get()
+                    ->toArray();
+                foreach ($insertArray2 as $key => $value){
+                    $insertArray2[$key]['top_depart_id'] = $top_depart_id_map[$value['customer_id']];
+                }
+
+                if(! empty($insertArray2)) CustomerAccessFlat::insert($insertArray2);
+            }
         }
 
         return [true, ''];
@@ -399,6 +454,21 @@ class DeleteService extends Service
         if(! is_array($data['id'])) $data_id = [$data['id']];
         else $data_id = $data['id'];
 
+        $top_depart_id_map = Customer::whereIn('id', $data_id)->pluck('top_depart_id','id')->all();
+
+        //获取删除id
+        $source_id = CustomerInfo::where('del_time',0)
+            ->whereIn('customer_id',$data_id)
+            ->where('type',CustomerInfo::type_two)
+            ->pluck('id')
+            ->all();
+
+        if(! empty($source_id)) {
+            CustomerAccessFlat::where('source_type', CustomerAccessFlat::source_type_one)
+                ->whereIn('source_id', $source_id)
+                ->delete();
+        }
+
         CustomerInfo::where('del_time',0)
             ->whereIn('customer_id', $data_id)
             ->where('type', CustomerInfo::type_two)
@@ -415,7 +485,23 @@ class DeleteService extends Service
                     ];
                 }
             }
-            CustomerInfo::insert($insert);
+
+            if(! empty($insert)){
+                CustomerInfo::insert($insert);
+
+                // 新增
+                $insertArray = CustomerInfo::whereIn('customer_id', $data_id)
+                    ->where('crt_time', $time)
+                    ->whereIn('type', [CustomerInfo::type_two])
+                    ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_one .' as source_type'), 'customer_id','data_id as target_id',DB::raw(CustomerAccessFlat::type_one .' as type'))
+                    ->get()
+                    ->toArray();
+                foreach ($insertArray as $key => $value){
+                    $insertArray[$key]['top_depart_id'] = $top_depart_id_map[$value['customer_id']];
+                }
+
+                if(! empty($insertArray)) CustomerAccessFlat::insert($insertArray);
+            }
 
             if(! is_array($data['id'])){
                 $title = Customer::where('id',$data['id'])->value('title') ?? "";
@@ -439,6 +525,18 @@ class DeleteService extends Service
             }
         }
 
+        $see_id = SeeRange::where('del_time',0)
+            ->whereIn('data_id', $data_id)
+            ->where('data_type',SeeRange::type_one)
+            ->where('type',SeeRange::data_three)
+            ->pluck('id')
+            ->all();
+        if(! empty($see_id)) {
+            CustomerAccessFlat::where('source_type', CustomerAccessFlat::source_type_two)
+                ->whereIn('source_id', $see_id)
+                ->delete();
+        }
+
         //客户被移交的门店 先清空
         SeeRange::where('del_time',0)
             ->whereIn('data_id', $data_id)
@@ -467,7 +565,23 @@ class DeleteService extends Service
                     ];
                 }
             }
-            if(! empty($insert)) SeeRange::insert($insert);
+            if(! empty($insert)) {
+                SeeRange::insert($insert);
+
+                // 新增
+                $insertArray2 = SeeRange::whereIn('data_id', $data_id)
+                    ->where('crt_time', $time)
+                    ->where('type', SeeRange::data_three)
+                    ->where('data_type', SeeRange::type_one)
+                    ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_two .' as source_type'), 'data_id as customer_id','param_id as target_id',DB::raw(CustomerAccessFlat::type_three .' as type'))
+                    ->get()
+                    ->toArray();
+                foreach ($insertArray2 as $key => $value){
+                    $insertArray2[$key]['top_depart_id'] = $top_depart_id_map[$value['customer_id']];
+                }
+
+                if(! empty($insertArray2)) CustomerAccessFlat::insert($insertArray2);
+            }
         }
 
         return [true, ''];

+ 16 - 1
app/Service/ImportService.php

@@ -8,6 +8,7 @@ use App\Import\ImportAll;
 use App\Model\BasicType;
 use App\Model\BasicTypeAllUse;
 use App\Model\Customer;
+use App\Model\CustomerAccessFlat;
 use App\Model\CustomerInfo;
 use App\Model\CustomerPerFormance;
 use App\Model\CustomerRemain;
@@ -446,7 +447,21 @@ class ImportService extends Service
             }unset($insert_detail3);
 
             if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
-            if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
+            if(! empty($insert_detail_2)) {
+                CustomerInfo::insert($insert_detail_2);
+                // 新增
+                $insertArray = CustomerInfo::whereIn('customer_id', $last_insert_id)
+                    ->where('crt_time', $time)
+                    ->whereIn('type', [CustomerInfo::type_two])
+                    ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_one .' as source_type'), 'customer_id','data_id as target_id',DB::raw(CustomerAccessFlat::type_one .' as type'))
+                    ->get()
+                    ->toArray();
+                foreach ($insertArray as $key => $value){
+                    $insertArray[$key]['top_depart_id'] = $head;
+                }
+
+                if(! empty($insertArray)) CustomerAccessFlat::insert($insertArray);
+            }
             if(! empty($insert_detail_3)) CustomerRemain::insert($insert_detail_3);
 
             DB::commit();

+ 32 - 0
app/Service/RangeService.php

@@ -216,6 +216,38 @@ class RangeService extends Service
         return $return_id;
     }
 
+    // 获取客户可见数据的子查询
+    public static function customerRange2($user, $search)
+    {
+        $userId = $user['id'];
+        $departIds = $user['depart_range'] ?? [];
+        $searchTopDepartId = $search['top_depart_id'] ?? 0;
+
+        // 构建对平铺表的查询
+        $query = DB::table('customer_access_flat as caf')
+            ->select('caf.customer_id')
+            ->where(function ($q) use ($userId, $departIds) {
+                // 1. 用户匹配
+                $q->where(function ($q1) use ($userId) {
+                    $q1->where('caf.target_id', $userId)->where('caf.type', 1);
+                });
+                // 2. 部门匹配
+                if (!empty($departIds)) {
+                    $q->orWhere(function ($q2) use ($departIds) {
+                        $q2->whereIn('caf.target_id', $departIds)->whereIn('caf.type', [2, 3]);
+                    });
+                }
+            });
+
+        // 如果指定了顶级公司过滤(针对全权限用户切换公司看数据)
+        if (!empty($searchTopDepartId) && !empty($user['is_all_depart'])) {
+            $query->where('caf.top_depart_id', $searchTopDepartId);
+        }
+
+        // 注意:这里返回的是 Query Builder 而不是 ->get()
+        return $query->distinct();
+    }
+
     //获取施工单可见数据
     public static function constructionRange($user,$search){
         //单据中选择的签订负责协同人