<?php

namespace Modules\Cms\Models;

use App\Models\Model;
use App\Traits\HasClicks;
use App\Traits\HasCovers;
use App\Traits\HasStatus;
use App\Traits\OrderByIdDesc;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Modules\Cms\Traits\HasTags;
use Overtrue\LaravelVersionable\Versionable;

class Page extends Model
{

    use Cachable,
        HasClicks,
        HasCovers,
        HasTags,
        HasStatus,
        OrderByIdDesc,
        Versionable,
        SoftDeletes;

    protected $table = 'cms_pages';

    protected $casts = [
        'pictures'    => 'json',
        'attachments' => 'json',
    ];

    /**
     * 不参与版本记录的字段
     * @var array|string[]
     */
    protected array $dontVersionable = ['updated_at'];

    /**
     * Notes   : 获取附件的下载地址
     * @Date   : 2021/4/16 12:01 下午
     * @Author : < Jason.C >
     * @return \Illuminate\Support\Collection
     */
    public function getAttachmentsUrlAttribute(): Collection
    {
        return collect($this->attachments)->map(function ($item) {
            return Storage::url($item);
        });
    }

}