Skip to content

Commit

Permalink
Merge pull request #14 from luxlzz6/main
Browse files Browse the repository at this point in the history
绘画列表显示统一了风格 添加了渠道
  • Loading branch information
Calcium-Ion authored Nov 23, 2023
2 parents d4b3f87 + 3e89112 commit 2ee3dbb
Show file tree
Hide file tree
Showing 3 changed files with 461 additions and 382 deletions.
21 changes: 19 additions & 2 deletions controller/midjourney.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,16 @@ func GetAllMidjourney(c *gin.Context) {
if p < 0 {
p = 0
}
logs := model.GetAllTasks(p*common.ItemsPerPage, common.ItemsPerPage)

// 解析其他查询参数
queryParams := model.TaskQueryParams{
ChannelID: c.Query("channel_id"),
MjID: c.Query("mj_id"),
StartTimestamp: c.Query("start_timestamp"),
EndTimestamp: c.Query("end_timestamp"),
}

logs := model.GetAllTasks(p*common.ItemsPerPage, common.ItemsPerPage, queryParams)
if logs == nil {
logs = make([]*model.Midjourney, 0)
}
Expand All @@ -164,9 +173,17 @@ func GetUserMidjourney(c *gin.Context) {
if p < 0 {
p = 0
}

userId := c.GetInt("id")
log.Printf("userId = %d \n", userId)
logs := model.GetAllUserTask(userId, p*common.ItemsPerPage, common.ItemsPerPage)

queryParams := model.TaskQueryParams{
MjID: c.Query("mj_id"),
StartTimestamp: c.Query("start_timestamp"),
EndTimestamp: c.Query("end_timestamp"),
}

logs := model.GetAllUserTask(userId, p*common.ItemsPerPage, common.ItemsPerPage, queryParams)
if logs == nil {
logs = make([]*model.Midjourney, 0)
}
Expand Down
59 changes: 49 additions & 10 deletions model/midjourney.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,68 @@ type Midjourney struct {
ChannelId int `json:"channel_id"`
}

func GetAllUserTask(userId int, startIdx int, num int) []*Midjourney {
// 用于包含所有搜索条件的结构体,可以根据需求添加更多字段
type TaskQueryParams struct {
ChannelID string
MjID string
StartTimestamp string
EndTimestamp string
}

func GetAllUserTask(userId int, startIdx int, num int, queryParams TaskQueryParams) []*Midjourney {
var tasks []*Midjourney
var err error
err = DB.Where("user_id = ?", userId).Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error

// 初始化查询构建器
query := DB.Where("user_id = ?", userId)

if queryParams.MjID != "" {
query = query.Where("mj_id = ?", queryParams.MjID)
}
if queryParams.StartTimestamp != "" {
// 假设您已将前端传来的时间戳转换为数据库所需的时间格式,并处理了时间戳的验证和解析
query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
}
if queryParams.EndTimestamp != "" {
query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
}

// 获取数据
err = query.Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error
if err != nil {
return nil
}
for _, task := range tasks {
task.ImageUrl = common.ServerAddress + "/mj/image/" + task.MjId
}

return tasks
}

func GetAllTasks(startIdx int, num int) []*Midjourney {
func GetAllTasks(startIdx int, num int, queryParams TaskQueryParams) []*Midjourney {
var tasks []*Midjourney
var err error
err = DB.Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error

// 初始化查询构建器
query := DB

// 添加过滤条件
if queryParams.ChannelID != "" {
query = query.Where("channel_id = ?", queryParams.ChannelID)
}
if queryParams.MjID != "" {
query = query.Where("mj_id = ?", queryParams.MjID)
}
if queryParams.StartTimestamp != "" {
query = query.Where("submit_time >= ?", queryParams.StartTimestamp)
}
if queryParams.EndTimestamp != "" {
query = query.Where("submit_time <= ?", queryParams.EndTimestamp)
}

// 获取数据
err = query.Order("id desc").Limit(num).Offset(startIdx).Find(&tasks).Error
if err != nil {
return nil
}
for _, task := range tasks {
task.ImageUrl = common.ServerAddress + "/mj/image/" + task.MjId
}

return tasks
}

Expand Down
Loading

0 comments on commit 2ee3dbb

Please sign in to comment.