Selaa lähdekoodia

Merge branch 'dev/v1.2.6_zxl' of SocialPlatform/messageCenter into feature/v1.2.6

wangchuanjin 2 vuotta sitten
vanhempi
commit
2db54681e0
6 muutettua tiedostoa jossa 206 lisäystä ja 12 poistoa
  1. 2 0
      README.md
  2. 4 2
      entity/util.go
  3. 1 0
      go.mod
  4. 18 0
      rpc/messagecenter/messagecenter.proto
  5. 133 5
      service/message_mail_box.go
  6. 48 5
      service/message_mail_box_test.go

+ 2 - 0
README.md

@@ -16,3 +16,5 @@ goctl 1.1.6
 ### 其他
 - 代理可使用 `GOPROXY=https://goproxy.io`
 - 目前使用的go 版本 1.16   (由于go-zero 对go版本有要求 ,go 1.15.1以下的版本,启动会报错)
+
+#

+ 4 - 2
entity/util.go

@@ -1,13 +1,14 @@
 package entity
 
 import (
-	quitl "app.yhyue.com/moapp/jybase/common"
-	"app.yhyue.com/moapp/jybase/redis"
 	"encoding/json"
 	"fmt"
 	"reflect"
 	"sort"
 	"strings"
+
+	quitl "app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/redis"
 )
 
 const (
@@ -27,6 +28,7 @@ const (
 	//mongo
 	ENTNICHE_DELETE    = "entniche_delete"
 	SOCIALIZE_APPRAISE = "socialize_message_appraise"
+	SOCIALIZE_SUMMARY  = "socialize_summary"
 )
 const (
 	SUCCESS_CODE = int64(0)

+ 1 - 0
go.mod

@@ -7,6 +7,7 @@ require (
 	app.yhyue.com/moapp/jypkg v0.0.0-20230407092858-0d6d33fa63d6
 	bp.jydev.jianyu360.cn/BaseService/gateway v1.3.4
 	github.com/go-sql-driver/mysql v1.7.0
+	github.com/gogf/gf/v2 v2.0.6
 	github.com/microcosm-cc/bluemonday v1.0.23
 	github.com/zeromicro/go-zero v1.4.4
 	google.golang.org/grpc v1.53.0

+ 18 - 0
rpc/messagecenter/messagecenter.proto

@@ -255,6 +255,22 @@ message ConversationReq {
   int64     entUserId = 6;  //企业标识
   string    filtrationId = 10; //客服列表过滤会话中派对中用户
 }
+
+//聊天保存
+message MessageSaveReq {
+  string        title = 1; //标题
+  string        content = 2; //内容
+  int64         type = 3; //类型; 1:聊天 2:系统消息
+  string        appid = 4;
+  repeated int64        groupIds =5;//群聊id [群组id]
+  repeated int64        receiverIds =6;//群聊接收人id [接收人为职位id]
+  int64  sendId =7; //发送人[发送人职位id]
+  int64 item = 8; //1-活动优惠 2-服务通知 3-订阅消息 4-项目动态 5-企业动态 6-分析报告 7-系统通知 8-聊天消息
+  int64 messageType =9;//1:文本 2:链接 3:图片 4:附件
+  string link =10; //链接
+}
+
+
 service messageCenter {
   // 查询数量
   rpc Count(CountReq) returns(CountResp);
@@ -296,4 +312,6 @@ service messageCenter {
   rpc AppraiseMessage(AppraiseReq) returns(CurrencyResp);
   // 客服列表查询
   rpc ConversationList(ConversationReq) returns(UserResp);
+  // 群聊
+  rpc GroupChat(MessageSaveReq)returns(CommonReq);
 }

+ 133 - 5
service/message_mail_box.go

@@ -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{}
@@ -866,3 +870,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
+}

+ 48 - 5
service/message_mail_box_test.go

@@ -1,15 +1,16 @@
 package service
 
 import (
+	"log"
+	"reflect"
+	"strings"
+	"testing"
+
 	"app.yhyue.com/moapp/jybase/mysql"
 	"app.yhyue.com/moapp/jybase/redis"
 	IC "bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/init"
 	"bp.jydev.jianyu360.cn/SocialPlatform/messageCenter/rpc/messagecenter/messagecenter"
 	_ "github.com/go-sql-driver/mysql"
-	"log"
-	"reflect"
-	"strings"
-	"testing"
 )
 
 func initMysql() {
@@ -31,7 +32,7 @@ func initMysql() {
 		MaxIdleConns: 5,
 	}
 	IC.MainMysql.Init()
-	redis.InitRedisBySize(strings.Join([]string{"other=127.0.0.1:6379"}, ","), 100, 30, 300)
+	redis.InitRedisBySize(strings.Join([]string{"other=192.168.3.11:1712"}, ","), 100, 30, 300)
 }
 func init() {
 	initMysql()
@@ -155,6 +156,7 @@ func TestMessaggeService_CreateChatSession(t *testing.T) {
 	}
 }
 
+/*
 func TestMessaggeService_FindMessage(t *testing.T) {
 	type args struct {
 		in *messagecenter.MessageReq
@@ -264,6 +266,8 @@ func TestMessaggeService_FindMessage(t *testing.T) {
 	}
 }
 
+*/
+
 func TestMessaggeService_SaveAutoReplyMsg(t *testing.T) {
 	type args struct {
 		userType  int64
@@ -642,3 +646,42 @@ func TestMessaggeService_UpdateReadById(t *testing.T) {
 		})
 	}
 }
+
+func TestChat(t *testing.T) {
+	type args struct {
+		in *messagecenter.MessageSaveReq
+	}
+	tests := []struct {
+		name          string
+		args          args
+		wantErrorCode bool
+		wantErrorMsg  string
+		wantContent   string
+		wantMessageId int64
+	}{
+		{name: "发送群聊",
+			args: args{
+				in: &messagecenter.MessageSaveReq{
+					Title:       "标题?",
+					Content:     "hello world1",
+					Item:        8,
+					MessageType: 1,
+					Link:        "",
+					Appid:       "10000",
+					ReceiverIds: []int64{1, 2, 3},
+					GroupIds:    []int64{8},
+					Type:        1,
+					SendId:      333,
+				},
+			},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			b := MessaggeService{}
+			b.Chat(tt.args.in)
+
+		})
+	}
+}