| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package service
-
- import (
- "context"
- "errors"
- "time"
-
- "git.x2erp.com/qdy/go-base/logger"
- "git.x2erp.com/qdy/go-db/factory/mongodb"
- "git.x2erp.com/qdy/go-svc-code/internal/model"
- "go.mongodb.org/mongo-driver/bson"
- )
-
- // SessionDetailStore 会话明细存储服务
- type SessionDetailStore struct {
- mongoFactory *mongodb.MongoDBFactory
- }
-
- // NewSessionDetailStore 创建新的会话明细存储服务
- func NewSessionDetailStore(factory *mongodb.MongoDBFactory) *SessionDetailStore {
- return &SessionDetailStore{mongoFactory: factory}
- }
-
- // Create 创建会话明细
- func (s *SessionDetailStore) Create(ctx context.Context, detail *model.SessionDetail) error {
- if detail.SessionID == "" {
- return errors.New("session ID cannot be empty")
- }
-
- // 设置时间戳
- now := time.Now()
- if detail.CreatedAt.IsZero() {
- detail.CreatedAt = now
- }
- if detail.UpdatedAt.IsZero() {
- detail.UpdatedAt = now
- }
-
- // 插入到MongoDB
- _, success := s.mongoFactory.InsertOneWithResult(detail.CollectionName(), detail)
- if !success {
- logger.Error("创建会话明细失败", "session_id", detail.SessionID)
- return errors.New("failed to create session detail")
- }
-
- logger.Debug("创建会话明细成功", "session_id", detail.SessionID)
- return nil
- }
-
- // GetBySessionID 根据会话ID获取明细
- func (s *SessionDetailStore) GetBySessionID(ctx context.Context, sessionID string) (*model.SessionDetail, error) {
- if sessionID == "" {
- return nil, errors.New("session ID cannot be empty")
- }
-
- filter := bson.M{"session_id": sessionID}
- var detail model.SessionDetail
-
- err := s.mongoFactory.FindOne(detail.CollectionName(), filter, &detail)
- if err != nil {
- if err.Error() == "mongo: no documents in result" {
- return nil, nil // 未找到明细
- }
- logger.Error("根据会话ID查询明细失败", "session_id", sessionID, "error", err)
- return nil, err
- }
-
- return &detail, nil
- }
-
- // Update 更新会话明细
- func (s *SessionDetailStore) Update(ctx context.Context, sessionID string, updateFields map[string]interface{}) error {
- if sessionID == "" {
- return errors.New("session ID cannot be empty")
- }
-
- // 检查明细是否存在
- detail, err := s.GetBySessionID(ctx, sessionID)
- if err != nil {
- return err
- }
- if detail == nil {
- return errors.New("session detail not found")
- }
-
- // 添加更新时间
- updateFields["updated_at"] = time.Now()
-
- filter := bson.M{"session_id": sessionID}
- success, _ := s.mongoFactory.UpdateOne(detail.CollectionName(), filter, updateFields)
- if !success {
- logger.Error("更新会话明细失败", "session_id", sessionID)
- return errors.New("failed to update session detail")
- }
-
- logger.Debug("更新会话明细成功", "session_id", sessionID)
- return nil
- }
-
- // UpdateOrCreate 更新或创建会话明细
- func (s *SessionDetailStore) UpdateOrCreate(ctx context.Context, detail *model.SessionDetail) error {
- if detail.SessionID == "" {
- return errors.New("session ID cannot be empty")
- }
-
- // 检查是否已存在
- existing, err := s.GetBySessionID(ctx, detail.SessionID)
- if err != nil {
- return err
- }
-
- if existing == nil {
- // 不存在,创建新的
- return s.Create(ctx, detail)
- }
-
- // 已存在,更新
- updateFields := bson.M{
- "requirement_doc": detail.RequirementDoc,
- "technical_doc": detail.TechnicalDoc,
- "code_items": detail.CodeItems,
- "updated_at": time.Now(),
- }
-
- filter := bson.M{"session_id": detail.SessionID}
- success, _ := s.mongoFactory.UpdateOne(detail.CollectionName(), filter, updateFields)
- if !success {
- logger.Error("更新会话明细失败", "session_id", detail.SessionID)
- return errors.New("failed to update session detail")
- }
-
- logger.Debug("更新会话明细成功", "session_id", detail.SessionID)
- return nil
- }
-
- // DeleteBySessionID 根据会话ID删除明细
- func (s *SessionDetailStore) DeleteBySessionID(ctx context.Context, sessionID string) error {
- if sessionID == "" {
- return errors.New("session ID cannot be empty")
- }
-
- filter := bson.M{"session_id": sessionID}
- success, deletedCount := s.mongoFactory.DeleteOne(model.SessionDetail{}.CollectionName(), filter)
- if !success {
- logger.Error("删除会话明细失败", "session_id", sessionID)
- return errors.New("failed to delete session detail")
- }
-
- logger.Debug("删除会话明细成功", "session_id", sessionID, "deleted_count", deletedCount)
- return nil
- }
-
- // EnsureIndexes 确保集合索引
- func (s *SessionDetailStore) EnsureIndexes(ctx context.Context) error {
- // 会话ID索引(常用查询条件)
- sessionIDIndexKeys := bson.D{{Key: "session_id", Value: 1}}
- sessionIDSuccess := s.mongoFactory.CreateIndex(model.SessionDetail{}.CollectionName(), sessionIDIndexKeys)
- if !sessionIDSuccess {
- logger.Error("创建会话ID索引失败")
- return errors.New("failed to create session_id index")
- }
-
- // 创建时间索引
- createdAtIndexKeys := bson.D{{Key: "created_at", Value: -1}}
- createdAtSuccess := s.mongoFactory.CreateIndex(model.SessionDetail{}.CollectionName(), createdAtIndexKeys)
- if !createdAtSuccess {
- logger.Error("创建创建时间索引失败")
- return errors.New("failed to create created_at index")
- }
-
- logger.Debug("会话明细集合索引创建成功")
- return nil
- }
|