cqp il y a 5 mois
Parent
commit
ed2b374d24
2 fichiers modifiés avec 63 ajouts et 0 suppressions
  1. 45 0
      app/Service/DeleteService.php
  2. 18 0
      app/Service/Service.php

+ 45 - 0
app/Service/DeleteService.php

@@ -6,7 +6,9 @@ use App\Model\Construction;
 use App\Model\ConstructionInfo;
 use App\Model\Customer;
 use App\Model\CustomerInfo;
+use App\Model\Depart;
 use App\Model\Employee;
+use App\Model\EmployeeDepartPermission;
 use App\Model\OrderOperation;
 use App\Model\SalesOrder;
 use App\Model\SalesOrderInfo;
@@ -242,6 +244,11 @@ class DeleteService extends Service
             if(! is_array($data['fp_top_depart_id'])) $fp_top_depart_id = [$data['fp_top_depart_id']];
             else $fp_top_depart_id = $data['fp_top_depart_id'];
 
+            $man = $data['man'] ?? [];
+            $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
+            list($status,$msg) = $this->checkRule($man, $fp_top_depart_id, $list);
+            if(! $status) return [false, $msg];
+
             $insert = [];
             foreach ($data_id as $id){
                 foreach ($fp_top_depart_id as $value){
@@ -414,6 +421,11 @@ class DeleteService extends Service
             if(! is_array($data['fp_top_depart_id'])) $fp_top_depart_id = [$data['fp_top_depart_id']];
             else $fp_top_depart_id = $data['fp_top_depart_id'];
 
+            $man = $data['man'] ?? [];
+            $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
+            list($status,$msg) = $this->checkRule($man, $fp_top_depart_id, $list);
+            if(! $status) return [false, $msg];
+
             $insert = [];
             foreach ($data_id as $id){
                 foreach ($fp_top_depart_id as $value){
@@ -432,6 +444,39 @@ class DeleteService extends Service
         return [true, ''];
     }
 
+    public function checkRule($man_id = [], $top_depart_id = [], $depart_list){
+        $depart = EmployeeDepartPermission::whereIn('employee_id', $man_id)
+            ->select('employee_id','depart_id')
+            ->get()->toArray();
+
+        $depart_has = [];
+        foreach ($depart as $value){
+            $p = $this->findTopLevelId($depart_list, $value['depart_id']);
+            if($p > 0) $depart_has[$value['employee_id']][] = $p;
+        }
+        if(empty($depart_has)) return [false, '无相关信息能获取到门店数据,校验失败'];
+
+        if(count($depart_has) == 1){
+            $intersection = array_values($depart_has);
+            $intersection = $intersection[0];
+        }else{
+            // 提取所有的子数组
+            $subArrays = array_map(function($item) {
+                return $item;
+            }, $depart_has);
+
+            // 使用 array_intersect 计算交集
+            $intersection = call_user_func_array('array_intersect', $subArrays);
+            if(empty($intersection)) return [false, '非法数据,请刷新网页重新操作'];
+        }
+
+        foreach ($top_depart_id as $value){
+            if(! in_array($value, $intersection)) return [false, '非法数据,请刷新网页重新操作'];
+        }
+
+        return [true, ''];
+    }
+
     public function pq($data,$user){
         list($status,$msg) = $this->pqRule($data,$user);
         if(! $status) return [false,$msg];

+ 18 - 0
app/Service/Service.php

@@ -542,4 +542,22 @@ class Service
         $seconds = $days * 86400;
         return [true,strtotime(date('Y-m-d 00:00:00', $epoch + $seconds))];
     }
+
+    function findTopLevelId($data, $id) {
+        // 遍历数据,查找给定ID的节点
+        foreach ($data as $item) {
+            if ($item['id'] == $id) {
+                // 如果找到了给定ID的节点,并且它的parent_id为null,则它就是最顶层节点
+                if ($item['parent_id'] === 0) {
+                    return $item['id'];
+                } else {
+                    // 否则,继续寻找其父节点的最顶层节点
+                    return $this->findTopLevelId($data, $item['parent_id']);
+                }
+            }
+        }
+
+        // 如果没有找到给定ID的节点,返回null或抛出异常
+        return 0;
+    }
 }