medal add gift_fee_factor and user_medal add priority

This commit is contained in:
xiaomlove
2023-01-29 20:00:58 +08:00
parent fb803d1989
commit 0845bca268
25 changed files with 335 additions and 55 deletions

View File

@@ -6,6 +6,7 @@ use App\Models\BonusLogs;
use App\Models\HitAndRun;
use App\Models\Invite;
use App\Models\Medal;
use App\Models\Message;
use App\Models\Setting;
use App\Models\User;
use App\Models\UserMedal;
@@ -58,19 +59,7 @@ class BonusRepository extends BaseRepository
if ($exists) {
throw new \LogicException("user: $uid already own this medal: $medalId.");
}
if ($medal->get_type != Medal::GET_TYPE_EXCHANGE) {
throw new \LogicException("This medal can not be buy.");
}
if ($medal->inventory !== null && $medal->inventory <= 0) {
throw new \LogicException("Inventory empty.");
}
$now = now();
if ($medal->sale_begin_time && $medal->sale_begin_time->gt($now)) {
throw new \LogicException("Before sale begin time.");
}
if ($medal->sale_end_time && $medal->sale_end_time->lt($now)) {
throw new \LogicException("After sale end time.");
}
$medal->checkCanBeBuy();
$requireBonus = $medal->price;
NexusDB::transaction(function () use ($user, $medal, $requireBonus) {
$comment = nexus_trans('bonus.comment_buy_medal', [
@@ -101,6 +90,67 @@ class BonusRepository extends BaseRepository
}
public function consumeToGiftMedal($uid, $medalId, $toUid): bool
{
$user = User::query()->findOrFail($uid);
$toUser = User::query()->findOrFail($toUid);
$medal = Medal::query()->findOrFail($medalId);
$exists = $toUser->valid_medals()->where('medal_id', $medalId)->exists();
do_log(last_query());
if ($exists) {
throw new \LogicException("user: $toUid already own this medal: $medalId.");
}
$medal->checkCanBeBuy();
$giftFee = $medal->price * ($medal->gift_fee_factor ?? 0);
$requireBonus = $medal->price + $giftFee;
NexusDB::transaction(function () use ($user, $toUser, $medal, $requireBonus, $giftFee) {
$comment = nexus_trans('bonus.comment_gift_medal', [
'bonus' => $requireBonus,
'medal_name' => $medal->name,
'to_username' => $toUser->username,
], $user->locale);
do_log("comment: $comment");
$this->consumeUserBonus($user, $requireBonus, BonusLogs::BUSINESS_TYPE_GIFT_MEDAL, "$comment(medal ID: {$medal->id})");
$expireAt = null;
if ($medal->duration > 0) {
$expireAt = Carbon::now()->addDays($medal->duration)->toDateTimeString();
}
$msg = [
'sender' => 0,
'receiver' => $toUser->id,
'subject' => nexus_trans('message.receive_medal.subject', [], $toUser->locale),
'msg' => nexus_trans('message.receive_medal.body', [
'username' => $user->username,
'cost_bonus' => $requireBonus,
'medal_name' => $medal->name,
'price' => $medal->price,
'gift_fee_total' => $giftFee,
'gift_fee_factor' => $medal->gift_fee_factor ?? 0,
'expire_at' => $expireAt ?? nexus_trans('label.permanent'),
'bonus_addition_factor' => $medal->bonus_addition_factor ?? 0,
], $toUser->locale),
'added' => now()
];
Message::add($msg);
$toUser->medals()->attach([$medal->id => ['expire_at' => $expireAt, 'status' => UserMedal::STATUS_NOT_WEARING]]);
if ($medal->inventory !== null) {
$affectedRows = NexusDB::table('medals')
->where('id', $medal->id)
->where('inventory', $medal->inventory)
->decrement('inventory')
;
if ($affectedRows != 1) {
throw new \RuntimeException("Decrement medal({$medal->id}) inventory affected rows != 1($affectedRows)");
}
}
});
return true;
}
public function consumeToBuyAttendanceCard($uid): bool
{
$user = User::query()->findOrFail($uid);

View File

@@ -74,7 +74,7 @@ class MedalRepository extends BaseRepository
return $user->medals()->attach([$medal->id => ['expire_at' => $expireAt, 'status' => UserMedal::STATUS_NOT_WEARING]]);
}
function toggleUserMedalStatus($id, $userId)
public function toggleUserMedalStatus($id, $userId)
{
$userMedal = UserMedal::query()->findOrFail($id);
if ($userMedal->uid != $userId) {
@@ -91,4 +91,32 @@ class MedalRepository extends BaseRepository
return $userMedal;
}
public function saveUserMedal(int $userId, array $userMedalData)
{
$user = User::query()->findOrFail($userId);
$validMedals = $user->valid_medals;
if ($validMedals->isEmpty()) {
return true;
}
$statusCaseWhens = $priorityCaseWhens = $idArr = [];
foreach ($validMedals as $medal) {
$id = $medal->pivot->id;
$idArr[] = $id;
if (isset($userMedalData[$id]['status'])) {
$status = UserMedal::STATUS_WEARING;
} else {
$status = UserMedal::STATUS_NOT_WEARING;
}
$statusCaseWhens[] = sprintf('when `id` = %s then %s', $id, $status);
$priorityCaseWhens[] = sprintf('when `id` = %s then %s', $id, $userMedalData[$id]['priority'] ?? 0);
}
$sql = sprintf(
'update user_medals set `status` = case %s end, `priority` = case %s end where id in (%s)',
implode(' ', $statusCaseWhens), implode(' ', $priorityCaseWhens), implode(',', $idArr)
);
do_log("sql: $sql");
clear_user_cache($userId);
return NexusDB::statement($sql);
}
}