knowledgeeditlogic.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "strconv"
  14. "time"
  15. )
  16. type KnowledgeEditLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewKnowledgeEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeEditLogic {
  22. return &KnowledgeEditLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. // KnowledgeEdit 知识编辑
  29. func (l *KnowledgeEditLogic) KnowledgeEdit(in *knowledgeclient.KnowledgeEditReq) (*knowledgeclient.AddResponse, error) {
  30. result := &knowledgeclient.AddResponse{}
  31. //获取问题分词
  32. keywords := util.HttpDo(in.Question)
  33. //通过entUserId获取创建人名称
  34. req := &usercenter.EntUserReq{
  35. EntId: in.EntId,
  36. EntUserId: in.EntUserId,
  37. AppId: in.AppId,
  38. }
  39. resp, err := entity.UserCenterLib.GetEntUserInfo(context.Background(), req)
  40. if err != nil {
  41. logx.Infof("查询用户中台创建人信息失败", in.EntId, in.EntUserId, "err:", err)
  42. return nil, err
  43. }
  44. createPerson := resp.Data.Name
  45. fool := Mysql.ExecTx("编辑问题、答案", func(tx *sql.Tx) bool {
  46. //修改答案
  47. answerUpdate := map[string]interface{}{
  48. "update_time": time.Now().Local().Format(util.Date_Full_Layout),
  49. "content": in.Answer,
  50. }
  51. ok1 := Mysql.UpdateByTx(tx, util.ANSWER, map[string]interface{}{"id": in.AnswerId}, answerUpdate)
  52. //修改问题
  53. questionUpdate := map[string]interface{}{
  54. "content": in.Question,
  55. "keywords": keywords,
  56. }
  57. ok2 := Mysql.UpdateByTx(tx, util.QUESTION, map[string]interface{}{"answer_id": in.AnswerId}, questionUpdate)
  58. return ok1 && ok2
  59. })
  60. if fool {
  61. query := `{"query":{"bool":{"must":[{"term":{"answerId":"` + strconv.Itoa(int(in.AnswerId)) + `"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"facets":{}}`
  62. //修改es数据
  63. newKnowledge := map[string]interface{}{
  64. "knowledgeId": in.KnowledgeId,
  65. "status": 1,
  66. "createTime": time.Now().Unix(),
  67. "createPerson": createPerson,
  68. "answer": in.Answer,
  69. "question": in.Question,
  70. "keywords": keywords,
  71. "answerId": in.AnswerId,
  72. "entId": in.EntId,
  73. }
  74. ok1 := elastic.Del(C.Es.Index, C.Es.Type, query)
  75. ok := elastic.Save(C.Es.Index, C.Es.Type, newKnowledge)
  76. if ok && ok1 {
  77. result.ErrorCode = 0
  78. result.ErrorMsg = "修改问题成功"
  79. } else {
  80. result.ErrorCode = -1
  81. result.ErrorMsg = "删除es问题失败"
  82. }
  83. } else {
  84. result.ErrorCode = -1
  85. result.ErrorMsg = "修改mysql问题失败"
  86. }
  87. return result, nil
  88. }