Files
hyperf_data/app/Model/Campus.php
2025-09-19 15:14:20 +08:00

105 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Model;
use Aether\AetherModel;
use Carbon\Carbon;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasMany;
/**
* 校区模型(支持层级结构).
* @property int $id
* @property string $name 校区名称
* @property int $parent_id 父级ID
* @property int $level 层级1-省份2-地级市3-校区
* @property string $province 省份
* @property string $city 地级市
* @property string $address 详细地址
* @property string $contact_phone 联系电话
* @property string $contact_person 联系人
* @property int $status 状态0-禁用1-启用
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon $deleted_at
*/
class Campus extends AetherModel
{
protected ?string $table = 'campus';
protected array $fillable = [
'name',
'parent_id',
'level',
'province',
'city',
'address',
'contact_phone',
'status',
];
protected array $casts = [
'id' => 'integer',
'parent_id' => 'integer',
'level' => 'integer',
'status' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
/**
* 获取父级校区.
* @return BelongsTo
*/
public function parent(): BelongsTo
{
return $this->belongsTo(Campus::class, 'parent_id', 'id')
->where('status', 1)
->whereNull('deleted_at');
}
/**
* 获取子校区.
*/
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id', 'id');
}
/**
* 按省份查询.
*/
public function scopeProvince(Builder $query, string $province): Builder
{
return $query->where('province', $province);
}
/**
* 按城市查询.
*/
public function scopeCity(Builder $query, string $city): Builder
{
return $query->where('city', $city);
}
/**
* 按层级查询.
*/
public function scopeLevel(Builder $query, int $level): Builder
{
return $query->where('level', $level);
}
/**
* 查询启用的校区.
*/
public function scopeEnabled(Builder $query): Builder
{
return $query->where('status', 1);
}
}