shop_category.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. bufferCats map[int]*utils.MySql_shop_category
  11. }
  12. func (this *ShopCategory) load(cache *map[int]*utils.MySql_shop_category) *ShopCategory {
  13. if this == nil {
  14. return this
  15. }
  16. if cache != nil {
  17. this.bufferCats = (*cache)
  18. return this
  19. }
  20. if this.bufferCats == nil {
  21. this.bufferCats = map[int]*utils.MySql_shop_category{}
  22. }
  23. if rows, err := this.wrap.DB.Query(`
  24. SELECT
  25. main.id,
  26. main.user,
  27. main.name,
  28. main.alias,
  29. main.lft,
  30. main.rgt,
  31. main.depth,
  32. parent.id AS parent_id
  33. FROM
  34. (
  35. SELECT
  36. node.id,
  37. node.user,
  38. node.name,
  39. node.alias,
  40. node.lft,
  41. node.rgt,
  42. (COUNT(parent.id) - 1) AS depth
  43. FROM
  44. shop_cats AS node,
  45. shop_cats AS parent
  46. WHERE
  47. node.lft BETWEEN parent.lft AND parent.rgt
  48. GROUP BY
  49. node.id
  50. ORDER BY
  51. node.lft ASC
  52. ) AS main
  53. LEFT JOIN (
  54. SELECT
  55. node.id,
  56. node.user,
  57. node.name,
  58. node.alias,
  59. node.lft,
  60. node.rgt,
  61. (COUNT(parent.id) - 0) AS depth
  62. FROM
  63. shop_cats AS node,
  64. shop_cats AS parent
  65. WHERE
  66. node.lft BETWEEN parent.lft AND parent.rgt
  67. GROUP BY
  68. node.id
  69. ORDER BY
  70. node.lft ASC
  71. ) AS parent ON
  72. parent.depth = main.depth AND
  73. main.lft > parent.lft AND
  74. main.rgt < parent.rgt
  75. WHERE
  76. main.id > 1
  77. ORDER BY
  78. main.lft ASC
  79. ;
  80. `); err == nil {
  81. defer rows.Close()
  82. for rows.Next() {
  83. row := utils.MySql_shop_category{}
  84. if err := rows.Scan(
  85. &row.A_id,
  86. &row.A_user,
  87. &row.A_name,
  88. &row.A_alias,
  89. &row.A_lft,
  90. &row.A_rgt,
  91. &row.A_depth,
  92. &row.A_parent,
  93. ); *this.wrap.LogCpError(&err) == nil {
  94. this.bufferCats[row.A_id] = &row
  95. if _, ok := this.bufferCats[row.A_parent]; ok {
  96. this.bufferCats[row.A_parent].A_childs = true
  97. }
  98. }
  99. }
  100. }
  101. return this
  102. }
  103. func (this *ShopCategory) loadById(id int) {
  104. if this == nil {
  105. return
  106. }
  107. if this.object != nil {
  108. return
  109. }
  110. this.object = &utils.MySql_shop_category{}
  111. if err := this.wrap.DB.QueryRow(`
  112. SELECT
  113. main.id,
  114. main.user,
  115. main.name,
  116. main.alias,
  117. main.lft,
  118. main.rgt,
  119. main.depth,
  120. parent.id AS parent_id
  121. FROM
  122. (
  123. SELECT
  124. node.id,
  125. node.user,
  126. node.name,
  127. node.alias,
  128. node.lft,
  129. node.rgt,
  130. (COUNT(parent.id) - 1) AS depth
  131. FROM
  132. shop_cats AS node,
  133. shop_cats AS parent
  134. WHERE
  135. node.lft BETWEEN parent.lft AND parent.rgt
  136. GROUP BY
  137. node.id
  138. ORDER BY
  139. node.lft ASC
  140. ) AS main
  141. LEFT JOIN (
  142. SELECT
  143. node.id,
  144. node.user,
  145. node.name,
  146. node.alias,
  147. node.lft,
  148. node.rgt,
  149. (COUNT(parent.id) - 0) AS depth
  150. FROM
  151. shop_cats AS node,
  152. shop_cats AS parent
  153. WHERE
  154. node.lft BETWEEN parent.lft AND parent.rgt
  155. GROUP BY
  156. node.id
  157. ORDER BY
  158. node.lft ASC
  159. ) AS parent ON
  160. parent.depth = main.depth AND
  161. main.lft > parent.lft AND
  162. main.rgt < parent.rgt
  163. WHERE
  164. main.id > 1 AND
  165. main.id = ?
  166. ORDER BY
  167. main.lft ASC
  168. ;`,
  169. id,
  170. ).Scan(
  171. &this.object.A_id,
  172. &this.object.A_user,
  173. &this.object.A_name,
  174. &this.object.A_alias,
  175. &this.object.A_lft,
  176. &this.object.A_rgt,
  177. &this.object.A_depth,
  178. &this.object.A_parent,
  179. ); *this.wrap.LogCpError(&err) != nil {
  180. return
  181. }
  182. }
  183. func (this *ShopCategory) Id() int {
  184. if this == nil {
  185. return 0
  186. }
  187. return this.object.A_id
  188. }
  189. func (this *ShopCategory) User() *User {
  190. if this == nil {
  191. return nil
  192. }
  193. if this.user != nil {
  194. return this.user
  195. }
  196. this.user = (&User{wrap: this.wrap}).load()
  197. this.user.loadById(this.object.A_user)
  198. return this.user
  199. }
  200. func (this *ShopCategory) Name() string {
  201. if this == nil {
  202. return ""
  203. }
  204. return this.object.A_name
  205. }
  206. func (this *ShopCategory) Alias() string {
  207. if this == nil {
  208. return ""
  209. }
  210. return this.object.A_alias
  211. }
  212. func (this *ShopCategory) Left() int {
  213. if this == nil {
  214. return 0
  215. }
  216. return this.object.A_lft
  217. }
  218. func (this *ShopCategory) Right() int {
  219. if this == nil {
  220. return 0
  221. }
  222. return this.object.A_rgt
  223. }
  224. func (this *ShopCategory) Permalink() string {
  225. if this == nil {
  226. return ""
  227. }
  228. return "/shop/category/" + this.object.A_alias + "/"
  229. }
  230. func (this *ShopCategory) Level() int {
  231. if this == nil {
  232. return 0
  233. }
  234. return this.object.A_depth
  235. }
  236. func (this *ShopCategory) Parent() *ShopCategory {
  237. if this == nil {
  238. return nil
  239. }
  240. if this.bufferCats == nil {
  241. return nil
  242. }
  243. if _, ok := this.bufferCats[this.object.A_parent]; !ok {
  244. return nil
  245. }
  246. cat := &ShopCategory{wrap: this.wrap, object: this.bufferCats[this.object.A_parent]}
  247. return cat.load(&this.bufferCats)
  248. }
  249. func (this *ShopCategory) HaveChilds() bool {
  250. if this == nil {
  251. return false
  252. }
  253. return this.object.A_childs
  254. }