2023-01-16 20:14:26 +08:00
package mysql
2022-02-07 08:44:21 +08:00
import (
2022-08-31 00:43:06 +08:00
"Open_IM/pkg/common/constant"
2022-02-07 08:44:21 +08:00
"time"
)
func GetActiveUserNum ( from , to time . Time ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := ChatLogDB . Table ( "chat_logs" ) . Select ( "count(distinct(send_id))" ) . Where ( "send_time >= ? and send_time <= ?" , from , to ) . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetIncreaseUserNum ( from , to time . Time ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := UserDB . Where ( "create_time >= ? and create_time <= ?" , from , to ) . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetTotalUserNum ( ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := UserDB . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetTotalUserNumByDate ( to time . Time ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := UserDB . Where ( "create_time <= ?" , to ) . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetPrivateMessageNum ( from , to time . Time ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := ChatLogDB . Where ( "send_time >= ? and send_time <= ? and session_type = ?" , from , to , 1 ) . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetGroupMessageNum ( from , to time . Time ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := ChatLogDB . Where ( "send_time >= ? and send_time <= ? and session_type = ?" , from , to , 2 ) . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetIncreaseGroupNum ( from , to time . Time ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := GroupDB . Where ( "create_time >= ? and create_time <= ?" , from , to ) . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetTotalGroupNum ( ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := GroupDB . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
func GetGroupNum ( to time . Time ) ( int32 , error ) {
2022-07-14 12:08:28 +08:00
var num int64
2023-01-11 16:23:16 +08:00
err := GroupDB . Where ( "create_time <= ?" , to ) . Count ( & num ) . Error
2022-07-14 12:08:28 +08:00
return int32 ( num ) , err
2022-02-07 08:44:21 +08:00
}
type activeGroup struct {
Name string
Id string ` gorm:"column:recv_id" `
MessageNum int ` gorm:"column:message_num" `
}
func GetActiveGroups ( from , to time . Time , limit int ) ( [ ] * activeGroup , error ) {
var activeGroups [ ] * activeGroup
2023-01-11 16:23:16 +08:00
err := ChatLogDB . 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
2022-02-07 08:44:21 +08:00
for _ , activeGroup := range activeGroups {
2023-01-04 17:22:55 +08:00
group := Group {
2022-02-07 08:44:21 +08:00
GroupID : activeGroup . Id ,
}
2023-01-11 16:23:16 +08:00
GroupDB . Where ( "group_id= ? " , group . GroupID ) . Find ( & group )
2022-02-07 08:44:21 +08:00
activeGroup . Name = group . GroupName
}
return activeGroups , err
}
type activeUser struct {
Name string
2022-08-31 00:43:06 +08:00
ID string ` gorm:"column:send_id" `
2022-02-07 08:44:21 +08:00
MessageNum int ` gorm:"column:message_num" `
}
func GetActiveUsers ( from , to time . Time , limit int ) ( [ ] * activeUser , error ) {
var activeUsers [ ] * activeUser
2023-01-11 16:23:16 +08:00
err := ChatLogDB . 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
2022-02-07 08:44:21 +08:00
for _ , activeUser := range activeUsers {
2023-01-04 17:22:55 +08:00
user := User {
2022-08-31 00:43:06 +08:00
UserID : activeUser . ID ,
}
2023-01-11 16:23:16 +08:00
err = UserDB . Select ( "user_id, name" ) . Find ( & user ) . Error
2022-08-31 00:43:06 +08:00
if err != nil {
continue
2022-02-07 08:44:21 +08:00
}
activeUser . Name = user . Nickname
2022-09-06 17:46:55 +08:00
activeUser . ID = user . UserID
2022-02-07 08:44:21 +08:00
}
return activeUsers , err
}