浏览代码

增加service层

renjiaojiao 3 年之前
父节点
当前提交
c7241f5ee0

+ 3 - 4
rpc/knowledge/internal/service/knowledgeService.go

@@ -1,13 +1,11 @@
 package service
 package service
 
 
-import "C"
 import (
 import (
 	cm "app.yhyue.com/moapp/jybase/common"
 	cm "app.yhyue.com/moapp/jybase/common"
 	. "app.yhyue.com/moapp/jybase/encrypt"
 	. "app.yhyue.com/moapp/jybase/encrypt"
 	elastic "app.yhyue.com/moapp/jybase/esv1"
 	elastic "app.yhyue.com/moapp/jybase/esv1"
 	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
 	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
 	. "bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/entity"
 	. "bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/entity"
-	//. "bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/init"
 	"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/knowledgeclient"
 	"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/knowledgeclient"
 	"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/util"
 	"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/util"
 	"context"
 	"context"
@@ -277,10 +275,11 @@ func (k *KnowledgeService) RecommendAnswer(param *knowledgeclient.FindAnswerReq,
 			keywords += val + " "
 			keywords += val + " "
 		}
 		}
 	}
 	}
+	log.Println("问题分词关键字1:", len(keyWords))
 	if keywords == "" {
 	if keywords == "" {
-		keywords = param.Question
+		keywords += param.Question
 	}
 	}
-	log.Println("问题分词关键字:", keyWords)
+	log.Println("问题分词关键字2:", keyWords)
 	var query = util.DSL4SearchByKwsOrid(keyWords, SE.Decode4Hex(param.RobotEntId))
 	var query = util.DSL4SearchByKwsOrid(keyWords, SE.Decode4Hex(param.RobotEntId))
 	res := elastic.GetAllByNgram(Index, Type, query, "", "", searchField, 0, 3, 0, false)
 	res := elastic.GetAllByNgram(Index, Type, query, "", "", searchField, 0, 3, 0, false)
 	//log.Println("推荐3个答案:", res)
 	//log.Println("推荐3个答案:", res)

+ 160 - 7
rpc/knowledge/internal/service/knowledgeService_test.go

@@ -1,11 +1,44 @@
 package service
 package service
 
 
 import (
 import (
+	elastic "app.yhyue.com/moapp/jybase/esv1"
+	"app.yhyue.com/moapp/jybase/mysql"
+	"bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
+	"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/entity"
 	"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/knowledgeclient"
 	"bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/knowledgeclient"
+	"github.com/zeromicro/go-zero/core/discov"
+	"github.com/zeromicro/go-zero/zrpc"
 	"reflect"
 	"reflect"
 	"testing"
 	"testing"
 )
 )
 
 
