Files
nexusphp/app/Repositories/ClaimRepository.php

446 lines
20 KiB
PHP
Raw Normal View History

2022-05-05 22:19:48 +08:00
<?php
namespace App\Repositories;
use App\Jobs\SettleClaim;
2026-01-07 01:20:44 +07:00
use App\Models\BonusLogs;
2022-05-05 22:19:48 +08:00
use App\Models\Claim;
use App\Models\Message;
use App\Models\Snatch;
use App\Models\Torrent;
use App\Models\User;
use Carbon\Carbon;
2022-06-01 13:15:55 +08:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
2022-05-05 22:19:48 +08:00
use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
class ClaimRepository extends BaseRepository
{
2022-09-05 19:32:24 +08:00
const STAT_CACHE_PREFIX = 'claim_stats_';
const SETTLE_PAGINATE_STARTS = 2000;
const SETTLE_MSG_SLICE_COUNT = 1000;
2022-05-05 22:19:48 +08:00
public function getList(array $params)
{
$query = Claim::query()->with(['family']);
if (!empty($params['family_id'])) {
$query->where('family_id', $params['family_id']);
}
$query->orderBy('family_id', 'desc');
return $query->paginate();
}
public function store($uid, $torrentId)
2023-03-04 00:52:08 +08:00
{
$snatch = $this->canBeClaimByUser($torrentId, $uid);
$insert = [
'uid' => $uid,
'torrent_id' => $torrentId,
'snatched_id' => $snatch->id,
'seed_time_begin' => $snatch->seedtime,
'uploaded_begin' => $snatch->uploaded,
];
$this->clearStatsCache($uid);
return Claim::query()->create($insert);
}
public function canBeClaimByUser(int $torrentId, int $uid)
2022-05-05 22:19:48 +08:00
{
2022-05-06 15:50:26 +08:00
$isEnabled = Claim::getConfigIsEnabled();
2022-05-06 22:25:00 +08:00
if (!$isEnabled) {
2022-05-06 15:50:26 +08:00
throw new \RuntimeException(nexus_trans("torrent.claim_disabled"));
}
2022-05-05 22:19:48 +08:00
$exists = Claim::query()->where('uid', $uid)->where('torrent_id', $torrentId)->exists();
if ($exists) {
throw new \RuntimeException(nexus_trans("torrent.claim_already"));
}
2023-03-04 00:52:08 +08:00
$snatch = Snatch::query()->where('userid', $uid)->where('torrentid', $torrentId)->first();
if (!$snatch) {
throw new \RuntimeException(nexus_trans("torrent.no_snatch"));
}
$claimTorrentTTL = Claim::getConfigTorrentTTL();
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
if ($torrent->added->addDays($claimTorrentTTL)->gte(Carbon::now())) {
throw new \RuntimeException(nexus_trans("torrent.can_no_be_claimed_yet"));
}
2023-03-13 17:41:03 +08:00
if (!has_role_work_seeding($uid)) {
2023-02-21 01:04:28 +08:00
$max = Claim::getConfigTorrentUpLimit();
$count = Claim::query()->where('uid', $uid)->count();
if ($count >= $max) {
throw new \RuntimeException(nexus_trans("torrent.claim_number_reach_torrent_maximum"));
}
$max = Claim::getConfigUserUpLimit();
$count = Claim::query()->where('torrent_id', $torrentId)->count();
if ($count >= $max) {
throw new \RuntimeException(nexus_trans("torrent.claim_number_reach_user_maximum"));
}
2022-05-05 22:19:48 +08:00
}
2023-03-04 00:52:08 +08:00
return $snatch;
2022-05-05 22:19:48 +08:00
}
public function update(array $params, $id)
{
$model = Claim::query()->findOrFail($id);
$model->update($params);
return $model;
}
public function getDetail($id)
{
$model = Claim::query()->findOrFail($id);
return $model;
}
public function delete($id, $uid)
{
2022-05-06 15:50:26 +08:00
$isEnabled = Claim::getConfigIsEnabled();
2022-05-07 15:10:14 +08:00
if (!$isEnabled) {
2022-05-06 15:50:26 +08:00
throw new \RuntimeException(nexus_trans("torrent.claim_disabled"));
}
2022-05-05 22:19:48 +08:00
$model = Claim::query()->findOrFail($id);
if ($model->uid != $uid) {
throw new \RuntimeException("No permission");
}
$deductBonus = Claim::getConfigGiveUpDeductBonus();
return NexusDB::transaction(function () use ($model, $deductBonus) {
2022-09-05 19:32:24 +08:00
$this->clearStatsCache($model->uid);
2022-05-05 22:19:48 +08:00
User::query()->where('id', $model->uid)->decrement('seedbonus', $deductBonus);
do_log(sprintf("[GIVE_UP_CLAIM_TORRENT], user: %s, deduct bonus: %s", $model->uid, $deductBonus), 'alert');
return $model->delete();
});
}
public function getStats($uid)
{
2022-09-05 19:32:24 +08:00
$key = self::STAT_CACHE_PREFIX . $uid;
return NexusDB::remember($key, 3600, function () use ($uid) {
2022-05-05 22:19:48 +08:00
$max = Claim::getConfigTorrentUpLimit();
return sprintf('%s/%s', Claim::query()->where('uid', $uid)->count(), $max);
});
}
2022-09-05 19:32:24 +08:00
public function clearStatsCache($uid)
{
NexusDB::cache_del(self::STAT_CACHE_PREFIX . $uid);
}
2022-05-05 22:19:48 +08:00
public function settleCronjob(): array
{
2022-05-06 15:50:26 +08:00
$startOfThisMonth = Carbon::now()->startOfMonth();
$query = Claim::query()
2025-05-16 02:43:45 +07:00
->selectRaw("uid, count(*) as count")
2023-07-06 03:23:28 +08:00
->where("created_at", "<", $startOfThisMonth)
2022-06-01 13:15:55 +08:00
->where(function (Builder $query) use ($startOfThisMonth) {
$query->where('last_settle_at', '<', $startOfThisMonth)->orWhereNull('last_settle_at');
})
2022-05-06 15:50:26 +08:00
->groupBy('uid')
;
2023-07-04 13:20:25 +08:00
$size = 1000;
$page = 1;
2022-05-05 22:19:48 +08:00
$successCount = $failCount = 0;
while (true) {
2023-07-04 13:20:25 +08:00
$logPrefix = "size: $size, page: $page";
$result = (clone $query)->forPage($page, $size)->get();
2022-05-05 22:19:48 +08:00
if ($result->isEmpty()) {
do_log("$logPrefix, no more data...");
break;
}
2022-05-06 15:50:26 +08:00
do_log("get counts: " . $result->count());
2022-05-05 22:19:48 +08:00
foreach ($result as $row) {
$uid = $row->uid;
if ($row->count > self::SETTLE_PAGINATE_STARTS) {
SettleClaim::dispatch($uid);
} else {
do_log("$logPrefix, begin to settle user: $uid...");
try {
$result = $this->settleUser($uid);
do_log("$logPrefix, settle user: $uid done!, result: " . var_export($result, true));
if ($result) {
$successCount++;
} else {
$failCount++;
}
} catch (\Throwable $exception) {
do_log("$logPrefix, settle user: $uid fail!, error: " . $exception->getMessage() . $exception->getTraceAsString(), 'error');
2022-05-06 15:50:26 +08:00
$failCount++;
}
2022-05-05 22:19:48 +08:00
}
}
2023-07-04 13:20:25 +08:00
$page++;
2022-05-05 22:19:48 +08:00
}
return ['success_count' => $successCount, 'fail_count' => $failCount];
}
public function settleUser($uid, $force = false, $test = false, $paginate = false): bool
2022-05-05 22:19:48 +08:00
{
$logMsg = sprintf("uid: %s, force: %s, test: %s, paginate: %s", $uid, $force, $test, $paginate);
2023-03-13 17:41:03 +08:00
$hasRoleWorkSeeding = has_role_work_seeding($uid);
2023-02-21 01:04:28 +08:00
if ($hasRoleWorkSeeding) {
do_log("$logMsg, filter: user_has_role_work_seeding => true, skip");
2022-12-13 13:51:39 +08:00
return false;
}
2022-05-05 22:19:48 +08:00
$now = Carbon::now();
$startOfThisMonth = $now->clone()->startOfMonth();
2023-07-04 01:00:02 +08:00
$user = User::query()->with('language')->findOrFail($uid);
$baseQuery = Claim::query()
2023-07-04 01:00:02 +08:00
->where('uid', $uid)
->where("created_at", "<", $startOfThisMonth)
;
$totalRecordCount = (clone $baseQuery)->count();
2022-05-05 22:19:48 +08:00
$seedTimeRequiredHours = Claim::getConfigStandardSeedTimeHours();
$uploadedRequiredTimes = Claim::getConfigStandardUploadedTimes();
$bonusMultiplier = Claim::getConfigBonusMultiplier();
$bonusDeduct = Claim::getConfigRemoveDeductBonus();
$reachedTorrentIdArr = $unReachedTorrentIdArr = $remainTorrentIdArr = $unReachedIdArr = $toUpdateIdArr = [];
$totalSeedTime = 0;
$seedTimeCaseWhen = $uploadedCaseWhen = [];
2023-07-04 01:16:06 +08:00
$toDelClaimId = [];
2022-05-05 22:19:48 +08:00
do_log(
"$logMsg, claim torrent count: $totalRecordCount"
2022-05-05 22:19:48 +08:00
. ", seedTimeRequiredHours: $seedTimeRequiredHours"
. ", uploadedRequiredTimes: $uploadedRequiredTimes"
. ", bonusMultiplier: $bonusMultiplier"
. ", bonusDeduct: $bonusDeduct"
);
if ($paginate) {
$page = 1;
while (true) {
$list = (clone $baseQuery)->forPage($page, self::SETTLE_PAGINATE_STARTS)->get();
if ($list->isEmpty()) {
break;
}
$handleResult = $this->handleClaimSettlement(
$uid, $list, $startOfThisMonth, $seedTimeRequiredHours, $uploadedRequiredTimes,
$reachedTorrentIdArr,$unReachedTorrentIdArr,$remainTorrentIdArr, $seedTimeCaseWhen, $uploadedCaseWhen,
$toUpdateIdArr, $unReachedIdArr, $toDelClaimId, $totalSeedTime, $force
);
if ($handleResult === false) {
do_log("$logMsg, handleClaimSettlement result false, return");
2022-05-05 22:19:48 +08:00
return false;
}
$page++;
2022-05-05 22:19:48 +08:00
}
} else {
$list = $baseQuery->with(['snatch', 'torrent' => fn ($query) => $query->select(Torrent::$commentFields)])->get();
$handleResult = $this->handleClaimSettlement(
$uid, $list, $startOfThisMonth, $seedTimeRequiredHours, $uploadedRequiredTimes,
$reachedTorrentIdArr,$unReachedTorrentIdArr,$remainTorrentIdArr, $seedTimeCaseWhen, $uploadedCaseWhen,
$toUpdateIdArr, $unReachedIdArr, $toDelClaimId, $totalSeedTime, $force
);
if ($handleResult === false) {
do_log("$logMsg, handleClaimSettlement result false, return");
return false;
2022-05-05 22:19:48 +08:00
}
}
$bonusResult = calculate_seed_bonus($uid, $reachedTorrentIdArr);
$seedTimeHoursAvg = $totalSeedTime / (count($reachedTorrentIdArr) ?: 1) / 3600;
do_log(sprintf(
"reachedTorrentIdArr: %s, unReachedIdArr: %s, bonusResult: %s, seedTimeHours: %s",
json_encode($reachedTorrentIdArr), json_encode($unReachedIdArr), json_encode($bonusResult), $seedTimeHoursAvg
), 'alert');
$bonusFinal = $bonusResult['seed_points'] * $seedTimeHoursAvg * $bonusMultiplier;
2022-05-05 22:19:48 +08:00
do_log("bonus final: $bonusFinal", 'alert');
$totalDeduct = $bonusDeduct * count($unReachedIdArr);
do_log("totalDeduct: $totalDeduct", 'alert');
2022-05-06 15:50:26 +08:00
$message = $this->buildMessage(
$user, $reachedTorrentIdArr, $unReachedTorrentIdArr, $remainTorrentIdArr,
$bonusResult, $bonusFinal, $seedTimeHoursAvg, $bonusDeduct, $totalDeduct
);
do_log("message: " . nexus_json_encode($message), 'alert');
2022-05-05 22:19:48 +08:00
/**
* Just do a test, debug from log
*/
if ($test) {
do_log("[TEST], return");
return true;
}
2022-05-06 15:50:26 +08:00
//Wrap with transaction
DB::transaction(function () use ($uid, $unReachedIdArr, $toUpdateIdArr, $bonusFinal, $totalDeduct, $uploadedCaseWhen, $seedTimeCaseWhen, $message, $now) {
2026-01-07 01:20:44 +07:00
//get latest
$oldBonus = User::query()->find($uid, ['seedbonus'])->seedbonus;
$delta = 0;
2022-05-06 15:50:26 +08:00
//Increase user bonus
2026-01-07 01:20:44 +07:00
$delta += $bonusFinal;
do_log("Increase user: $uid bonus: $bonusFinal", 'alert');
BonusLogs::add($uid, $oldBonus, $bonusFinal, $oldBonus + $bonusFinal, "", BonusLogs::BUSINESS_TYPE_CLAIMED_REACHED);
$oldBonus += $bonusFinal;
2022-05-05 22:19:48 +08:00
2022-05-06 15:50:26 +08:00
//Handle unreached
if (!empty($unReachedIdArr)) {
Claim::query()->whereIn('id', $unReachedIdArr)->delete();
2026-01-07 01:20:44 +07:00
$delta -= $totalDeduct;
do_log("Deduct user: $uid bonus: $totalDeduct", 'alert');
BonusLogs::add($uid, $oldBonus, $totalDeduct, $oldBonus - $totalDeduct, "", BonusLogs::BUSINESS_TYPE_CLAIMED_UNREACHED);
$oldBonus -= $totalDeduct;
2022-05-06 15:50:26 +08:00
}
2026-01-07 01:20:44 +07:00
User::query()->where('id', $uid)->increment('seedbonus', $delta);
2022-05-05 22:19:48 +08:00
2022-05-06 15:50:26 +08:00
//Update claim `last_settle_at` and init `seed_time_begin` & `uploaded_begin`
2022-07-04 04:03:11 +08:00
if (!empty($toUpdateIdArr)) {
$sql = sprintf(
"update claims set uploaded_begin = case id %s end, seed_time_begin = case id %s end, last_settle_at = '%s', updated_at = '%s' where id in (%s)",
implode(' ', $uploadedCaseWhen), implode(' ', $seedTimeCaseWhen), $now->toDateTimeString(), $now->toDateTimeString(), implode(',', $toUpdateIdArr)
);
$affectedRows = DB::update($sql);
do_log("query: $sql, affectedRows: $affectedRows");
}
2022-05-05 22:19:48 +08:00
2022-05-06 15:50:26 +08:00
//Send message
2023-02-08 13:33:25 +08:00
Message::add($message);
2022-05-06 15:50:26 +08:00
});
2023-07-04 01:16:06 +08:00
if (!empty($toDelClaimId)) {
do_log("del claim: %s", json_encode($toDelClaimId));
Claim::query()->whereIn("id", array_keys($toDelClaimId))->delete();
}
2022-05-06 15:50:26 +08:00
do_log("[DONE], cost time: " . (time() - $now->timestamp) . " seconds");
return true;
}
private function buildMessage(
User $user, $reachedTorrentIdArr, $unReachedTorrentIdArr, $remainTorrentIdArr,
$bonusResult, $bonusFinal, $seedTimeHoursAvg, $deductPerTorrent, $deductTotal
) {
$now = Carbon::now();
$allTorrentIdArr = array_merge($reachedTorrentIdArr, $unReachedTorrentIdArr, $remainTorrentIdArr);
2023-08-15 03:35:13 +08:00
//这里不使用占位符,在 $allTorrentIdArr 过大超过3000时容易结果为空且不报异常
2022-05-05 22:19:48 +08:00
$torrentInfo = Torrent::query()
2023-08-15 03:35:13 +08:00
->whereRaw(sprintf("id in (%s)", implode(',', $allTorrentIdArr)))
2022-05-05 22:19:48 +08:00
->get(Torrent::$commentFields)
->keyBy('id')
;
$msg = [];
$locale = $user->locale;
2022-05-06 15:50:26 +08:00
do_log("build message, user: {$user->id}, locale: $locale");
2022-05-05 22:19:48 +08:00
$msg[] = nexus_trans('claim.msg_title', ['month' => $now->clone()->subMonths(1)->format('Y-m')], $locale);
2022-05-06 15:50:26 +08:00
$msg[] = nexus_trans('claim.claim_total', [ 'total' => count($allTorrentIdArr)], $locale);
2022-05-05 22:19:48 +08:00
//列表数据只取部分展示
2025-05-16 14:19:06 +07:00
$sliceCount = self::SETTLE_MSG_SLICE_COUNT;
$sliceTip = "... (" . nexus_trans('claim.slice_tip', ['slice_count' => $sliceCount], $locale) . ")";
$reachPart = nexus_trans("claim.claim_reached_counts", ['counts' => count($reachedTorrentIdArr), 'slice_count' => $sliceCount], $locale);
if (!empty($reachedTorrentIdArr)) {
$reachList = collect(array_slice($reachedTorrentIdArr, 0, $sliceCount))->map(
fn($item) => sprintf("[url=details.php?id=%s]%s[/url]", $item, $torrentInfo->get($item)->name)
)->implode("\n");
$reachPart .= sprintf("\n%s\n%s", $reachList, $sliceTip);
}
$msg[] = $reachPart;
2022-05-05 22:19:48 +08:00
$msg[] = nexus_trans(
"claim.claim_reached_summary", [
'bonus_per_hour' => number_format($bonusResult['seed_bonus'], 2),
'hours'=> number_format($seedTimeHoursAvg, 2),
'bonus_total'=> number_format($bonusFinal, 2)
], $locale
2022-05-05 22:19:48 +08:00
);
2025-05-16 14:19:06 +07:00
$remainPart = nexus_trans("claim.claim_unreached_remain_counts", ['counts' => count($remainTorrentIdArr), 'slice_count' => $sliceCount], $locale);
if (!empty($remainTorrentIdArr)) {
$remainList = collect(array_slice($remainTorrentIdArr, 0, $sliceCount))->map(
fn($item) => sprintf("[url=details.php?id=%s]%s[/url]", $item, $torrentInfo->get($item)->name)
)->implode("\n");
$remainPart .= sprintf("\n%s\n%s", $remainList, $sliceTip);
}
$msg[] = $remainPart;
$removePart = nexus_trans("claim.claim_unreached_remove_counts", ['counts' => count($unReachedTorrentIdArr), 'slice_count' => $sliceCount], $locale);
if (!empty($unReachedTorrentIdArr)) {
$unReachList = collect(array_slice($unReachedTorrentIdArr, 0, $sliceCount))->map(
fn($item) => sprintf("[url=details.php?id=%s]%s[/url]", $item, $torrentInfo->get($item)->name)
)->implode("\n");
$removePart .= sprintf("\n%s\n%s", $unReachList, $sliceTip);
}
$msg[] = $removePart;
2022-05-05 22:19:48 +08:00
2022-05-06 15:50:26 +08:00
if ($deductTotal) {
2022-05-05 22:19:48 +08:00
$msg[] = nexus_trans(
"claim.claim_unreached_summary", [
2022-07-02 15:08:23 +08:00
'deduct_per_torrent' => number_format($deductPerTorrent, 2),
'deduct_total' => number_format($deductTotal, 2)
], $locale
2022-05-05 22:19:48 +08:00
);
}
2022-05-06 15:50:26 +08:00
return [
'receiver' => $user->id,
2022-05-05 22:19:48 +08:00
'added' => $now,
'subject' => nexus_trans('claim.msg_subject', ['month' => $now->clone()->subMonths(1)->format('Y-m')], $locale),
'msg' => implode("\n\n", $msg),
];
}
2022-09-05 19:23:10 +08:00
private function handleClaimSettlement(
int $uid, Collection $list, Carbon $startOfThisMonth, int $seedTimeRequiredHours, int $uploadedRequiredTimes,
array &$reachedTorrentIdArr, array &$unReachedTorrentIdArr, array &$remainTorrentIdArr, array &$seedTimeCaseWhen, array &$uploadedCaseWhen,
array &$toUpdateIdArr, array &$unReachedIdArr, array &$toDelIdArr, int &$totalSeedTime, bool $force
): bool|null
{
foreach ($list as $row) {
$logItem = "uid: $uid, claimId: {$row['id']}";
if ($row->last_settle_at && $row->last_settle_at->gte($startOfThisMonth)) {
do_log("$logItem already settle", 'alert');
if (!$force) {
do_log("$logItem,already settle, No force, return", 'alert');
return false;
}
}
if (!$row->snatch) {
$toDelIdArr[$row->id] = $row->id;
do_log("$logItem, No snatch, continue", 'alert');
continue;
}
if (!$row->torrent) {
$toDelIdArr[$row->id] = $row->id;
do_log("$logItem, No torrent, continue", 'alert');
continue;
}
if (
bcsub($row->snatch->seedtime, $row->seed_time_begin) >= $seedTimeRequiredHours * 3600
|| bcsub($row->snatch->uploaded, $row->uploaded_begin) >= $uploadedRequiredTimes * $row->torrent->size
) {
do_log("$logItem, [REACHED], uid: $uid, torrent: " . $row->torrent_id);
$reachedTorrentIdArr[] = $row->torrent_id;
$toUpdateIdArr[] = $row->id;
$totalSeedTime += (int)bcsub($row->snatch->seedtime, $row->seed_time_begin);
$seedTimeCaseWhen[] = sprintf('when %s then %s', $row->id, $row->snatch->seedtime);
$uploadedCaseWhen[] = sprintf('when %s then %s', $row->id, $row->snatch->uploaded);
} else {
$targetStartOfMonth = $row->created_at->startOfMonth();
if ($startOfThisMonth->diffInMonths($targetStartOfMonth, true) > 1) {
do_log("$logItem, [UNREACHED_REMOVE], uid: $uid, torrent: " . $row->torrent_id);
$unReachedIdArr[] = $row->id;
$unReachedTorrentIdArr[] = $row->torrent_id;
} else {
do_log("$logItem, [UNREACHED_FIRST_MONTH], uid: $uid, torrent: " . $row->torrent_id);
$seedTimeCaseWhen[] = sprintf('when %s then %s', $row->id, $row->snatch->seedtime);
$uploadedCaseWhen[] = sprintf('when %s then %s', $row->id, $row->snatch->uploaded);
$toUpdateIdArr[] = $row->id;
$remainTorrentIdArr[] = $row->torrent_id;
}
}
}
return null;
}
2022-09-05 19:23:10 +08:00
public function buildActionButtons($torrentId, $claimData, $reload = 0): string
{
$buttonHtml = '<button data-action="%s" data-reload="%s" data-confirm="%s" data-claim_id="%s" data-torrent_id="%s" style="width: max-content;display: %s;align-items: center"><img style="margin-right: 4px;" class="staff_%s" src="pic/trans.gif">%s</button>';
$addButton = sprintf($buttonHtml, 'addClaim', $reload, nexus_trans('claim.add_claim_confirm'), $claimData ? $claimData->id : 0, $torrentId, '%s', 'edit', nexus_trans('claim.add_claim'));
$removeButton = sprintf($buttonHtml, 'removeClaim', $reload, nexus_trans('claim.remove_claim_confirm'), $claimData ? $claimData->id : 0, $torrentId, '%s', 'delete', nexus_trans('claim.remove_claim'));
if ($claimData) {
//Only show remove
return sprintf($addButton, 'none') . sprintf($removeButton, 'flex');
} else {
//Only show add
return sprintf($addButton, 'flex') . sprintf($removeButton, 'none');
}
}
2023-03-04 00:52:08 +08:00
2023-04-09 16:33:36 +08:00
2022-05-05 22:19:48 +08:00
}