where('status', 0) ->where('create_time', '<=', $expireBefore); if ($userId !== null && $userId > 0) { $query->where('user_id', $userId); } if ($orderNo !== null && $orderNo !== '') { $query->where('order_no', $orderNo); } $rows = $query->field(['id', 'remark'])->select()->toArray(); if ($rows === []) { return 0; } $now = time(); $affectedCount = 0; foreach ($rows as $row) { $id = isset($row['id']) && is_numeric($row['id']) ? intval($row['id']) : 0; if ($id <= 0) { continue; } $oldRemark = isset($row['remark']) && is_string($row['remark']) ? trim($row['remark']) : ''; $reason = '[timeout] unpaid over ' . self::EXPIRE_SECONDS . 's'; $remark = $oldRemark === '' ? $reason : mb_substr($oldRemark . ' | ' . $reason, 0, 255); $affected = Db::name('deposit_order') ->where('id', $id) ->where('status', 0) ->update([ 'status' => 2, 'remark' => $remark, 'update_time' => $now, ]); if (is_numeric($affected) && intval($affected) > 0) { $affectedCount++; } } return $affectedCount; } public static function pendingCountByUserId(int $userId): int { if ($userId <= 0) { return 0; } return Db::name('deposit_order') ->where('user_id', $userId) ->where('status', 0) ->count(); } }