mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-03 14:10:57 +08:00
bonus logs show category seeding and common
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Filament\Resources\User;
|
||||
|
||||
use App\Repositories\BonusRepository;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
@@ -9,7 +10,6 @@ use Filament\Forms\Components\TextInput;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use App\Filament\Resources\User\BonusLogResource\Pages\ManageBonusLogs;
|
||||
use App\Filament\Resources\User\BonusLogResource\Pages;
|
||||
use App\Filament\Resources\User\BonusLogResource\RelationManagers;
|
||||
use App\Models\BonusLogs;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\Resource;
|
||||
@@ -17,6 +17,7 @@ use Filament\Tables\Table;
|
||||
use Filament\Tables;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Arr;
|
||||
use function Filament\Support\get_model_label;
|
||||
|
||||
@@ -51,8 +52,10 @@ class BonusLogResource extends Resource
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->records(function (int $page, int $recordsPerPage, array $filters) {
|
||||
return self::listRecords($page, $recordsPerPage, $filters);
|
||||
})
|
||||
->columns([
|
||||
TextColumn::make('id')->sortable(),
|
||||
TextColumn::make('uid')
|
||||
->formatStateUsing(fn ($state) => username_for_admin($state))
|
||||
->label(__('label.username'))
|
||||
@@ -79,8 +82,18 @@ class BonusLogResource extends Resource
|
||||
->label(__('label.created_at'))
|
||||
,
|
||||
])
|
||||
->defaultSort('id', 'desc')
|
||||
->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')
|
||||
@@ -91,21 +104,6 @@ class BonusLogResource extends Resource
|
||||
return $query->when($data['uid'], fn (Builder $query, $value) => $query->where("uid", $value));
|
||||
})
|
||||
,
|
||||
SelectFilter::make('business_type')
|
||||
->options(BonusLogs::listBusinessTypeOptions(BonusLogs::CATEGORY_COMMON))
|
||||
->label(__('bonus-log.fields.business_type'))
|
||||
->searchable(true)
|
||||
,
|
||||
// Tables\Filters\Filter::make('exclude_seeding_bonus')
|
||||
// ->toggle()
|
||||
// ->label(__('bonus-log.exclude_seeding_bonus'))
|
||||
// ->query(function (Builder $query, array $data) {
|
||||
// if ($data['isActive']) {
|
||||
// $query->whereNotIn("business_type", BonusLogs::$businessTypeBonus);
|
||||
// }
|
||||
// })
|
||||
// ->default()
|
||||
// ,
|
||||
])
|
||||
->recordActions([
|
||||
// Tables\Actions\EditAction::make(),
|
||||
@@ -122,4 +120,15 @@ class BonusLogResource extends Resource
|
||||
'index' => ManageBonusLogs::route('/'),
|
||||
];
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,51 +363,71 @@ class BonusRepository extends BaseRepository
|
||||
});
|
||||
}
|
||||
|
||||
public function getCount(int $userId, string $category, int $businessType = 0): int
|
||||
public function getCount(string $category = '', int $userId = 0, int $businessType = 0): int
|
||||
{
|
||||
if ($category == BonusLogs::CATEGORY_COMMON) {
|
||||
$query = BonusLogs::query()->where('uid', $userId);
|
||||
if ($businessType > 0) {
|
||||
$query->where('business_type', $businessType);
|
||||
}
|
||||
$query = $this->buildQuery($userId, $businessType);
|
||||
return $query->count();
|
||||
} else if ($category == BonusLogs::CATEGORY_SEEDING) {
|
||||
$whereStr = "uid = :uid";
|
||||
$binds = ["uid" => $userId];
|
||||
if ($businessType > 0) {
|
||||
$whereStr .= " AND business_type = :business_type";
|
||||
$binds["business_type"] = $businessType;
|
||||
}
|
||||
list($whereStr, $binds) = $this->buildWhereStrAndBinds($userId, $businessType);
|
||||
return ClickHouse::count("bonus_logs", $whereStr, $binds);
|
||||
}
|
||||
throw new \InvalidArgumentException("Invalid category: $category");
|
||||
}
|
||||
|
||||
public function getList(int $userId, string $category, int $businessType = 0, int $page = 1, int $perPage = 50)
|
||||
public function getList(string $category = '', int $userId = 0, int $businessType = 0, int $page = 1, int $perPage = 50)
|
||||
{
|
||||
if ($category == BonusLogs::CATEGORY_COMMON) {
|
||||
$query = BonusLogs::query()->where('uid', $userId);
|
||||
if ($businessType > 0) {
|
||||
$query->where('business_type', $businessType);
|
||||
}
|
||||
$query = $this->buildQuery($userId, $businessType);
|
||||
return $query->orderBy("id", "desc")->forPage($page, $perPage)->get();
|
||||
} else if ($category == BonusLogs::CATEGORY_SEEDING) {
|
||||
$sql = "select * from bonus_logs where uid = :uid";
|
||||
$binds = ["uid" => $userId];
|
||||
if ($businessType > 0) {
|
||||
$sql .= " AND business_type = :business_type";
|
||||
$binds["business_type"] = $businessType;
|
||||
}
|
||||
list($whereStr, $binds) = $this->buildWhereStrAndBinds($userId, $businessType);
|
||||
$offset = ($page - 1) * $perPage;
|
||||
$rows = ClickHouse::list("$sql order by created_at desc limit $offset, $perPage", $binds);
|
||||
$rows = ClickHouse::list("select * from bonus_logs $whereStr order by created_at desc limit $offset, $perPage", $binds);
|
||||
$result = [];
|
||||
$id = 1;//fake id
|
||||
foreach ($rows as $row) {
|
||||
$result[] = new BonusLogs($row);
|
||||
$record = new BonusLogs($row);
|
||||
$record->id = $id;
|
||||
$result[] = $record;
|
||||
$id++;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
throw new \InvalidArgumentException("Invalid category: $category");
|
||||
}
|
||||
|
||||
private function buildWhereStrAndBinds(int $userId = 0, int $businessType = 0)
|
||||
{
|
||||
$whereArr = [];
|
||||
$binds = [];
|
||||
if ($userId > 0) {
|
||||
$whereArr[] = "uid = :uid";
|
||||
$binds['uid'] = $userId;
|
||||
}
|
||||
if ($businessType > 0) {
|
||||
$whereArr[] = "business_type = :business_type";
|
||||
$binds["business_type"] = $businessType;
|
||||
}
|
||||
if (empty($whereArr)) {
|
||||
$whereStr = "";
|
||||
} else {
|
||||
$whereStr = sprintf("where %s", implode(' AND ', $whereArr));
|
||||
}
|
||||
return [$whereStr, $binds];
|
||||
}
|
||||
|
||||
private function buildQuery(int $userId = 0, int $businessType = 0): Builder
|
||||
{
|
||||
$query = BonusLogs::query();
|
||||
if ($userId > 0) {
|
||||
$query->where('uid', $userId);
|
||||
}
|
||||
if ($businessType > 0) {
|
||||
$query->where('business_type', $businessType);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user