| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace App\Module\Activity\AdminControllers\Helper;
- use App\Module\Activity\Enums\ACTIVITY_STATUS;
- use App\Module\Activity\Enums\ACTIVITY_TYPE;
- use App\Module\Activity\Enums\CONDITION_TYPE;
- use App\Module\Activity\Enums\PARTICIPATION_STATUS;
- use App\Module\Activity\Enums\REWARD_STATUS;
- use App\Module\Game\Enums\REWARD_TYPE;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Grid\Column;
- /**
- * 列表页辅助特性
- *
- * 提供活动模块后台控制器的列表页构建功能的具体实现
- */
- trait GridHelperTrait
- {
- /**
- * 添加活动状态列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnActivityStatus(string $field = 'status', string $label = '活动状态'): Column
- {
- return $this->grid->column($field, $label)
- ->using(ACTIVITY_STATUS::getAll())
- ->label([
- ACTIVITY_STATUS::NOT_STARTED->value => 'default',
- ACTIVITY_STATUS::IN_PROGRESS->value => 'success',
- ACTIVITY_STATUS::ENDED->value => 'warning',
- ACTIVITY_STATUS::CLOSED->value => 'danger',
- ]);
- }
- /**
- * 添加活动类型列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnActivityType(string $field = 'type', string $label = '活动类型'): Column
- {
- return $this->grid->column($field, $label)
- ->using(ACTIVITY_TYPE::getAll())
- ->label([
- ACTIVITY_TYPE::GIFT_PACKAGE->value => 'info',
- ACTIVITY_TYPE::TIME_LIMITED->value => 'primary',
- ACTIVITY_TYPE::TASK->value => 'success',
- ACTIVITY_TYPE::SIGN_IN->value => 'warning',
- ACTIVITY_TYPE::FESTIVAL->value => 'danger',
- ]);
- }
- /**
- * 添加条件类型列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnConditionType(string $field = 'condition_type', string $label = '条件类型'): Column
- {
- return $this->grid->column($field, $label)->using(CONDITION_TYPE::getAll());
- }
- /**
- * 添加参与状态列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnParticipationStatus(string $field = 'completion_status', string $label = '参与状态'): Column
- {
- return $this->grid->column($field, $label)
- ->using(PARTICIPATION_STATUS::getAll())
- ->label([
- PARTICIPATION_STATUS::IN_PROGRESS->value => 'primary',
- PARTICIPATION_STATUS::COMPLETED->value => 'success',
- PARTICIPATION_STATUS::FAILED->value => 'danger',
- ]);
- }
- /**
- * 添加奖励状态列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnRewardStatus(string $field = 'reward_status', string $label = '奖励状态'): Column
- {
- return $this->grid->column($field, $label)
- ->using(REWARD_STATUS::getAll())
- ->label([
- REWARD_STATUS::NOT_CLAIMED->value => 'default',
- REWARD_STATUS::CLAIMED->value => 'success',
- REWARD_STATUS::EXPIRED->value => 'danger',
- ]);
- }
- /**
- * 添加奖励类型列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnRewardType(string $field = 'reward_type', string $label = '奖励类型'): Column
- {
- return $this->grid->column($field, $label)->using(REWARD_TYPE::getAll());
- }
- /**
- * 添加奖励来源类型列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnRewardSourceType(string $field = 'source_type', string $label = '奖励来源'): Column
- {
- return $this->grid->column($field, $label)->using(REWARD_SOURCE_TYPE::getAll());
- }
- /**
- * 添加活动时间范围列
- *
- * @param string $startField 开始时间字段
- * @param string $endField 结束时间字段
- * @param string $label 标签名
- * @return Column
- */
- public function columnActivityTimeRange(string $startField = 'start_time', string $endField = 'end_time', string $label = '活动时间'): Column
- {
- return $this->grid->column($startField, $label)->display(function ($startTime) use ($endField) {
- $endTime = $this->{$endField};
- return date('Y-m-d H:i:s', strtotime($startTime)) . '<br>至<br>' . date('Y-m-d H:i:s', strtotime($endTime));
- });
- }
- /**
- * 添加活动状态标签列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnActivityStatusWithAutoUpdate(string $field = 'status', string $label = '活动状态'): Column
- {
- return $this->grid->column($field, $label)->display(function ($status) {
- $now = time();
- $startTime = strtotime($this->start_time);
- $endTime = strtotime($this->end_time);
-
- // 自动计算实际状态
- $actualStatus = $status;
- if ($status == ACTIVITY_STATUS::NOT_STARTED->value && $now >= $startTime) {
- $actualStatus = ACTIVITY_STATUS::IN_PROGRESS->value;
- } elseif ($status == ACTIVITY_STATUS::IN_PROGRESS->value && $now >= $endTime) {
- $actualStatus = ACTIVITY_STATUS::ENDED->value;
- }
-
- $statusText = ACTIVITY_STATUS::getAll()[$actualStatus] ?? '未知';
- $statusClass = '';
-
- switch ($actualStatus) {
- case ACTIVITY_STATUS::NOT_STARTED->value:
- $statusClass = 'default';
- break;
- case ACTIVITY_STATUS::IN_PROGRESS->value:
- $statusClass = 'success';
- break;
- case ACTIVITY_STATUS::ENDED->value:
- $statusClass = 'warning';
- break;
- case ACTIVITY_STATUS::CLOSED->value:
- $statusClass = 'danger';
- break;
- }
-
- $html = "<span class='label bg-{$statusClass}'>{$statusText}</span>";
-
- // 如果实际状态与数据库状态不同,添加提示
- if ($actualStatus != $status) {
- $html .= " <span class='badge badge-warning'>需更新</span>";
- }
-
- return $html;
- });
- }
- /**
- * 添加活动参与人数列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnParticipantCount(string $field = 'id', string $label = '参与人数'): Column
- {
- return $this->grid->column($field, $label)->display(function ($id) {
- return \App\Module\Activity\Models\ActivityParticipation::where('activity_id', $id)->count();
- });
- }
- /**
- * 添加活动完成人数列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnCompletedCount(string $field = 'id', string $label = '完成人数'): Column
- {
- return $this->grid->column($field, $label)->display(function ($id) {
- return \App\Module\Activity\Models\ActivityParticipation::where('activity_id', $id)
- ->where('completion_status', PARTICIPATION_STATUS::COMPLETED->value)
- ->count();
- });
- }
- /**
- * 添加活动奖励领取人数列
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnRewardClaimedCount(string $field = 'id', string $label = '奖励领取人数'): Column
- {
- return $this->grid->column($field, $label)->display(function ($id) {
- return \App\Module\Activity\Models\ActivityParticipation::where('activity_id', $id)
- ->where('reward_status', REWARD_STATUS::CLAIMED->value)
- ->count();
- });
- }
- }
|