Skip to content

Commit 1e997e4

Browse files
committed
bonus logs show category seeding and common
1 parent fb440c6 commit 1e997e4

File tree

2 files changed

+71
-42
lines changed

2 files changed

+71
-42
lines changed

app/Filament/Resources/User/BonusLogResource.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
namespace App\Filament\Resources\User;
44

5+
use App\Repositories\BonusRepository;
56
use Filament\Schemas\Schema;
67
use Filament\Tables\Columns\TextColumn;
78
use Filament\Tables\Filters\Filter;
89
use Filament\Forms\Components\TextInput;
910
use Filament\Tables\Filters\SelectFilter;
1011
use App\Filament\Resources\User\BonusLogResource\Pages\ManageBonusLogs;
1112
use App\Filament\Resources\User\BonusLogResource\Pages;
12-
use App\Filament\Resources\User\BonusLogResource\RelationManagers;
1313
use App\Models\BonusLogs;
1414
use Filament\Forms;
1515
use Filament\Resources\Resource;
1616
use Filament\Tables\Table;
1717
use Filament\Tables;
1818
use Illuminate\Database\Eloquent\Builder;
1919
use Illuminate\Database\Eloquent\SoftDeletingScope;
20+
use Illuminate\Pagination\LengthAwarePaginator;
2021
use Illuminate\Support\Arr;
2122
use function Filament\Support\get_model_label;
2223

@@ -51,8 +52,10 @@ public static function form(Schema $schema): Schema
5152
public static function table(Table $table): Table
5253
{
5354
return $table
55+
->records(function (int $page, int $recordsPerPage, array $filters) {
56+
return self::listRecords($page, $recordsPerPage, $filters);
57+
})
5458
->columns([
55-
TextColumn::make('id')->sortable(),
5659
TextColumn::make('uid')
5760
->formatStateUsing(fn ($state) => username_for_admin($state))
5861
->label(__('label.username'))
@@ -79,8 +82,18 @@ public static function table(Table $table): Table
7982
->label(__('label.created_at'))
8083
,
8184
])
82-
->defaultSort('id', 'desc')
8385
->filters([
86+
SelectFilter::make('category')
87+
->options(BonusLogs::listCategoryOptions(true))
88+
->default(BonusLogs::CATEGORY_COMMON)
89+
->selectablePlaceholder(false)
90+
->label(__('bonus-log.category'))
91+
,
92+
SelectFilter::make('business_type')
93+
->options(BonusLogs::listBusinessTypeOptions())
94+
->label(__('bonus-log.fields.business_type'))
95+
->searchable(true)
96+
,
8497
Filter::make('uid')
8598
->schema([
8699
TextInput::make('uid')
@@ -91,21 +104,6 @@ public static function table(Table $table): Table
91104
return $query->when($data['uid'], fn (Builder $query, $value) => $query->where("uid", $value));
92105
})
93106
,
94-
SelectFilter::make('business_type')
95-
->options(BonusLogs::listBusinessTypeOptions(BonusLogs::CATEGORY_COMMON))
96-
->label(__('bonus-log.fields.business_type'))
97-
->searchable(true)
98-
,
99-
// Tables\Filters\Filter::make('exclude_seeding_bonus')
100-
// ->toggle()
101-
// ->label(__('bonus-log.exclude_seeding_bonus'))
102-
// ->query(function (Builder $query, array $data) {
103-
// if ($data['isActive']) {
104-
// $query->whereNotIn("business_type", BonusLogs::$businessTypeBonus);
105-
// }
106-
// })
107-
// ->default()
108-
// ,
109107
])
110108
->recordActions([
111109
// Tables\Actions\EditAction::make(),
@@ -122,4 +120,15 @@ public static function getPages(): array
122120
'index' => ManageBonusLogs::route('/'),
123121
];
124122
}
123+
124+
private static function listRecords(int $page, int $perPage, array $filters = []): LengthAwarePaginator
125+
{
126+
$rep = new BonusRepository();
127+
$category = $filters['category']['value'] ?: BonusLogs::CATEGORY_COMMON;
128+
$userId = intval($filters['userId']['value'] ?? 0);
129+
$businessType = intval($filters['businessType']['value'] ?? 0);
130+
$list = $rep->getList($category, $userId, $businessType, $page, $perPage);
131+
$count = $rep->getCount($category, $userId, $businessType);
132+
return new LengthAwarePaginator($list, $count, $perPage, $page);
133+
}
125134
}

