blog_category.go 4.3 KB

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