Files
iotlcm/server/internal/logic/iot/channel_display.go
T
2026-10-29 14:36:49 +08:00

86 lines
2.6 KiB
Go

package iot
import (
"context"
"fmt"
"strings"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
"github.com/gogf/gf/v2/frame/g"
)
type channelDisplayScope struct {
TenantID string
DeptID int64
FullName bool
}
// Channel names are private to the tenant's top organization. A broad data
// scope or an administrator role does not grant downstream organizations names.
func canViewChannelFullName(ctx context.Context) bool {
return resolveChannelDisplayScope(ctx, func() (*entity.SysDept, error) {
var dept *entity.SysDept
cols := dao.SysDept.Columns()
err := dao.SysDept.Ctx(ctx).Fields(cols.DeptId, cols.ParentId, cols.Ancestors).
Where(cols.TenantId, contexts.GetTenantId(ctx)).
Where(cols.DeptId, contexts.GetDeptId(ctx)).Scan(&dept)
return dept, err
})
}
func resolveChannelDisplayScope(ctx context.Context, load func() (*entity.SysDept, error)) bool {
user := contexts.GetUser(ctx)
if user == nil || user.DeptId <= 0 || strings.TrimSpace(user.TenantId) == "" {
return false
}
const key = "iot.channelDisplayScope"
if cached, ok := contexts.GetDataValue(ctx, key).(channelDisplayScope); ok && cached.TenantID == user.TenantId && cached.DeptID == user.DeptId {
return cached.FullName
}
dept, err := load()
fullName := err == nil && dept != nil && dept.DeptId == user.DeptId &&
(dept.ParentId == 0 || strings.Trim(dept.Ancestors, ",") == "0")
if err != nil {
g.Log().Warningf(ctx, "resolve channel display organization failed: %v", err)
}
contexts.SetDataValue(ctx, key, channelDisplayScope{TenantID: user.TenantId, DeptID: user.DeptId, FullName: fullName})
return fullName
}
func channelDisplayNameColumn(ctx context.Context) string {
if canViewChannelFullName(ctx) {
return dao.TSimChannels.Columns().ChanName
}
return dao.TSimChannels.Columns().ChanAlias
}
func channelDisplayNameSQL(ctx context.Context, table string) string {
return fmt.Sprintf("COALESCE(NULLIF(TRIM(%s.%s), ''), '--')", table, channelDisplayNameColumn(ctx))
}
func channelDisplayName(fullName bool, name, alias string) string {
if fullName {
return name
}
if alias = strings.TrimSpace(alias); alias != "" {
return alias
}
return "--"
}
func applyCardChannelDisplayNames(ctx context.Context, list []*model.TSimCardInfoListModel) {
if len(list) == 0 {
return
}
fullName := canViewChannelFullName(ctx)
for _, item := range list {
if item != nil {
item.ChanName = channelDisplayName(fullName, item.ChanName, item.ChanAlias)
}
}
}