package model import ( "ArmedPolice/app/common/model" "ArmedPolice/serve/orm" "fmt" ) 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 %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 ORDER BY T1.lvl DESC` out := make([]*SysTenantBasic, 0) err := orm.GetDB().Raw(fmt.Sprintf(sql, m.TableName(), tenantID, m.TableName(), m.TableName())).Scan(&out).Error return out, err } func NewSysTenant() *SysTenant { return &SysTenant{SysTenant: model.NewSysTenant()} }