Category.php
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 模型层:AI分类 模型类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Category
* @package App\Models\Category
* @author Richer <yangzi1028@163.com>
* @date 2023年4月20日13:48:05
* @copyright 2014-2023 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Category;
use App\Models\BaseModel;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Class Category
* @property mixed name
* @property mixed icon
* @property mixed body
* @property mixed description
* @package App\Models
*/
class Category extends BaseModel
{
// 指定数据库表
const TABLE = 'categories';
protected $table = self::TABLE ;
// 指定对象显示名称:方便系统统一查询和做其他处理
const OBJ_NAME = 'category';
const OBJ_NAME_ZH = 'AI分类';
/**
* 类型业务常量配置
*/
const DYSFUNCTION = 1;// 功能异常
const OTHER_PROBLEM = 99;// 其他问题
/**
* 类型业务常量配置[1 => '功能异常', 992 => '其他问题']
*/
const TYPE_OPTIONS = [
self::DYSFUNCTION => '功能异常',
self::OTHER_PROBLEM => '其他问题',
];
/**
* @return HasMany
*/
public function examples(): HasMany
{
return $this->hasMany(CategoryExample::class);
}
/**
* @return HasMany
*/
public function labels(): HasMany
{
return $this->hasMany(CategoryLabel::class);
}
/**
* @return BelongsTo
*/
public function parent(): BelongsTo
{
return $this->belongsTo(get_class($this), 'pid', 'id');
}
/**
* @return HasMany
*/
public function children(): HasMany
{
return $this->hasMany(get_class($this), 'pid', 'id');
}
/**
* @param $value
*/
public function setIconAttribute($value)
{
// 需要判断前端传来的路径是否是全路径,如果是全路径需要截取为相对路径
$this->attributes['icon'] = substr_file_path($value);
}
/**
* @param string $value 值
*
* @return array
* @throws GuzzleException
*/
public function getIconAttribute($value)
{
return splice_file_path($value);
}
/**
* 生成每个级别的code
* @param string $oprate
* @return array
*/
public static function getParents($oprate = 'query')
{
$models = self::where(function ($query) {
$query->whereNull('pid')->orWhere('pid', 0)->orWhere('pid', '');
})->get(['id','name']);
$options = [];
if ($oprate !== 'query') {
$options = [0 => '无'];
}
foreach ($models as $vo) {
$options[$vo->id] = $vo->name;
}
return $options;
return $models;
}
}