Files
ArmedPolice/app/model/sys_tenant.go

45 lines
1.0 KiB
Go
Raw Normal View History

2021-11-02 10:02:52 +08:00
package model
import (
2021-11-02 16:22:07 +08:00
"ArmedPolice/app/common/model"
2021-11-04 11:59:33 +08:00
"ArmedPolice/serve/orm"
2021-11-02 10:02:52 +08:00
)
type SysTenant struct {
*model.SysTenant
}
type (
// SysTenantBasic 租户基本信息
SysTenantBasic struct {
2021-11-04 11:59:33 +08:00
ID uint64 `json:"id"`
Name string `json:"name"`
2021-11-02 10:02:52 +08:00
}
// SysTenantInfo 租户信息
SysTenantInfo struct {
*model.SysTenant
DeviceCount int `json:"device_count"`
CustomerDeviceCount int `json:"customer_device_count"`
}
)
2021-11-04 11:59:33 +08:00
// 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
}
2021-11-02 10:02:52 +08:00
func NewSysTenant() *SysTenant {
return &SysTenant{SysTenant: model.NewSysTenant()}
}