+func initMysql() {
+	entity.Mysql = &mysql.Mysql{
+		Address:      "192.168.3.217:4000",
+		UserName:     "root",
+		PassWord:     "=PDT49#80Z!RVv52_z",
+		DBName:       "base_service",
+		MaxOpenConns: 5,
+		MaxIdleConns: 5,
+	}
+	entity.Mysql.Init()
+
+	elastic.InitElasticSize("http://192.168.3.204:1500", 10)
+
+	//初始化资源中台相关
+	userCenterClient := zrpc.MustNewClient(zrpc.RpcClientConf{
+		Etcd: discov.EtcdConf{
+			Hosts: []string{"192.168.3.240:2379"},
+			Key:   "usercenter.rpc",
+		},
+	})
+	entity.UserCenterLib = usercenter.NewUserCenter(userCenterClient)
+
+}
+func init() {
+	initMysql()
+}
+
 func TestKnowledgeService_FindAnswer(t *testing.T) {
 func TestKnowledgeService_FindAnswer(t *testing.T) {
 	type args struct {
 	type args struct {
 		param   *knowledgeclient.FindAnswerReq
 		param   *knowledgeclient.FindAnswerReq
@@ -18,7 +51,58 @@ func TestKnowledgeService_FindAnswer(t *testing.T) {
 		args args
 		args args
 		want *knowledgeclient.Question
 		want *knowledgeclient.Question
 	}{
 	}{
-		// TODO: Add test cases.
+		{
+			name: "根据问题查询答案",
+			args: args{
+				param: &knowledgeclient.FindAnswerReq{
+					Type:       1,
+					Question:   "超级订阅为什么购买不了",
+					RobotEntId: "14929",
+				},
+				addr:    "http://192.168.3.204:1500",
+				index:   "smart_new",
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+		},
+		{
+			name: "根据问题查询答案",
+			args: args{
+				param: &knowledgeclient.FindAnswerReq{
+					Type:       2,
+					Question:   "超级订阅为什么购买不了",
+					RobotEntId: "14929",
+				},
+				addr:    "http://192.168.3.204:1500",
+				index:   "smart_new",
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+		},
+		{
+			name: "根据问题查询答案",
+			args: args{
+				param: &knowledgeclient.FindAnswerReq{
+					Type:       1,
+					Question:   "我",
+					RobotEntId: "14929",
+				},
+				addr:    "http://192.168.3.204:1500",
+				index:   "smart_new",
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+		},
+		{
+			name: "根据问题查询答案",
+			args: args{
+				param: &knowledgeclient.FindAnswerReq{
+					Type:       1,
+					Question:   "我得问题",
+					RobotEntId: "123",
+				},
+				addr:    "http://192.168.3.204:1500",
+				index:   "smart_new",
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+		},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
@@ -42,7 +126,21 @@ func TestKnowledgeService_KnowledgeAdd(t *testing.T) {
 		want1   string
 		want1   string
 		wantErr bool
 		wantErr bool
 	}{
 	}{
-		// TODO: Add test cases.
+		{
+			name: "知识添加",
+			args: args{
+				param: &knowledgeclient.AddRequest{
+					Question:  "任姣姣测试的测添加问题",
+					Answer:    "答案",
+					AppId:     "10000",
+					EntUserId: 4315,
+					EntId:     14929,
+				},
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+			want:  true,
+			want1: "插入es成功",
+		},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
@@ -72,7 +170,12 @@ func TestKnowledgeService_KnowledgeDel(t *testing.T) {
 		wantOk  bool
 		wantOk  bool
 		wantMsg string
 		wantMsg string
 	}{
 	}{
-		// TODO: Add test cases.
+		{
+			name: "删除知识",
+			args: args{
+				answerId: 30192,
+			},
+		},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
@@ -100,7 +203,21 @@ func TestKnowledgeService_KnowledgeEdit(t *testing.T) {
 		wantMsg string
 		wantMsg string
 		wantErr bool
 		wantErr bool
 	}{
 	}{
-		// TODO: Add test cases.
+		{
+			name: "知识编辑",
+			args: args{
+				param: &knowledgeclient.KnowledgeEditReq{
+					Question:    "",
+					Answer:      "",
+					AnswerId:    0,
+					KnowledgeId: 0,
+					EntId:       14929,
+					EntUserId:   4315,
+					AppId:       "10000",
+				},
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+		},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
@@ -130,7 +247,13 @@ func TestKnowledgeService_KnowledgeInfo(t *testing.T) {
 		wantData *knowledgeclient.KnowledgeEntity
 		wantData *knowledgeclient.KnowledgeEntity
 		wantOk   bool
 		wantOk   bool
 	}{
 	}{
-		// TODO: Add test cases.
+		{
+			name: "知识详情",
+			args: args{
+				answerId: 30191,
+			},
+			wantOk: true,
+		},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
@@ -155,7 +278,16 @@ func TestKnowledgeService_KnowledgeList(t *testing.T) {
 		args args
 		args args
 		want *knowledgeclient.ListData
 		want *knowledgeclient.ListData
 	}{
 	}{
-		// TODO: Add test cases.
+		{
+			name: "知识列表",
+			args: args{
+				param: &knowledgeclient.ListRequest{
+					PageSize:  20,
+					PageIndex: 1,
+					EntId:     14929,
+				},
+			},
+		},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
@@ -177,7 +309,28 @@ func TestKnowledgeService_RecommendAnswer(t *testing.T) {
 		args args
 		args args
 		want []*knowledgeclient.Question
 		want []*knowledgeclient.Question
 	}{
 	}{
-		// TODO: Add test cases.
+		{
+			name: "根据问题查询答案",
+			args: args{
+				param: &knowledgeclient.FindAnswerReq{
+					Type:       1,
+					Question:   "任姣姣测试添加问题",
+					RobotEntId: "14929",
+				},
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+		},
+		{
+			name: "根据问题查询答案",
+			args: args{
+				param: &knowledgeclient.FindAnswerReq{
+					Type:       1,
+					Question:   "我的",
+					RobotEntId: "14929",
+				},
+				segment: "http://192.168.3.204:9070/api/segment",
+			},
+		},
 	}
 	}
 	for _, tt := range tests {
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {

+ 1 - 2
rpc/knowledge/util/hanlp.go

@@ -2,7 +2,6 @@ package util
 
 
 import (
 import (
 	cm "app.yhyue.com/moapp/jybase/common"
 	cm "app.yhyue.com/moapp/jybase/common"
-	. "bp.jydev.jianyu360.cn/SocialPlatform/knowledgeBase/rpc/knowledge/init"
 	"encoding/base64"
 	"encoding/base64"
 	"encoding/json"
 	"encoding/json"
 	"io/ioutil"
 	"io/ioutil"
@@ -122,7 +121,7 @@ func HttpDo(ques string, url string) (result string) {
 	)
 	)
 	client := &http.Client{}
 	client := &http.Client{}
 	//req, err := http.NewRequest("POST", "http://39.106.145.77:8080/api/segment", strings.NewReader("content="+ques))
 	//req, err := http.NewRequest("POST", "http://39.106.145.77:8080/api/segment", strings.NewReader("content="+ques))
-	req, err := http.NewRequest("POST", C.Segment, strings.NewReader("content="+ques))
+	req, err := http.NewRequest("POST", url, strings.NewReader("content="+ques))
 	if err != nil {
 	if err != nil {
 		log.Println("问题分词出错err1:", err)
 		log.Println("问题分词出错err1:", err)
 		return ""
 		return ""