Commit 6a817fba authored by hujiebin's avatar hujiebin

分批获取所有家族成员

parent 16b757e5
......@@ -4,7 +4,6 @@ import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/utils"
"hilo-group/_const/enum/groupPower_e"
)
type GroupPowerInfo struct {
......@@ -59,13 +58,19 @@ func GetGroupPowerUser(model *domain.Model, groupPowerId mysql.ID) ([]*GroupPowe
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
if err := model.DB().Model(GroupPowerUser{}).Select("user_id").
Where("role = ?", groupPower_e.GroupPowerUserRoleMgr).Find(&userIds).Error; err != nil {
model.Log.Errorf("GetAllGroupPowerOwners fail:%v", err)
return userIds, err
var nextLastId mysql.ID
if err := model.DB().Model(GroupPowerUser{}).
Where("id > ?", lastId).Order("id asc").Limit(limit).Find(&res).Error; err != nil {
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() {
// 势力等级即将过期
group_power_ev.AddGroupPowerExpireNoticeAsync(func(model *domain.Model, e interface{}) error {
model.Log.Infof("msg AddGroupPowerExpireNoticeAsync")
powerOwners, err := groupPower_m.GetAllGroupPowerOwners(model)
start := time.Now()
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 {
model.Log.Errorf("AddGroupPowerExpireNoticeAsync fail:%v", 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 {
return err
}
......@@ -210,8 +216,15 @@ func GroupPowerEvents() {
model.Log.Errorf("msg AddGroupPowerExpireNoticeAsync NewUserRecord err:%v", err)
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
})
}
......
......@@ -7,7 +7,7 @@ import (
)
func TestGetAllGroupPowerOwner(t *testing.T) {
owners, err := groupPower_m.GetAllGroupPowerOwners(domain.CreateModelNil())
owners, err := groupPower_m.BatchGetAllGroupPowerUsers(domain.CreateModelNil())
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