mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 03:57:22 +08:00
add back to top
This commit is contained in:
@@ -86,7 +86,7 @@ class AttendanceRepository extends BaseRepository
|
||||
return $query->first();
|
||||
}
|
||||
|
||||
private function getContinuousPoints($days)
|
||||
public function getContinuousPoints($days)
|
||||
{
|
||||
$settings = Setting::get('bonus');
|
||||
$initial = $settings['attendance_initial'] ?? Attendance::INITIAL_BONUS;
|
||||
@@ -96,7 +96,7 @@ class AttendanceRepository extends BaseRepository
|
||||
$points = min($initial + $days * $step, $max);
|
||||
krsort($extraAwards);
|
||||
foreach ($extraAwards as $key => $value) {
|
||||
if ($days >= $key) {
|
||||
if ($days >= $key - 1) {
|
||||
$points += $value;
|
||||
break;
|
||||
}
|
||||
@@ -224,11 +224,10 @@ class AttendanceRepository extends BaseRepository
|
||||
$period = new \DatePeriod($row->added->addDay(1), $interval, $row->days, \DatePeriod::EXCLUDE_START_DATE);
|
||||
$i = 0;
|
||||
foreach ($period as $periodValue) {
|
||||
$insert[] = [
|
||||
'uid' => $row->uid,
|
||||
'points' => ($i == 0 ? $row->points : 0),
|
||||
'date' => $periodValue->format('Y-m-d'),
|
||||
];
|
||||
$insert[] = sprintf(
|
||||
"(%d, %d, '%s')",
|
||||
$row->uid, $i == 0 ? $row->points : 0, $periodValue->format('Y-m-d')
|
||||
);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
@@ -238,9 +237,13 @@ class AttendanceRepository extends BaseRepository
|
||||
do_log("no data to insert...", 'info', app()->runningInConsole());
|
||||
return 0;
|
||||
}
|
||||
NexusDB::table($table)->insert($insert);
|
||||
$sql = sprintf(
|
||||
"insert into `%s` (`uid`, `points`, `date`) values %s on duplicate key update `points` = values(`points`)",
|
||||
$table, implode(',', $insert)
|
||||
);
|
||||
NexusDB::statement($sql);
|
||||
$insertCount = count($insert);
|
||||
do_log("[MIGRATE_ATTENDANCE_LOGS] DONE! insert count: " . $insertCount, 'info', app()->runningInConsole());
|
||||
do_log("[MIGRATE_ATTENDANCE_LOGS] DONE! insert sql: " . $sql, 'info', app()->runningInConsole());
|
||||
|
||||
return $insertCount;
|
||||
}
|
||||
@@ -252,7 +255,7 @@ class AttendanceRepository extends BaseRepository
|
||||
} else {
|
||||
$start = $attendance->added;
|
||||
}
|
||||
$logQuery = $attendance->logs()->where('created_at', '<=', $start)->orderBy('date', 'desc');
|
||||
$logQuery = $attendance->logs()->where('date', '<=', $start)->orderBy('date', 'desc');
|
||||
$attendanceLogs = $logQuery->get(['date'])->keyBy('date');
|
||||
$counts = $attendanceLogs->count();
|
||||
do_log(sprintf('user: %s, log counts: %s from query: %s', $attendance->uid, $counts, last_query()));
|
||||
@@ -286,6 +289,10 @@ class AttendanceRepository extends BaseRepository
|
||||
throw new \LogicException("Haven't attendance yet");
|
||||
}
|
||||
$date = Carbon::createFromTimestampMs($timestampMs);
|
||||
$now = Carbon::now();
|
||||
if ($date->gte($now) || $now->diffInDays($date) > Attendance::MAX_RETROACTIVE_DAYS) {
|
||||
throw new \LogicException(sprintf("date: %s can't be retroactive attend", $date->format('Y-m-d')));
|
||||
}
|
||||
return NexusDB::transaction(function () use ($user, $attendance, $date) {
|
||||
if (AttendanceLog::query()->where('uid', $user->id)->where('date', $date->format('Y-m-d'))->exists()) {
|
||||
throw new \RuntimeException("Already attendance");
|
||||
|
||||
@@ -75,6 +75,24 @@ class BonusRepository extends BaseRepository
|
||||
|
||||
}
|
||||
|
||||
public function consumeToBuyAttendanceCard($uid): bool
|
||||
{
|
||||
$user = User::query()->findOrFail($uid);
|
||||
$requireBonus = BonusLogs::getBonusForBuyAttendanceCard();
|
||||
NexusDB::transaction(function () use ($user, $requireBonus) {
|
||||
$comment = nexus_trans('bonus.comment_buy_attendance_card', [
|
||||
'bonus' => $requireBonus,
|
||||
], $user->locale);
|
||||
$comment = addslashes($comment);
|
||||
do_log("comment: $comment");
|
||||
$this->consumeUserBonus($user, $requireBonus, BonusLogs::BUSINESS_TYPE_BUY_ATTENDANCE_CARD, "$comment");
|
||||
User::query()->where('id', $user->id)->increment('attendance_card');
|
||||
});
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private function consumeUserBonus($user, $requireBonus, $logBusinessType, $logComment = '')
|
||||
{
|
||||
if ($user->seedbonus < $requireBonus) {
|
||||
|
||||
@@ -451,8 +451,12 @@ class ExamRepository extends BaseRepository
|
||||
return false;
|
||||
}
|
||||
if ($examUser->is_done == ExamUser::IS_DONE_YES) {
|
||||
do_log("examUser: {$examUser->id} is done, won't update progress.");
|
||||
return false;
|
||||
/**
|
||||
* continue update
|
||||
* @since v1.7.0
|
||||
*/
|
||||
// do_log("examUser: {$examUser->id} is done, won't update progress.");
|
||||
// return false;
|
||||
}
|
||||
$exam = $examUser->exam;
|
||||
if (!$user instanceof User) {
|
||||
|
||||
@@ -6,8 +6,8 @@ use App\Models\Setting;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\TorrentTag;
|
||||
use App\Models\User;
|
||||
use Elastic\Elasticsearch\Client;
|
||||
use Elastic\Elasticsearch\ClientBuilder;
|
||||
use Elasticsearch\Client;
|
||||
use Elasticsearch\ClientBuilder;
|
||||
use Illuminate\Support\Arr;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
@@ -126,11 +126,11 @@ class SearchRepository extends BaseRepository
|
||||
{
|
||||
if (is_null($this->es)) {
|
||||
$config = nexus_config('nexus.elasticsearch');
|
||||
$es = ClientBuilder::create()->setHosts($config['hosts']);
|
||||
$builder = ClientBuilder::create()->setHosts($config['hosts']);
|
||||
if (!empty($config['ssl_verification'])) {
|
||||
$es->setSSLVerification($config['ssl_verification']);
|
||||
$builder->setSSLVerification($config['ssl_verification']);
|
||||
}
|
||||
$this->es = $es;
|
||||
$this->es = $builder->build();
|
||||
}
|
||||
return $this->es;
|
||||
}
|
||||
|
||||
@@ -81,10 +81,10 @@ class ToolRepository extends BaseRepository
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
public function cronjobBackup()
|
||||
public function cronjobBackup($force = false): bool|array
|
||||
{
|
||||
$setting = Setting::get('backup');
|
||||
if ($setting['enabled'] != 'yes') {
|
||||
if ($setting['enabled'] != 'yes' && !$force) {
|
||||
do_log("Backup not enabled.");
|
||||
return false;
|
||||
}
|
||||
@@ -94,23 +94,25 @@ class ToolRepository extends BaseRepository
|
||||
$settingMinute = (int)$setting['minute'];
|
||||
$nowHour = (int)$now->format('H');
|
||||
$nowMinute = (int)$now->format('i');
|
||||
do_log("Backup frequency: $frequency");
|
||||
if ($frequency == 'daily') {
|
||||
if ($settingHour != $nowHour) {
|
||||
do_log(sprintf('Backup setting hour: %s != now hour: %s', $settingHour, $nowHour));
|
||||
return false;
|
||||
do_log("Backup frequency: $frequency, force: " . strval($force));
|
||||
if (!$force) {
|
||||
if ($frequency == 'daily') {
|
||||
if ($settingHour != $nowHour) {
|
||||
do_log(sprintf('Backup setting hour: %s != now hour: %s', $settingHour, $nowHour));
|
||||
return false;
|
||||
}
|
||||
if ($settingMinute != $nowMinute) {
|
||||
do_log(sprintf('Backup setting minute: %s != now minute: %s', $settingMinute, $nowMinute));
|
||||
return false;
|
||||
}
|
||||
} elseif ($frequency == 'hourly') {
|
||||
if ($settingMinute != $nowMinute) {
|
||||
do_log(sprintf('Backup setting minute: %s != now minute: %s', $settingMinute, $nowMinute));
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException("Unknown backup frequency: $frequency");
|
||||
}
|
||||
if ($settingMinute != $nowMinute) {
|
||||
do_log(sprintf('Backup setting minute: %s != now minute: %s', $settingMinute, $nowMinute));
|
||||
return false;
|
||||
}
|
||||
} elseif ($frequency == 'hourly') {
|
||||
if ($settingMinute != $nowMinute) {
|
||||
do_log(sprintf('Backup setting minute: %s != now minute: %s', $settingMinute, $nowMinute));
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException("Unknown backup frequency: $frequency");
|
||||
}
|
||||
$backupResult = $this->backupAll();
|
||||
do_log("Backup all result: " . json_encode($backupResult));
|
||||
@@ -143,17 +145,17 @@ class ToolRepository extends BaseRepository
|
||||
$service = new \Google\Service\Drive($client);
|
||||
$adapter = new \Masbug\Flysystem\GoogleDriveAdapter($service, $folderId);
|
||||
|
||||
$filesystem = new \League\Flysystem\Filesystem($adapter, new \League\Flysystem\Config([\League\Flysystem\Config::OPTION_VISIBILITY => \League\Flysystem\Visibility::PRIVATE]));
|
||||
$filesystem = new \League\Flysystem\Filesystem($adapter);
|
||||
|
||||
$localAdapter = new \League\Flysystem\Local\LocalFilesystemAdapter('/');
|
||||
$localFilesystem = new \League\Flysystem\Filesystem($localAdapter, [\League\Flysystem\Config::OPTION_VISIBILITY => \League\Flysystem\Visibility::PRIVATE]);
|
||||
$localFilesystem = new \League\Flysystem\Filesystem($localAdapter);
|
||||
|
||||
$filename = $backupResult['filename'];
|
||||
$time = Carbon::now();
|
||||
$start = Carbon::now();
|
||||
try {
|
||||
$filesystem->writeStream(basename($filename), $localFilesystem->readStream($filename), new \League\Flysystem\Config());
|
||||
$speed = !(float)$time->diffInSeconds() ? 0 :filesize($filename) / (float)$time->diffInSeconds();
|
||||
$log = 'Elapsed time: '.$time->diffForHumans(null, true);
|
||||
$filesystem->writeStream(basename($filename), $localFilesystem->readStream($filename));
|
||||
$speed = !(float)$start->diffInSeconds() ? 0 :filesize($filename) / (float)$start->diffInSeconds();
|
||||
$log = 'Elapsed time: '.$start->diffForHumans(null, true);
|
||||
$log .= ', Speed: '. number_format($speed/1024,2) . ' KB/s';
|
||||
do_log($log);
|
||||
$backupResult['upload_result'] = 'success: ' .$log;
|
||||
@@ -174,7 +176,7 @@ class ToolRepository extends BaseRepository
|
||||
$log = "[SEND_MAIL]";
|
||||
do_log("$log, to: $to, subject: $subject, body: $body");
|
||||
$factory = new EsmtpTransportFactory();
|
||||
$smtp = Setting::get('smtp');
|
||||
$smtp = Setting::getFromDb('smtp');
|
||||
$encryption = null;
|
||||
if (isset($smtp['encryption']) && in_array($smtp['encryption'], ['ssl', 'tls'])) {
|
||||
$encryption = $smtp['encryption'];
|
||||
|
||||
Reference in New Issue
Block a user