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

@@ -76,6 +76,12 @@ class MedalResource extends Resource
->numeric()
->default(0)
,
Forms\Components\TextInput::make('gift_fee_factor')
->label(__('medal.fields.gift_fee_factor'))
->helperText(__('medal.fields.gift_fee_factor_help'))
->numeric()
->default(0)
,
Forms\Components\Textarea::make('description')
->label(__('label.description'))
,
@@ -96,6 +102,7 @@ class MedalResource extends Resource
->formatStateUsing(fn ($record) => new HtmlString(sprintf('%s ~<br/>%s', $record->sale_begin_time ?? '--', $record->sale_end_time ?? '--')))
,
Tables\Columns\TextColumn::make('bonus_addition_factor')->label(__('medal.fields.bonus_addition_factor')),
Tables\Columns\TextColumn::make('gift_fee_factor')->label(__('medal.fields.gift_fee_factor')),
Tables\Columns\TextColumn::make('price')->label(__('label.price')),
Tables\Columns\TextColumn::make('duration')->label(__('label.medal.duration')),

View File

@@ -32,6 +32,7 @@ class BonusLogs extends NexusModel
const BUSINESS_TYPE_BUY_TEMPORARY_INVITE = 15;
const BUSINESS_TYPE_BUY_RAINBOW_ID = 16;
const BUSINESS_TYPE_BUY_CHANGE_USERNAME_CARD = 17;
const BUSINESS_TYPE_GIFT_MEDAL = 18;
public static array $businessTypes = [
self::BUSINESS_TYPE_CANCEL_HIT_AND_RUN => ['text' => 'Cancel H&R'],
@@ -51,6 +52,7 @@ class BonusLogs extends NexusModel
self::BUSINESS_TYPE_BUY_TEMPORARY_INVITE => ['text' => 'Buy temporary invite'],
self::BUSINESS_TYPE_BUY_RAINBOW_ID => ['text' => 'Buy rainbow ID'],
self::BUSINESS_TYPE_BUY_CHANGE_USERNAME_CARD => ['text' => 'Buy change username card'],
self::BUSINESS_TYPE_GIFT_MEDAL => ['text' => 'Gift medal to someone'],
];
public static function getBonusForCancelHitAndRun()

View File

@@ -18,6 +18,7 @@ class Medal extends NexusModel
protected $fillable = [
'name', 'description', 'image_large', 'image_small', 'price', 'duration', 'get_type',
'display_on_medal_page', 'sale_begin_time', 'sale_end_time', 'inventory', 'bonus_addition_factor',
'gift_fee_factor',
];
public $timestamps = true;
@@ -55,6 +56,23 @@ class Medal extends NexusModel
return nexus_trans("label.permanent");
}
public function checkCanBeBuy()
{
if ($this->get_type == self::GET_TYPE_GRANT) {
throw new \RuntimeException(nexus_trans('medal.grant_only'));
}
$now = now();
if ($this->sale_begin_time && $this->sale_begin_time->gt($now)) {
throw new \RuntimeException(nexus_trans('medal.before_sale_begin_time'));
}
if ($this->sale_end_time && $this->sale_end_time->lt($now)) {
throw new \RuntimeException(nexus_trans('medal.after_sale_end_time'));
}
if ($this->inventory !== null && $this->inventory <= 0) {
throw new \RuntimeException(nexus_trans('medal.inventory_empty'));
}
}
public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(User::class, 'user_medals', 'medal_id', 'uid')->withTimestamps();

View File

@@ -459,9 +459,9 @@ class User extends Authenticatable implements FilamentUser, HasName
public function medals(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Medal::class, 'user_medals', 'uid', 'medal_id')
->withPivot(['id', 'expire_at', 'status'])
->withPivot(['id', 'expire_at', 'status', 'priority'])
->withTimestamps()
->orderByPivot('id', 'desc')
->orderByPivot('priority', 'desc')
;
}

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);
}
}