knowledgeeditlogic.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "strconv"
  12. "time"
  13. )
  14. type KnowledgeEditLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewKnowledgeEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *KnowledgeEditLogic {
  20. return &KnowledgeEditLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // KnowledgeEdit 知识编辑
  27. func (l *KnowledgeEditLogic) KnowledgeEdit(in *knowledgeclient.KnowledgeEditReq) (*knowledgeclient.AddResponse, error) {
  28. // todo: add your logic here and delete this line
  29. result := &knowledgeclient.AddResponse{}
  30. //修改答案
  31. answerUpdate := map[string]interface{}{
  32. "update_time": time.Now().Local().Format(util.Date_Full_Layout),
  33. "content": in.Answer,
  34. }
  35. //获取问题分词
  36. keywords := util.HttpDo(in.Question)
  37. fool := Mysql.ExecTx("编辑问题、答案", func(tx *sql.Tx) bool {
  38. ok1 := Mysql.UpdateByTx(tx, util.ANSWER, map[string]interface{}{"id": in.AnswerId}, answerUpdate)
  39. //修改问题
  40. questionUpdate := map[string]interface{}{
  41. "content": in.Question,
  42. "keywords": keywords,
  43. }
  44. ok2 := Mysql.UpdateByTx(tx, util.QUESTION, map[string]interface{}{"answer_id": in.AnswerId}, questionUpdate)
  45. return ok1 && ok2
  46. })
  47. if fool {
  48. //先查询es获取es _id
  49. query := `{"query":{"bool":{"must":[{"term":{"answerId":"` + strconv.Itoa(int(in.AnswerId)) + `"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"facets":{}}`
  50. //修改es数据
  51. newKnowledge := map[string]interface{}{
  52. "knowledgeId": in.KnowledgeId,
  53. "status": 1,
  54. "createTime": time.Now().Unix(),
  55. "createPerson": in.Person,
  56. "answer": in.Answer,
  57. "question": in.Question,
  58. "keywords": keywords,
  59. "answerId": in.AnswerId,
  60. "entId": in.EntId,
  61. }
  62. ok1 := elastic.Del(C.Es.Index, C.Es.Type, query)
  63. ok := elastic.Save(C.Es.Index, C.Es.Type, newKnowledge)
  64. if ok && ok1 {
  65. result.ErrorCode = 0
  66. result.ErrorMsg = "修改问题成功"
  67. } else {
  68. result.ErrorCode = -1
  69. result.ErrorMsg = "删除es问题失败"
  70. }
  71. } else {
  72. result.ErrorCode = -1
  73. result.ErrorMsg = "修改mysql问题失败"
  74. }
  75. return result, nil
  76. }