From a4c244cb61e8a73adfd6e6e9a7b87262d2777f39 Mon Sep 17 00:00:00 2001 From: UlricQin Date: Fri, 23 Jul 2021 12:03:47 +0800 Subject: [PATCH] modify user_group_ids of alert_rule_group when user_group deleted --- models/alert_rule_group.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/models/alert_rule_group.go b/models/alert_rule_group.go index e121ad7e..eab6f44a 100644 --- a/models/alert_rule_group.go +++ b/models/alert_rule_group.go @@ -1,6 +1,8 @@ package models import ( + "fmt" + "sort" "strings" "time" @@ -86,10 +88,26 @@ func (arg *AlertRuleGroup) FillUserGroups() error { return internalServerError } - if len(ugs) > 0 { - arg.UserGroups = ugs - } else { - arg.UserGroups = []UserGroup{} + arg.UserGroups = ugs + + // 这里附一个清理逻辑,如果某个团队已经删除了,就顺带把这个团队id从arg中删除 + ugslen := len(ugs) + idslst := make([]string, 0, ugslen) + + for i := 0; i < ugslen; i++ { + idslst = append(idslst, fmt.Sprint(ugs[i].Id)) + } + + sort.Strings(idslst) + newids := strings.Join(idslst, " ") + + // 把原来的ids也排个序,两相比较,如果发生变化,就说明有团队已经被删了,更新之 + sort.Strings(ids) + oldids := strings.Join(ids, " ") + + if newids != oldids { + arg.UserGroupIds = newids + arg.Update("user_group_ids") } return nil