<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class DistAdminDistributor extends Model
{

    protected $table = 'dist_admin_distributor';


    // 关联关系：一个 Distributor 可能有多个 AppearancePublish
    public function appearancePublishes()
    {
        return $this->hasMany(DistAppearancePublishList::class, 'dist_id', 'id');
    }

    /**
     * 关联到 DistAppearance
     */
    public function appearance()
    {
        return $this->belongsTo(DistAppearance::class, 'appearance_id', 'id');
    }

    /**
     * 根据域名查找 Distributor 并加载关联数据
     *
     * @param string $domain
     * @return self|null
     */
    public static function findByDomainWithAppearances(string $domain)
    {
        return self::with('appearancePublishes') // 预加载关联数据
        ->where('custom_domain', $domain)
            ->orWhere('secondary_domain', $domain)
            ->first();
    }

    /**
     * 根据域名查找 Distributor（不加载关联数据）
     *
     * @param string $domain
     * @return self|null
     */
    public static function findByDomain(string $domain)
    {
        return self::where('custom_domain', $domain)
            ->orWhere('secondary_domain', $domain)
            ->first(); // 只查找当前模型数据
    }

}
