| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\Farm\Enums\DISASTER_TYPE;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Models\FarmGodBuff;
- use App\Module\Farm\Models\FarmHouseConfig;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmUser;
- use App\Module\Farm\Services\DisasterService;
- use App\Module\Fund\Models\FundModel;
- use App\Module\Fund\Services\AccountService;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use App\Module\GameItems\Models\ItemUser;
- use App\Module\Pet\Models\PetUser;
- use App\Module\Pet\Models\PetActiveSkill;
- use App\Module\Pet\Enums\PetStatus;
- use App\Module\User\Models\User;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Layout\Row;
- use Dcat\Admin\Widgets\Card;
- use Dcat\Admin\Widgets\Table;
- use Dcat\Admin\Widgets\Alert;
- use Spatie\RouteAttributes\Attributes\Get;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- /**
- * 农场用户信息汇总控制器
- *
- * 用于展示用户的农场信息汇总,包括房屋、土地、作物、物品和代币信息
- */
- #[Resource('farm-user-summary', names: 'dcat.admin.farm-user-summary')]
- class FarmUserSummaryController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '农场用户信息汇总';
- /**
- * 用户列表页面
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->title($this->title)
- ->description('查看用户的农场信息汇总')
- ->body($this->grid());
- }
- /**
- * 查看指定用户的农场信息汇总
- *
- * @param int $userId 用户ID
- * @param Content $content
- * @return Content
- */
- #[Get('farm-user-summary/{userId}', name: 'dcat.admin.farm-user-summary.show')]
- public function show($userId, Content $content)
- {
- // 记录请求信息
- \Illuminate\Support\Facades\Log::info('访问农场用户信息汇总', [
- 'user_id' => $userId,
- 'referer' => request()->header('referer'),
- 'user_agent' => request()->header('user-agent'),
- 'ip' => request()->ip(),
- 'url' => request()->fullUrl(),
- ]);
- // 查找用户
- $user = User::find($userId);
- if (!$user) {
- admin_error('错误', "用户 {$userId} 不存在");
- \Illuminate\Support\Facades\Log::warning('访问不存在的用户', [ 'user_id' => $userId ]);
- return redirect()->route('dcat.admin.farm-user-summary');
- }
- // 检查是否存在农场用户记录
- $farmUser = FarmUser::where('user_id', $userId)->first();
- if (!$farmUser) {
- admin_warning('提示', "用户 {$userId} 没有农场信息");
- \Illuminate\Support\Facades\Log::info('用户没有农场信息', [ 'user_id' => $userId ]);
- // 不重定向,继续显示用户信息,只是提示没有农场信息
- }
- return $content
- ->title($this->title)
- ->description("用户 {$user->username}(ID: {$user->id})的农场信息汇总")
- ->body(function (Row $row) use ($user) {
- // 第一行:用户基本信息和房屋信息
- $row->column(6, $this->userInfoCard($user));
- $row->column(6, $this->houseInfoCard($user->id));
- // 第二行:土地信息和作物信息
- $row->column(12, $this->landInfoCard($user->id));
- // 第三行:物品信息
- $row->column(12, $this->itemInfoCard($user->id));
- // 第四行:代币信息
- $row->column(12, $this->fundInfoCard($user->id));
- // 第五行:宠物信息
- $row->column(12, $this->petInfoCard($user->id));
- // 第六行:宠物生活技能情况
- $row->column(12, $this->petLifeSkillsCard($user->id));
- // 第七行:神像buff信息
- $row->column(12, $this->buffInfoCard($user->id));
- });
- }
- /**
- * 用户基本信息卡片
- *
- * @param User $user 用户对象
- * @return Card
- */
- protected function userInfoCard(User $user)
- {
- $userInfo = $user->info;
- $avatar = $userInfo ? $userInfo->avatar : '';
- $nickname = $userInfo ? $userInfo->nickname : '';
- $content = <<<HTML
- <div class="row">
- <div class="col-md-4">
- <img src="/saaa/{$avatar}" class="img-fluid rounded" style="max-width: 100px;" >
- </div>
- <div class="col-md-8">
- <p><strong>用户ID:</strong>{$user->id}</p>
- <p><strong>用户名:</strong>{$user->username}</p>
- <p><strong>昵称:</strong>{$nickname}</p>
- <p><strong>注册时间:</strong>{$user->created_at}</p>
- </div>
- </div>
- HTML;
- return new Card('用户基本信息', $content);
- }
- /**
- * 房屋信息卡片
- *
- * @param int $userId 用户ID
- * @return Card
- */
- protected function houseInfoCard($userId)
- {
- // 获取用户的农场信息
- $farmUser = FarmUser::where('user_id', $userId)->first();
- if (!$farmUser) {
- return new Card('房屋信息', new Alert('warning', '该用户没有农场信息'));
- }
- // 获取房屋配置信息
- $houseConfig = FarmHouseConfig::where('level', $farmUser->house_level)->first();
- if (!$houseConfig) {
- return new Card('房屋信息', new Alert('warning', "找不到房屋等级 {$farmUser->house_level} 的配置信息"));
- }
- $content = <<<HTML
- <div class="row">
- <div class="col-md-12">
- <p><strong>房屋等级:</strong>{$farmUser->house_level}</p>
- <p><strong>最后升级时间:</strong>{$farmUser->last_upgrade_time}</p>
- <p><strong>产出加成:</strong>{$houseConfig->output_bonus}</p>
- <p><strong>特殊土地上限:</strong>{$houseConfig->special_land_limit}</p>
- <p><strong>可用土地数量:</strong>{$houseConfig->available_lands}</p>
- HTML;
- // 如果有降级天数,显示降级信息
- if ($houseConfig->downgrade_days) {
- $content .= "<p><strong>降级天数:</strong>{$houseConfig->downgrade_days}</p>";
- }
- $content .= <<<HTML
- </div>
- </div>
- <div class="row mt-2">
- <div class="col-md-12">
- <a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="window.open('/admin/farm-house-configs', '_blank')">查看房屋配置</a>
- </div>
- </div>
- HTML;
- return new Card('房屋信息', $content);
- }
- /**
- * 土地信息卡片
- *
- * @param int $userId 用户ID
- * @return Card
- */
- protected function landInfoCard($userId)
- {
- // 获取用户的土地信息
- /**
- * @var FarmLand $land
- */
- $lands = FarmLand::with([ 'landType', 'crop.seed' ])
- ->where('user_id', $userId)
- ->get();
- if ($lands->isEmpty()) {
- return new Card('土地信息', new Alert('warning', '该用户没有土地信息'));
- }
- // 土地类型统计
- $landTypeStats = $lands->groupBy('land_type')->map->count();
- // 土地状态统计
- $landStatusStats = $lands->groupBy('status')->map->count();
- // 灾害统计
- $disasterStats = [
- 1 => 0, // 干旱
- 2 => 0, // 虫害
- 3 => 0, // 杂草
- ];
- // 统计活跃的灾害
- foreach ($lands as $land) {
- if ($land->crop && !empty($land->crop->disasters)) {
- foreach ($land->crop->disasters as $disaster) {
- if (($disaster['status'] ?? '') === 'active') {
- $type = $disaster['type'] ?? 0;
- if (isset($disasterStats[$type])) {
- $disasterStats[$type]++;
- }
- }
- }
- }
- }
- // 创建土地类型、状态和灾害的统计表格
- $statsContent = '<div class="row">';
- // 土地类型统计
- $statsContent .= '<div class="col-md-4">';
- $statsContent .= '<h5>土地类型统计</h5>';
- $statsContent .= '<table class="table table-sm table-bordered">';
- $statsContent .= '<thead><tr><th>土地类型</th><th>数量</th></tr></thead>';
- $statsContent .= '<tbody>';
- $landTypeNames = [
- 1 => '普通土地',
- 2 => '红土地',
- 3 => '黑土地',
- 4 => '金土地',
- 5 => '蓝土地',
- 6 => '紫土地',
- ];
- foreach ($landTypeStats as $typeId => $count) {
- $typeName = $landTypeNames[$typeId] ?? "类型{$typeId}";
- $statsContent .= "<tr><td>{$typeName}</td><td>{$count}</td></tr>";
- }
- $statsContent .= '</tbody></table></div>';
- // 土地状态统计
- $statsContent .= '<div class="col-md-4">';
- $statsContent .= '<h5>土地状态统计</h5>';
- $statsContent .= '<table class="table table-sm table-bordered">';
- $statsContent .= '<thead><tr><th>土地状态</th><th>数量</th></tr></thead>';
- $statsContent .= '<tbody>';
- $landStatusNames = [
- 0 => '空闲',
- 1 => '种植中',
- 2 => '灾害',
- 3 => '可收获',
- 4 => '枯萎',
- ];
- foreach ($landStatusStats as $statusId => $count) {
- $statusName = $landStatusNames[$statusId] ?? "状态{$statusId}";
- $statsContent .= "<tr><td>{$statusName}</td><td>{$count}</td></tr>";
- }
- $statsContent .= '</tbody></table></div>';
- // 灾害统计
- $statsContent .= '<div class="col-md-4">';
- $statsContent .= '<h5>灾害统计</h5>';
- $statsContent .= '<table class="table table-sm table-bordered">';
- $statsContent .= '<thead><tr><th>灾害类型</th><th>数量</th><th>减产比例</th></tr></thead>';
- $statsContent .= '<tbody>';
- $disasterNames = DISASTER_TYPE::getAll();
- $totalDisasters = 0;
- $d = DisasterService::getAllDisasters();
- foreach ($disasterStats as $typeId => $count) {
- $typeName = $disasterNames[$typeId] ?? "未知灾害{$typeId}";
- $penalty = $d[$typeId];
- $totalDisasters += $count;
- $statsContent .= "<tr><td>{$typeName}</td><td>{$count}</td><td>{$penalty}</td></tr>";
- }
- // 添加总计行
- $totalPenalty = $totalDisasters > 0 ? ($totalDisasters * 5) . '%' : '0%';
- $statsContent .= "<tr class='table-info'><td><strong>总计</strong></td><td>{$totalDisasters}</td><td>{$totalPenalty}</td></tr>";
- $statsContent .= '</tbody></table></div>';
- $statsContent .= '</div>';
- // 创建土地详情表格
- $headers = [
- 'ID', '位置', '土地类型', '状态', '种植作物', '种植时间', '生长阶段', '本阶段开始时间', '本阶段结束时间(剩余)',
- '灾害情况'
- ];
- $rows = [];
- foreach ($lands as $land) {
- $landType = $land->landType ? $land->landType->name : "类型{$land->land_type}";
- $status = $landStatusNames[$land->status] ?? "状态{$land->status}";
- $crop = $land->crop;
- $cropInfo = '无';
- $plantTime = '';
- $growthStage = '';
- $stageStartTime = '';
- $stageEndTime = '';
- $disasterInfo = '无';
- if ($crop) {
- $seedName = $crop->seed ? $crop->seed->name : "种子{$crop->seed_id}";
- $cropInfo = $seedName;
- $plantTime = $crop->plant_time;
- $growthStage = $this->getGrowthStageName($crop->growth_stage);
- $stageStartTime = $crop->stage_start_time;
- $stageEndTime = $this->formatRelativeTime($crop->stage_end_time);
- // 处理灾害信息
- if (!empty($crop->disasters)) {
- $disasterInfo = $this->formatDisasterInfo($crop->disasters);
- }
- }
- $rows[] = [
- $land->id,
- $land->position,
- $landType,
- $status,
- $cropInfo,
- $plantTime,
- $growthStage,
- $stageStartTime,
- $stageEndTime,
- $disasterInfo,
- ];
- }
- $table = new Table($headers, $rows);
- $content = $statsContent . '<div class="mt-3">' . $table->render() . '</div>';
- $content .= <<<HTML
- <div class="row mt-2">
- <div class="col-md-12">
- <a href="javascript:void(0);" class="btn btn-sm btn-primary"
- onclick="window.open('/admin/farm-lands?user_id={$userId}', '_blank')">查看土地详情</a>
- <a href="javascript:void(0);" class="btn btn-sm btn-primary"
- onclick="window.open('/admin/farm-crops?user_id={$userId}', '_blank')">查看作物详情</a>
- </div>
- </div>
- HTML;
- return new Card('土地信息', $content);
- }
- /**
- * 获取生长阶段名称
- *
- * @param GROWTH_STAGE|int $stage 生长阶段枚举或整数值
- * @return string 生长阶段名称
- */
- protected function getGrowthStageName($stage)
- {
- // 如果传入的是整数,转换为枚举
- if (is_int($stage)) {
- try {
- $stage = GROWTH_STAGE::from($stage);
- } catch (\ValueError) {
- return "未知阶段{$stage}";
- }
- }
- // 如果是null,返回未知
- if ($stage === null) {
- return "未知阶段";
- }
- // 获取阶段名称
- try {
- $stageNames = GROWTH_STAGE::getAll();
- $stageValue = $stage->value;
- return $stageNames[$stageValue] ?? "阶段{$stageValue}";
- } catch (\Throwable) {
- return "错误阶段";
- }
- }
- /**
- * 格式化相对时间描述
- *
- * @param string|\Carbon\Carbon|null $dateTime 时间
- * @return string 相对时间描述
- */
- protected function formatRelativeTime($dateTime): string
- {
- if (!$dateTime) {
- return '无';
- }
- try {
- // 确保是Carbon实例
- if (is_string($dateTime)) {
- $dateTime = \Carbon\Carbon::parse($dateTime);
- }
- $now = now();
- // 如果时间已经过去
- if ($dateTime->isPast()) {
- return '<span class="text-danger">已过期</span>';
- }
- // 计算时间差
- $diffInSeconds = $now->diffInSeconds($dateTime);
- $diffInMinutes = $now->diffInMinutes($dateTime);
- $diffInHours = $now->diffInHours($dateTime);
- $diffInDays = $now->diffInDays($dateTime);
- // 根据时间差返回合适的描述
- if ($diffInSeconds < 60) {
- $seconds = round($diffInSeconds);
- return "<span class='text-warning'>{$seconds}秒后</span>";
- } elseif ($diffInMinutes < 60) {
- $minutes = round($diffInMinutes);
- return "<span class='text-info'>{$minutes}分钟后</span>";
- } elseif ($diffInHours < 24) {
- // 小时显示保留1位小数,但如果是整数则不显示小数
- $hours = round($diffInHours, 1);
- $hoursDisplay = $hours == intval($hours) ? intval($hours) : $hours;
- return "<span class='text-primary'>{$hoursDisplay}小时后</span>";
- } elseif ($diffInDays < 7) {
- $days = round($diffInDays);
- return "<span class='text-secondary'>{$days}天后</span>";
- } else {
- // 超过7天显示具体日期
- return $dateTime->format('Y-m-d H:i:s');
- }
- } catch (\Throwable $e) {
- return '<span class="text-muted">时间格式错误</span>';
- }
- }
- /**
- * 物品信息卡片
- *
- * @param int $userId 用户ID
- * @return Card
- */
- protected function itemInfoCard($userId)
- {
- // 获取用户的物品信息
- $items = ItemUser::with('item')
- ->where('user_id', $userId)
- ->orderBy('quantity', 'desc')
- ->limit(20)
- ->get();
- if ($items->isEmpty()) {
- return new Card('物品信息', new Alert('warning', '该用户没有物品信息'));
- }
- // 创建物品表格
- $headers = [ '物品ID', '物品名称', '数量', '物品类型', '过期时间' ];
- $rows = [];
- foreach ($items as $item) {
- $itemName = $item->item ? $item->item->name : "物品{$item->item_id}";
- $itemType = $item->item ? $this->getItemTypeName($item->item->type) : '';
- $rows[] = [
- $item->item_id,
- $itemName,
- $item->quantity,
- $itemType,
- $item->expire_at ?: '永久',
- ];
- }
- $table = new Table($headers, $rows);
- // 获取用户物品总数
- $totalCount = ItemUser::where('user_id', $userId)->count();
- $content = <<<HTML
- <div class="alert alert-info">
- 用户共有 {$totalCount} 种物品,下表显示数量最多的前20种物品
- </div>
- {$table->render()}
- <div class="row mt-2">
- <div class="col-md-12">
- <a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="window.open('/admin/game-items-user-items?user_id={$userId}', '_blank')">查看物品详情</a>
- </div>
- </div>
- HTML;
- return new Card('物品信息', $content);
- }
- /**
- * 获取物品类型名称
- *
- * @param ITEM_TYPE|int $type 物品类型枚举或整数值
- * @return string 物品类型名称
- */
- protected function getItemTypeName($type)
- {
- // 如果传入的是整数,转换为枚举
- if (is_int($type)) {
- try {
- $type = ITEM_TYPE::from($type);
- } catch (\ValueError) {
- return "未知类型{$type}";
- }
- }
- // 如果是null,返回未知
- if ($type === null) {
- return "未知类型";
- }
- // 获取类型名称
- try {
- $typeNames = ITEM_TYPE::getValueDescription();
- $typeValue = $type->value;
- return $typeNames[$typeValue] ?? "类型{$typeValue}";
- } catch (\Throwable) {
- return "错误类型";
- }
- }
- /**
- * 格式化灾害信息
- *
- * @param array $disasters 灾害数组
- * @return string 格式化后的灾害信息
- */
- protected function formatDisasterInfo(array $disasters): string
- {
- if (empty($disasters)) {
- return '无';
- }
- $result = [];
- foreach ($disasters as $disaster) {
- $type = $disaster['type'] ?? 0;
- $status = $disaster['status'] ?? 'active'; // 默认为活跃状态
- // 获取灾害类型名称
- $typeName = DISASTER_TYPE::getName($type);
- // 灾害状态
- $statusText = $status === 'active' ? '<span class="badge badge-danger">活跃</span>' : '<span class="badge badge-secondary">已处理</span>';
- // 灾害开始时间
- $startTime = isset($disaster['start_time']) ? date('Y-m-d H:i:s', $disaster['start_time']) : '未知';
- // 灾害结束时间(如果有)
- $endTime = '';
- if (isset($disaster['end_time']) && $disaster['end_time'] > 0) {
- $endTime = date('Y-m-d H:i:s', $disaster['end_time']);
- }
- // 减产比例 - 从DisasterService获取默认值
- $defaultPenalties = \App\Module\Farm\Services\DisasterService::getAllDisasters();
- $defaultPenalty = $defaultPenalties[$type] ?? 0.05;
- $penalty = isset($disaster['penalty']) ? ($disaster['penalty'] * 100) . '%' : ($defaultPenalty * 100) . '%';
- // 组合灾害信息
- $disasterInfo = "<div><strong>{$typeName}</strong>: {$statusText}</div>";
- $disasterInfo .= "<div>开始: {$startTime}</div>";
- if ($endTime) {
- $disasterInfo .= "<div>结束: {$endTime}</div>";
- }
- $disasterInfo .= "<div>减产: {$penalty}</div>";
- // 如果有额外的灾害信息,也显示出来
- if (isset($disaster['id'])) {
- $disasterInfo .= "<div>ID: {$disaster['id']}</div>";
- }
- $result[] = $disasterInfo;
- }
- return implode('<hr style="margin: 5px 0;">', $result);
- }
- /**
- * 代币信息卡片
- *
- * @param int $userId 用户ID
- * @return Card
- */
- protected function fundInfoCard($userId)
- {
- // 获取用户的代币信息
- $funds = FundModel::where('user_id', $userId)->get();
- if ($funds->isEmpty()) {
- return new Card('代币信息', new Alert('warning', '该用户没有代币信息'));
- }
- // 获取资金类型名称映射
- $fundNames = AccountService::getFundsDesc();
- // 创建代币表格
- $headers = [ '账户ID', '账户名称', '余额', '更新时间' ];
- $rows = [];
- foreach ($funds as $fund) {
- try {
- $fundIdValue = $fund->fund_id->value();
- $fundName = $fundNames[$fundIdValue] ?? "账户{$fundIdValue}";
- $balance = $fund->balance;
- $rows[] = [
- $fundIdValue,
- $fundName,
- $balance,
- $fund->update_time ? date('Y-m-d H:i:s', $fund->update_time) : '',
- ];
- } catch (\Throwable) {
- // 如果出现异常,添加一个错误行
- $rows[] = [
- $fund->id ?? '未知',
- '数据错误',
- $fund->balance ?? '未知',
- $fund->update_time ? date('Y-m-d H:i:s', $fund->update_time) : '',
- ];
- }
- }
- $table = new Table($headers, $rows);
- $content = <<<HTML
- {$table->render()}
- <div class="row mt-2">
- <div class="col-md-12">
- <a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="window.open('/admin/fund-accounts?user_id={$userId}', '_blank')">查看账户详情</a>
- </div>
- </div>
- HTML;
- return new Card('代币信息', $content);
- }
- /**
- * 宠物信息卡片
- *
- * @param int $userId 用户ID
- * @return Card
- */
- protected function petInfoCard($userId)
- {
- // 获取用户的宠物信息
- $pets = PetUser::where('user_id', $userId)
- ->orderBy('level', 'desc')
- ->orderBy('grade', 'desc')
- ->get();
- if ($pets->isEmpty()) {
- return new Card('宠物信息', new Alert('warning', '该用户没有宠物信息'));
- }
- // 宠物统计
- $totalPets = $pets->count();
- $gradeStats = $pets->groupBy('grade')->map->count();
- $statusStats = $pets->groupBy('status')->map->count();
- $levelStats = [
- 'max_level' => $pets->max('level'),
- 'avg_level' => round($pets->avg('level'), 1),
- 'total_exp' => $pets->sum('experience'),
- ];
- // 创建宠物表格
- $headers = ['宠物名称', '品阶', '等级', '经验', '体力', '状态', '创建时间'];
- $rows = [];
- $gradeNames = [
- 1 => '一品',
- 2 => '二品',
- 3 => '三品',
- 4 => '四品',
- ];
- $statusNames = [
- PetStatus::NONE->value => '未知',
- PetStatus::NORMAL->value => '正常',
- PetStatus::FIGHTING->value => '战斗中',
- PetStatus::DEAD->value => '死亡',
- PetStatus::FEEDING->value => '喂养中',
- PetStatus::TRAINING->value => '训练中',
- PetStatus::RESTING->value => '休息中',
- PetStatus::TRAVELING->value => '外出中',
- ];
- foreach ($pets as $pet) {
- try {
- $gradeName = $gradeNames[$pet->grade] ?? "品阶{$pet->grade}";
- $statusName = $statusNames[$pet->status->value] ?? "状态{$pet->status->value}";
- // 状态颜色
- $statusBadge = match($pet->status) {
- PetStatus::NORMAL => '<span class="badge badge-success">' . $statusName . '</span>',
- PetStatus::FIGHTING => '<span class="badge badge-warning">' . $statusName . '</span>',
- PetStatus::DEAD => '<span class="badge badge-danger">' . $statusName . '</span>',
- PetStatus::FEEDING, PetStatus::TRAINING => '<span class="badge badge-info">' . $statusName . '</span>',
- PetStatus::RESTING, PetStatus::TRAVELING => '<span class="badge badge-primary">' . $statusName . '</span>',
- default => '<span class="badge badge-secondary">' . $statusName . '</span>',
- };
- $rows[] = [
- $pet->name,
- $gradeName,
- $pet->level,
- $pet->experience,
- $pet->stamina,
- $statusBadge,
- $pet->created_at->format('Y-m-d H:i:s'),
- ];
- } catch (\Throwable) {
- // 如果出现异常,添加一个错误行
- $rows[] = [
- $pet->name ?? '未知',
- '数据错误',
- $pet->level ?? 0,
- $pet->experience ?? 0,
- $pet->stamina ?? 0,
- '<span class="badge badge-danger">数据错误</span>',
- $pet->created_at ? $pet->created_at->format('Y-m-d H:i:s') : '未知',
- ];
- }
- }
- $table = new Table($headers, $rows);
- // 统计信息
- $statsHtml = '<div class="row mb-3">';
- $statsHtml .= '<div class="col-md-3"><strong>宠物总数:</strong> ' . $totalPets . '</div>';
- $statsHtml .= '<div class="col-md-3"><strong>最高等级:</strong> ' . $levelStats['max_level'] . '</div>';
- $statsHtml .= '<div class="col-md-3"><strong>平均等级:</strong> ' . $levelStats['avg_level'] . '</div>';
- $statsHtml .= '<div class="col-md-3"><strong>总经验:</strong> ' . number_format($levelStats['total_exp']) . '</div>';
- $statsHtml .= '</div>';
- // 品阶统计
- if (!$gradeStats->isEmpty()) {
- $statsHtml .= '<div class="row mb-3"><div class="col-md-12"><strong>品阶分布:</strong> ';
- foreach ($gradeStats as $grade => $count) {
- $gradeName = $gradeNames[$grade] ?? "品阶{$grade}";
- $statsHtml .= "{$gradeName}: {$count}只 ";
- }
- $statsHtml .= '</div></div>';
- }
- // 状态统计
- if (!$statusStats->isEmpty()) {
- $statsHtml .= '<div class="row mb-3"><div class="col-md-12"><strong>状态分布:</strong> ';
- foreach ($statusStats as $status => $count) {
- $statusName = $statusNames[$status] ?? "状态{$status}";
- $statsHtml .= "{$statusName}: {$count}只 ";
- }
- $statsHtml .= '</div></div>';
- }
- $content = $statsHtml . $table->render();
- return new Card('宠物信息', $content);
- }
- /**
- * 宠物生活技能情况卡片
- *
- * @param int $userId 用户ID
- * @return Card
- */
- protected function petLifeSkillsCard($userId)
- {
- // 获取用户的宠物
- $pets = PetUser::where('user_id', $userId)->get();
- if ($pets->isEmpty()) {
- return new Card('宠物生活技能情况', new Alert('warning', '该用户没有宠物,无法使用生活技能'));
- }
- // 获取所有宠物的激活技能
- $petIds = $pets->pluck('id')->toArray();
- $activeSkills = PetActiveSkill::with(['pet', 'skill'])
- ->whereIn('pet_id', $petIds)
- ->orderBy('status', 'asc')
- ->orderBy('end_time', 'desc')
- ->get();
- // 统计信息
- $totalSkills = $activeSkills->count();
- $activeCount = $activeSkills->where('status', 'active')->count();
- $expiredCount = $activeSkills->where('status', 'expired')->count();
- $cancelledCount = $activeSkills->where('status', 'cancelled')->count();
- // 技能类型统计
- $skillTypeStats = $activeSkills->groupBy('skill_name')->map->count();
- // 创建统计信息
- $statsHtml = '<div class="row mb-3">';
- $statsHtml .= '<div class="col-md-3"><strong>总技能使用次数:</strong> ' . $totalSkills . '</div>';
- $statsHtml .= '<div class="col-md-3"><strong>当前激活:</strong> <span class="badge badge-success">' . $activeCount . '</span></div>';
- $statsHtml .= '<div class="col-md-3"><strong>已过期:</strong> <span class="badge badge-secondary">' . $expiredCount . '</span></div>';
- $statsHtml .= '<div class="col-md-3"><strong>已取消:</strong> <span class="badge badge-warning">' . $cancelledCount . '</span></div>';
- $statsHtml .= '</div>';
- // 技能类型统计
- if (!$skillTypeStats->isEmpty()) {
- $statsHtml .= '<div class="row mb-3"><div class="col-md-12"><strong>技能使用统计:</strong> ';
- foreach ($skillTypeStats as $skillName => $count) {
- $statsHtml .= "<span class='badge badge-info mr-2'>{$skillName}: {$count}次</span> ";
- }
- $statsHtml .= '</div></div>';
- }
- if ($activeSkills->isEmpty()) {
- $content = $statsHtml . '<div class="alert alert-info">该用户的宠物还没有使用过生活技能</div>';
- return new Card('宠物生活技能情况', $content);
- }
- // 创建技能详情表格
- $headers = ['宠物名称', '技能名称', '开始时间', '结束时间', '状态', '剩余时间', '技能配置'];
- $rows = [];
- foreach ($activeSkills as $activeSkill) {
- try {
- $petName = $activeSkill->pet ? $activeSkill->pet->name : "宠物{$activeSkill->pet_id}";
- $skillName = $activeSkill->skill_name;
- $startTime = $activeSkill->start_time->format('Y-m-d H:i:s');
- $endTime = $activeSkill->end_time->format('Y-m-d H:i:s');
- // 状态显示
- $statusBadge = match($activeSkill->status) {
- 'active' => '<span class="badge badge-success">生效中</span>',
- 'expired' => '<span class="badge badge-secondary">已过期</span>',
- 'cancelled' => '<span class="badge badge-warning">已取消</span>',
- default => '<span class="badge badge-dark">' . $activeSkill->status . '</span>',
- };
- // 剩余时间
- $remainingTime = '';
- if ($activeSkill->status === 'active') {
- $remaining = $activeSkill->getRemainingSeconds();
- if ($remaining > 0) {
- $hours = floor($remaining / 3600);
- $minutes = floor(($remaining % 3600) / 60);
- $seconds = $remaining % 60;
- $remainingTime = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
- } else {
- $remainingTime = '<span class="text-danger">已过期</span>';
- }
- } else {
- $remainingTime = '-';
- }
- // 技能配置信息
- $configInfo = '';
- if (!empty($activeSkill->config)) {
- $config = $activeSkill->config;
- $configItems = [];
- // 显示关键配置信息
- if (isset($config['check_interval'])) {
- $configItems[] = "检查间隔: {$config['check_interval']}秒";
- }
- if (isset($config['preferred_seeds']) && !empty($config['preferred_seeds'])) {
- $seedCount = count($config['preferred_seeds']);
- $configItems[] = "优先种子: {$seedCount}种";
- }
- if (isset($config['protected_types']) && !empty($config['protected_types'])) {
- $protectedTypes = implode(', ', $config['protected_types']);
- $configItems[] = "防护类型: {$protectedTypes}";
- }
- if (isset($config['statistics']) && !empty($config['statistics'])) {
- $statsCount = count($config['statistics']);
- $configItems[] = "执行记录: {$statsCount}次";
- }
- $configInfo = implode('<br>', $configItems);
- }
- $rows[] = [
- $petName,
- $skillName,
- $startTime,
- $endTime,
- $statusBadge,
- $remainingTime,
- $configInfo ?: '-'
- ];
- } catch (\Throwable $e) {
- // 如果出现异常,添加一个错误行
- $rows[] = [
- $activeSkill->pet ? $activeSkill->pet->name : '未知',
- $activeSkill->skill_name ?? '未知',
- $activeSkill->start_time ? $activeSkill->start_time->format('Y-m-d H:i:s') : '未知',
- $activeSkill->end_time ? $activeSkill->end_time->format('Y-m-d H:i:s') : '未知',
- '<span class="badge badge-danger">数据错误</span>',
- '-',
- '数据解析错误'
- ];
- }
- }
- $table = new Table($headers, $rows);
- $content = $statsHtml . $table->render();
- // 添加说明信息
- $content .= '<div class="alert alert-info mt-3">';
- $content .= '<strong>说明:</strong><br>';
- $content .= '• <strong>自动收菜</strong>:每分钟自动收获成熟的作物<br>';
- $content .= '• <strong>自动播种</strong>:每分钟自动在空闲土地上播种<br>';
- $content .= '• <strong>灾害防护</strong>:每5分钟检查并自动清除作物灾害<br>';
- $content .= '• 所有自动操作仍需消耗相应的道具(种子、化肥等)';
- $content .= '</div>';
- return new Card('宠物生活技能情况', $content);
- }
- /**
- * 神像buff信息卡片
- *
- * @param int $userId 用户ID
- * @return Card
- */
- protected function buffInfoCard($userId)
- {
- // 获取用户的神像buff信息
- $buffs = FarmGodBuff::where('user_id', $userId)
- ->orderBy('expire_time', 'desc')
- ->get();
- if ($buffs->isEmpty()) {
- return new Card('神像加持信息', new Alert('warning', '该用户没有神像加持信息'));
- }
- // 创建buff表格
- $headers = [ '加持类型', '过期时间', '状态' ];
- $rows = [];
- $buffTypeNames = [
- 1 => '丰收之神',
- 2 => '雨露之神',
- 3 => '屠草之神',
- 4 => '拭虫之神',
- ];
- foreach ($buffs as $buff) {
- try {
- $buffType = $buffTypeNames[$buff->buff_type] ?? "类型{$buff->buff_type}";
- $isActive = now()->lt($buff->expire_time);
- $status = $isActive ? '<span class="badge badge-success">生效中</span>' : '<span class="badge badge-secondary">已过期</span>';
- $rows[] = [
- $buffType,
- $buff->expire_time,
- $status,
- ];
- } catch (\Throwable) {
- // 如果出现异常,添加一个错误行
- $rows[] = [
- '数据错误',
- $buff->expire_time ?? '未知',
- '<span class="badge badge-danger">数据错误</span>',
- ];
- }
- }
- $table = new Table($headers, $rows);
- $content = $table->render();
- return new Card('神像加持信息', $content);
- }
- /**
- * 用户列表网格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(User::with([ 'info', 'farmUser' ]), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- // 用户基本信息
- $grid->column('username', '用户名');
- $grid->column('info.nickname', '昵称');
- // 农场信息
- $grid->column('farmUser.house_level', '房屋等级')->sortable();
- // 土地统计
- $grid->column('id', '土地统计')->display(function ($userId) {
- $lands = FarmLand::where('user_id', $userId)->get();
- if ($lands->isEmpty()) {
- return '<span class="text-muted">无土地</span>';
- }
- $landTypeStats = $lands->groupBy('land_type')->map->count();
- $totalLands = $lands->count();
- $html = "<div>总计: {$totalLands}块土地</div>";
- $landTypeNames = [
- 1 => '普通土地',
- 2 => '红土地',
- 3 => '黑土地',
- 4 => '金土地',
- 5 => '蓝土地',
- 6 => '紫土地',
- ];
- foreach ($landTypeStats as $typeId => $count) {
- $typeName = $landTypeNames[$typeId] ?? "类型{$typeId}";
- $html .= "<div>{$typeName}: {$count}块</div>";
- }
- return $html;
- });
- // 作物统计
- $grid->column('id', '作物统计')->display(function ($userId) {
- $crops = FarmCrop::where('user_id', $userId)->count();
- return $crops > 0 ? "{$crops}种作物" : '<span class="text-muted">无作物</span>';
- });
- // 物品统计
- $grid->column('id', '物品统计')->display(function ($userId) {
- $itemCount = ItemUser::where('user_id', $userId)->count();
- return $itemCount > 0 ? "{$itemCount}种物品" : '<span class="text-muted">无物品</span>';
- });
- // 代币统计
- $grid->column('id', '代币统计')->display(function ($userId) {
- $fundCount = FundModel::where('user_id', $userId)->count();
- return $fundCount > 0 ? "{$fundCount}个账户" : '<span class="text-muted">无账户</span>';
- });
- // 宠物统计
- $grid->column('id', '宠物统计')->display(function ($userId) {
- $pets = PetUser::where('user_id', $userId)->get();
- if ($pets->isEmpty()) {
- return '<span class="text-muted">无宠物</span>';
- }
- $totalPets = $pets->count();
- $maxLevel = $pets->max('level');
- $gradeStats = $pets->groupBy('grade')->map->count();
- $html = "<div>总计: {$totalPets}只宠物</div>";
- $html .= "<div>最高等级: {$maxLevel}</div>";
- // 显示品阶分布
- $gradeNames = [1 => '一品', 2 => '二品', 3 => '三品', 4 => '四品'];
- foreach ($gradeStats as $grade => $count) {
- $gradeName = $gradeNames[$grade] ?? "品阶{$grade}";
- $html .= "<div>{$gradeName}: {$count}只</div>";
- }
- return $html;
- });
- $grid->column('created_at', '创建时间')->sortable();
- // 添加查看详情操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- // 禁用默认操作按钮
- $actions->disableDelete();
- $actions->disableEdit();
- $actions->disableQuickEdit();
- // 修改查看按钮,使其打开详情页
- $actions->append('<a href="' . admin_url('farm-user-summary/' . $actions->getKey()) . '" class="btn btn-sm btn-primary">查看详情</a>');
- });
- // 禁用创建按钮
- $grid->disableCreateButton();
- // 禁用批量操作
- $grid->disableBatchActions();
- // 添加搜索
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', '用户ID');
- $filter->like('username', '用户名');
- $filter->like('info.nickname', '昵称');
- $filter->equal('farmUser.house_level', '房屋等级');
- });
- });
- }
- }
|