|
@@ -1,17 +1,21 @@
|
|
package service
|
|
package service
|
|
|
|
|
|
import (
|
|
import (
|
|
- quitl "app.yhyue.com/moapp/jybase/common"
|
|
|
|
- "app.yhyue.com/moapp/jybase/encrypt"
|
|
|
|
- util "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
|
|
|
|
- IC "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/init"
|
|
|
|
- "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
|
|
|
|
"database/sql"
|
|
"database/sql"
|
|
"fmt"
|
|
"fmt"
|
|
"log"
|
|
"log"
|
|
|
|
+ "strconv"
|
|
"strings"
|
|
"strings"
|
|
"sync"
|
|
"sync"
|
|
"time"
|
|
"time"
|
|
|
|
+
|
|
|
|
+ quitl "app.yhyue.com/moapp/jybase/common"
|
|
|
|
+ "app.yhyue.com/moapp/jybase/date"
|
|
|
|
+ "app.yhyue.com/moapp/jybase/encrypt"
|
|
|
|
+ util "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/entity"
|
|
|
|
+ IC "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/init"
|
|
|
|
+ "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
|
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
)
|
|
|
|
|
|
type MessaggeService struct{}
|
|
type MessaggeService struct{}
|
|
@@ -866,3 +870,127 @@ func NewEndId(messageId, iType int64) (newUserId, entUserId int64) {
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// 聊天
|
|
|
|
+// 包含 1v1 ,群聊,群发
|
|
|
|
+/*
|
|
|
|
+入参 appid,title,content,sendid,link,item,messagetype,receiverIds,groupIds
|
|
|
|
+*/
|
|
|
|
+func (this *MessaggeService) Chat(in *messagecenter.MessageSaveReq) bool {
|
|
|
|
+ return IC.BaseMysql.ExecTx("消息存储", func(tx *sql.Tx) bool {
|
|
|
|
+ nowTime := time.Now().Format(date.Date_Full_Layout)
|
|
|
|
+ isGroup := len(in.GroupIds) > 0
|
|
|
|
+ isOneToOne := len(in.ReceiverIds) > 0
|
|
|
|
+ createperson := strconv.Itoa(int(in.SendId))
|
|
|
|
+ messageId := MessageAdd(tx, in.Appid, in.Title, in.Content, createperson, in.Link, in.Item, in.MessageType)
|
|
|
|
+ //是否客服介入
|
|
|
|
+ isCustomerServiceAccess := 0
|
|
|
|
+
|
|
|
|
+ //群聊
|
|
|
|
+ if isGroup {
|
|
|
|
+ fieids := []string{"appid", "messag_id", "type", "send_user_id", "send_user_type", "receive_user_id", "receive_user_type", "own_type", "own_id", "create_time", "chat_group_id", "isread"}
|
|
|
|
+ for _, v := range in.GroupIds {
|
|
|
|
+ args := []interface{}{}
|
|
|
|
+ summaryArgs := []interface{}{}
|
|
|
|
+ args = append(args, in.Appid, messageId, in.Type, in.SendId, 2, in.SendId, 2, 2, in.SendId, nowTime, v, 1)
|
|
|
|
+ groupUser := GetUserByGroupId(tx, v, in.SendId)
|
|
|
|
+ for _, vv := range groupUser {
|
|
|
|
+ log.Println("获取到群组下员工:", vv)
|
|
|
|
+ args = append(args, in.Appid, messageId, in.Type, in.SendId, 2, vv, 2, 2, vv, nowTime, v, 0)
|
|
|
|
+ }
|
|
|
|
+ summaryArgs = append(summaryArgs, in.SendId, nil, nil, v, messageId, nil, isCustomerServiceAccess, nowTime)
|
|
|
|
+ MessageMailBoxAdd(tx, fieids, args)
|
|
|
|
+ //最后一次聊天
|
|
|
|
+ SocializeSummaryAdd(tx, summaryArgs)
|
|
|
|
+ //更新未读消息
|
|
|
|
+ GroupUserUnReadUpdate(tx, groupUser)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //1v1
|
|
|
|
+ if isOneToOne {
|
|
|
|
+ fieids := []string{"appid", "messag_id", "type", "send_user_id", "send_user_type", "receive_user_id", "receive_user_type", "own_type", "own_id", "create_time", "isread"}
|
|
|
|
+ args := []interface{}{}
|
|
|
|
+ summaryArgs := []interface{}{}
|
|
|
|
+ args = append(args, in.Appid, messageId, in.Type, in.SendId, 2, in.SendId, 2, 2, in.SendId, nowTime, 1)
|
|
|
|
+ for _, v := range in.ReceiverIds {
|
|
|
|
+ args = append(args, in.Appid, messageId, in.Type, in.SendId, 2, v, 2, 2, v, nowTime, 0)
|
|
|
|
+ //发送方,未读消息应该是0
|
|
|
|
+ summaryArgs = append(summaryArgs, in.SendId, v, nil, nil, messageId, 0, isCustomerServiceAccess, nowTime)
|
|
|
|
+ //接收方,未读消息应该是1
|
|
|
|
+ summaryArgs = append(summaryArgs, v, in.SendId, nil, nil, messageId, 1, isCustomerServiceAccess, nowTime)
|
|
|
|
+ }
|
|
|
|
+ MessageMailBoxAdd(tx, fieids, args)
|
|
|
|
+ //最后一次聊天
|
|
|
|
+ SocializeSummaryAdd(tx, summaryArgs)
|
|
|
|
+ }
|
|
|
|
+ return true
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 消息存储
|
|
|
|
+func MessageAdd(tx *sql.Tx, appid, title, content, createperson, link string, item, messageType int64) int64 {
|
|
|
|
+ nowTime := time.Now()
|
|
|
|
+ message := map[string]interface{}{
|
|
|
|
+ "appid": appid,
|
|
|
|
+ "title": title,
|
|
|
|
+ "content": content,
|
|
|
|
+ "item": item,
|
|
|
|
+ "type": messageType,
|
|
|
|
+ "link": link,
|
|
|
|
+ "create_time": nowTime.Format(date.Date_Full_Layout),
|
|
|
|
+ "create_person": createperson, //系统消息时,创建人是群聊id或接收人id
|
|
|
|
+ }
|
|
|
|
+ return IC.BaseMysql.InsertByTx(tx, util.SOCIALIZE_MESSAGE, message)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 消息信息箱存储
|
|
|
|
+// types 2:点对点 3:群消息
|
|
|
|
+func MessageMailBoxAdd(tx *sql.Tx, fieids []string, args []interface{}) (int64, int64) {
|
|
|
|
+ length, lastId := IC.BaseMysql.InsertBatchByTx(tx, util.SOCIALIZE_MESSAGE_MAILBOX, fieids, args)
|
|
|
|
+ log.Println("MessageMailBoxAdd length:", length, "MessageMailBoxAdd lastId:", lastId)
|
|
|
|
+ return length, lastId
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 获取除发送人以外的群成员
|
|
|
|
+func GetUserByGroupId(tx *sql.Tx, groupId, sendId int64) []int64 {
|
|
|
|
+ arr := []int64{}
|
|
|
|
+ data := IC.BaseMysql.SelectBySqlByTx(tx, "select position_id from "+util.SOCIALIZE_CHAT_GROUP_PERSON+" where status = 1 and chat_group_id = ? AND position_id != ? ", groupId, sendId)
|
|
|
|
+ log.Println("~~~~", data)
|
|
|
|
+ if data == nil || len(*data) <= 0 {
|
|
|
|
+ return arr
|
|
|
|
+ }
|
|
|
|
+ for _, v := range *data {
|
|
|
|
+ position_id := quitl.Int64All(v["position_id"])
|
|
|
|
+ if position_id == 0 {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ arr = append(arr, position_id)
|
|
|
|
+ }
|
|
|
|
+ return arr
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//最后一次聊天存储
|
|
|
|
+func SocializeSummaryAdd(tx *sql.Tx, args []interface{}) (int64, int64) {
|
|
|
|
+ fieids := []string{"my_position_id", "your_position_id", "ent_id", "chat_group_id", "message_id", "unread", "customer_service_access", "timestamp"}
|
|
|
|
+
|
|
|
|
+ length, lastId := IC.BaseMysql.InsertBatchByTx(tx, util.SOCIALIZE_SUMMARY, fieids, args)
|
|
|
|
+
|
|
|
|
+ log.Println("SocializeSummaryAdd length:", length, "SocializeSummaryAdd lastId:", lastId)
|
|
|
|
+
|
|
|
|
+ return length, lastId
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//群组人员未读消息更新
|
|
|
|
+func GroupUserUnReadUpdate(tx *sql.Tx, ids []int64) bool {
|
|
|
|
+ whs := []string{}
|
|
|
|
+ for i := 0; i < len(ids); i++ {
|
|
|
|
+ whs = append(whs, "?")
|
|
|
|
+ }
|
|
|
|
+ wh := strings.Join(whs, ",")
|
|
|
|
+ interfaces := gconv.Interfaces(ids)
|
|
|
|
+ count := IC.BaseMysql.UpdateOrDeleteBySql(`UPDATE `+util.SOCIALIZE_CHAT_GROUP_PERSON+` SET unread = unread + 1 WHERE id in (`+wh+`)`, interfaces...)
|
|
|
|
+ if count > 0 {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ return true
|
|
|
|
+}
|