完善功能

This commit is contained in:
2025-07-16 15:16:40 +08:00
parent b2871ec0d2
commit f68a5b360b
123 changed files with 4643 additions and 931 deletions

View File

@ -0,0 +1,97 @@
package user_read_record
import (
"context"
"server/internal/dao"
"server/internal/model"
"server/internal/model/do"
"server/internal/service"
"server/utility/ecode"
"github.com/gogf/gf/v2/os/gtime"
)
type sUserReadRecord struct{}
func New() service.IUserReadRecord {
return &sUserReadRecord{}
}
func init() {
service.RegisterUserReadRecord(New())
}
// List retrieves a paginated list of user read records
func (s *sUserReadRecord) List(ctx context.Context, in *model.UserReadRecordListIn) (out *model.UserReadRecordListOut, err error) {
out = &model.UserReadRecordListOut{}
m := dao.UserReadRecords.Ctx(ctx)
if in.UserId != 0 {
m = m.Where(dao.UserReadRecords.Columns().UserId, in.UserId)
}
if in.BookId != 0 {
m = m.Where(dao.UserReadRecords.Columns().BookId, in.BookId)
}
if in.ChapterId != 0 {
m = m.Where(dao.UserReadRecords.Columns().ChapterId, in.ChapterId)
}
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
return
}
return
}
// Create adds a new user read record
func (s *sUserReadRecord) Create(ctx context.Context, in *model.UserReadRecordAddIn) (out *model.UserReadRecordCRUDOut, err error) {
// 检查是否已存在相同的阅读记录
exist, err := dao.UserReadRecords.Ctx(ctx).
Where(dao.UserReadRecords.Columns().UserId, in.UserId).
Where(dao.UserReadRecords.Columns().BookId, in.BookId).
Where(dao.UserReadRecords.Columns().ChapterId, in.ChapterId).
Exist()
if err != nil {
return nil, ecode.Fail.Sub("read_record_query_failed")
}
if exist {
// 如果记录已存在,更新进度和时间
_, err = dao.UserReadRecords.Ctx(ctx).
Where(dao.UserReadRecords.Columns().UserId, in.UserId).
Where(dao.UserReadRecords.Columns().BookId, in.BookId).
Where(dao.UserReadRecords.Columns().ChapterId, in.ChapterId).
Data(do.UserReadRecords{
Progress: in.Progress,
ReadAt: gtime.Now(),
}).Update()
} else {
// 创建新记录
_, err = dao.UserReadRecords.Ctx(ctx).Data(do.UserReadRecords{
UserId: in.UserId,
BookId: in.BookId,
ChapterId: in.ChapterId,
Progress: in.Progress,
ReadAt: gtime.Now(),
}).Insert()
}
if err != nil {
return nil, ecode.Fail.Sub("read_record_create_failed")
}
return &model.UserReadRecordCRUDOut{Success: true}, nil
}
// Delete removes user read records by userId and bookIds
func (s *sUserReadRecord) Delete(ctx context.Context, in *model.UserReadRecordDelIn) (out *model.UserReadRecordCRUDOut, err error) {
if len(in.BookIds) == 0 {
return nil, ecode.Params.Sub("bookshelve_bookids_empty")
}
// 批量删除指定用户的指定书籍历史记录
_, err = dao.UserReadRecords.Ctx(ctx).
Where(dao.UserReadRecords.Columns().UserId, in.UserId).
WhereIn(dao.UserReadRecords.Columns().BookId, in.BookIds).
Delete()
if err != nil {
return nil, ecode.Fail.Sub("read_record_delete_failed")
}
return &model.UserReadRecordCRUDOut{Success: true}, nil
}