45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package model
|
|
|
|
import (
|
|
"ArmedPolice/app/common/model"
|
|
"ArmedPolice/serve/orm"
|
|
)
|
|
|
|
type SysTenant struct {
|
|
*model.SysTenant
|
|
}
|
|
|
|
type (
|
|
// SysTenantBasic 租户基本信息
|
|
SysTenantBasic struct {
|
|
ID uint64 `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
// SysTenantInfo 租户信息
|
|
SysTenantInfo struct {
|
|
*model.SysTenant
|
|
DeviceCount int `json:"device_count"`
|
|
CustomerDeviceCount int `json:"customer_device_count"`
|
|
}
|
|
)
|
|
|
|
// Parent 父集元素,包含自己
|
|
func (m *SysTenant) Parent(tenantID uint64) ([]*SysTenantBasic, error) {
|
|
sql := `SELECT T2.id, T2.name FROM
|
|
(SELECT @r AS _id, (SELECT @r := parent_id FROM ? WHERE id = _id) AS parent_id,
|
|
@l := @l + 1 AS lvl
|
|
FROM (SELECT @r := ?, @l := 0 ) vars, ? h WHERE @r <> 0) T1
|
|
JOIN ? T2 ON T1._id = T2.id
|
|
ORDER BY T1.lvl DESC`
|
|
|
|
out := make([]*SysTenantBasic, 0)
|
|
|
|
err := orm.GetDB().Raw(sql, m.TableName(), tenantID, m.TableName(), m.TableName()).Scan(&out).Error
|
|
|
|
return out, err
|
|
}
|
|
|
|
func NewSysTenant() *SysTenant {
|
|
return &SysTenant{SysTenant: model.NewSysTenant()}
|
|
}
|