paid torrent

This commit is contained in:
xiaomlove
2023-02-11 16:08:48 +08:00
parent 43b35b4e77
commit 3c64e76011
50 changed files with 556 additions and 87 deletions

View File

@@ -8,6 +8,8 @@ use App\Models\Invite;
use App\Models\Medal;
use App\Models\Message;
use App\Models\Setting;
use App\Models\Torrent;
use App\Models\TorrentBuyLog;
use App\Models\User;
use App\Models\UserMedal;
use App\Models\UserMeta;
@@ -250,6 +252,57 @@ class BonusRepository extends BaseRepository
}
public function consumeToBuyTorrent($uid, $torrentId, $channel = 'Web'): bool
{
$user = User::query()->findOrFail($uid);
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
$requireBonus = $torrent->price;
NexusDB::transaction(function () use ($user, $requireBonus, $torrent, $channel) {
$comment = nexus_trans('bonus.comment_buy_torrent', [
'bonus' => $requireBonus,
'torrent_id' => $torrent->id,
], $user->locale);
do_log("comment: $comment");
$this->consumeUserBonus($user, $requireBonus, BonusLogs::BUSINESS_TYPE_BUY_TORRENT, $comment);
TorrentBuyLog::query()->create([
'uid' => $user->id,
'torrent_id' => $torrent->id,
'price' => $requireBonus,
'channel' => $channel,
]);
//increment owner bonus
$taxFactor = Setting::get('torrent.tax_factor');
if (!is_numeric($taxFactor) || $taxFactor < 0 || $taxFactor > 1) {
throw new \RuntimeException("Invalid tax_factor: $taxFactor");
}
$increaseBonus = $requireBonus * (1 - $taxFactor);
$owner = $torrent->user;
if ($owner->id) {
$nowStr = now()->toDateTimeString();
$businessType = BonusLogs::BUSINESS_TYPE_TORRENT_BE_DOWNLOADED;
$owner->increment('seedbonus', $increaseBonus);
$comment = nexus_trans('bonus.comment_torrent_be_downloaded', [
'username' => $user->username,
'uid' => $user->id,
], $owner->locale);
$bonusLog = [
'business_type' => $businessType,
'uid' => $owner->id,
'old_total_value' => $owner->seedbonus,
'value' => $increaseBonus,
'new_total_value' => bcadd($owner->seedbonus, $increaseBonus),
'comment' => sprintf('[%s] %s', BonusLogs::$businessTypes[$businessType]['text'], $comment),
'created_at' => $nowStr,
'updated_at' => $nowStr,
];
BonusLogs::query()->insert($bonusLog);
}
});
return true;
}
public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logComment = '', array $userUpdates = [])
{
if (!isset(BonusLogs::$businessTypes[$logBusinessType])) {