knowledgeaddlogic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package logic
  2. import (
  3. elastic "app.yhyue.com/moapp/jybase/esv1"
  4. "context"
  5. "database/sql"
  6. "fmt"
  7. "github.com/satori/go.uuid"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. . "knowledgeBase/rpc/knowledge/init"
  10. "knowledgeBase/rpc/knowledge/internal/svc"
  11. "knowledgeBase/rpc/knowledge/knowledgeclient"
  12. "knowledgeBase/rpc/knowledge/util"
  13. "log"
  14. "time"
  15. )
  16. type KnowledgeAddLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewKnowledgeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeAddLogic {
  22. return &KnowledgeAddLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. type Question struct {
  29. Question string `json:"question"`
  30. KeyWords string `json:"keyWords"`
  31. }
  32. // KnowledgeAdd 知识新增
  33. func (l *KnowledgeAddLogic) KnowledgeAdd(in *knowledgeclient.AddRequest) (*knowledgeclient.AddResponse, error) {
  34. // todo: add your logic here and delete this line
  35. result := &knowledgeclient.AddResponse{}
  36. //先查找知识库Id
  37. query := map[string]interface{}{"status": 1, "appid": in.AppId, "tenant_id": in.TenantId}
  38. log.Println(query)
  39. datalist := Mysql.Find(util.KNOWLEDGE, query, "id", "", -1, -1)
  40. if datalist != nil && *datalist != nil && len(*datalist) > 0 {
  41. //问题进行分词
  42. keywords := util.HttpDo(in.Question)
  43. log.Println("分词", keywords)
  44. nowTime := time.Now().Local().Format(util.Date_Full_Layout)
  45. var answerId int64
  46. fool := Mysql.ExecTx("添加知识", func(tx *sql.Tx) bool {
  47. //插入答案
  48. log.Println(in.Answer, "------------------")
  49. answerData := map[string]interface{}{
  50. "knowledge_id": (*datalist)[0]["id"],
  51. "status": 1,
  52. "create_time": nowTime,
  53. "update_time": nowTime,
  54. "create_person": in.Person,
  55. "content": in.Answer,
  56. }
  57. answerId = Mysql.Insert(util.ANSWER, answerData)
  58. if answerId <= 0 {
  59. return false
  60. }
  61. //插入问题
  62. questionData := map[string]interface{}{
  63. "answer_id": answerId,
  64. "content": in.Question,
  65. "keywords": keywords,
  66. }
  67. questionId := Mysql.Insert(util.QUESTION, questionData)
  68. if questionId <= 0 {
  69. return false
  70. }
  71. return true
  72. })
  73. if fool {
  74. u1 := uuid.NewV4()
  75. fmt.Printf("UUIDv4: %s\n", u1)
  76. //插入es
  77. knowledge := map[string]interface{}{
  78. "knowledgeId": (*datalist)[0]["id"],
  79. "status": 1,
  80. "createTime": time.Now().Unix(),
  81. "createPerson": in.Person,
  82. "answer": in.Answer,
  83. "question": in.Question,
  84. "keywords": keywords,
  85. "answerId": answerId,
  86. "tenantId": in.TenantId,
  87. }
  88. b := elastic.Save(C.Es.Index, C.Es.Type, knowledge)
  89. log.Println("存es", b)
  90. if b {
  91. result.ErrorCode = 0
  92. result.ErrorMsg = "插入数据成功"
  93. } else {
  94. result.ErrorCode = -1
  95. result.ErrorMsg = "插入es失败"
  96. }
  97. } else {
  98. result.ErrorCode = -1
  99. result.ErrorMsg = "新建失败"
  100. }
  101. } else {
  102. result.ErrorCode = -1
  103. result.ErrorMsg = "租户不存在"
  104. }
  105. log.Println(result)
  106. return result, nil
  107. }