app/Repositories/BonusRepository.php

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -363,51 +363,71 @@ public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logCom
363363
});
364364
}
365365

366-
public function getCount(int $userId, string $category, int $businessType = 0): int
366+
public function getCount(string $category = '', int $userId = 0, int $businessType = 0): int
367367
{
368368
if ($category == BonusLogs::CATEGORY_COMMON) {
369-
$query = BonusLogs::query()->where('uid', $userId);
370-
if ($businessType > 0) {
371-
$query->where('business_type', $businessType);
372-
}
369+
$query = $this->buildQuery($userId, $businessType);
373370
return $query->count();
374371
} else if ($category == BonusLogs::CATEGORY_SEEDING) {
375-
$whereStr = "uid = :uid";
376-
$binds = ["uid" => $userId];
377-
if ($businessType > 0) {
378-
$whereStr .= " AND business_type = :business_type";
379-
$binds["business_type"] = $businessType;
380-
}
372+
list($whereStr, $binds) = $this->buildWhereStrAndBinds($userId, $businessType);
381373
return ClickHouse::count("bonus_logs", $whereStr, $binds);
382374
}
383375
throw new \InvalidArgumentException("Invalid category: $category");
384376
}
385377

386-
public function getList(int $userId, string $category, int $businessType = 0, int $page = 1, int $perPage = 50)
378+
public function getList(string $category = '', int $userId = 0, int $businessType = 0, int $page = 1, int $perPage = 50)
387379
{
388380
if ($category == BonusLogs::CATEGORY_COMMON) {
389-
$query = BonusLogs::query()->where('uid', $userId);
390-
if ($businessType > 0) {
391-
$query->where('business_type', $businessType);
392-
}
381+
$query = $this->buildQuery($userId, $businessType);
393382
return $query->orderBy("id", "desc")->forPage($page, $perPage)->get();
394383
} else if ($category == BonusLogs::CATEGORY_SEEDING) {
395-
$sql = "select * from bonus_logs where uid = :uid";
396-
$binds = ["uid" => $userId];
397-
if ($businessType > 0) {
398-
$sql .= " AND business_type = :business_type";
399-
$binds["business_type"] = $businessType;
400-
}
384+
list($whereStr, $binds) = $this->buildWhereStrAndBinds($userId, $businessType);
401385
$offset = ($page - 1) * $perPage;
402-
$rows = ClickHouse::list("$sql order by created_at desc limit $offset, $perPage", $binds);
386+
$rows = ClickHouse::list("select * from bonus_logs $whereStr order by created_at desc limit $offset, $perPage", $binds);
403387
$result = [];
388+
$id = 1;//fake id
404389
foreach ($rows as $row) {
405-
$result[] = new BonusLogs($row);
390+
$record = new BonusLogs($row);
391+
$record->id = $id;
392+
$result[] = $record;
393+
$id++;
406394
}
407395
return $result;
408396
}
409397
throw new \InvalidArgumentException("Invalid category: $category");
410398
}
411399

400+
private function buildWhereStrAndBinds(int $userId = 0, int $businessType = 0)
401+
{
402+
$whereArr = [];
403+
$binds = [];
404+
if ($userId > 0) {
405+
$whereArr[] = "uid = :uid";
406+
$binds['uid'] = $userId;
407+
}
408+
if ($businessType > 0) {
409+
$whereArr[] = "business_type = :business_type";
410+
$binds["business_type"] = $businessType;
411+
}
412+
if (empty($whereArr)) {
413+
$whereStr = "";
414+
} else {
415+
$whereStr = sprintf("where %s", implode(' AND ', $whereArr));
416+
}
417+
return [$whereStr, $binds];
418+
}
419+
420+
private function buildQuery(int $userId = 0, int $businessType = 0): Builder
421+
{
422+
$query = BonusLogs::query();
423+
if ($userId > 0) {
424+
$query->where('uid', $userId);
425+
}
426+
if ($businessType > 0) {
427+
$query->where('business_type', $businessType);
428+
}
429+
return $query;
430+
}
431+
412432

413433
}

0 commit comments

Comments
 (0)