|
@@ -1,8 +1,13 @@
|
|
package service
|
|
package service
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "context"
|
|
|
|
+ "database/sql"
|
|
|
|
+ "encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "go.etcd.io/etcd/client/v3/concurrency"
|
|
"log"
|
|
"log"
|
|
|
|
+ "strconv"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"app.yhyue.com/moapp/MessageCenter/entity"
|
|
"app.yhyue.com/moapp/MessageCenter/entity"
|
|
@@ -11,48 +16,47 @@ import (
|
|
|
|
|
|
// 类型的顺序
|
|
// 类型的顺序
|
|
const order = "1,4"
|
|
const order = "1,4"
|
|
|
|
+const EtcdCount = "Count.%s.%s" //etcd 消息未读数量 Count.用户id.消息类型=数量
|
|
|
|
|
|
func SendMsg(this message.SendMsgRequest) (int64, string) {
|
|
func SendMsg(this message.SendMsgRequest) (int64, string) {
|
|
- orm := entity.Engine.NewSession()
|
|
|
|
- defer orm.Close()
|
|
|
|
- err := orm.Begin()
|
|
|
|
- fmt.Println(err)
|
|
|
|
- count, _ := orm.Table("conversation").Where("receive_id = ? and send_id = ?", this.ReceiveUserId, this.SendUserId).Count()
|
|
|
|
|
|
+ //orm := entity.Engine.NewSession()
|
|
|
|
+ //defer orm.Close()
|
|
|
|
+ //err := orm.Begin()
|
|
|
|
+ //fmt.Println(err)
|
|
|
|
+ count := entity.Mysql.Count("conversation", map[string]interface{}{"receive_id": this.ReceiveUserId, "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)
|
|
|
|
+ values ("%s",'%s','%s','%s','%s','%s','%s','%d','%s',0,'%s',0,1);`
|
|
|
|
+ sql3 = fmt.Sprintf(sql3, this.Appid, this.ReceiveUserId, this.ReceiveName, this.SendUserId, this.SendName, this.Title, this.Content, this.MsgType, this.Link, time.Now().Format("2006-01-02 15:04:05"))
|
|
if count < 1 {
|
|
if count < 1 {
|
|
sql1 := `INSERT INTO conversation(appid,` + "`key`" + `,user_id,receive_id,receive_name,send_id,send_name,sort,createtime)
|
|
sql1 := `INSERT INTO conversation(appid,` + "`key`" + `,user_id,receive_id,receive_name,send_id,send_name,sort,createtime)
|
|
values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
|
|
values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
|
|
sql1 = fmt.Sprintf(sql1, this.Appid, this.SendUserId, this.ReceiveUserId, this.ReceiveName, this.SendUserId, this.SendName, time.Now().Format("2006-01-02 15:04:05"))
|
|
sql1 = fmt.Sprintf(sql1, this.Appid, this.SendUserId, this.ReceiveUserId, this.ReceiveName, this.SendUserId, this.SendName, time.Now().Format("2006-01-02 15:04:05"))
|
|
- //_, err = orm.Table("conversation").Insert(&conversation)
|
|
|
|
- _, err = orm.Exec(sql1)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Panicln("会话创建失败:", err)
|
|
|
|
- orm.Rollback()
|
|
|
|
- return 0, "会话创建失败"
|
|
|
|
- }
|
|
|
|
- sql2 := `INSERT INTO conversation(appid,` + "`key`" + `,user_id,receive_id,receive_name,send_id,send_name,sort,createtime)
|
|
|
|
|
|
+ ok := entity.Mysql.ExecTx("发送消息事务", func(tx *sql.Tx) bool {
|
|
|
|
+ //插入会话表
|
|
|
|
+ _, err := entity.Mysql.DB.Exec(sql1)
|
|
|
|
+
|
|
|
|
+ sql2 := `INSERT INTO conversation(appid,` + "`key`" + `,user_id,receive_id,receive_name,send_id,send_name,sort,createtime)
|
|
values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
|
|
values ('%s','','%s','%s','%s','%s','%s',0,'%s');`
|
|
- sql2 = fmt.Sprintf(sql2, this.Appid, this.ReceiveUserId, this.SendUserId, this.SendName, this.ReceiveUserId, this.ReceiveName, time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
- //_, err = orm.Table("conversation").Insert(&conversation)
|
|
|
|
- _, err = orm.Exec(sql2)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Panicln("会话创建失败:", err)
|
|
|
|
- orm.Rollback()
|
|
|
|
- return 0, "会话创建失败"
|
|
|
|
|
|
+ sql2 = fmt.Sprintf(sql2, this.Appid, this.ReceiveUserId, this.SendUserId, this.SendName, this.ReceiveUserId, this.ReceiveName, time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
+ _, err = entity.Mysql.DB.Exec(sql2)
|
|
|
|
+ //插入消息表
|
|
|
|
+ _, err = entity.Mysql.DB.Exec(sql3)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ return true
|
|
|
|
+ })
|
|
|
|
+ if ok {
|
|
|
|
+ return 1, "消息发送成功"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- sql := `INSERT INTO message(appid,receive_userid,receive_name,send_userid,send_name,title,content,msg_type,link,cite_id,createtime,isRead,isdel)
|
|
|
|
- values ("%s",'%s','%s','%s','%s','%s','%s','%d','%s',0,'%s',0,1);`
|
|
|
|
- sql = fmt.Sprintf(sql, this.Appid, this.ReceiveUserId, this.ReceiveName, this.SendUserId, this.SendName, this.Title, this.Content, this.MsgType, this.Link, time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
- //_, err = orm.Table("conversation").Insert(&conversation)
|
|
|
|
- _, err = orm.Table("message").Exec(sql)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Panicln("消息发送失败:", err)
|
|
|
|
- orm.Rollback()
|
|
|
|
- return 0, "消息发送失败"
|
|
|
|
|
|
+ _, err := entity.Mysql.DB.Exec(sql3)
|
|
|
|
+ if err == nil {
|
|
|
|
+ go EtcdCountAdd(this.ReceiveUserId, strconv.Itoa(int(this.MsgType)))
|
|
|
|
+ return 1, "消息发送成功"
|
|
}
|
|
}
|
|
- orm.Commit()
|
|
|
|
- return 1, "消息发送成功"
|
|
|
|
|
|
+ return 0, "消息发送失败"
|
|
}
|
|
}
|
|
|
|
|
|
func FindUserMsg(this message.FindUserMsgReq) message.FindUserMsgRes {
|
|
func FindUserMsg(this message.FindUserMsgReq) message.FindUserMsgRes {
|
|
@@ -107,13 +111,167 @@ func FindUserMsg(this message.FindUserMsgReq) message.FindUserMsgRes {
|
|
|
|
|
|
// 指定分类未读消息合计
|
|
// 指定分类未读消息合计
|
|
func ClassCountUnread(msgType int, userId string, appId string) (int64, string, int64) {
|
|
func ClassCountUnread(msgType int, userId string, appId string) (int64, string, int64) {
|
|
- orm := entity.Engine
|
|
|
|
- count, err := orm.Table("message").Where("msg_type=? and receive_userid=? and isdel=1 and appid=? and isRead=0", msgType, userId, appId).Count()
|
|
|
|
- // data, err := orm.Sql("explain select count(*) from message where msg_type = ? and receive_userid=? and isdel=1 and appid=?", msgType, userId, appId).QueryInterface()
|
|
|
|
- if err != nil {
|
|
|
|
- log.Println(err)
|
|
|
|
- return 0, "查询未读消息失败", 0
|
|
|
|
|
|
+ //orm := entity.Engine
|
|
|
|
+ //count, err := orm.Table("message").Where("msg_type=? and receive_userid=? and isdel=1 and appid=? and isRead=0", msgType, userId, appId).Count()
|
|
|
|
+ query := map[string]interface{}{
|
|
|
|
+ "msg_type": msgType,
|
|
|
|
+ "receive_userid": userId,
|
|
|
|
+ "isdel": 1,
|
|
|
|
+ "appid": appId,
|
|
|
|
+ "isRead": 0,
|
|
}
|
|
}
|
|
- //log.Println(count)
|
|
|
|
|
|
+ count := entity.Mysql.Count("message", query)
|
|
return 1, "查询指定分类未读消息成功", count
|
|
return 1, "查询指定分类未读消息成功", count
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// etcd数量加1
|
|
|
|
+func EtcdCountAdd(userId, msgType string) {
|
|
|
|
+ s1, err := concurrency.NewSession(entity.EtcdCli)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ defer s1.Close()
|
|
|
|
+ keyString := fmt.Sprintf(EtcdCount, userId, msgType)
|
|
|
|
+ m1 := concurrency.NewMutex(s1, keyString)
|
|
|
|
+ // 会话s1获取锁
|
|
|
|
+ if err := m1.Lock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("获取锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ // 操作数量
|
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
+ resp, err := s1.Client().Get(ctx, keyString)
|
|
|
|
+ cancel()
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("get from etcd failed, err:%v\n", err)
|
|
|
|
+
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ var count int
|
|
|
|
+ for _, ev := range resp.Kvs {
|
|
|
|
+ if ev.Value != nil {
|
|
|
|
+ err := json.Unmarshal([]byte(ev.Value), &count)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println("etcd get err:", err)
|
|
|
|
+ } else {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ ctx, cancel = context.WithTimeout(context.Background(), time.Second)
|
|
|
|
+ _, err = s1.Client().Put(ctx, keyString, strconv.Itoa(count+1))
|
|
|
|
+ cancel()
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("put to etcd failed, err:%v\n", err)
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//单条消息,-1
|
|
|
|
+func EtcdCountMinusOne(userId, msgType string) {
|
|
|
|
+ s1, err := concurrency.NewSession(entity.EtcdCli)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ defer s1.Close()
|
|
|
|
+ keyString := fmt.Sprintf(EtcdCount, userId, msgType)
|
|
|
|
+ m1 := concurrency.NewMutex(s1, keyString)
|
|
|
|
+ // 会话s1获取锁
|
|
|
|
+ if err := m1.Lock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("获取锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ // 操作数量
|
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
+ resp, err := s1.Client().Get(ctx, keyString)
|
|
|
|
+ cancel()
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("get from etcd failed, err:%v\n", err)
|
|
|
|
+
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ var count int
|
|
|
|
+ for _, ev := range resp.Kvs {
|
|
|
|
+ if ev.Value != nil {
|
|
|
|
+ err := json.Unmarshal([]byte(ev.Value), &count)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println("etcd get err:", err)
|
|
|
|
+ } else {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ ctx, cancel = context.WithTimeout(context.Background(), time.Second)
|
|
|
|
+ var fin string
|
|
|
|
+ if count > 0 {
|
|
|
|
+ fin = strconv.Itoa(count - 1)
|
|
|
|
+ } else {
|
|
|
|
+ fin = "0"
|
|
|
|
+ }
|
|
|
|
+ _, err = s1.Client().Put(ctx, keyString, fin)
|
|
|
|
+ cancel()
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("put to etcd failed, err:%v\n", err)
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 消息类别置0
|
|
|
|
+func EtcdSetCountZero(userId, msgType string) {
|
|
|
|
+ log.Println(" 消息类别置0", userId, msgType)
|
|
|
|
+ s1, err := concurrency.NewSession(entity.EtcdCli)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ defer s1.Close()
|
|
|
|
+ keyString := fmt.Sprintf(EtcdCount, userId, msgType)
|
|
|
|
+ m1 := concurrency.NewMutex(s1, keyString)
|
|
|
|
+ // 会话s1获取锁
|
|
|
|
+ if err := m1.Lock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("获取锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ // 操作数量
|
|
|
|
+
|
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
+ _, err = s1.Client().Put(ctx, keyString, "0")
|
|
|
|
+ cancel()
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("put to etcd failed, err:%v\n", err)
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 释放锁
|
|
|
|
+ if err := m1.Unlock(context.TODO()); err != nil {
|
|
|
|
+ log.Fatal("释放锁失败", err)
|
|
|
|
+ }
|
|
|
|
+}
|