Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

session_detail_store.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "git.x2erp.com/qdy/go-base/logger"
  7. "git.x2erp.com/qdy/go-db/factory/mongodb"
  8. "git.x2erp.com/qdy/go-svc-code/internal/model"
  9. "go.mongodb.org/mongo-driver/bson"
  10. )
  11. // SessionDetailStore 会话明细存储服务
  12. type SessionDetailStore struct {
  13. mongoFactory *mongodb.MongoDBFactory
  14. }
  15. // NewSessionDetailStore 创建新的会话明细存储服务
  16. func NewSessionDetailStore(factory *mongodb.MongoDBFactory) *SessionDetailStore {
  17. return &SessionDetailStore{mongoFactory: factory}
  18. }
  19. // Create 创建会话明细
  20. func (s *SessionDetailStore) Create(ctx context.Context, detail *model.SessionDetail) error {
  21. if detail.SessionID == "" {
  22. return errors.New("session ID cannot be empty")
  23. }
  24. // 设置时间戳
  25. now := time.Now()
  26. if detail.CreatedAt.IsZero() {
  27. detail.CreatedAt = now
  28. }
  29. if detail.UpdatedAt.IsZero() {
  30. detail.UpdatedAt = now
  31. }
  32. // 插入到MongoDB
  33. _, success := s.mongoFactory.InsertOneWithResult(detail.CollectionName(), detail)
  34. if !success {
  35. logger.Error("创建会话明细失败", "session_id", detail.SessionID)
  36. return errors.New("failed to create session detail")
  37. }
  38. logger.Debug("创建会话明细成功", "session_id", detail.SessionID)
  39. return nil
  40. }
  41. // GetBySessionID 根据会话ID获取明细
  42. func (s *SessionDetailStore) GetBySessionID(ctx context.Context, sessionID string) (*model.SessionDetail, error) {
  43. if sessionID == "" {
  44. return nil, errors.New("session ID cannot be empty")
  45. }
  46. filter := bson.M{"session_id": sessionID}
  47. var detail model.SessionDetail
  48. err := s.mongoFactory.FindOne(detail.CollectionName(), filter, &detail)
  49. if err != nil {
  50. if err.Error() == "mongo: no documents in result" {
  51. return nil, nil // 未找到明细
  52. }
  53. logger.Error("根据会话ID查询明细失败", "session_id", sessionID, "error", err)
  54. return nil, err
  55. }
  56. return &detail, nil
  57. }
  58. // Update 更新会话明细
  59. func (s *SessionDetailStore) Update(ctx context.Context, sessionID string, updateFields map[string]interface{}) error {
  60. if sessionID == "" {
  61. return errors.New("session ID cannot be empty")
  62. }
  63. // 检查明细是否存在
  64. detail, err := s.GetBySessionID(ctx, sessionID)
  65. if err != nil {
  66. return err
  67. }
  68. if detail == nil {
  69. return errors.New("session detail not found")
  70. }
  71. // 添加更新时间
  72. updateFields["updated_at"] = time.Now()
  73. filter := bson.M{"session_id": sessionID}
  74. success, _ := s.mongoFactory.UpdateOne(detail.CollectionName(), filter, updateFields)
  75. if !success {
  76. logger.Error("更新会话明细失败", "session_id", sessionID)
  77. return errors.New("failed to update session detail")
  78. }
  79. logger.Debug("更新会话明细成功", "session_id", sessionID)
  80. return nil
  81. }
  82. // UpdateOrCreate 更新或创建会话明细
  83. func (s *SessionDetailStore) UpdateOrCreate(ctx context.Context, detail *model.SessionDetail) error {
  84. if detail.SessionID == "" {
  85. return errors.New("session ID cannot be empty")
  86. }
  87. // 检查是否已存在
  88. existing, err := s.GetBySessionID(ctx, detail.SessionID)
  89. if err != nil {
  90. return err
  91. }
  92. if existing == nil {
  93. // 不存在,创建新的
  94. return s.Create(ctx, detail)
  95. }
  96. // 已存在,更新
  97. updateFields := bson.M{
  98. "requirement_doc": detail.RequirementDoc,
  99. "technical_doc": detail.TechnicalDoc,
  100. "code_items": detail.CodeItems,
  101. "updated_at": time.Now(),
  102. }
  103. filter := bson.M{"session_id": detail.SessionID}
  104. success, _ := s.mongoFactory.UpdateOne(detail.CollectionName(), filter, updateFields)
  105. if !success {
  106. logger.Error("更新会话明细失败", "session_id", detail.SessionID)
  107. return errors.New("failed to update session detail")
  108. }
  109. logger.Debug("更新会话明细成功", "session_id", detail.SessionID)
  110. return nil
  111. }
  112. // DeleteBySessionID 根据会话ID删除明细
  113. func (s *SessionDetailStore) DeleteBySessionID(ctx context.Context, sessionID string) error {
  114. if sessionID == "" {
  115. return errors.New("session ID cannot be empty")
  116. }
  117. filter := bson.M{"session_id": sessionID}
  118. success, deletedCount := s.mongoFactory.DeleteOne(model.SessionDetail{}.CollectionName(), filter)
  119. if !success {
  120. logger.Error("删除会话明细失败", "session_id", sessionID)
  121. return errors.New("failed to delete session detail")
  122. }
  123. logger.Debug("删除会话明细成功", "session_id", sessionID, "deleted_count", deletedCount)
  124. return nil
  125. }
  126. // EnsureIndexes 确保集合索引
  127. func (s *SessionDetailStore) EnsureIndexes(ctx context.Context) error {
  128. // 会话ID索引(常用查询条件)
  129. sessionIDIndexKeys := bson.D{{Key: "session_id", Value: 1}}
  130. sessionIDSuccess := s.mongoFactory.CreateIndex(model.SessionDetail{}.CollectionName(), sessionIDIndexKeys)
  131. if !sessionIDSuccess {
  132. logger.Error("创建会话ID索引失败")
  133. return errors.New("failed to create session_id index")
  134. }
  135. // 创建时间索引
  136. createdAtIndexKeys := bson.D{{Key: "created_at", Value: -1}}
  137. createdAtSuccess := s.mongoFactory.CreateIndex(model.SessionDetail{}.CollectionName(), createdAtIndexKeys)
  138. if !createdAtSuccess {
  139. logger.Error("创建创建时间索引失败")
  140. return errors.New("failed to create created_at index")
  141. }
  142. logger.Debug("会话明细集合索引创建成功")
  143. return nil
  144. }