|
@@ -8,14 +8,68 @@ use Illuminate\Support\Facades\DB;
|
|
|
class FeeService extends Service
|
|
class FeeService extends Service
|
|
|
{
|
|
{
|
|
|
public function feeEdit($data, $user){
|
|
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];
|
|
if(!$status) return [$status, $msg];
|
|
|
|
|
|
|
|
$update = $msg['data'][0];
|
|
$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){
|
|
public function feeAdd($data,$user){
|
|
@@ -31,6 +85,7 @@ class FeeService extends Service
|
|
|
$model->title = $value['title'];
|
|
$model->title = $value['title'];
|
|
|
$model->code = $value['code'];
|
|
$model->code = $value['code'];
|
|
|
$model->is_other = $value['is_other'] ?? Fee::IS_OTHER_ZERO;
|
|
$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->sort = $value['sort'] ?? 0;
|
|
|
$model->top_depart_id = $value['top_depart_id'];
|
|
$model->top_depart_id = $value['top_depart_id'];
|
|
|
$model->save();
|
|
$model->save();
|
|
@@ -75,6 +130,8 @@ class FeeService extends Service
|
|
|
->orderby('id', 'asc');
|
|
->orderby('id', 'asc');
|
|
|
|
|
|
|
|
if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
|
|
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['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
|
|
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_code'] = $tmp['code'] ?? "";
|
|
|
$list['data'][$key]['parent_title'] = $tmp['title'] ?? "";
|
|
$list['data'][$key]['parent_title'] = $tmp['title'] ?? "";
|
|
|
$list['data'][$key]['is_other_title'] = Fee::IS_OTHER[$value['is_other']] ?? "";
|
|
$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'];
|
|
$top_depart_id = $user['top_depart_id'];
|
|
|
if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
|
|
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]['top_depart_id'] = $top_depart_id;
|
|
|
$data['data'][$key]['upd_time'] = time();
|
|
$data['data'][$key]['upd_time'] = time();
|
|
|
|
|
|