Commit 6a817fba authored by hujiebin's avatar hujiebin

分批获取所有家族成员

parent 16b757e5
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/utils" "git.hilo.cn/hilo-common/utils"
"hilo-group/_const/enum/groupPower_e"
) )
type GroupPowerInfo struct { type GroupPowerInfo struct {
...@@ -59,13 +58,19 @@ func GetGroupPowerUser(model *domain.Model, groupPowerId mysql.ID) ([]*GroupPowe ...@@ -59,13 +58,19 @@ func GetGroupPowerUser(model *domain.Model, groupPowerId mysql.ID) ([]*GroupPowe
return m[groupPowerId], nil return m[groupPowerId], nil
} }
// 获取所有家族的主 // 分批获取所有家族成员
func GetAllGroupPowerOwners(model *domain.Model) ([]mysql.ID, error) { func BatchGetAllGroupPowerUsers(model *domain.Model, lastId mysql.ID, limit int) ([]mysql.ID, mysql.ID, error) {
var res []GroupPowerUser
var userIds []mysql.ID var userIds []mysql.ID
if err := model.DB().Model(GroupPowerUser{}).Select("user_id"). var nextLastId mysql.ID
Where("role = ?", groupPower_e.GroupPowerUserRoleMgr).Find(&userIds).Error; err != nil { if err := model.DB().Model(GroupPowerUser{}).
model.Log.Errorf("GetAllGroupPowerOwners fail:%v", err) Where("id > ?", lastId).Order("id asc").Limit(limit).Find(&res).Error; err != nil {
return userIds, err model.Log.Errorf("BatchGetAllGroupPowerUsers fail:%v", err)
return userIds, 0, err
} }
return userIds, nil for _, v := range res {
userIds = append(userIds, v.UserId)
nextLastId = v.ID
}
return userIds, nextLastId, nil
} }
...@@ -195,13 +195,19 @@ func GroupPowerEvents() { ...@@ -195,13 +195,19 @@ func GroupPowerEvents() {
// 势力等级即将过期 // 势力等级即将过期
group_power_ev.AddGroupPowerExpireNoticeAsync(func(model *domain.Model, e interface{}) error { group_power_ev.AddGroupPowerExpireNoticeAsync(func(model *domain.Model, e interface{}) error {
model.Log.Infof("msg AddGroupPowerExpireNoticeAsync") start := time.Now()
powerOwners, err := groupPower_m.GetAllGroupPowerOwners(model) model.Log.Infof("msg AddGroupPowerExpireNoticeAsync,start:%v", start)
lastId := mysql.ID(0)
for {
powerUsers, nextLastId, err := groupPower_m.BatchGetAllGroupPowerUsers(model, lastId, 500)
if err != nil { if err != nil {
model.Log.Errorf("AddGroupPowerExpireNoticeAsync fail:%v", err) model.Log.Errorf("AddGroupPowerExpireNoticeAsync fail:%v", err)
return err return err
} }
userMaps, err := user_m.GetUserMapByIds(model, powerOwners) if len(powerUsers) <= 0 {
break
}
userMaps, err := user_m.GetUserMapByIds(model, powerUsers)
if err != nil { if err != nil {
return err return err
} }
...@@ -210,8 +216,15 @@ func GroupPowerEvents() { ...@@ -210,8 +216,15 @@ func GroupPowerEvents() {
model.Log.Errorf("msg AddGroupPowerExpireNoticeAsync NewUserRecord err:%v", err) model.Log.Errorf("msg AddGroupPowerExpireNoticeAsync NewUserRecord err:%v", err)
return nil return nil
} }
msg_m.SendEmasMsgAssistant(model, u.ExternalId, u.DeviceType) _ = msg_m.SendEmasMsgAssistant(model, u.ExternalId, u.DeviceType)
}
if nextLastId <= 0 {
break
}
lastId = nextLastId
time.Sleep(time.Second) // 停顿一秒
} }
model.Log.Infof("msg AddGroupPowerExpireNoticeAsync,end:%v", time.Now().Sub(start).Seconds())
return nil return nil
}) })
} }
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
) )
func TestGetAllGroupPowerOwner(t *testing.T) { func TestGetAllGroupPowerOwner(t *testing.T) {
owners, err := groupPower_m.GetAllGroupPowerOwners(domain.CreateModelNil()) owners, err := groupPower_m.BatchGetAllGroupPowerUsers(domain.CreateModelNil())
println(owners, err) println(owners, err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment