按时间筛选

This commit is contained in:
2024-11-09 07:06:13 +08:00
parent 85a71a64ff
commit b01c186acc

View File

@@ -388,8 +388,8 @@ func NewSchema(config Config) (graphql.Schema, error) {
"collect_count": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中收藏数等于指定值的"}, "collect_count": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中收藏数等于指定值的"},
"article_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中属于指定文章ID的"}, "article_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中属于指定文章ID的"},
"user_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中属于指定用户ID的"}, "user_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选图像中属于指定用户ID的"},
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选图像中创建时间等于指定值的"}, "create_time": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中创建时间等于指定值的"},
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选图像中更新时间等于指定值的"}, "update_time": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中更新时间等于指定值的"},
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"}, "first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"}, "last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
"after": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之後的元素)"}, "after": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
@@ -409,9 +409,13 @@ func NewSchema(config Config) (graphql.Schema, error) {
Orientation string Orientation string
Sort string Sort string
Order string Order string
CreateTime string `json:"create_time"`
UpdateTime string
} }
mapstructure.Decode(p.Args, &args) mapstructure.Decode(p.Args, &args)
fmt.Println("args.CreateTime:", args.CreateTime)
// 限制长度防止全表扫描 // 限制长度防止全表扫描
var limit = 10 var limit = 10
if args.First != 0 { if args.First != 0 {
@@ -425,7 +429,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
var query = db.Limit(limit) var query = db.Limit(limit)
// 参数映射 // 参数映射
var argFormat = []string{"id", "width", "height", "content", "remark", "description", "tags", "rank", "comment_num", "praise_count", "collect_count", "article_id", "user_id", "create_time", "update_time"} var argFormat = []string{"id", "width", "height", "content", "remark", "description", "tags", "rank", "comment_num", "praise_count", "collect_count", "article_id", "user_id"}
// 筛选条件 // 筛选条件
for _, format := range argFormat { for _, format := range argFormat {
@@ -519,6 +523,24 @@ func NewSchema(config Config) (graphql.Schema, error) {
id_list = append(id_list, ids) id_list = append(id_list, ids)
} }
// 筛选:时间段
applyTimeCondition := func(name string, str string) {
parts := strings.Split(str, "-")
query = query.Where(fmt.Sprintf("YEAR(%s) = ?", name), parts[0])
if len(parts) > 1 {
query = query.Where(fmt.Sprintf("MONTH(%s) = ?", name), parts[1])
}
if len(parts) > 2 {
query = query.Where(fmt.Sprintf("DAY(%s) = ?", name), parts[2])
}
}
if createTime, ok := p.Args["create_time"].(string); ok && createTime != "" {
applyTimeCondition("create_time", createTime)
} else if updateTime, ok := p.Args["update_time"].(string); ok && updateTime != "" {
applyTimeCondition("update_time", updateTime)
}
// 数据库中筛选:横屏纵屏 // 数据库中筛选:横屏纵屏
if args.Orientation != "" { if args.Orientation != "" {
query.Where("article_orientation = ?", args.Orientation) query.Where("article_orientation = ?", args.Orientation)