• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / app / 4291224680

pending completion
4291224680

push

github

twinh
feat: 分组功能从 `admin` 插件迁移到 `app` 插件中

0 of 18 new or added lines in 1 file covered. (0.0%)

8 of 41 relevant lines covered (19.51%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Service/GroupModel.php
1
<?php
2

3
namespace Miaoxing\App\Service;
4

5
use Miaoxing\App\Metadata\GroupTrait;
6
use Miaoxing\Plugin\BaseModel;
7
use Miaoxing\Plugin\Model\HasAppIdTrait;
8
use Miaoxing\Plugin\Model\ModelTrait;
9
use Miaoxing\Plugin\Model\ReqQueryTrait;
10
use Miaoxing\Plugin\Model\SnowflakeTrait;
11
use Miaoxing\Plugin\Service\UserModel;
12
use Wei\Model\SoftDeleteTrait;
13
use Wei\Ret;
14

15
/**
16
 * GroupModel
17
 *
18
 * @OA\Schema(schema="Group")
19
 * @OA\Property(
20
 *   property="id",
21
 *   type="string",
22
 *   description="编号"
23
 * )
24
 * @OA\Property(
25
 *   property="appId",
26
 *   type="string",
27
 *   description="应用编号"
28
 * )
29
 * @OA\Property(
30
 *   property="name",
31
 *   type="string",
32
 *   description="名称"
33
 * )
34
 * @OA\Property(
35
 *   property="sort",
36
 *   type="int",
37
 *   description="顺序"
38
 * )
39
 * @OA\Property(
40
 *   property="createdBy",
41
 *   type="string",
42
 *   description="创建用户编号"
43
 * )
44
 * @OA\Property(
45
 *   property="createdAt",
46
 *   type="string",
47
 *   description="创建时间"
48
 * )
49
 *  @OA\Property(
50
 *   property="updatedBy",
51
 *   type="string",
52
 *   description="更新用户编号"
53
 * )
54
 * @OA\Property(
55
 *   property="updatedAt",
56
 *   type="string",
57
 *   description="更新时间"
58
 * )
59
 * @property GroupModel $parent
60
 * @property GroupModel[]|GroupModel $children
61
 */
62
class GroupModel extends BaseModel
63
{
64
    use GroupTrait;
65
    use HasAppIdTrait;
66
    use ModelTrait;
67
    use ReqQueryTrait;
68
    use SnowflakeTrait;
69
    use SoftDeleteTrait;
70

71
    protected $attributes = [
72
        'sort' => 50,
73
    ];
74

75
    /**
76
     * @var GroupModel|GroupModel[]
77
     */
78
    protected $parents;
79

80
    public function parent(): self
81
    {
NEW
82
        return $this->hasOne(static::class, 'id', 'parentId');
×
83
    }
84

85
    public function children(): self
86
    {
NEW
87
        return $this->hasMany(static::class, 'parentId')->desc('sort');
×
88
    }
89

90
    public function afterSave()
91
    {
NEW
92
        wei()->cache->delete('groups:' . wei()->app->getId());
×
93
    }
94

95
    public function afterDestroy()
96
    {
NEW
97
        wei()->cache->delete('groups:' . wei()->app->getId());
×
98
    }
99

100
    public function getFullName()
101
    {
NEW
102
        return implode('-', array_reverse($this->getParents()->getAll('name')));
×
103
    }
104

105
    /**
106
     * 获取分组的所有上级分组
107
     *
108
     * @return GroupModel|GroupModel[]
109
     * @throws \Exception
110
     */
111
    public function getParents()
112
    {
NEW
113
        if (!$this->parents) {
×
NEW
114
            $groups = wei()->group->getGroupsFromCache();
×
115

NEW
116
            $parents = wei()->groupModel()->beColl();
×
NEW
117
            $parents[] = $this;
×
118

NEW
119
            $group = $this;
×
NEW
120
            while ($group->parentId) {
×
NEW
121
                $group = $groups[$group->parentId];
×
NEW
122
                $parents[] = $group;
×
123
            }
124

NEW
125
            $this->parents = $parents;
×
126
        }
127

NEW
128
        return $this->parents;
×
129
    }
130

131
    public function checkDestroy(): Ret
132
    {
NEW
133
        if (UserModel::findBy('groupId', $this->id)) {
×
NEW
134
            return err(['很抱歉,该%s已被%s使用,不能删除', '分组', '用户']);
×
135
        }
NEW
136
        return suc();
×
137
    }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc