Files
nexusphp/app/Filament/Resources/User/BonusLogResource.php

135 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2023-01-31 16:38:21 +08:00
<?php
namespace App\Filament\Resources\User;
use App\Repositories\BonusRepository;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Filters\SelectFilter;
use App\Filament\Resources\User\BonusLogResource\Pages\ManageBonusLogs;
2023-01-31 16:38:21 +08:00
use App\Filament\Resources\User\BonusLogResource\Pages;
use App\Models\BonusLogs;
use Filament\Forms;
use Filament\Resources\Resource;
2024-12-25 23:09:07 +08:00
use Filament\Tables\Table;
2023-01-31 16:38:21 +08:00
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Pagination\LengthAwarePaginator;
2025-09-19 21:02:34 +07:00
use Illuminate\Support\Arr;
use function Filament\Support\get_model_label;
2023-01-31 16:38:21 +08:00
class BonusLogResource extends Resource
{
protected static ?string $model = BonusLogs::class;
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';
2023-01-31 16:38:21 +08:00
protected static string | \UnitEnum | null $navigationGroup = 'User';
2023-01-31 16:38:21 +08:00
protected static ?int $navigationSort = 10;
2024-12-25 23:09:07 +08:00
public static function getNavigationLabel(): string
2023-01-31 16:38:21 +08:00
{
return __('admin.sidebar.bonus_log');
}
2026-01-31 20:06:43 +07:00
// public static function getModelLabel(): string
// {
// return sprintf('%s(%s)', get_model_label(static::getModel()), __('bonus-log.exclude_seeding_bonus'));
// }
2023-01-31 16:38:21 +08:00
public static function form(Schema $schema): Schema
2023-01-31 16:38:21 +08:00
{
return $schema
->components([
2023-01-31 16:38:21 +08:00
//
]);
}
public static function table(Table $table): Table
{
return $table
->records(function (int $page, int $recordsPerPage, array $filters) {
return self::listRecords($page, $recordsPerPage, $filters);
})
2023-01-31 16:38:21 +08:00
->columns([
TextColumn::make('uid')
2023-01-31 16:38:21 +08:00
->formatStateUsing(fn ($state) => username_for_admin($state))
->label(__('label.username'))
,
TextColumn::make('business_type_text')
2023-01-31 16:38:21 +08:00
->label(__('bonus-log.fields.business_type'))
,
TextColumn::make('old_total_value')
2023-01-31 16:38:21 +08:00
->label(__('bonus-log.fields.old_total_value'))
2025-09-08 03:05:55 +07:00
->formatStateUsing(fn ($state) => $state >= 0 ? number_format($state) : '-')
2023-01-31 16:38:21 +08:00
,
TextColumn::make('value')
2023-02-08 15:08:07 +08:00
->formatStateUsing(fn ($record) => $record->old_total_value > $record->new_total_value ? "-" . number_format($record->value) : "+" . number_format($record->value))
2023-01-31 16:38:21 +08:00
->label(__('bonus-log.fields.value'))
,
TextColumn::make('new_total_value')
2023-01-31 16:38:21 +08:00
->label(__('bonus-log.fields.new_total_value'))
2025-09-08 03:05:55 +07:00
->formatStateUsing(fn ($state) => $state >= 0 ? number_format($state) : '-')
2023-01-31 16:38:21 +08:00
,
TextColumn::make('comment')
2023-01-31 16:38:21 +08:00
->label(__('label.comment'))
,
TextColumn::make('created_at')
2023-01-31 16:38:21 +08:00
->label(__('label.created_at'))
,
])
->filters([
SelectFilter::make('category')
->options(BonusLogs::listCategoryOptions(true))
->default(BonusLogs::CATEGORY_COMMON)
->selectablePlaceholder(false)
->label(__('bonus-log.category'))
,
SelectFilter::make('business_type')
->options(BonusLogs::listBusinessTypeOptions())
->label(__('bonus-log.fields.business_type'))
->searchable(true)
,
Filter::make('uid')
->schema([
TextInput::make('uid')
2023-01-31 16:38:21 +08:00
->label(__('label.username'))
->placeholder('UID')
,
])->query(function (Builder $query, array $data) {
return $query->when($data['uid'], fn (Builder $query, $value) => $query->where("uid", $value));
})
,
])
->recordActions([
2023-01-31 16:38:21 +08:00
// Tables\Actions\EditAction::make(),
// Tables\Actions\DeleteAction::make(),
])
->toolbarActions([
2023-01-31 16:38:21 +08:00
// Tables\Actions\DeleteBulkAction::make(),
]);
}
public static function getPages(): array
{
return [
'index' => ManageBonusLogs::route('/'),
2023-01-31 16:38:21 +08:00
];
}
private static function listRecords(int $page, int $perPage, array $filters = []): LengthAwarePaginator
{
$rep = new BonusRepository();
$category = $filters['category']['value'] ?: BonusLogs::CATEGORY_COMMON;
$userId = intval($filters['userId']['value'] ?? 0);
$businessType = intval($filters['businessType']['value'] ?? 0);
$list = $rep->getList($category, $userId, $businessType, $page, $perPage);
$count = $rep->getCount($category, $userId, $businessType);
return new LengthAwarePaginator($list, $count, $perPage, $page);
}
2023-01-31 16:38:21 +08:00
}