knowledgeaddlogic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "time"
  14. )
  15. type KnowledgeAddLogic struct {
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. logx.Logger
  19. }
  20. func NewKnowledgeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeAddLogic {
  21. return &KnowledgeAddLogic{
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. Logger: logx.WithContext(ctx),
  25. }
  26. }
  27. type Question struct {
  28. Question string `json:"question"`
  29. KeyWords string `json:"keyWords"`
  30. }
  31. // KnowledgeAdd 知识新增
  32. func (l *KnowledgeAddLogic) KnowledgeAdd(in *knowledgeclient.AddRequest) (*knowledgeclient.AddResponse, error) {
  33. result := &knowledgeclient.AddResponse{}
  34. //先查找知识库Id
  35. query := map[string]interface{}{"status": 1, "appid": in.AppId, "ent_id": in.EntId}
  36. datalist := Mysql.Find(util.KNOWLEDGE, query, "id", "", -1, -1)
  37. if datalist != nil && *datalist != nil && len(*datalist) > 0 {
  38. //问题进行分词
  39. keywords := util.HttpDo(in.Question)
  40. //通过entUserId获取创建人名称,调用用户中心
  41. req := &usercenter.EntUserReq{
  42. EntId: in.EntId,
  43. EntUserId: in.EntUserId,
  44. AppId: in.AppId,
  45. }
  46. resp, err := entity.UserCenterLib.GetEntUserInfo(context.Background(), req)
  47. if err != nil {
  48. logx.Infof("查询用户中台创建人信息失败", in.EntId, in.EntUserId, "err:", err)
  49. return nil, err
  50. }
  51. createPerson := resp.Data.Name
  52. var answerId int64
  53. nowTime := time.Now().Local().Format(util.Date_Full_Layout)
  54. fool := Mysql.ExecTx("添加知识", func(tx *sql.Tx) bool {
  55. //插入答案
  56. answerData := map[string]interface{}{
  57. "knowledge_id": (*datalist)[0]["id"],
  58. "status": 1,
  59. "create_time": nowTime,
  60. "update_time": nowTime,
  61. "create_person": createPerson,
  62. "content": in.Answer,
  63. }
  64. answerId = Mysql.Insert(util.ANSWER, answerData)
  65. //插入问题
  66. questionData := map[string]interface{}{
  67. "answer_id": answerId,
  68. "content": in.Question,
  69. "keywords": keywords,
  70. }
  71. questionId := Mysql.Insert(util.QUESTION, questionData)
  72. return answerId > 0 && questionId > 0
  73. })
  74. if fool {
  75. //插入es
  76. knowledge := map[string]interface{}{
  77. "knowledgeId": (*datalist)[0]["id"],
  78. "status": 1,
  79. "createTime": time.Now().Unix(),
  80. "createPerson": createPerson,
  81. "answer": in.Answer,
  82. "question": in.Question,
  83. "keywords": keywords,
  84. "answerId": answerId,
  85. "entId": in.EntId,
  86. }
  87. b := elastic.Save(C.Es.Index, C.Es.Type, knowledge)
  88. if b {
  89. result.ErrorCode = 0
  90. result.ErrorMsg = "插入数据成功"
  91. } else {
  92. result.ErrorCode = -1
  93. result.ErrorMsg = "插入es失败"
  94. }
  95. } else {
  96. result.ErrorCode = -1
  97. result.ErrorMsg = "新建失败"
  98. }
  99. } else {
  100. result.ErrorCode = -1
  101. result.ErrorMsg = "租户不存在"
  102. }
  103. return result, nil
  104. }