blog_category.go 4.9 KB

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