shop.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package fetdata
  2. import (
  3. "math"
  4. "strings"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. type ShopPagination struct {
  9. Num string
  10. Link string
  11. Current bool
  12. Dots bool
  13. }
  14. type Shop struct {
  15. wrap *wrapper.Wrapper
  16. category *ShopCategory
  17. product *ShopProduct
  18. products []*ShopProduct
  19. productsCount int
  20. productsPerPage int
  21. productsMaxPage int
  22. productsCurrPage int
  23. pagination []*ShopPagination
  24. paginationPrev *ShopPagination
  25. paginationNext *ShopPagination
  26. bufferCats map[string][]*ShopCategory
  27. }
  28. func (this *Shop) load() {
  29. if this == nil {
  30. return
  31. }
  32. sql_nums := `
  33. SELECT
  34. COUNT(*)
  35. FROM
  36. shop_products
  37. WHERE
  38. active = 1
  39. ;
  40. `
  41. sql_rows := `
  42. SELECT
  43. shop_products.id,
  44. shop_products.user,
  45. shop_products.currency,
  46. shop_products.price,
  47. shop_products.name,
  48. shop_products.alias,
  49. shop_products.briefly,
  50. shop_products.content,
  51. UNIX_TIMESTAMP(shop_products.datetime) as datetime,
  52. shop_products.active,
  53. users.id,
  54. users.first_name,
  55. users.last_name,
  56. users.email,
  57. users.admin,
  58. users.active,
  59. shop_currencies.id,
  60. shop_currencies.name,
  61. shop_currencies.coefficient,
  62. shop_currencies.code,
  63. shop_currencies.symbol
  64. FROM
  65. shop_products
  66. LEFT JOIN users ON users.id = shop_products.user
  67. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  68. WHERE
  69. shop_products.active = 1
  70. ORDER BY
  71. shop_products.id DESC
  72. LIMIT ?, ?;
  73. `
  74. // Category selected
  75. if this.category != nil {
  76. var cat_ids []string
  77. if rows, err := this.wrap.DB.Query(
  78. `SELECT
  79. node.id
  80. FROM
  81. shop_cats AS node,
  82. shop_cats AS parent
  83. WHERE
  84. node.lft BETWEEN parent.lft AND parent.rgt AND
  85. node.id > 1 AND
  86. parent.id = ?
  87. GROUP BY
  88. node.id
  89. ORDER BY
  90. node.lft ASC
  91. ;`,
  92. this.category.Id(),
  93. ); err == nil {
  94. defer rows.Close()
  95. for rows.Next() {
  96. var cat_id string
  97. if err := rows.Scan(&cat_id); err == nil {
  98. cat_ids = append(cat_ids, cat_id)
  99. }
  100. }
  101. }
  102. sql_nums = `
  103. SELECT
  104. COUNT(*)
  105. FROM
  106. (
  107. SELECT
  108. COUNT(*)
  109. FROM
  110. shop_products
  111. LEFT JOIN shop_cat_product_rel ON shop_cat_product_rel.product_id = shop_products.id
  112. WHERE
  113. shop_products.active = 1 AND
  114. shop_cat_product_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
  115. GROUP BY
  116. shop_products.id
  117. ) AS tbl
  118. ;
  119. `
  120. sql_rows = `
  121. SELECT
  122. shop_products.id,
  123. shop_products.user,
  124. shop_products.currency,
  125. shop_products.price,
  126. shop_products.name,
  127. shop_products.alias,
  128. shop_products.briefly,
  129. shop_products.content,
  130. UNIX_TIMESTAMP(shop_products.datetime) AS datetime,
  131. shop_products.active,
  132. users.id,
  133. users.first_name,
  134. users.last_name,
  135. users.email,
  136. users.admin,
  137. users.active,
  138. shop_currencies.id,
  139. shop_currencies.name,
  140. shop_currencies.coefficient,
  141. shop_currencies.code,
  142. shop_currencies.symbol
  143. FROM
  144. shop_products
  145. LEFT JOIN shop_cat_product_rel ON shop_cat_product_rel.product_id = shop_products.id
  146. LEFT JOIN users ON users.id = shop_products.user
  147. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  148. WHERE
  149. shop_products.active = 1 AND
  150. shop_cat_product_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
  151. GROUP BY
  152. shop_products.id
  153. ORDER BY
  154. shop_products.id DESC
  155. LIMIT ?, ?;
  156. `
  157. }
  158. if err := this.wrap.DB.QueryRow(sql_nums).Scan(&this.productsCount); err == nil {
  159. if this.category == nil {
  160. this.productsPerPage = (*this.wrap.Config).Shop.Pagination.Index
  161. } else {
  162. this.productsPerPage = (*this.wrap.Config).Shop.Pagination.Category
  163. }
  164. this.productsMaxPage = int(math.Ceil(float64(this.productsCount) / float64(this.productsPerPage)))
  165. this.productsCurrPage = this.wrap.GetCurrentPage(this.productsMaxPage)
  166. offset := this.productsCurrPage*this.productsPerPage - this.productsPerPage
  167. if rows, err := this.wrap.DB.Query(sql_rows, offset, this.productsPerPage); err == nil {
  168. defer rows.Close()
  169. for rows.Next() {
  170. rp := utils.MySql_shop_product{}
  171. ru := utils.MySql_user{}
  172. rc := utils.MySql_shop_currency{}
  173. if err := rows.Scan(
  174. &rp.A_id,
  175. &rp.A_user,
  176. &rp.A_currency,
  177. &rp.A_price,
  178. &rp.A_name,
  179. &rp.A_alias,
  180. &rp.A_briefly,
  181. &rp.A_content,
  182. &rp.A_datetime,
  183. &rp.A_active,
  184. &ru.A_id,
  185. &ru.A_first_name,
  186. &ru.A_last_name,
  187. &ru.A_email,
  188. &ru.A_admin,
  189. &ru.A_active,
  190. &rc.A_id,
  191. &rc.A_name,
  192. &rc.A_coefficient,
  193. &rc.A_code,
  194. &rc.A_symbol,
  195. ); err == nil {
  196. this.products = append(this.products, &ShopProduct{
  197. wrap: this.wrap,
  198. object: &rp,
  199. user: &User{wrap: this.wrap, object: &ru},
  200. currency: &Currency{wrap: this.wrap, object: &rc},
  201. })
  202. }
  203. }
  204. }
  205. }
  206. // Build pagination
  207. if true {
  208. for i := 1; i < this.productsCurrPage; i++ {
  209. if this.productsCurrPage >= 5 && i > 1 && i < this.productsCurrPage-1 {
  210. continue
  211. }
  212. if this.productsCurrPage >= 5 && i > 1 && i < this.productsCurrPage {
  213. this.pagination = append(this.pagination, &ShopPagination{
  214. Dots: true,
  215. })
  216. }
  217. link := this.wrap.R.URL.Path
  218. if i > 1 {
  219. link = link + "?p=" + utils.IntToStr(i)
  220. }
  221. this.pagination = append(this.pagination, &ShopPagination{
  222. Num: utils.IntToStr(i),
  223. Link: link,
  224. Current: false,
  225. })
  226. }
  227. // Current page
  228. link := this.wrap.R.URL.Path
  229. if this.productsCurrPage > 1 {
  230. link = link + "?p=" + utils.IntToStr(this.productsCurrPage)
  231. }
  232. this.pagination = append(this.pagination, &ShopPagination{
  233. Num: utils.IntToStr(this.productsCurrPage),
  234. Link: link,
  235. Current: true,
  236. })
  237. for i := this.productsCurrPage + 1; i <= this.productsMaxPage; i++ {
  238. if this.productsCurrPage < this.productsMaxPage-3 && i == this.productsCurrPage+3 {
  239. this.pagination = append(this.pagination, &ShopPagination{
  240. Dots: true,
  241. })
  242. }
  243. if this.productsCurrPage < this.productsMaxPage-3 && i > this.productsCurrPage+1 && i <= this.productsMaxPage-1 {
  244. continue
  245. }
  246. link := this.wrap.R.URL.Path
  247. if i > 1 {
  248. link = link + "?p=" + utils.IntToStr(i)
  249. }
  250. this.pagination = append(this.pagination, &ShopPagination{
  251. Num: utils.IntToStr(i),
  252. Link: link,
  253. Current: false,
  254. })
  255. }
  256. } else {
  257. for i := 1; i <= this.productsMaxPage; i++ {
  258. link := this.wrap.R.URL.Path
  259. if i > 1 {
  260. link = link + "?p=" + utils.IntToStr(i)
  261. }
  262. this.pagination = append(this.pagination, &ShopPagination{
  263. Num: utils.IntToStr(i),
  264. Link: link,
  265. Current: i == this.productsCurrPage,
  266. })
  267. }
  268. }
  269. // Pagination prev/next
  270. if this.productsMaxPage > 1 {
  271. link := this.wrap.R.URL.Path
  272. if this.productsCurrPage-1 > 1 {
  273. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.productsCurrPage-1)
  274. }
  275. this.paginationPrev = &ShopPagination{
  276. Num: utils.IntToStr(this.productsCurrPage - 1),
  277. Link: link,
  278. Current: this.productsCurrPage <= 1,
  279. }
  280. if this.productsCurrPage >= 1 && this.productsCurrPage < this.productsMaxPage {
  281. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.productsCurrPage+1)
  282. } else {
  283. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.productsMaxPage)
  284. }
  285. this.paginationNext = &ShopPagination{
  286. Num: utils.IntToStr(this.productsCurrPage + 1),
  287. Link: link,
  288. Current: this.productsCurrPage >= this.productsMaxPage,
  289. }
  290. }
  291. }
  292. func (this *Shop) Category() *ShopCategory {
  293. if this == nil {
  294. return nil
  295. }
  296. return this.category
  297. }
  298. func (this *Shop) Product() *ShopProduct {
  299. if this == nil {
  300. return nil
  301. }
  302. return this.product
  303. }
  304. func (this *Shop) HaveProducts() bool {
  305. if this == nil {
  306. return false
  307. }
  308. if len(this.products) <= 0 {
  309. return false
  310. }
  311. return true
  312. }
  313. func (this *Shop) Products() []*ShopProduct {
  314. if this == nil {
  315. return []*ShopProduct{}
  316. }
  317. return this.products
  318. }
  319. func (this *Shop) ProductsCount() int {
  320. if this == nil {
  321. return 0
  322. }
  323. return this.productsCount
  324. }
  325. func (this *Shop) ProductsPerPage() int {
  326. if this == nil {
  327. return 0
  328. }
  329. return this.productsPerPage
  330. }
  331. func (this *Shop) ProductsMaxPage() int {
  332. if this == nil {
  333. return 0
  334. }
  335. return this.productsMaxPage
  336. }
  337. func (this *Shop) ProductsCurrPage() int {
  338. if this == nil {
  339. return 0
  340. }
  341. return this.productsCurrPage
  342. }
  343. func (this *Shop) Pagination() []*ShopPagination {
  344. if this == nil {
  345. return []*ShopPagination{}
  346. }
  347. return this.pagination
  348. }
  349. func (this *Shop) PaginationPrev() *ShopPagination {
  350. if this == nil {
  351. return nil
  352. }
  353. return this.paginationPrev
  354. }
  355. func (this *Shop) PaginationNext() *ShopPagination {
  356. if this == nil {
  357. return nil
  358. }
  359. return this.paginationNext
  360. }
  361. func (this *Shop) Categories(mlvl int) []*ShopCategory {
  362. if this == nil {
  363. return []*ShopCategory{}
  364. }
  365. if this.bufferCats == nil {
  366. this.bufferCats = map[string][]*ShopCategory{}
  367. }
  368. key := ""
  369. where := ``
  370. if mlvl > 0 {
  371. where += `AND tbl.depth <= ` + utils.IntToStr(mlvl)
  372. }
  373. if _, ok := this.bufferCats[key]; !ok {
  374. var cats []*ShopCategory
  375. if rows, err := this.wrap.DB.Query(`
  376. SELECT
  377. tbl.*
  378. FROM
  379. (
  380. SELECT
  381. node.id,
  382. node.user,
  383. node.name,
  384. node.alias,
  385. node.lft,
  386. node.rgt,
  387. (COUNT(parent.id) - 1) AS depth
  388. FROM
  389. shop_cats AS node,
  390. shop_cats AS parent
  391. WHERE
  392. node.lft BETWEEN parent.lft AND parent.rgt
  393. GROUP BY
  394. node.id
  395. ORDER BY
  396. node.lft ASC
  397. ) AS tbl
  398. WHERE
  399. tbl.id > 1
  400. ` + where + `
  401. ;
  402. `); err == nil {
  403. defer rows.Close()
  404. for rows.Next() {
  405. row := utils.MySql_shop_category{}
  406. var Depth int
  407. if err := rows.Scan(&row.A_id, &row.A_user, &row.A_name, &row.A_alias, &row.A_lft, &row.A_rgt, &Depth); err == nil {
  408. cats = append(cats, &ShopCategory{object: &row, depth: Depth})
  409. }
  410. }
  411. }
  412. this.bufferCats[key] = cats
  413. }
  414. return this.bufferCats[key]
  415. }