where('user_id', $user->id)->firstOrFail(); switch ($action) { case PointLogAction::Recharge: if ($points < 0) { throw new BizException('积分不能小于 0'); } break; case PointLogAction::Deduction: case PointLogAction::Consumption: if ($points > 0) { throw new BizException('积分不能大于 0'); } break; } $before = $userinfo->points; $after = $before + $points; if ($after < 0) { throw new BizException('积分不足'); } $userinfo->update([ 'points' => $after, ]); $remark = data_get($options, 'remark'); $loggable = data_get($options, 'loggable'); $administrator = data_get($options, 'administrator'); PointLog::create([ 'loggable_id' => $loggable?->id, 'loggable_type' => $loggable?->getMorphClass(), 'user_id' => $user->id, 'action' => $action, 'change_points' => $points, 'before_points' => $before, 'after_points' => $after, 'remark' => $remark, 'administrator_id' => $administrator?->id, 'store_id' => data_get($options, 'store_id'), ]); } }