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-11 14:05:52 +08:00
|
|
|
"fmt"
|
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) {
|
2021-11-11 14:05:52 +08:00
|
|
|
sql := `SELECT T2.id, T2.name FROM
|
|
|
|
(SELECT @r AS _id, (SELECT @r := parent_id FROM %s WHERE id = _id) AS parent_id,
|
|
|
|
@l := @l + 1 AS lvl
|
|
|
|
FROM (SELECT @r := %d, @l := 0 ) vars, %s h WHERE @r <> 0) T1
|
|
|
|
JOIN %s T2 ON T1._id = T2.id
|
2021-11-04 11:59:33 +08:00
|
|
|
ORDER BY T1.lvl DESC`
|
|
|
|
|
|
|
|
out := make([]*SysTenantBasic, 0)
|
|
|
|
|
2021-11-11 14:05:52 +08:00
|
|
|
err := orm.GetDB().Raw(fmt.Sprintf(sql, m.TableName(), tenantID, m.TableName(), m.TableName())).Scan(&out).Error
|
2021-11-04 11:59:33 +08:00
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:02:52 +08:00
|
|
|
func NewSysTenant() *SysTenant {
|
|
|
|
return &SysTenant{SysTenant: model.NewSysTenant()}
|
|
|
|
}
|