shop_category.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package fetdata
  2. import (
  3. "golang-fave/engine/wrapper"
  4. "golang-fave/utils"
  5. )
  6. type ShopCategory struct {
  7. wrap *wrapper.Wrapper
  8. object *utils.MySql_shop_category
  9. user *User
  10. }
  11. func (this *ShopCategory) load() *ShopCategory {
  12. return this
  13. }
  14. func (this *ShopCategory) loadById(id int) {
  15. if this == nil {
  16. return
  17. }
  18. if this.object != nil {
  19. return
  20. }
  21. this.object = &utils.MySql_shop_category{}
  22. if err := this.wrap.DB.QueryRow(`
  23. SELECT
  24. main.id,
  25. main.user,
  26. main.name,
  27. main.alias,
  28. main.lft,
  29. main.rgt,
  30. depth.depth,
  31. MAX(main.parent_id) AS parent_id
  32. FROM
  33. (
  34. SELECT
  35. node.id,
  36. node.user,
  37. node.name,
  38. node.alias,
  39. node.lft,
  40. node.rgt,
  41. parent.id AS parent_id
  42. FROM
  43. shop_cats AS node,
  44. shop_cats AS parent
  45. WHERE
  46. node.lft BETWEEN parent.lft AND parent.rgt AND
  47. node.id > 1
  48. ORDER BY
  49. node.lft ASC
  50. ) AS main
  51. LEFT JOIN (
  52. SELECT
  53. node.id,
  54. (COUNT(parent.id) - 1) AS depth
  55. FROM
  56. shop_cats AS node,
  57. shop_cats AS parent
  58. WHERE
  59. node.lft BETWEEN parent.lft AND parent.rgt
  60. GROUP BY
  61. node.id
  62. ORDER BY
  63. node.lft ASC
  64. ) AS depth ON depth.id = main.id
  65. WHERE
  66. main.id > 1 AND
  67. main.id <> main.parent_id AND
  68. main.id = ?
  69. GROUP BY
  70. main.id
  71. ;`,
  72. id,
  73. ).Scan(
  74. &this.object.A_id,
  75. &this.object.A_user,
  76. &this.object.A_name,
  77. &this.object.A_alias,
  78. &this.object.A_lft,
  79. &this.object.A_rgt,
  80. &this.object.A_depth,
  81. &this.object.A_parent,
  82. ); err != nil {
  83. return
  84. }
  85. }
  86. func (this *ShopCategory) Id() int {
  87. if this == nil {
  88. return 0
  89. }
  90. return this.object.A_id
  91. }
  92. func (this *ShopCategory) User() *User {
  93. if this == nil {
  94. return nil
  95. }
  96. if this.user != nil {
  97. return this.user
  98. }
  99. this.user = &User{wrap: this.wrap}
  100. this.user.loadById(this.object.A_user)
  101. return this.user
  102. }
  103. func (this *ShopCategory) Name() string {
  104. if this == nil {
  105. return ""
  106. }
  107. return this.object.A_name
  108. }
  109. func (this *ShopCategory) Alias() string {
  110. if this == nil {
  111. return ""
  112. }
  113. return this.object.A_alias
  114. }
  115. func (this *ShopCategory) Left() int {
  116. if this == nil {
  117. return 0
  118. }
  119. return this.object.A_lft
  120. }
  121. func (this *ShopCategory) Right() int {
  122. if this == nil {
  123. return 0
  124. }
  125. return this.object.A_rgt
  126. }
  127. func (this *ShopCategory) Permalink() string {
  128. if this == nil {
  129. return ""
  130. }
  131. return "/shop/category/" + this.object.A_alias + "/"
  132. }
  133. func (this *ShopCategory) Level() int {
  134. if this == nil {
  135. return 0
  136. }
  137. return this.object.A_depth
  138. }
  139. func (this *ShopCategory) ParentId() int {
  140. if this == nil {
  141. return 0
  142. }
  143. return this.object.A_parent
  144. }