shop_category.go 4.8 KB

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