123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package logic
- import (
- elastic "app.yhyue.com/moapp/jybase/esv1"
- "context"
- "database/sql"
- "fmt"
- "github.com/satori/go.uuid"
- "github.com/zeromicro/go-zero/core/logx"
- . "knowledgeBase/rpc/knowledge/init"
- "knowledgeBase/rpc/knowledge/internal/svc"
- "knowledgeBase/rpc/knowledge/knowledgeclient"
- "knowledgeBase/rpc/knowledge/util"
- "log"
- "time"
- )
- type KnowledgeAddLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewKnowledgeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeAddLogic {
- return &KnowledgeAddLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- type Question struct {
- Question string `json:"question"`
- KeyWords string `json:"keyWords"`
- }
- // KnowledgeAdd 知识新增
- func (l *KnowledgeAddLogic) KnowledgeAdd(in *knowledgeclient.AddRequest) (*knowledgeclient.AddResponse, error) {
- // todo: add your logic here and delete this line
- result := &knowledgeclient.AddResponse{}
- //先查找知识库Id
- query := map[string]interface{}{"status": 1, "appid": in.AppId, "tenant_id": in.TenantId}
- log.Println(query)
- datalist := Mysql.Find(util.KNOWLEDGE, query, "id", "", -1, -1)
- if datalist != nil && *datalist != nil && len(*datalist) > 0 {
- //问题进行分词
- keywords := util.HttpDo(in.Question)
- log.Println("分词", keywords)
- nowTime := time.Now().Local().Format(util.Date_Full_Layout)
- var answerId int64
- fool := Mysql.ExecTx("添加知识", func(tx *sql.Tx) bool {
- //插入答案
- log.Println(in.Answer, "------------------")
- answerData := map[string]interface{}{
- "knowledge_id": (*datalist)[0]["id"],
- "status": 1,
- "create_time": nowTime,
- "update_time": nowTime,
- "create_person": in.Person,
- "content": in.Answer,
- }
- answerId = Mysql.Insert(util.ANSWER, answerData)
- if answerId <= 0 {
- return false
- }
- //插入问题
- questionData := map[string]interface{}{
- "answer_id": answerId,
- "content": in.Question,
- "keywords": keywords,
- }
- questionId := Mysql.Insert(util.QUESTION, questionData)
- if questionId <= 0 {
- return false
- }
- return true
- })
- if fool {
- u1 := uuid.NewV4()
- fmt.Printf("UUIDv4: %s\n", u1)
- //插入es
- knowledge := map[string]interface{}{
- "knowledgeId": (*datalist)[0]["id"],
- "status": 1,
- "createTime": time.Now().Unix(),
- "createPerson": in.Person,
- "answer": in.Answer,
- "question": in.Question,
- "keywords": keywords,
- "answerId": answerId,
- "tenantId": in.TenantId,
- }
- b := elastic.Save(C.Es.Index, C.Es.Type, knowledge)
- log.Println("存es", b)
- if b {
- result.ErrorCode = 0
- result.ErrorMsg = "插入数据成功"
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "插入es失败"
- }
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "新建失败"
- }
- } else {
- result.ErrorCode = -1
- result.ErrorMsg = "租户不存在"
- }
- log.Println(result)
- return result, nil
- }
|