mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-05 07:20:58 +08:00
add medal max allow wear number
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Exceptions\NexusException;
|
||||
use App\Models\Medal;
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Models\UserMedal;
|
||||
use Carbon\Carbon;
|
||||
@@ -99,17 +101,23 @@ class MedalRepository extends BaseRepository
|
||||
return true;
|
||||
}
|
||||
$statusCaseWhens = $priorityCaseWhens = $idArr = [];
|
||||
$wearCount = 0;
|
||||
foreach ($validMedals as $medal) {
|
||||
$id = $medal->pivot->id;
|
||||
$idArr[] = $id;
|
||||
if (isset($userMedalData[$id]['status'])) {
|
||||
$status = UserMedal::STATUS_WEARING;
|
||||
$wearCount++;
|
||||
} 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);
|
||||
}
|
||||
$maxWearAllow = Setting::get('system.maximum_number_of_medals_can_be_worn');
|
||||
if ($maxWearAllow && $wearCount > $maxWearAllow) {
|
||||
throw new NexusException(nexus_trans('medal.max_allow_wearing', ['count' => $maxWearAllow]));
|
||||
}
|
||||
$sql = sprintf(
|
||||
'update user_medals set `status` = case %s end, `priority` = case %s end where id in (%s)',
|
||||
implode(' ', $statusCaseWhens), implode(' ', $priorityCaseWhens), implode(',', $idArr)
|
||||
|
||||
Reference in New Issue
Block a user