package groupPower_m import ( "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" ) type GroupPowerExpRank struct { GroupPowerId mysql.ID Exp mysql.Num Rank int } // 获取家族经验排行榜 // param limit: 排行榜人数 func GetGroupPowerExpRank(model *domain.Model, beginDate, endDate string, limit int) ([]GroupPowerExpRank, error) { var res []GroupPowerExpRank //if err := model.DB().Table("group_power_day_exp").Select("group_power_id,SUM(exp) as exp").Where("date BETWEEN ? AND ?", beginDate, endDate).Group("group_power_id") if err := model.DB().Table("group_power_day_exp p").Joins(",(SELECT @curRank := 0) p").Where("date BETWEEN ? AND ?", beginDate, endDate). Select("group_power_id,SUM(exp) as exp,@curRank := @curRank + 1 `rank`"). Group("group_power_id").Order("exp DESC").Limit(limit).Find(&res).Error; err != nil { model.Log.Errorf("GetGroupPowerExpRank fail:%v", err) return res, err } return res, nil } // 获取我的家族排行 func GetGroupPowerExpMyRank(model *domain.Model, beginDate, endDate string, groupPowerId mysql.ID) (GroupPowerExpRank, error) { table := fmt.Sprintf("(select group_power_id,SUM(exp) as exp,@curRank := @curRank + 1 `rank` from group_power_day_exp p,(SELECT @curRank := 0) tmp"+ " WHERE `date` BETWEEN '%s' AND '%s' group BY group_power_id ORDER BY exp DESC ) t", beginDate, endDate) var res GroupPowerExpRank if err := model.DB().Table(table).Select("t.group_power_id,t.exp,t.`rank`").Where("t.group_power_id = ?", groupPowerId).Scan(&res).Error; err != nil { return res, err } return res, nil }