cqp 4 часов назад
Родитель
Сommit
c1b9d38bc9
1 измененных файлов с 93 добавлено и 69 удалено
  1. 93 69
      app/Service/ImportService.php

+ 93 - 69
app/Service/ImportService.php

@@ -23,6 +23,7 @@ use App\Model\RuleSet;
 use App\Model\RuleSetDetails;
 use App\Model\Team;
 use App\Model\TeamDetails;
+use App\Model\WxEmployee;
 use Illuminate\Support\Facades\DB;
 use Maatwebsite\Excel\Facades\Excel;
 use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\F;
@@ -167,10 +168,21 @@ class ImportService extends Service
         $update_data = [];
         $all_detail_insert = []; // 用于聚合所有部门权限
         $update_main_ids = [];
+        $unbindWxEmployeeIds = []; // 用于记录需要微信解绑的员工 ID
 
         // 获取工号在配置中的索引
         $keys = array_column($table_config, 'key');
         $codeIdx = array_search('number', $keys);
+        //手机号索引
+        $mobileIdx = array_search('mobile', $keys);
+
+        //获取旧手机号
+        $dbOldMobiles = [];
+        if (!empty($update_map)) {
+            $dbOldMobiles = Employee::whereIn('id', array_values($update_map))
+                ->pluck('mobile', 'id')
+                ->toArray();
+        }
 
         // 3. 数据分拣与聚合
         foreach ($array as $key => $value) {
@@ -192,6 +204,14 @@ class ImportService extends Service
                 $update_main_ids[$empId] = $empId;
                 $update_data[$empId] = array_merge($main_tmp, ['id' => $empId]);
 
+                if ($mobileIdx !== false) {
+                    $newMobile = trim((string)($value[$mobileIdx] ?? ''));
+                    $oldMobile = trim((string)($dbOldMobiles[$empId] ?? ''));
+                    if ($oldMobile !== $newMobile) {
+                        $unbindWxEmployeeIds[$empId] = $empId; // 记录需要解绑的员工
+                    }
+                }
+
                 // 收集部门明细
                 if (isset($detail_data_map[$key])) {
                     foreach ($detail_data_map[$key] as $d) {
@@ -251,6 +271,10 @@ class ImportService extends Service
                 }
             }
 
+            if (!empty($unbindWxEmployeeIds)) {
+                WxEmployee::whereIn('employee_id', array_values($unbindWxEmployeeIds))->delete();
+            }
+
             // 6. 处理明细表 (先删后插策略)
             // 注意:分行模式下,更新人员的所有旧权限都要先清空
             if (!empty($update_main_ids)) {
@@ -296,7 +320,7 @@ class ImportService extends Service
             ->toArray();
     }
 
-    private function employeeCheck1(&$array, $user, $table_config)
+    private function employeeCheck(&$array, $user, $table_config)
     {
         $keys = array_column($table_config, 'key');
         $codeIdx = array_search('number', $keys);
@@ -308,13 +332,35 @@ class ImportService extends Service
         $depIdx = array_search('depart_code', $keys);
         $employeeTypeIdx = array_search('employee_type', $keys);
 
+        // 获取手机号字段在 Excel 中的列索引
+        $mobileIdx = array_search('mobile', $keys);
+
         $code_map = $this->getEmployeeList($array, $user, $codeIdx);
         $dep_map = $this->getEDataList($array, $user, $depIdx);
 
+        // --- 核心:提取 Excel 中所有不为空的手机号,批量去数据库核对唯一性 ---
+        $excelMobiles = [];
+        if ($mobileIdx !== false) {
+            $excelMobiles = array_unique(array_filter(array_map(function($row) use ($mobileIdx) {
+                return isset($row[$mobileIdx]) ? trim((string)$row[$mobileIdx]) : '';
+            }, $array)));
+        }
+
+        $dbMobileMap = [];
+        if (!empty($excelMobiles)) {
+            // 如果是全系统(跨租户)唯一,就不加 top_depart_id 条件
+            // 如果只需当前公司内唯一,请追加 ->where('top_depart_id', $user['top_depart_id'])
+            $dbMobileMap = Employee::where('del_time', 0)
+                ->whereIn('mobile', $excelMobiles)
+                ->get(['id', 'number', 'mobile'])
+                ->keyBy('mobile')
+                ->toArray();
+        }
+
         $errors = [];
         $update_mapping = [];
         $detail_storage = [];
-        $mainDataConsistency = []; // 用于存放工号对应的主表数据备份
+        $mainDataConsistency = [];
 
         $sex_map = array_flip(Employee::SEX_TYPE);
         $e_map = array_flip(Employee::Education);
@@ -323,7 +369,6 @@ class ImportService extends Service
         $wt_map = array_flip(Employee::WT_Type);
         $employeeT_map = array_flip(Employee::E_State_Type);
 
-        // 获取所有标记为 is_main 的列索引,用于一致性对比
         $mainColIndices = [];
         foreach ($table_config as $index => $conf) {
             if (!empty($conf['is_main'])) $mainColIndices[$index] = $conf['value'];
@@ -334,14 +379,12 @@ class ImportService extends Service
             $valCode = trim($rowValue[$codeIdx] ?? '');
             if ($valCode === '') continue;
 
-            // --- 核心:一致性校验 ---
+            // --- 一致性校验 ---
             if (!isset($mainDataConsistency[$valCode])) {
-                // 第一次遇见该工号,记录其所有主表字段的值
                 foreach ($mainColIndices as $idx => $label) {
                     $mainDataConsistency[$valCode][$idx] = trim($rowValue[$idx] ?? '');
                 }
             } else {
-                // 再次遇见该工号,对比主表字段是否一致
                 foreach ($mainColIndices as $idx => $label) {
                     $currentVal = trim($rowValue[$idx] ?? '');
                     if ($currentVal !== $mainDataConsistency[$valCode][$idx]) {
@@ -355,7 +398,22 @@ class ImportService extends Service
                 $update_mapping[$rowIndex] = $code_map[$valCode];
             }
 
-            // 2. 校验(性别、学历、状态映射转换)
+            // --- 核心:仅与数据库比对的手机号系统唯一性校验 ---
+            if ($mobileIdx !== false) {
+                $valMobile = trim((string)($rowValue[$mobileIdx] ?? ''));
+                if ($valMobile !== '') {
+                    if (isset($dbMobileMap[$valMobile])) {
+                        $existUser = $dbMobileMap[$valMobile];
+                        // 如果占用了这个手机号的人的工号,跟当前填写的工号对不上,说明是侵占别人的
+                        if ($existUser['number'] !== $valCode) {
+//                            $errors[] = "第{$displayLine}行:手机号[{$valMobile}]已被系统内其他人员(工号:[{$existUser['number']}])占用";
+                            $errors[] = "第{$displayLine}行:手机号[{$valMobile}]已被系统内其他人员占用";
+                        }
+                    }
+                }
+            }
+
+            // 2. 校验及映射转换(性别、学历、状态等)
             // 性别
             $sex_text = trim($rowValue[$sexIdx] ?? '');
             if (!empty($sex_text)) {
@@ -389,7 +447,7 @@ class ImportService extends Service
                 $array[$rowIndex][$employeeTypeIdx] = $employeeT_map[$e_text];
             }
 
-            //人员类别
+            // 人员类别
             $man_text = trim($rowValue[$manIdx] ?? '');
             if (!isset($man_map[$man_text])) {
                 $errors[] = "第{$displayLine}行:人员类别[{$man_text}]无效";
@@ -397,7 +455,7 @@ class ImportService extends Service
                 $array[$rowIndex][$manIdx] = $man_map[$man_text];
             }
 
-            //委托类型
+            // 委托类型
             $wt_text = trim($rowValue[$entrustIdx] ?? '');
             if (!isset($wt_map[$wt_text])) {
                 $errors[] = "第{$displayLine}行:委托方式[{$wt_text}]无效";
@@ -420,7 +478,24 @@ class ImportService extends Service
         return [$error_str, $update_mapping, $detail_storage];
     }
 
-    private function employeeCheck(&$array, $user, $table_config)
+    private function getEDataList($array, $user, $index1)
+    {
+        $depNums = [];
+
+        // 去重收集
+        foreach ($array as $row) {
+            if (!empty($row[$index1])) {
+                foreach (explode(',', $row[$index1]) as $v) $depNums[trim($v)] = true;
+            }
+        }
+
+        return Depart::where('del_time', 0)
+            ->where('top_depart_id', $user['top_depart_id'])
+            ->whereIn('code', array_keys($depNums))
+            ->pluck('id', 'code')->toArray();
+    }
+
+    private function employeeCheck1(&$array, $user, $table_config)
     {
         $keys = array_column($table_config, 'key');
         $codeIdx = array_search('number', $keys);
@@ -432,35 +507,13 @@ class ImportService extends Service
         $depIdx = array_search('depart_code', $keys);
         $employeeTypeIdx = array_search('employee_type', $keys);
 
-        // 获取手机号字段在 Excel 中的列索引
-        $mobileIdx = array_search('mobile', $keys);
-
         $code_map = $this->getEmployeeList($array, $user, $codeIdx);
         $dep_map = $this->getEDataList($array, $user, $depIdx);
 
-        // --- 核心:提取 Excel 中所有不为空的手机号,批量去数据库核对唯一性 ---
-        $excelMobiles = [];
-        if ($mobileIdx !== false) {
-            $excelMobiles = array_unique(array_filter(array_map(function($row) use ($mobileIdx) {
-                return isset($row[$mobileIdx]) ? trim((string)$row[$mobileIdx]) : '';
-            }, $array)));
-        }
-
-        $dbMobileMap = [];
-        if (!empty($excelMobiles)) {
-            // 如果是全系统(跨租户)唯一,就不加 top_depart_id 条件
-            // 如果只需当前公司内唯一,请追加 ->where('top_depart_id', $user['top_depart_id'])
-            $dbMobileMap = Employee::where('del_time', 0)
-                ->whereIn('mobile', $excelMobiles)
-                ->get(['id', 'number', 'mobile'])
-                ->keyBy('mobile')
-                ->toArray();
-        }
-
         $errors = [];
         $update_mapping = [];
         $detail_storage = [];
-        $mainDataConsistency = [];
+        $mainDataConsistency = []; // 用于存放工号对应的主表数据备份
 
         $sex_map = array_flip(Employee::SEX_TYPE);
         $e_map = array_flip(Employee::Education);
@@ -469,6 +522,7 @@ class ImportService extends Service
         $wt_map = array_flip(Employee::WT_Type);
         $employeeT_map = array_flip(Employee::E_State_Type);
 
+        // 获取所有标记为 is_main 的列索引,用于一致性对比
         $mainColIndices = [];
         foreach ($table_config as $index => $conf) {
             if (!empty($conf['is_main'])) $mainColIndices[$index] = $conf['value'];
@@ -479,12 +533,14 @@ class ImportService extends Service
             $valCode = trim($rowValue[$codeIdx] ?? '');
             if ($valCode === '') continue;
 
-            // --- 一致性校验 ---
+            // --- 核心:一致性校验 ---
             if (!isset($mainDataConsistency[$valCode])) {
+                // 第一次遇见该工号,记录其所有主表字段的值
                 foreach ($mainColIndices as $idx => $label) {
                     $mainDataConsistency[$valCode][$idx] = trim($rowValue[$idx] ?? '');
                 }
             } else {
+                // 再次遇见该工号,对比主表字段是否一致
                 foreach ($mainColIndices as $idx => $label) {
                     $currentVal = trim($rowValue[$idx] ?? '');
                     if ($currentVal !== $mainDataConsistency[$valCode][$idx]) {
@@ -498,22 +554,7 @@ class ImportService extends Service
                 $update_mapping[$rowIndex] = $code_map[$valCode];
             }
 
-            // --- 核心:仅与数据库比对的手机号系统唯一性校验 ---
-            if ($mobileIdx !== false) {
-                $valMobile = trim((string)($rowValue[$mobileIdx] ?? ''));
-                if ($valMobile !== '') {
-                    if (isset($dbMobileMap[$valMobile])) {
-                        $existUser = $dbMobileMap[$valMobile];
-                        // 如果占用了这个手机号的人的工号,跟当前填写的工号对不上,说明是侵占别人的
-                        if ($existUser['number'] !== $valCode) {
-//                            $errors[] = "第{$displayLine}行:手机号[{$valMobile}]已被系统内其他人员(工号:[{$existUser['number']}])占用";
-                            $errors[] = "第{$displayLine}行:手机号[{$valMobile}]已被系统内其他人员占用";
-                        }
-                    }
-                }
-            }
-
-            // 2. 校验及映射转换(性别、学历、状态等)
+            // 2. 校验(性别、学历、状态映射转换)
             // 性别
             $sex_text = trim($rowValue[$sexIdx] ?? '');
             if (!empty($sex_text)) {
@@ -547,7 +588,7 @@ class ImportService extends Service
                 $array[$rowIndex][$employeeTypeIdx] = $employeeT_map[$e_text];
             }
 
-            // 人员类别
+            //人员类别
             $man_text = trim($rowValue[$manIdx] ?? '');
             if (!isset($man_map[$man_text])) {
                 $errors[] = "第{$displayLine}行:人员类别[{$man_text}]无效";
@@ -555,7 +596,7 @@ class ImportService extends Service
                 $array[$rowIndex][$manIdx] = $man_map[$man_text];
             }
 
-            // 委托类型
+            //委托类型
             $wt_text = trim($rowValue[$entrustIdx] ?? '');
             if (!isset($wt_map[$wt_text])) {
                 $errors[] = "第{$displayLine}行:委托方式[{$wt_text}]无效";
@@ -578,23 +619,6 @@ class ImportService extends Service
         return [$error_str, $update_mapping, $detail_storage];
     }
 
-    private function getEDataList($array, $user, $index1)
-    {
-        $depNums = [];
-
-        // 去重收集
-        foreach ($array as $row) {
-            if (!empty($row[$index1])) {
-                foreach (explode(',', $row[$index1]) as $v) $depNums[trim($v)] = true;
-            }
-        }
-
-        return Depart::where('del_time', 0)
-            ->where('top_depart_id', $user['top_depart_id'])
-            ->whereIn('code', array_keys($depNums))
-            ->pluck('id', 'code')->toArray();
-    }
-
     // 部门 ----------------------------------
     public function departImport($array, $user, $other_param)
     {