diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 12f5dc6..b5727c3 100755 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -834,20 +834,46 @@ class UserController extends BaseController if (empty($idsArray)) { return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, '编号不能为空']); } - if (isset($all['is_schoolmate'])) { - if ($all['is_schoolmate'] == 1) { - // 仅当 非校友→校友 时写入 schoolmate_time;已是校友的不更新,避免覆盖 - $this->model->whereIn('id', $idsArray) - ->whereRaw('COALESCE(is_schoolmate, 0) != 1') - ->update(['is_schoolmate' => 1, 'schoolmate_time' => now()]); - } else { - $this->model->whereIn('id', $idsArray)->update([ - 'is_schoolmate' => $all['is_schoolmate'], - 'schoolmate_time' => null, - ]); + DB::beginTransaction(); + try { + $updatedCount = 0; + + if (isset($all['is_schoolmate'])) { + if ($all['is_schoolmate'] == 1) { + // 改为逐条 save(),确保触发审计日志 + $users = $this->model->whereIn('id', $idsArray) + ->whereRaw('COALESCE(is_schoolmate, 0) != 1') + ->get(); + + foreach ($users as $user) { + $user->is_schoolmate = 1; + $user->schoolmate_time = now(); + if ($user->isDirty()) { + $user->save(); + $updatedCount++; + } + } + } else { + // 改为逐条 save(),确保触发审计日志 + $users = $this->model->whereIn('id', $idsArray)->get(); + + foreach ($users as $user) { + $user->is_schoolmate = $all['is_schoolmate']; + $user->schoolmate_time = null; + if ($user->isDirty()) { + $user->save(); + $updatedCount++; + } + } + } } + + DB::commit(); + return $this->success('批量更新成功,共更新 ' . $updatedCount . ' 条记录'); + } catch (\Exception $exception) { + DB::rollBack(); + return $this->fail([$exception->getCode(), $exception->getMessage()]); } - return $this->success('批量更新成功'); } /** @@ -911,9 +937,20 @@ class UserController extends BaseController DB::beginTransaction(); try { - $this->model->whereIn('id', $idsArray)->update($data); + $users = $this->model->whereIn('id', $idsArray)->get(); + $updatedCount = 0; + + foreach ($users as $user) { + // 改为逐条 save(),确保触发审计日志 + $user->fill($data); + if ($user->isDirty()) { + $user->save(); + $updatedCount++; + } + } + DB::commit(); - return $this->success('批量更新成功,共更新 ' . count($idsArray) . ' 条记录'); + return $this->success('批量更新成功,共更新 ' . $updatedCount . ' 条记录'); } catch (\Exception $exception) { DB::rollBack(); return $this->fail([$exception->getCode(), $exception->getMessage()]); diff --git a/app/Models/User.php b/app/Models/User.php index b00543f..e7bf63a 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -349,13 +349,22 @@ class User extends Authenticatable implements Auditable // 将课程开课时间转换为 datetime 格式 $schoolmateTime = Carbon::parse($courseStartDate)->format('Y-m-d H:i:s'); - // 批量更新:只更新还不是校友的学员;从 非校友→校友 时使用课程开课时间作为 schoolmate_time - return self::whereIn('id', $userIds) + // 改为逐条 save(),确保自动任务也能触发审计日志 + $users = self::whereIn('id', $userIds) ->whereRaw('COALESCE(is_schoolmate, 0) != 1') - ->update([ - 'is_schoolmate' => 1, - 'schoolmate_time' => $schoolmateTime - ]); + ->get(); + + $updatedCount = 0; + foreach ($users as $user) { + $user->is_schoolmate = 1; + $user->schoolmate_time = $schoolmateTime; + if ($user->isDirty()) { + $user->save(); + $updatedCount++; + } + } + + return $updatedCount; } } diff --git a/config/audit.php b/config/audit.php index ad0ae2d..c92f614 100755 --- a/config/audit.php +++ b/config/audit.php @@ -195,5 +195,5 @@ return [ | */ - 'console' => false, + 'console' => true, ]; diff --git a/doc/is_schoolmate字段更新说明.md b/doc/is_schoolmate字段更新说明.md new file mode 100644 index 0000000..262e5cd --- /dev/null +++ b/doc/is_schoolmate字段更新说明.md @@ -0,0 +1,20 @@ +目前会影响“校友状态”的主要有 3 类操作: + +## 1. 后台批量把一批人设为校友 + +后台支持批量操作,可以一次把多个人加入校友库,也可以一次把多个人从校友库移除。 + +这种方式的特点:一次会改很多人。过去这类操作没有审计日志,现在追加上了。 + +## 2. 导入数据时把用户设为校友 + +如果导入的表格里本身带有“是否校友”这一列,系统在导入时也可能把用户设为校友。 + +## 3. 系统自动把符合条件的学员加入校友库 + +系统有一个半小时一次的自动任务,检测课程开启了自动加入校友库设置,并且课程已经开始的审核通过的学员设置成校友。 + +## 问题排查说明 +- 经排查,创业课-企业出海与境外投资法律风险及规划。这里人员被设置成校友,来源于后台的批量设置功能。比如沈杰,是在2025-11-20 10:56:00被批量设置成校友的,一批的还有:周炫坊,李融。王洋是在2026-01-12 15:33:42被批量设置成校友的,同一批的还有杨湾湾,王婷。王文岩是在2026-01-23 17:40:02被批量设置成校友的。 + +- 终身校友的人员进入了校友库,是由于3.20日反馈有人员是终身校友,但是没有校友权益的问题时候,我理解错误并且误操作把终身校友课程下人员批量设置成了校友但是没有及时发现导致的。 \ No newline at end of file