medal add sale_begin_time + sale_end_time + inventory

This commit is contained in:
xiaomlove
2023-01-25 20:22:32 +08:00
parent 3762c15dc7
commit 1ca8502b91
13 changed files with 149 additions and 11 deletions

View File

@@ -61,6 +61,16 @@ class BonusRepository extends BaseRepository
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.");
}
$requireBonus = $medal->price;
NexusDB::transaction(function () use ($user, $medal, $requireBonus) {
$comment = nexus_trans('bonus.comment_buy_medal', [
@@ -74,6 +84,16 @@ class BonusRepository extends BaseRepository
$expireAt = Carbon::now()->addDays($medal->duration)->toDateTimeString();
}
$user->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)");
}
}
});