<?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');
    }

    /*
     * 得到分销商域名
     * type 0:当前域名 1:二级域名 2:自定义域名
     */
    public function getDomain($distId,$type = 0)
    {
        $domain = '';
        $row = $this->where('id', $distId)->first();
        $http = 'http://';
        if (env('ADMIN_HTTPS') == true) {
            $http = 'https://';
        }
        if ($row) {
            if ($type == 0) {
                if ($row->domain_type == 0) {
                    $domain = $http.$row->secondary_domain;
                } else {
                    $domain = $http.$row->custom_domain;
                }
            } else if ($type == 1) {
                $domain = $http.$row->secondary_domain;
            } else {
                $domain = $http.$row->custom_domain;
            }
        }
        if (env('DIST_SITE_PORT') != 80 && env('DIST_SITE_PORT') != 443) {
            $domain .= ':' . env('DIST_SITE_PORT');
        }
        return $domain;
    }

}