74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
|
|
use Illuminate\Support\Facades\Cache;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* App\Models\DrugCategory
|
||
|
|
*
|
||
|
|
* @property int $id
|
||
|
|
* @property int $level 分类等级
|
||
|
|
* @property int $parent_id 父级id
|
||
|
|
* @property string $category_name 分类名称
|
||
|
|
* @property int $sort 排序
|
||
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
||
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
||
|
|
* @property \Illuminate\Support\Carbon|null $deleted_at
|
||
|
|
* @property-read string $created_at_format 创建时间的格式化
|
||
|
|
* @property-read string $status_string 状态名称
|
||
|
|
* @property-read string $updated_at_format 更新时间的格式化
|
||
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\DrugCategory[] $subs 子分类
|
||
|
|
* @property-read int|null $subs_count
|
||
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DrugCategory newModelQuery()
|
||
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DrugCategory newQuery()
|
||
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\DrugCategory onlyTrashed()
|
||
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DrugCategory query()
|
||
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\DrugCategory withTrashed()
|
||
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\DrugCategory withoutTrashed()
|
||
|
|
* @mixin \Eloquent
|
||
|
|
*/
|
||
|
|
class DrugCategory extends BaseModel
|
||
|
|
{
|
||
|
|
use SoftDeletes;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 药品分类
|
||
|
|
*
|
||
|
|
* @param $cache
|
||
|
|
* @return \Illuminate\Cache\|mixed|mixed[]
|
||
|
|
*/
|
||
|
|
public static function getCategory($cache = true)
|
||
|
|
{
|
||
|
|
$cacheKey = __CLASS__ . '_data_cache';
|
||
|
|
if (Cache::has($cacheKey) && $cache) {
|
||
|
|
return Cache::get($cacheKey, []);
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = self::with(['subs:id,parent_id,category_name'])
|
||
|
|
->select(['id', 'parent_id', 'category_name'])
|
||
|
|
->where('level', 1)
|
||
|
|
->orderBy('sort')
|
||
|
|
->get()
|
||
|
|
->toArray();
|
||
|
|
|
||
|
|
if ($data) {
|
||
|
|
Cache::put($cacheKey, $data, 60);
|
||
|
|
}
|
||
|
|
return $data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @comment 子分类
|
||
|
|
* @return HasMany
|
||
|
|
*/
|
||
|
|
public function subs(): HasMany
|
||
|
|
{
|
||
|
|
return $this->hasMany(self::class, 'parent_id', 'id')
|
||
|
|
->where('level', 2)
|
||
|
|
->orderBy('sort');
|
||
|
|
}
|
||
|
|
}
|