86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package biz
|
||
|
||
import (
|
||
"ai_scheduler/internal/data/impl"
|
||
"ai_scheduler/internal/data/model"
|
||
"ai_scheduler/internal/entitys"
|
||
"context"
|
||
"fmt"
|
||
"github.com/gofiber/fiber/v2/utils"
|
||
"time"
|
||
|
||
"ai_scheduler/internal/config"
|
||
)
|
||
|
||
type SessionBiz struct {
|
||
sessionRepo *impl.SessionImpl
|
||
sysRepo *impl.SysImpl
|
||
|
||
conf *config.Config
|
||
}
|
||
|
||
func NewSessionBiz(conf *config.Config, sessionImpl *impl.SessionImpl, sysImpl *impl.SysImpl) *SessionBiz {
|
||
return &SessionBiz{
|
||
sessionRepo: sessionImpl,
|
||
sysRepo: sysImpl,
|
||
conf: conf,
|
||
}
|
||
}
|
||
|
||
// InitSession 初始化会话 ,当天存在则返回会话,如果不存在则创建一个
|
||
func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRequest) (sessionId string, err error) {
|
||
|
||
// 获取系统配置
|
||
sysConfig, has, err := s.sysRepo.FindOne(s.sysRepo.WithSysId(req.SysId))
|
||
if err != nil {
|
||
return "", err
|
||
} else if !has {
|
||
return "", fmt.Errorf("sys not found")
|
||
}
|
||
|
||
// 获取 当天的session
|
||
t := time.Now().Truncate(24 * time.Hour)
|
||
session, has, err := s.sessionRepo.FindOne(
|
||
s.sessionRepo.WithUserId(req.UserId), // 条件:用户ID
|
||
s.sessionRepo.WithStartTime(t), // 条件:会话开始时间
|
||
s.sysRepo.WithSysId(sysConfig.SysID), // 条件:系统ID
|
||
)
|
||
if err != nil {
|
||
return "", err
|
||
} else if !has {
|
||
// 不存在,创建一个
|
||
session = model.AiSession{
|
||
SysID: sysConfig.SysID,
|
||
SessionID: utils.UUID(),
|
||
CreateAt: time.Now(),
|
||
UpdateAt: time.Now(),
|
||
}
|
||
err = s.sessionRepo.Create(&session)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
}
|
||
|
||
return session.SessionID, nil
|
||
}
|
||
|
||
// SessionList 会话列表
|
||
func (s *SessionBiz) SessionList(ctx context.Context, req *entitys.SessionListRequest) (list []model.AiSession, err error) {
|
||
|
||
if req.Page <= 0 {
|
||
req.Page = 1
|
||
}
|
||
if req.PageSize <= 0 {
|
||
req.PageSize = 10
|
||
}
|
||
|
||
list, err = s.sessionRepo.FindAll(
|
||
s.sessionRepo.WithUserId(req.UserId), // 条件:用户ID
|
||
s.sessionRepo.WithSysId(req.SysId), // 条件:系统ID
|
||
s.sessionRepo.PaginateScope(req.Page, req.PageSize), // 分页
|
||
s.sessionRepo.OrderByDesc("create_at"), // 排序:按创建时间降序
|
||
)
|
||
|
||
return
|
||
}
|