<?php
/**
 *  ╔═══════════════════════════════════════════════════╗
 *  ║                                                   ║
 *  ║     ██╗  ██╗   █████╗    ██████╗                  ║
 *  ║     ██║  ██║  ██╔══██╗  ██╔═══██╗                 ║
 *  ║     ███████║  ███████║  ██║   ██║                 ║
 *  ║     ██╔══██║  ██╔══██║  ██║   ██║                 ║
 *  ║     ██║  ██║  ██║  ██║  ╚██████╔╝   SNS           ║
 *  ║                                                   ║    
 *  ║                                                   ║    
 *  ║     © 2023 HaoSNS™ All Rights Reserved            ║
 *  ║     官方网站: https://www.haosns.com                *
 *  ║     本代码由赣州乐易网络科技有限公司®提供             *
 *  ║                                                    *
 *  ║   未经授权禁止复制、传播或用于其他商业目的            *
 *  ║                                                   ║
 *  ╚═══════════════════════════════════════════════════╝
 */


namespace app\common\model;
use app\common\service\FileService;
use think\model\concern\SoftDelete;

class Circle extends BaseModel
{
    // use SoftDelete;

    // protected $deleteTime = 'delete_time';

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->field('id,nickname,avatar');
    }

    public function subCategory()
    {
        if(isActivePluginByPluginName('field')){
             return $this->hasMany(CircleSubCategory::class,'circle_id','id')->order('sort','desc')->with('circleSubCategoryField');
        }else{
             return $this->hasMany(CircleSubCategory::class,'circle_id','id')->order('sort','desc');
        }
       
    }

    public function circleField()
    {
        return $this->hasMany(CircleField::class,'circle_id','id')->order('sort','desc');
    }

    public function fieldAll()
    {
        return $this->hasMany(CircleField::class,'circle_id','id')->order('sort','desc');;
    }

    public function vipLevel()
    {
        return $this->hasOne(VipLevel::class,'id','vip_level_id');
    }

    public function field()
    {
        return $this->hasMany(CircleField::class,'circle_id','id');
    }

    public function userCircle()
    {
        return $this->hasOne(UserCircle::class,'circle_id','id');
    }



    public function getIconAttr($value ,$data)
    {
        return trim($value) ? FileService::getFileUrl($value) : '';
    }

    public function getBgImageAttr($value ,$data)
    {
        return trim($value) ? FileService::getFileUrl($value) : '';
    }


    /**
     * @notes 获取收藏数
     */
    public function getCollectNumAttr($value ,$data)
    {
        return UserCircle::where([ 'circle_id' => $data['id']])->count();
    }

}