This commit is contained in:
wangchuxiao
2022-08-31 00:43:06 +08:00
parent a4b95cb7cf
commit c2831e6676
7 changed files with 28 additions and 22 deletions
@@ -1,13 +1,14 @@
package im_mysql_model
import (
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db"
"time"
)
func GetActiveUserNum(from, to time.Time) (int32, error) {
var num int64
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Select("count(distinct(send_id))").Where("create_time >= ? and create_time <= ?", from, to).Count(&num).Error
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Select("count(distinct(send_id))").Where("send_time >= ? and send_time <= ?", from, to).Count(&num).Error
return int32(num), err
}
@@ -31,13 +32,13 @@ func GetTotalUserNumByDate(to time.Time) (int32, error) {
func GetPrivateMessageNum(from, to time.Time) (int32, error) {
var num int64
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Where("create_time >= ? and create_time <= ? and session_type = ?", from, to, 1).Count(&num).Error
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, 1).Count(&num).Error
return int32(num), err
}
func GetGroupMessageNum(from, to time.Time) (int32, error) {
var num int64
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Where("create_time >= ? and create_time <= ? and session_type = ?", from, to, 2).Count(&num).Error
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, 2).Count(&num).Error
return int32(num), err
}
@@ -67,7 +68,7 @@ type activeGroup struct {
func GetActiveGroups(from, to time.Time, limit int) ([]*activeGroup, error) {
var activeGroups []*activeGroup
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Select("recv_id, count(*) as message_num").Where("create_time >= ? and create_time <= ? and session_type = ?", from, to, 2).Group("recv_id").Limit(limit).Order("message_num DESC").Find(&activeGroups).Error
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Select("recv_id, count(*) as message_num").Where("send_time >= ? and send_time <= ? and session_type in (?)", from, to, []int{constant.GroupChatType, constant.SuperGroupChatType}).Group("recv_id").Limit(limit).Order("message_num DESC").Find(&activeGroups).Error
for _, activeGroup := range activeGroups {
group := db.Group{
GroupID: activeGroup.Id,
@@ -80,18 +81,21 @@ func GetActiveGroups(from, to time.Time, limit int) ([]*activeGroup, error) {
type activeUser struct {
Name string
Id string `gorm:"column:send_id"`
ID string `gorm:"column:send_id"`
MessageNum int `gorm:"column:message_num"`
}
func GetActiveUsers(from, to time.Time, limit int) ([]*activeUser, error) {
var activeUsers []*activeUser
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Select("send_id, count(*) as message_num").Where("create_time >= ? and create_time <= ? and session_type = ?", from, to, 1).Group("send_id").Limit(limit).Order("message_num DESC").Find(&activeUsers).Error
err := db.DB.MysqlDB.DefaultGormDB().Table("chat_logs").Select("send_id, count(*) as message_num").Where("send_time >= ? and send_time <= ? and session_type = ?", from, to, constant.SingleChatType).Group("send_id").Limit(limit).Order("message_num DESC").Find(&activeUsers).Error
for _, activeUser := range activeUsers {
user := db.User{
UserID: activeUser.Id,
UserID: activeUser.ID,
}
err = db.DB.MysqlDB.DefaultGormDB().Table("users").Select("user_id, name").Find(&user).Error
if err != nil {
continue
}
db.DB.MysqlDB.DefaultGormDB().Table("users").Select("user_id, name").Find(&user)
activeUser.Name = user.Nickname
}
return activeUsers, err
@@ -11,14 +11,14 @@ func InsertInToUserBlackList(black db.Black) error {
return db.DB.MysqlDB.DefaultGormDB().Table("blacks").Create(black).Error
}
//type Black struct {
// OwnerUserID string `gorm:"column:owner_user_id;primaryKey;"`
// BlockUserID string `gorm:"column:block_user_id;primaryKey;"`
// CreateTime time.Time `gorm:"column:create_time"`
// AddSource int32 `gorm:"column:add_source"`
// OperatorUserID int32 `gorm:"column:operator_user_id"`
// Ex string `gorm:"column:ex"`
//}
// type Black struct {
// OwnerUserID string `gorm:"column:owner_user_id;primaryKey;"`
// BlockUserID string `gorm:"column:block_user_id;primaryKey;"`
// CreateTime time.Time `gorm:"column:create_time"`
// AddSource int32 `gorm:"column:add_source"`
// OperatorUserID int32 `gorm:"column:operator_user_id"`
// Ex string `gorm:"column:ex"`
// }
func CheckBlack(ownerUserID, blockUserID string) error {
var black db.Black