|
@@ -6,6 +6,7 @@ import (
|
|
|
"app.yhyue.com/moapp/MessageCenter/util"
|
|
|
"app.yhyue.com/moapp/jybase/common"
|
|
|
"app.yhyue.com/moapp/jybase/redis"
|
|
|
+ "database/sql"
|
|
|
"fmt"
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
"log"
|
|
@@ -99,6 +100,29 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
|
|
|
var (
|
|
|
unread, count int64
|
|
|
)
|
|
|
+ data := new(message.UserMsgList)
|
|
|
+ if !this.IsMsgList && !this.IsColumnNewMsg && !this.IsColumn { //消息未读数统计
|
|
|
+ var isUnread, isColumn int
|
|
|
+ for _, v := range entity.MessageColumn {
|
|
|
+ if util.IntAll(v["group_id"]) > 0 && util.IntAll(v["group_id"]) < 999 {
|
|
|
+ isColumn++ //去除全部与私信
|
|
|
+ key := fmt.Sprintf(MsgCountKey, this.UserId, util.IntAll(v["group_id"]))
|
|
|
+ if exists, _ := redis.Exists(redisModule, key); exists {
|
|
|
+ isUnread++
|
|
|
+ unread += util.Int64All(redis.GetInt(redisModule, key))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if isUnread != isColumn {
|
|
|
+ m := &MessageService{}
|
|
|
+ unread = m.CountUnread(this.UserId)
|
|
|
+ }
|
|
|
+ if this.IsContainLetter { //私信统计
|
|
|
+ unread += unreadMsg(this)
|
|
|
+ }
|
|
|
+ data.Unread = unread
|
|
|
+ return data
|
|
|
+ }
|
|
|
cquery := map[string]interface{}{
|
|
|
"receive_userid": this.UserId,
|
|
|
"isdel": 1,
|
|
@@ -110,7 +134,7 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
|
|
|
if this.Read != -1 {
|
|
|
cquery["isRead"] = this.Read
|
|
|
}
|
|
|
- data := new(message.UserMsgList)
|
|
|
+
|
|
|
//获取栏目下的数据
|
|
|
sData := make(map[string][]*message.Messages)
|
|
|
t := time.Now()
|
|
@@ -150,24 +174,9 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
//消息栏目下的最新消息
|
|
|
var columnData []*message.AllSortData
|
|
|
- var msgTypes []string
|
|
|
- for _, v := range entity.MessageColumn {
|
|
|
- if common.IntAll(v["group_id"]) > 0 {
|
|
|
- msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, common.InterfaceToStr(v["group_id"])))
|
|
|
- }
|
|
|
- }
|
|
|
if this.IsColumn {
|
|
|
- columnUnread := make(map[string]int64)
|
|
|
- query := entity.Mysql.SelectBySql(fmt.Sprintf("SELECT group_id,COUNT(CASE WHEN isRead=0 THEN 1 END) as count FROM message where receive_userid=? and isdel=1 and appid=? GROUP BY group_id ORDER BY FIELD(`group_id`,%s)", strings.Join(msgTypes, ",")), this.UserId, this.Appid)
|
|
|
- log.Println("消息列表耗时2:", time.Since(t))
|
|
|
- if query != nil && len(*query) > 0 {
|
|
|
- for _, v := range *query {
|
|
|
- columnUnread[common.InterfaceToStr(v["group_id"])] = common.Int64All(v["count"])
|
|
|
- }
|
|
|
- }
|
|
|
for _, v := range entity.MessageColumn {
|
|
|
var column message.AllSortData
|
|
|
column.Name = common.InterfaceToStr(v["name"])
|
|
@@ -175,10 +184,10 @@ func UserMsgList(this *message.UserMsgListReq) *message.UserMsgList {
|
|
|
column.MsgType = common.Int64All(v["group_id"])
|
|
|
if column.Name == "私信" {
|
|
|
column.UnreadMessages = unreadMsg(this)
|
|
|
- } else {
|
|
|
+ } else if common.IntAll(v["group_id"]) > 0 {
|
|
|
//消息未读数
|
|
|
msgType := common.InterfaceToStr(v["group_id"])
|
|
|
- column.UnreadMessages = columnUnread[msgType]
|
|
|
+ column.UnreadMessages = util.Int64All(redis.GetInt(redisModule, fmt.Sprintf(MsgCountKey, this.UserId, util.IntAll(msgType))))
|
|
|
column.Data = sData[msgType]
|
|
|
}
|
|
|
unread += column.UnreadMessages
|
|
@@ -270,7 +279,6 @@ func UserUnreadMsgList(this *message.UserUnreadMsgListReq) (int64, []*message.Me
|
|
|
"appid": this.Appid,
|
|
|
"isRead": 0,
|
|
|
}
|
|
|
- log.Println(query)
|
|
|
res := entity.Mysql.Find("message", query, "", "createtime desc", (int(this.OffSet)-1)*int(this.PageSize), int(this.PageSize))
|
|
|
if res != nil && len(*res) > 0 {
|
|
|
for _, val := range *res {
|
|
@@ -307,6 +315,9 @@ func UserUnreadMsgList(this *message.UserUnreadMsgListReq) (int64, []*message.Me
|
|
|
}
|
|
|
|
|
|
func MessageGetLast(this *message.UserMsgListReq) *message.Messages {
|
|
|
+ if !this.IsMsgList && !this.IsColumnNewMsg && !this.IsColumn {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
query := map[string]interface{}{
|
|
|
"receive_userid": this.UserId,
|
|
|
"isdel": 1,
|
|
@@ -343,34 +354,42 @@ func MessageGetLast(this *message.UserMsgListReq) *message.Messages {
|
|
|
// MsgCountAdd 消息未读数量加1
|
|
|
func MsgCountAdd(userId, appId string, msgType int64) bool {
|
|
|
keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
|
|
|
- in := redis.Incr(redisModule, keyString)
|
|
|
- FindUserMsg(message.FindUserMsgReq{
|
|
|
- UserId: userId,
|
|
|
- Appid: appId,
|
|
|
- OffSet: 1,
|
|
|
- PageSize: 5,
|
|
|
- MsgType: -1,
|
|
|
- Read: 0,
|
|
|
- }, true)
|
|
|
- return in > 0
|
|
|
+ exists, _ := redis.Exists(redisModule, keyString)
|
|
|
+ if exists {
|
|
|
+ in := redis.Incr(redisModule, keyString)
|
|
|
+ FindUserMsg(message.FindUserMsgReq{
|
|
|
+ UserId: userId,
|
|
|
+ Appid: appId,
|
|
|
+ OffSet: 1,
|
|
|
+ PageSize: 5,
|
|
|
+ MsgType: -1,
|
|
|
+ Read: 0,
|
|
|
+ }, true)
|
|
|
+ return in > 0
|
|
|
+ }
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
// MsgCountMinusOne 根据消息类型未读消息数量减1
|
|
|
func MsgCountMinusOne(userId, appId string, msgType int64) bool {
|
|
|
keyString := fmt.Sprintf(MsgCountKey, userId, msgType)
|
|
|
- FindUserMsg(message.FindUserMsgReq{
|
|
|
- UserId: userId,
|
|
|
- Appid: appId,
|
|
|
- OffSet: 1,
|
|
|
- PageSize: 5,
|
|
|
- MsgType: -1,
|
|
|
- Read: 0,
|
|
|
- }, true)
|
|
|
- if redis.GetInt(redisModule, keyString) <= 0 {
|
|
|
- return redis.Put(redisModule, keyString, 0, -1)
|
|
|
+ exists, _ := redis.Exists(redisModule, keyString)
|
|
|
+ if exists {
|
|
|
+ FindUserMsg(message.FindUserMsgReq{
|
|
|
+ UserId: userId,
|
|
|
+ Appid: appId,
|
|
|
+ OffSet: 1,
|
|
|
+ PageSize: 5,
|
|
|
+ MsgType: -1,
|
|
|
+ Read: 0,
|
|
|
+ }, true)
|
|
|
+ if redis.GetInt(redisModule, keyString) <= 0 {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ in := redis.Decrby(redisModule, keyString, 1)
|
|
|
+ return in > 0
|
|
|
}
|
|
|
- in := redis.Decrby(redisModule, keyString, 1)
|
|
|
- return in > 0
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
// MsgCountZero 把该消息类型未读数量置0
|
|
@@ -422,22 +441,24 @@ func MultSave(this message.MultipleSaveMsgReq) (int64, string) {
|
|
|
c := entity.Mysql.Count("conversation", map[string]interface{}{"receive_id": v, "send_id": this.SendUserId})
|
|
|
sql3 := `INSERT INTO message(appid,receive_userid,receive_name,send_userid,send_name,title,content,msg_type,link,cite_id,createtime,isRead,isdel,msg_log_id,show_buoy,show_content,group_id,position_id) values ("%s",'%s','%s','%s','%s','%s','%s',%d,'%s',0,'%s',0,1,%d,%d,'%s',%d,?);`
|
|
|
sql3 = fmt.Sprintf(sql3, this.Appid, v, userName, this.SendUserId, this.SendName, this.Title, this.Content, this.MsgType, this.Link, time.Now().Format("2006-01-02 15:04:05"), this.MsgLogId, this.ShowBuoy, this.ShowContent, group_id)
|
|
|
-
|
|
|
if c <= 0 {
|
|
|
sql1 := `INSERT INTO conversation(appid,secret_key,user_id,receive_id,receive_name,send_id,send_name,sort,createtime) values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
|
|
|
sql1 = fmt.Sprintf(sql1, this.Appid, this.SendUserId, v, userName, this.SendUserId, this.SendName, time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ ok := entity.Mysql.ExecTx("发送消息事务", func(tx *sql.Tx) bool {
|
|
|
+ //插入会话表
|
|
|
+ in1 := entity.Mysql.InsertBySqlByTx(tx, sql1)
|
|
|
+ sql2 := `INSERT INTO conversation(appid,secret_key,user_id,receive_id,receive_name,send_id,send_name,sort,createtime) values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
|
|
|
+ sql2 = fmt.Sprintf(sql2, this.Appid, v, this.SendUserId, this.SendName, v, userName, time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ in2 := entity.Mysql.InsertBySqlByTx(tx, sql2)
|
|
|
+ //插入消息表
|
|
|
+ in3 := entity.Mysql.InsertBySqlByTx(tx, sql3, common.If(positionId != 0, positionId, nil))
|
|
|
+ logx.Info(in1, in2, in3)
|
|
|
|
|
|
- //插入会话表
|
|
|
- in1 := entity.Mysql.InsertBySql(sql1)
|
|
|
- sql2 := `INSERT INTO conversation(appid,secret_key,user_id,receive_id,receive_name,send_id,send_name,sort,createtime) values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
|
|
|
- sql2 = fmt.Sprintf(sql2, this.Appid, v, this.SendUserId, this.SendName, v, userName, time.Now().Format("2006-01-02 15:04:05"))
|
|
|
- in2 := entity.Mysql.InsertBySql(sql2)
|
|
|
- //插入消息表
|
|
|
- in3 := entity.Mysql.InsertBySql(sql3, common.If(positionId != 0, positionId, nil))
|
|
|
- logx.Info(in1, in2, in3)
|
|
|
-
|
|
|
- if in1 > -1 && in2 > -1 && in3 > -1 {
|
|
|
- ok1 := MsgCountAdd(v, this.Appid, this.MsgType)
|
|
|
+ return in1 > -1 && in2 > -1 && in3 > -1
|
|
|
+ })
|
|
|
+ logx.Info("执行事务是否成功:", ok)
|
|
|
+ if ok {
|
|
|
+ ok1 := MsgCountAdd(v, this.Appid, util.Int64All(group_id))
|
|
|
if !ok1 {
|
|
|
log.Println("存redis:", ok1, v)
|
|
|
}
|
|
@@ -446,7 +467,7 @@ func MultSave(this message.MultipleSaveMsgReq) (int64, string) {
|
|
|
in := entity.Mysql.InsertBySql(sql3, common.If(positionId != 0, positionId, nil))
|
|
|
logx.Info("插入消息返回 in1 id:", in)
|
|
|
if in > -1 {
|
|
|
- ok := MsgCountAdd(v, this.Appid, this.MsgType)
|
|
|
+ ok := MsgCountAdd(v, this.Appid, util.Int64All(group_id))
|
|
|
if !ok {
|
|
|
log.Println("存redis:", ok, v)
|
|
|
}
|