<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class DistAdminDistributor extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'dist_admin_distributor';

    /*
     * 用户一对多关联
     */
    public function user()
    {
        return $this->hasMany(DistAdminUser::class, 'dist_id', 'id');
    }

    /*
     * 外观一对一关联
     */
    public function appearance()
    {
        return $this->hasOne(DistAppearance::class, 'id', 'appearance_id');
    }

    /*
     * 得到分销商域名
     */
    public function getDomain($distId)
    {
        $domain = '';
        $row = $this->where('id', $distId)->first();
        if ($row) {
            if ($row->domain_type == 0) {
                $domain = 'http://'.$row->secondary_domain;
            } else {
                $domain = 'http://'.$row->custom_domain;
            }
        }
        if (env('DIST_SITE_PORT') != 80) {
            $domain .= ':' . env('DIST_SITE_PORT');
        }
        return $domain;
    }
}
