条件查询
This commit is contained in:
		
							
								
								
									
										48
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -49,6 +49,7 @@ type Images struct {
 | 
			
		||||
	Page     int     `json:"page"`
 | 
			
		||||
	PageSize int     `json:"page_size"`
 | 
			
		||||
	Total    int     `json:"total"`
 | 
			
		||||
	Next     bool    `json:"next"`
 | 
			
		||||
	List     []Image `json:"list"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -68,30 +69,40 @@ func main() {
 | 
			
		||||
		// 排序
 | 
			
		||||
		// 分页
 | 
			
		||||
 | 
			
		||||
		// 按标签查询
 | 
			
		||||
		var tags string
 | 
			
		||||
		if list := strings.Split(r.URL.Query().Get("tags"), ","); len(list) > 0 {
 | 
			
		||||
			for _, tag := range list {
 | 
			
		||||
				tags += fmt.Sprintf("'%s',", tag)
 | 
			
		||||
		// 获取查询条件(忽略空值), 超级简洁写法
 | 
			
		||||
		QueryConditions := func(key string) (list []string) {
 | 
			
		||||
			for _, item := range strings.Split(r.URL.Query().Get(key), ",") {
 | 
			
		||||
				if item != "" {
 | 
			
		||||
					list = append(list, fmt.Sprintf("'%s'", item))
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			tags = tags[:len(tags)-1]
 | 
			
		||||
			fmt.Println(tags)
 | 
			
		||||
			return list
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 按分类查询
 | 
			
		||||
		var categories string
 | 
			
		||||
		if list := strings.Split(r.URL.Query().Get("categories"), ","); len(list) > 0 {
 | 
			
		||||
			for _, category := range list {
 | 
			
		||||
				categories += fmt.Sprintf("'%s',", category)
 | 
			
		||||
			}
 | 
			
		||||
			categories = categories[:len(categories)-1]
 | 
			
		||||
			fmt.Println(categories)
 | 
			
		||||
		// 拼接查询条件, 超级简洁写法
 | 
			
		||||
		conditions := ""
 | 
			
		||||
		if authors := QueryConditions("authors"); len(authors) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND author IN (%s)", strings.Join(authors, ","))
 | 
			
		||||
		}
 | 
			
		||||
		if tags := QueryConditions("tags"); len(tags) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND tag IN (%s)", strings.Join(tags, ","))
 | 
			
		||||
		}
 | 
			
		||||
		if categories := QueryConditions("categories"); len(categories) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND categorie IN (%s)", strings.Join(categories, ","))
 | 
			
		||||
		}
 | 
			
		||||
		if sets := QueryConditions("sets"); len(sets) > 0 {
 | 
			
		||||
			conditions += fmt.Sprintf(" AND sets IN (%s)", strings.Join(sets, ","))
 | 
			
		||||
		}
 | 
			
		||||
		if conditions != "" {
 | 
			
		||||
			conditions = strings.Replace(conditions, " AND", "", 1) // 去掉第一个 AND
 | 
			
		||||
			conditions = " WHERE" + conditions                      // 拼接 WHERE
 | 
			
		||||
			fmt.Println(conditions)                                 // 打印查询条件
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 获取图片列表
 | 
			
		||||
		var images Images
 | 
			
		||||
		images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 10)
 | 
			
		||||
		rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
 | 
			
		||||
		rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images"+conditions+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("获取图片列表失败", err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
@@ -110,13 +121,16 @@ func main() {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 获取总数
 | 
			
		||||
		err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images").Scan(&images.Total)
 | 
			
		||||
		err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions).Scan(&images.Total)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("获取图片总数失败", err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusBadRequest)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 是否有下一页
 | 
			
		||||
		images.Next = images.Total > images.Page*images.PageSize
 | 
			
		||||
 | 
			
		||||
		// 将对象转换为有缩进的JSON输出
 | 
			
		||||
		data, err := json.MarshalIndent(images, "", "  ")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user