SingerExcel.php 1.73 KB
<?php

namespace App\Excel;

use App\Enums\UserSexEnum;
use App\Support\ExcelExport;
use Illuminate\Support\Arr;

class SingerExcel extends ExcelExport
{
    protected function beforeHandle(): void
    {
        $this->builder->addSelect(['company'])->with(['styleTags:id,name', 'authTags:id,name']);
        $this->setHeader([
            '用户艺名', '用户真名', '性别', '用户邮箱', '手机号码', '注册时间', '认证通过时间',
            '队长艺名', '队长真名', '用户权限', '音乐人能力', '是否关注服务号', '公司', '常居地', '擅长的曲风', '状态'
        ]);
        $this->setColumns(['A:P' => 20]);
    }

    protected function setData($item): array
    {
        return Arr::map([
            $item->getAttribute('nick_name'),
            $item->getAttribute('real_name'),
            UserSexEnum::tryFrom($item->getAttribute('sex'))?->label(),
            $item->getAttribute('email'),
            sprintf('(%s)%s', $item->getFullPhone()->getPrefixedIDDCode('+'), $item->getFullPhone()->getNumber()),
            (string)$item->getAttribute('created_at'),
            (string)$item->getAttribute('audit_at'),
            $item->getRelation('business')?->getAttribute('nick_name'),
            $item->getRelation('business')?->getAttribute('real_name'),
            $item->getAttribute('scope')?->label(),
            $item->getRelation('authTags')?->implode('name', '|'),
            $item->getAttribute('official_status')?->label(),
            $item->getAttribute('company'),
            $item->getFullAddress(),
            $item->getRelation('styleTags')?->implode('name', ','),
            $item->getAttribute('status')?->label(),
        ], static fn($value) => empty($value) ? '无' : $value);
    }
}