Browse Source

Add parent id to shop category

Vova Tkach 5 years ago
parent
commit
0bb3902b1b
3 changed files with 95 additions and 25 deletions
  1. 31 7
      engine/fetdata/shop.go
  2. 56 12
      engine/fetdata/shop_category.go
  3. 8 6
      utils/mysql_struct_shop_category.go

+ 31 - 7
engine/fetdata/shop.go

@@ -462,13 +462,20 @@ func (this *Shop) Categories(mlvl int) []*ShopCategory {
 	key := ""
 	where := ``
 	if mlvl > 0 {
-		where += `AND tbl.depth <= ` + utils.IntToStr(mlvl)
+		where += `AND depth.depth <= ` + utils.IntToStr(mlvl)
 	}
 	if _, ok := this.bufferCats[key]; !ok {
 		var cats []*ShopCategory
 		if rows, err := this.wrap.DB.Query(`
 			SELECT
-				tbl.*
+				main.id,
+				main.user,
+				main.name,
+				main.alias,
+				main.lft,
+				main.rgt,
+				depth.depth,
+				MAX(main.parent_id) AS parent_id
 			FROM
 				(
 					SELECT
@@ -478,6 +485,19 @@ func (this *Shop) Categories(mlvl int) []*ShopCategory {
 						node.alias,
 						node.lft,
 						node.rgt,
+						parent.id AS parent_id
+					FROM
+						shop_cats AS node,
+						shop_cats AS parent
+					WHERE
+						node.lft BETWEEN parent.lft AND parent.rgt AND
+						node.id > 1
+					ORDER BY
+						node.lft ASC
+				) AS main
+				LEFT JOIN (
+					SELECT
+						node.id,
 						(COUNT(parent.id) - 1) AS depth
 					FROM
 						shop_cats AS node,
@@ -488,18 +508,22 @@ func (this *Shop) Categories(mlvl int) []*ShopCategory {
 						node.id
 					ORDER BY
 						node.lft ASC
-				) AS tbl
+				) AS depth ON depth.id = main.id
 			WHERE
-				tbl.id > 1
+				main.id > 1 AND
+				main.id <> main.parent_id
 				` + where + `
+			GROUP BY
+				main.id
+			ORDER BY
+				main.lft ASC
 			;
 		`); err == nil {
 			defer rows.Close()
 			for rows.Next() {
 				row := utils.MySql_shop_category{}
-				var Depth int
-				if err := rows.Scan(&row.A_id, &row.A_user, &row.A_name, &row.A_alias, &row.A_lft, &row.A_rgt, &Depth); err == nil {
-					cats = append(cats, &ShopCategory{object: &row, depth: Depth})
+				if err := rows.Scan(&row.A_id, &row.A_user, &row.A_name, &row.A_alias, &row.A_lft, &row.A_rgt, &row.A_depth, &row.A_parent); err == nil {
+					cats = append(cats, &ShopCategory{object: &row})
 				}
 			}
 		}

+ 56 - 12
engine/fetdata/shop_category.go

@@ -8,7 +8,6 @@ import (
 type ShopCategory struct {
 	wrap   *wrapper.Wrapper
 	object *utils.MySql_shop_category
-	depth  int
 
 	user *User
 }
@@ -27,18 +26,54 @@ func (this *ShopCategory) loadById(id int) {
 	this.object = &utils.MySql_shop_category{}
 	if err := this.wrap.DB.QueryRow(`
 		SELECT
-			id,
-			user,
-			name,
-			alias,
-			lft,
-			rgt
+			main.id,
+			main.user,
+			main.name,
+			main.alias,
+			main.lft,
+			main.rgt,
+			depth.depth,
+			MAX(main.parent_id) AS parent_id
 		FROM
-			shop_cats
+			(
+				SELECT
+					node.id,
+					node.user,
+					node.name,
+					node.alias,
+					node.lft,
+					node.rgt,
+					parent.id AS parent_id
+				FROM
+					shop_cats AS node,
+					shop_cats AS parent
+				WHERE
+					node.lft BETWEEN parent.lft AND parent.rgt AND
+					node.id > 1
+				ORDER BY
+					node.lft ASC
+			) AS main
+			LEFT JOIN (
+				SELECT
+					node.id,
+					(COUNT(parent.id) - 1) AS depth
+				FROM
+					shop_cats AS node,
+					shop_cats AS parent
+				WHERE
+					node.lft BETWEEN parent.lft AND parent.rgt
+				GROUP BY
+					node.id
+				ORDER BY
+					node.lft ASC
+			) AS depth ON depth.id = main.id
 		WHERE
-			id = ? AND
-			id > 1
-		LIMIT 1;`,
+			main.id > 1 AND
+			main.id <> main.parent_id AND
+			main.id = ?
+		GROUP BY
+			main.id
+		;`,
 		id,
 	).Scan(
 		&this.object.A_id,
@@ -47,6 +82,8 @@ func (this *ShopCategory) loadById(id int) {
 		&this.object.A_alias,
 		&this.object.A_lft,
 		&this.object.A_rgt,
+		&this.object.A_depth,
+		&this.object.A_parent,
 	); err != nil {
 		return
 	}
@@ -110,5 +147,12 @@ func (this *ShopCategory) Level() int {
 	if this == nil {
 		return 0
 	}
-	return this.depth
+	return this.object.A_depth
+}
+
+func (this *ShopCategory) ParentId() int {
+	if this == nil {
+		return 0
+	}
+	return this.object.A_parent
 }

+ 8 - 6
utils/mysql_struct_shop_category.go

@@ -1,10 +1,12 @@
 package utils
 
 type MySql_shop_category struct {
-	A_id    int
-	A_user  int
-	A_name  string
-	A_alias string
-	A_lft   int
-	A_rgt   int
+	A_id     int
+	A_user   int
+	A_name   string
+	A_alias  string
+	A_lft    int
+	A_rgt    int
+	A_depth  int
+	A_parent int
 }