ActivityDemoExcel.php 1.78 KB
<?php

namespace App\Excel;

use App\Support\ExcelExport;

class ActivityDemoExcel extends ExcelExport
{
    protected function beforeHandle(): void
    {
        $this->builder->with(['project:id,name', 'user:id,real_name,nick_name,identity', 'tags:id,name', 'links:id,nick_name,real_name']);
        $this->builder->withCount(['viewUsers', 'collectionUsers']);
        $this->setHeader(['歌曲名称', '曲风标签', '关联厂牌', '词作者', '曲作者', '试听人数', '收藏人数', '创建人艺名', '创建人真名', '创建时间', '通过时间 ', '状态']);
        $this->setColumns(['A:G' => 18, 'H:M' => 20]);
    }

    protected function setData($item): array
    {
        $lyricistIds   = data_get($item, 'expand.lyricist.ids', []);
        $lyricistNames = data_get($item, 'expand.lyricist.supplement', []);
        $composerIds   = data_get($item, 'expand.composer.ids', []);
        $composerNames = data_get($item, 'expand.composer.supplement', []);

        return [
            $item->song_name,
            $item->getRelation('tags')?->implode('name', ',') ?? '',
            $item->getRelation('project')?->getAttribute('name') ?? '无关联厂牌',
            $item->getRelation('links')->whereIn('id', $lyricistIds)->pluck('nick_name')?->unique()?->merge($lyricistNames)?->implode(','),
            $item->getRelation('links')->whereIn('id', $composerIds)->pluck('nick_name')?->unique()?->merge($composerNames)?->implode(','),
            $item->view_users_count ?? 0,
            $item->collection_users_count ?? 0,
            $item->getRelation('user')?->getAttribute('nick_name'),
            $item->getRelation('user')?->getAttribute('real_name'),
            (string)$item->created_at,
            (string)$item->audit_at,
            $item->status?->label(),
        ];
    }
}