cqp il y a 1 semaine
Parent
commit
622ed560db
3 fichiers modifiés avec 99 ajouts et 7 suppressions
  1. 8 1
      app/Model/Fee.php
  2. 67 4
      app/Service/FeeService.php
  3. 24 2
      config/excel/fee.php

+ 8 - 1
app/Model/Fee.php

@@ -13,7 +13,7 @@ class Fee extends DataScopeBaseModel
     protected $dateFormat = 'U';
     const IS_UES = 1;//启用
 
-    public static $field = ['title','id','code','is_use','parent_id','is_other','sort'];
+    public static $field = ['title','id','code','is_use','parent_id','is_other','sort','is_direct'];
 
     const IS_OTHER_ZERO = 0;
     const IS_OTHER_ONE = 1;
@@ -21,4 +21,11 @@ class Fee extends DataScopeBaseModel
         self::IS_OTHER_ZERO => '否',
         self::IS_OTHER_ONE => '是',
     ];
+
+    const IS_DIRECT_ZERO = 0;
+    const IS_DIRECT_ONE = 1;
+    const IS_DIRECT = [
+        self::IS_DIRECT_ZERO => '否',
+        self::IS_DIRECT_ONE => '是',
+    ];
 }

+ 67 - 4
app/Service/FeeService.php

@@ -8,14 +8,68 @@ use Illuminate\Support\Facades\DB;
 class FeeService extends Service
 {
     public function feeEdit($data, $user){
-        list($status,$msg) = $this->feeRule($data,$user,false);
+        // 1. 验证数据与获取清洗后的数据
+        list($status, $msg) = $this->feeRule($data, $user, false);
         if(!$status) return [$status, $msg];
 
         $update = $msg['data'][0];
-        $model = new Fee();
-        $model->where('id',$data['id'])->update($update);
 
-        return [true, ''];
+        try {
+            // 2. 开启事务
+            DB::beginTransaction();
+
+            // 3. 更新当前节点本身
+            $model = new Fee();
+            $model->where('id', $data['id'])->update($update);
+
+            // 4. 判断如果是顶级节点(parent_id == 0),则更新下面所有的子集
+            if (isset($update['parent_id']) && $update['parent_id'] == 0) {
+                // 获取当前顶级节点最新的两个字段值
+                $isOther = $update['is_other'] ?? Fee::IS_OTHER_ZERO;
+                $isDirect = $update['is_direct'] ?? Fee::IS_DIRECT_ZERO;
+
+                // 调用递归方法,向下同步所有后代节点
+                $this->syncChildrenAttributes($data['id'], $isOther, $isDirect);
+            }
+
+            DB::commit();
+            return [true, ''];
+
+        } catch (\Exception $exception) {
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+    }
+
+    /**
+     * 辅助方法:递归更新所有子集的 is_other 和 is_direct 属性
+     * * @param int $parentId 当前父级ID
+     * @param int $isOther
+     * @param int $isDirect
+     */
+    public function syncChildrenAttributes($parentId, $isOther, $isDirect) {
+        // 1. 找出当前节点下的所有直接子节点
+        $children = Fee::where('parent_id', $parentId)
+            ->where('del_time', 0)
+            ->get();
+
+        // 如果没有子节点了,直接退出递归
+        if ($children->isEmpty()) {
+            return;
+        }
+
+        // 2. 批量更新这些直接子节点的这两个字段
+        Fee::where('parent_id', $parentId)
+            ->where('del_time', 0)
+            ->update([
+                'is_other'  => $isOther,
+                'is_direct' => $isDirect,
+            ]);
+
+        // 3. 递归向下:让每个子节点再去更新它们自己的子节点(孙子辈)
+        foreach ($children as $child) {
+            $this->syncChildrenAttributes($child->id, $isOther, $isDirect);
+        }
     }
 
     public function feeAdd($data,$user){
@@ -31,6 +85,7 @@ class FeeService extends Service
                 $model->title = $value['title'];
                 $model->code = $value['code'];
                 $model->is_other = $value['is_other'] ?? Fee::IS_OTHER_ZERO;
+                $model->is_direct = $value['is_direct'] ?? Fee::IS_DIRECT_ZERO;
                 $model->sort = $value['sort'] ?? 0;
                 $model->top_depart_id = $value['top_depart_id'];
                 $model->save();
@@ -75,6 +130,8 @@ class FeeService extends Service
             ->orderby('id', 'asc');
 
         if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
+        if(isset($data['is_direct'])) $model->where('is_direct', $data['is_direct']);
+        if(isset($data['is_other'])) $model->where('is_other', $data['is_other']);
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
         if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
 
@@ -111,6 +168,7 @@ class FeeService extends Service
                 $list['data'][$key]['parent_code'] = $tmp['code'] ?? "";
                 $list['data'][$key]['parent_title'] = $tmp['title'] ?? "";
                 $list['data'][$key]['is_other_title'] = Fee::IS_OTHER[$value['is_other']] ?? "";
+                $list['data'][$key]['is_direct_title'] = Fee::IS_DIRECT[$value['is_direct']] ?? "";
             }
         }
 
@@ -143,6 +201,11 @@ class FeeService extends Service
             $top_depart_id = $user['top_depart_id'];
             if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
 
+            // 1. 基础互斥校验
+            $isOther = $value['is_other'] ?? Fee::IS_OTHER_ZERO;
+            $isDirect = $value['is_direct'] ?? Fee::IS_DIRECT_ZERO;
+            if ($isOther && $isDirect) return [false, '是否其他费用和是否直接投入费用不能同时开启'];
+
             $data['data'][$key]['top_depart_id'] = $top_depart_id;
             $data['data'][$key]['upd_time'] = time();
 

+ 24 - 2
config/excel/fee.php

@@ -28,12 +28,34 @@ return [
             'key' =>'is_other',
             'export' =>'is_other_title',
             'value' => '是否其他费用',
-            'required' => true,
+            'required' => false,
             'is_main' => true,
             'default' => \App\Model\Fee::IS_OTHER_ZERO,
             'unique' => false,
             'enums' => array_values(\App\Model\Fee::IS_OTHER),
-            'comments' => '必填'
+            'comments' => '选填(父级填写后子级继承)'
+        ],
+        [
+            'key' =>'is_direct',
+            'export' =>'is_direct_title',
+            'value' => '是否直接投入费用',
+            'required' => false,
+            'is_main' => true,
+            'default' => \App\Model\Fee::IS_DIRECT_ZERO,
+            'unique' => false,
+            'enums' => array_values(\App\Model\Fee::IS_DIRECT),
+            'comments' => '选填(父级填写后子级继承)'
+        ],
+        [
+            'key' =>'sort',
+            'export' =>'sort',
+            'value' => '排序',
+            'required' => false,
+            'is_main' => true,
+            'default' => 0,
+            'unique' => false,
+            'enums' => [],
+            'comments' => '选填'
         ],
         [
             'key' =>'parent_id',