knowledgeeditlogic.go 2.2 KB

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