From c2831e6676a9f19f571359fdd2e07a8e6e7967a8 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 31 Aug 2022 00:43:06 +0800 Subject: [PATCH] cms --- cmd/open_im_cms_api/main.go | 1 + internal/cms_api/message_cms/message.go | 4 ++-- internal/rpc/admin_cms/admin_cms.go | 2 +- pkg/cms_api_struct/message_cms.go | 1 + pkg/common/db/model_struct.go | 6 +++--- .../im_mysql_model/statistics_model.go | 20 +++++++++++-------- .../im_mysql_model/user_black_list_model.go | 16 +++++++-------- 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/cmd/open_im_cms_api/main.go b/cmd/open_im_cms_api/main.go index b2b1cae7c..622d72924 100644 --- a/cmd/open_im_cms_api/main.go +++ b/cmd/open_im_cms_api/main.go @@ -8,6 +8,7 @@ import ( "strconv" "Open_IM/pkg/common/config" + "github.com/gin-gonic/gin" ) diff --git a/internal/cms_api/message_cms/message.go b/internal/cms_api/message_cms/message.go index 277076271..c3f6cf231 100644 --- a/internal/cms_api/message_cms/message.go +++ b/internal/cms_api/message_cms/message.go @@ -32,7 +32,7 @@ func GetChatLogs(c *gin.Context) { } utils.CopyStructFields(&reqPb, &req) log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req) - etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImMessageCMSName, reqPb.OperationID) + etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAdminCMSName, reqPb.OperationID) if etcdConn == nil { errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil" log.NewError(reqPb.OperationID, errMsg) @@ -43,7 +43,7 @@ func GetChatLogs(c *gin.Context) { respPb, err := client.GetChatLogs(context.Background(), &reqPb) if err != nil { log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetChatLogs rpc failed", err.Error()) - c.JSON(http.StatusInternalServerError, gin.H{"errCode": 400, "errMsg": err.Error()}) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) return } for _, v := range respPb.ChatLogs { diff --git a/internal/rpc/admin_cms/admin_cms.go b/internal/rpc/admin_cms/admin_cms.go index ead456677..392841fe2 100644 --- a/internal/rpc/admin_cms/admin_cms.go +++ b/internal/rpc/admin_cms/admin_cms.go @@ -296,7 +296,7 @@ func (s *adminCMSServer) GetActiveUser(_ context.Context, req *pbAdminCMS.GetAct for _, activeUser := range activeUsers { resp.Users = append(resp.Users, &pbAdminCMS.UserResp{ - UserId: activeUser.Id, + UserId: activeUser.ID, NickName: activeUser.Name, MessageNum: int32(activeUser.MessageNum), }, diff --git a/pkg/cms_api_struct/message_cms.go b/pkg/cms_api_struct/message_cms.go index 891e24772..2b6dfb4be 100644 --- a/pkg/cms_api_struct/message_cms.go +++ b/pkg/cms_api_struct/message_cms.go @@ -13,6 +13,7 @@ type GetChatLogsReq struct { GroupID string `json:"groupID"` SendTime string `json:"sendTime"` RequestPagination + OperationID string `json:"operationID"` } type GetChatLogsResp struct { diff --git a/pkg/common/db/model_struct.go b/pkg/common/db/model_struct.go index 29486c16d..7023996d7 100644 --- a/pkg/common/db/model_struct.go +++ b/pkg/common/db/model_struct.go @@ -92,7 +92,7 @@ type Group struct { Notification string `gorm:"column:notification;size:255" json:"notification"` Introduction string `gorm:"column:introduction;size:255" json:"introduction"` FaceURL string `gorm:"column:face_url;size:255" json:"faceURL"` - CreateTime time.Time `gorm:"column:create_time"` + CreateTime time.Time `gorm:"column:create_time;index:create_time"` Ex string `gorm:"column:ex" json:"ex;size:1024" json:"ex"` Status int32 `gorm:"column:status"` CreatorUserID string `gorm:"column:creator_user_id;size:64"` @@ -176,7 +176,7 @@ type User struct { Birth time.Time `gorm:"column:birth"` Email string `gorm:"column:email;size:64"` Ex string `gorm:"column:ex;size:1024"` - CreateTime time.Time `gorm:"column:create_time"` + CreateTime time.Time `gorm:"column:create_time;index:create_time"` AppMangerLevel int32 `gorm:"column:app_manger_level"` GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"` InvitationCode string `gorm:"column:invitation_code"` @@ -232,7 +232,7 @@ type ChatLog struct { SenderPlatformID int32 `gorm:"column:sender_platform_id" json:"senderPlatformID"` SenderNickname string `gorm:"column:sender_nick_name;type:varchar(255)" json:"senderNickname"` SenderFaceURL string `gorm:"column:sender_face_url;type:varchar(255)" json:"senderFaceURL"` - SessionType int32 `gorm:"column:session_type;;index:session_type" json:"sessionType"` + SessionType int32 `gorm:"column:session_type;index:session_type" json:"sessionType"` MsgFrom int32 `gorm:"column:msg_from" json:"msgFrom"` ContentType int32 `gorm:"column:content_type;index:content_type" json:"contentType"` Content string `gorm:"column:content;type:varchar(3000);index:search" json:"content"` diff --git a/pkg/common/db/mysql_model/im_mysql_model/statistics_model.go b/pkg/common/db/mysql_model/im_mysql_model/statistics_model.go index b8d3e9d17..4a8626cf0 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/statistics_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/statistics_model.go @@ -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 diff --git a/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go b/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go index b4bfc1038..b848bff41 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/user_black_list_model.go @@ -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