|
@@ -1,17 +1,21 @@
|
|
|
package service
|
|
|
|
|
|
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"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"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{}
|
|
@@ -565,6 +569,7 @@ func UserSynchronousList(customerServiceId, userId, entId, messageId int64, crea
|
|
|
func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string]interface{} {
|
|
|
sqlStr := ""
|
|
|
lastStr := ""
|
|
|
+ positionStr := ""
|
|
|
if in.LastId > 0 {
|
|
|
if in.Sort == "asc" {
|
|
|
lastStr = fmt.Sprintf("AND a.messag_id > %d ", in.LastId)
|
|
@@ -587,7 +592,34 @@ func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string
|
|
|
util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.BASE_USER, util.BASE_USER,
|
|
|
in.NewUserId, in.NewUserId, in.SendId, in.SendId, in.NewUserId, lastStr, in.PageSize)
|
|
|
break
|
|
|
+ case 3: //群聊天
|
|
|
+
|
|
|
+ sqlStr = fmt.Sprintf("SELECT a.messag_id AS messageId,b.*,"+
|
|
|
+ "IF ( a.own_id = a.send_user_id, 1, 2 ) AS fool,"+
|
|
|
+ "IF ( a.own_id = a.send_user_id, 0, a.send_user_id ) AS send_position_id,"+
|
|
|
+ "a.send_user_type,a.type AS itemType FROM %s a "+
|
|
|
+ "LEFT JOIN %s b ON a.messag_id = b.id "+
|
|
|
+ "WHERE a.own_type = 1 AND a.own_id = %d "+
|
|
|
+ "AND chat_group_id = %d AND a.type IN ( 3, 6 ) "+
|
|
|
+ "ORDER BY a.create_time desc,a.id DESC "+
|
|
|
+ "LIMIT 0, %d",
|
|
|
+ util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, in.SendId,
|
|
|
+ in.ChatGroupId, in.PageSize)
|
|
|
case 4, 5, 6, 7: //客服聊天
|
|
|
+ //查询用户所有的职位id
|
|
|
+ sqlPosition := fmt.Sprintf("SELECT b.id FROM %s a LEFT JOIN %s b ON a.id = b.user_id "+
|
|
|
+ "WHERE a.phone = (SELECT a.phone FROM %s a LEFT JOIN %s b ON a.id = b.user_id "+
|
|
|
+ "WHERE b.id = %d)",
|
|
|
+ util.BASE_USER, util.BASE_POSITION, util.BASE_USER, util.BASE_POSITION, in.NewUserId)
|
|
|
+ positionArr := IC.BaseMysql.SelectBySql(sqlPosition)
|
|
|
+ for k, val := range *positionArr {
|
|
|
+ if k < len(*positionArr)-1 {
|
|
|
+ positionStr += quitl.ObjToString(val["id"]) + ","
|
|
|
+ } else {
|
|
|
+ positionStr += quitl.ObjToString(val["id"])
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if in.UserType == 1 {
|
|
|
//客服聊天记录查看
|
|
|
sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId,b.*, IF ( a.own_id = a.send_user_id, 1, 2 ) AS fool, a.send_user_type, a.type AS itemType, '' AS robotName, '' AS robotImg, c.customer_service_name AS setName , c.user_id "+
|
|
@@ -597,11 +629,11 @@ func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string
|
|
|
"WHERE a.own_type = 1 and a.iswithdraw = 0 "+
|
|
|
"AND (a.type = 5 or a.type=4 or a.type=6 or a.type=7 or a.type=8 ) "+
|
|
|
"AND c.ent_id = %d "+
|
|
|
- "AND c.user_id = %d %s "+
|
|
|
- "ORDER BY a.create_time desc ,a.id asc "+
|
|
|
+ "AND c.user_id in (%s) %s "+
|
|
|
+ "ORDER BY a.create_time desc ,a.id asc "+
|
|
|
"limit 0 , %d ",
|
|
|
util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION,
|
|
|
- in.EntId, in.SendId, lastStr, in.PageSize)
|
|
|
+ in.EntId, positionStr, lastStr, in.PageSize)
|
|
|
} else {
|
|
|
//用户聊天记录查看
|
|
|
sqlStr = fmt.Sprintf("SELECT a.messag_id as messageId,e.appraise as appraise, b.*, IF ( a.own_id = a.send_user_id, 1, 2 ) AS fool , a.send_user_type, a.type as itemType, d.nickname as robotName, d.headimage as robotImg, c.customer_service_name as setName "+
|
|
@@ -609,21 +641,21 @@ func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string
|
|
|
"LEFT JOIN %s b ON a.messag_id = b.id "+
|
|
|
"LEFT JOIN %s c ON IF ( a.send_user_type = 1, a.send_user_id, a.receive_user_id ) = c.id AND c.ent_id = %d AND c.user_id = %d "+
|
|
|
"LEFT JOIN %s d on c.ent_id=d.ent_id "+
|
|
|
- "LEFT JOIN %s e on e.messag_id=b.id "+
|
|
|
- "WHERE a.own_type = 2 and a.iswithdraw = 0 "+
|
|
|
- "AND a.own_id = %d "+
|
|
|
+ "WHERE a.own_type = 2 "+
|
|
|
+ "AND a.own_id in (%s) "+
|
|
|
"AND c.ent_id = %d "+
|
|
|
- "AND c.user_id = %d "+
|
|
|
- "AND ( a.type = 4 OR a.type = 5 or a.type=6 or a.type=7 or a.type=8) %s "+
|
|
|
- "ORDER BY a.create_time desc,a.id asc "+
|
|
|
+ "AND c.user_id in (%s) "+
|
|
|
+ "AND ( a.type = 4 OR a.type = 5 or a.type=6 or a.type=7) %s "+
|
|
|
+ "ORDER BY a.create_time desc ,a.id asc "+
|
|
|
"limit 0 , %d ",
|
|
|
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, in.SendId, in.NewUserId, util.SOCIALIZE_TENANT_ROBOT, util.SOCIALIZE_APPRAISE,
|
|
|
- in.NewUserId, in.SendId, in.NewUserId, lastStr, in.PageSize)
|
|
|
+ util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_MESSAGE, util.SOCIALIZE_CHAT_SESSION, in.SendId, in.NewUserId, util.SOCIALIZE_TENANT_ROBOT,
|
|
|
+ positionStr, in.SendId, positionStr, lastStr, in.PageSize)
|
|
|
}
|
|
|
break
|
|
|
}
|
|
|
log.Println(sqlStr)
|
|
|
data := IC.BaseMysql.SelectBySql(sqlStr)
|
|
|
+ //查询非自己发送消息得发送人名字
|
|
|
//自己头像处理
|
|
|
if in.UserType == 2 {
|
|
|
userData := IC.BaseMysql.FindOne(util.BASE_USER, map[string]interface{}{"id": in.NewUserId}, "headimg", "")
|
|
@@ -634,6 +666,15 @@ func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+ if in.MsgType == 3 && data != nil && len(*data) > 0 {
|
|
|
+ _, _, positionData := EntPerson(in.EntId, true)
|
|
|
+ for _, v := range *data {
|
|
|
+ positionId := quitl.IntAll(v["send_user_id"])
|
|
|
+ if positionId != 0 {
|
|
|
+ v["userName"] = positionData[positionId]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
go func() {
|
|
|
updateMap := map[string]interface{}{}
|
|
|
if len(*data) > 0 && data != nil {
|
|
@@ -642,34 +683,42 @@ func (b MessaggeService) FindMessage(in *messagecenter.MessageReq) *[]map[string
|
|
|
switch in.MsgType {
|
|
|
case 2: //点对点聊天
|
|
|
updateMap = map[string]interface{}{
|
|
|
- "own_type": 2,
|
|
|
- "own_id": in.NewUserId,
|
|
|
- "type": 2,
|
|
|
- "isread": 0,
|
|
|
+ "own_type": 2,
|
|
|
+ "own_id": in.NewUserId,
|
|
|
+ "send_user_id": in.SendId,
|
|
|
+ "type": 2,
|
|
|
+ "isread": 0,
|
|
|
}
|
|
|
IC.BaseMysql.Update(util.SOCIALIZE_MESSAGE_MAILBOX, updateMap, map[string]interface{}{"isread": 1, "read_time": time.Now().Local().Format(util.Date_Full_Layout)})
|
|
|
break
|
|
|
- case 4, 5:
|
|
|
+ case 3: //群聊天
|
|
|
+ sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
|
|
|
+ "WHERE a.own_type = 1 "+
|
|
|
+ "AND a.type IN (3,6) "+
|
|
|
+ "AND a.isread = 0 "+
|
|
|
+ "AND a.own_id = %d AND a.chat_group_id = %d",
|
|
|
+ util.SOCIALIZE_MESSAGE_MAILBOX, in.SendId, in.ChatGroupId)
|
|
|
+ case 4, 5: //UserType 2用户1客服
|
|
|
sqlStr := ""
|
|
|
if in.UserType == 1 {
|
|
|
sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
|
|
|
"WHERE a.own_type = 1 and a.iswithdraw = 0 "+
|
|
|
"AND a.type IN ( 4,5,6,7) "+
|
|
|
"AND a.isread = 0 "+
|
|
|
- "AND a.own_id IN ( SELECT b.id FROM %s b WHERE b.customer_service_id=%d AND b.user_id=%d )",
|
|
|
- util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, in.EntUserId, in.SendId)
|
|
|
+ "AND a.own_id IN ( SELECT b.id FROM %s b WHERE b.customer_service_id=%d AND b.user_id in (%s) )",
|
|
|
+ util.SOCIALIZE_MESSAGE_MAILBOX, util.SOCIALIZE_CHAT_SESSION, in.EntUserId, positionStr)
|
|
|
} else {
|
|
|
sqlStr = fmt.Sprintf("UPDATE %s a SET a.isread = 1, a.read_time = now( ) "+
|
|
|
"WHERE a.own_type = 2 and a.iswithdraw = 0 "+
|
|
|
"AND a.type IN ( 4,5,6,7 ) "+
|
|
|
"AND a.isread = 0 "+
|
|
|
- "AND a.own_id =%d ", util.SOCIALIZE_MESSAGE_MAILBOX, in.NewUserId)
|
|
|
+ "AND a.own_id in (%s) ", util.SOCIALIZE_MESSAGE_MAILBOX, positionStr)
|
|
|
}
|
|
|
IC.BaseMysql.UpdateOrDeleteBySql(sqlStr)
|
|
|
break
|
|
|
}
|
|
|
//redis缓存处理
|
|
|
- b.Count(in.NewUserId, in.UserType, in.EntUserId, true)
|
|
|
+ //b.Count(in.NewUserId, in.UserType, in.EntUserId, true)
|
|
|
}
|
|
|
}()
|
|
|
return data
|
|
@@ -893,3 +942,127 @@ func NewEndId(messageId, iType int64) (newUserId, entUserId int64) {
|
|
|
}
|
|
|
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
|
|
|
+}
|