knowledgeaddlogic.go 2.8 KB

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