knowledgeaddlogic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package logic
  2. import (
  3. elastic "app.yhyue.com/moapp/jybase/esv1"
  4. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  5. "context"
  6. "database/sql"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "knowledgeBase/entity"
  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, "ent_id": in.EntId}
  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. //通过entUserId获取创建人名称
  45. req := &usercenter.EntUserReq{
  46. EntId: in.EntId,
  47. EntUserId: in.EntUserId,
  48. AppId: in.AppId,
  49. }
  50. resp, err := entity.UserCenterLib.GetEntUserInfo(context.Background(), req)
  51. //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
  52. if resp.ErrorCode == 0 || err != nil {
  53. logx.Infof("查询用户中台创建人信息失败", in.EntId, resp.ErrorCode, "err:", err)
  54. return nil, err
  55. }
  56. createPerson := resp.Data.Name
  57. nowTime := time.Now().Local().Format(util.Date_Full_Layout)
  58. var answerId int64
  59. fool := Mysql.ExecTx("添加知识", func(tx *sql.Tx) bool {
  60. //插入答案
  61. answerData := map[string]interface{}{
  62. "knowledge_id": (*datalist)[0]["id"],
  63. "status": 1,
  64. "create_time": nowTime,
  65. "update_time": nowTime,
  66. "create_person": createPerson,
  67. "content": in.Answer,
  68. }
  69. answerId = Mysql.Insert(util.ANSWER, answerData)
  70. if answerId <= 0 {
  71. return false
  72. }
  73. //插入问题
  74. questionData := map[string]interface{}{
  75. "answer_id": answerId,
  76. "content": in.Question,
  77. "keywords": keywords,
  78. }
  79. questionId := Mysql.Insert(util.QUESTION, questionData)
  80. if questionId <= 0 {
  81. return false
  82. }
  83. return true
  84. })
  85. if fool {
  86. //插入es
  87. knowledge := map[string]interface{}{
  88. "knowledgeId": (*datalist)[0]["id"],
  89. "status": 1,
  90. "createTime": time.Now().Unix(),
  91. "createPerson": createPerson,
  92. "answer": in.Answer,
  93. "question": in.Question,
  94. "keywords": keywords,
  95. "answerId": answerId,
  96. "entId": in.EntId,
  97. }
  98. b := elastic.Save(C.Es.Index, C.Es.Type, knowledge)
  99. log.Println("存es", b)
  100. if b {
  101. result.ErrorCode = 0
  102. result.ErrorMsg = "插入数据成功"
  103. } else {
  104. result.ErrorCode = -1
  105. result.ErrorMsg = "插入es失败"
  106. }
  107. } else {
  108. result.ErrorCode = -1
  109. result.ErrorMsg = "新建失败"
  110. }
  111. } else {
  112. result.ErrorCode = -1
  113. result.ErrorMsg = "租户不存在"
  114. }
  115. log.Println(result)
  116. return result, nil
  117. }