shop.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. package fetdata
  2. import (
  3. "math"
  4. "sort"
  5. "strings"
  6. "golang-fave/engine/wrapper"
  7. "golang-fave/utils"
  8. )
  9. type ShopPagination struct {
  10. Num string
  11. Link string
  12. Current bool
  13. Dots bool
  14. }
  15. type Shop struct {
  16. wrap *wrapper.Wrapper
  17. category *ShopCategory
  18. product *ShopProduct
  19. products []*ShopProduct
  20. productsCount int
  21. productsPerPage int
  22. productsMaxPage int
  23. productsCurrPage int
  24. pagination []*ShopPagination
  25. paginationPrev *ShopPagination
  26. paginationNext *ShopPagination
  27. bufferCats map[int]*utils.MySql_shop_category
  28. }
  29. func (this *Shop) load() *Shop {
  30. if this == nil {
  31. return this
  32. }
  33. sql_nums := `
  34. SELECT
  35. COUNT(*)
  36. FROM
  37. shop_products
  38. WHERE
  39. active = 1
  40. ;
  41. `
  42. sql_rows := `
  43. SELECT
  44. shop_products.id,
  45. shop_products.user,
  46. shop_products.currency,
  47. shop_products.price,
  48. shop_products.name,
  49. shop_products.alias,
  50. shop_products.vendor,
  51. shop_products.quantity,
  52. shop_products.category,
  53. shop_products.briefly,
  54. shop_products.content,
  55. UNIX_TIMESTAMP(shop_products.datetime) as datetime,
  56. shop_products.active,
  57. users.id,
  58. users.first_name,
  59. users.last_name,
  60. users.email,
  61. users.admin,
  62. users.active,
  63. shop_currencies.id,
  64. shop_currencies.name,
  65. shop_currencies.coefficient,
  66. shop_currencies.code,
  67. shop_currencies.symbol,
  68. cats.id,
  69. cats.user,
  70. cats.name,
  71. cats.alias,
  72. cats.lft,
  73. cats.rgt,
  74. cats.depth,
  75. cats.parent_id
  76. FROM
  77. shop_products
  78. LEFT JOIN users ON users.id = shop_products.user
  79. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  80. LEFT JOIN (
  81. SELECT
  82. main.id,
  83. main.user,
  84. main.name,
  85. main.alias,
  86. main.lft,
  87. main.rgt,
  88. main.depth,
  89. parent.id AS parent_id
  90. FROM
  91. (
  92. SELECT
  93. node.id,
  94. node.user,
  95. node.name,
  96. node.alias,
  97. node.lft,
  98. node.rgt,
  99. (COUNT(parent.id) - 1) AS depth
  100. FROM
  101. shop_cats AS node,
  102. shop_cats AS parent
  103. WHERE
  104. node.lft BETWEEN parent.lft AND parent.rgt
  105. GROUP BY
  106. node.id
  107. ORDER BY
  108. node.lft ASC
  109. ) AS main
  110. LEFT JOIN (
  111. SELECT
  112. node.id,
  113. node.user,
  114. node.name,
  115. node.alias,
  116. node.lft,
  117. node.rgt,
  118. (COUNT(parent.id) - 0) AS depth
  119. FROM
  120. shop_cats AS node,
  121. shop_cats AS parent
  122. WHERE
  123. node.lft BETWEEN parent.lft AND parent.rgt
  124. GROUP BY
  125. node.id
  126. ORDER BY
  127. node.lft ASC
  128. ) AS parent ON
  129. parent.depth = main.depth AND
  130. main.lft > parent.lft AND
  131. main.rgt < parent.rgt
  132. WHERE
  133. main.id > 1
  134. ORDER BY
  135. main.lft ASC
  136. ) AS cats ON cats.id = shop_products.category
  137. WHERE
  138. shop_products.active = 1
  139. ORDER BY
  140. shop_products.id DESC
  141. LIMIT ?, ?;
  142. `
  143. // Category selected
  144. if this.category != nil {
  145. var cat_ids []string
  146. if rows, err := this.wrap.DB.Query(
  147. `SELECT
  148. node.id
  149. FROM
  150. shop_cats AS node,
  151. shop_cats AS parent
  152. WHERE
  153. node.lft BETWEEN parent.lft AND parent.rgt AND
  154. node.id > 1 AND
  155. parent.id = ?
  156. GROUP BY
  157. node.id
  158. ORDER BY
  159. node.lft ASC
  160. ;`,
  161. this.category.Id(),
  162. ); err == nil {
  163. defer rows.Close()
  164. for rows.Next() {
  165. var cat_id string
  166. if err := rows.Scan(&cat_id); err == nil {
  167. cat_ids = append(cat_ids, cat_id)
  168. }
  169. }
  170. }
  171. sql_nums = `
  172. SELECT
  173. COUNT(*)
  174. FROM
  175. (
  176. SELECT
  177. COUNT(*)
  178. FROM
  179. shop_products
  180. LEFT JOIN shop_cat_product_rel ON shop_cat_product_rel.product_id = shop_products.id
  181. WHERE
  182. shop_products.active = 1 AND
  183. shop_cat_product_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
  184. GROUP BY
  185. shop_products.id
  186. ) AS tbl
  187. ;
  188. `
  189. sql_rows = `
  190. SELECT
  191. shop_products.id,
  192. shop_products.user,
  193. shop_products.currency,
  194. shop_products.price,
  195. shop_products.name,
  196. shop_products.alias,
  197. shop_products.vendor,
  198. shop_products.quantity,
  199. shop_products.category,
  200. shop_products.briefly,
  201. shop_products.content,
  202. UNIX_TIMESTAMP(shop_products.datetime) AS datetime,
  203. shop_products.active,
  204. users.id,
  205. users.first_name,
  206. users.last_name,
  207. users.email,
  208. users.admin,
  209. users.active,
  210. shop_currencies.id,
  211. shop_currencies.name,
  212. shop_currencies.coefficient,
  213. shop_currencies.code,
  214. shop_currencies.symbol,
  215. cats.id,
  216. cats.user,
  217. cats.name,
  218. cats.alias,
  219. cats.lft,
  220. cats.rgt,
  221. cats.depth,
  222. cats.parent_id
  223. FROM
  224. shop_products
  225. LEFT JOIN shop_cat_product_rel ON shop_cat_product_rel.product_id = shop_products.id
  226. LEFT JOIN users ON users.id = shop_products.user
  227. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  228. LEFT JOIN (
  229. SELECT
  230. main.id,
  231. main.user,
  232. main.name,
  233. main.alias,
  234. main.lft,
  235. main.rgt,
  236. main.depth,
  237. parent.id AS parent_id
  238. FROM
  239. (
  240. SELECT
  241. node.id,
  242. node.user,
  243. node.name,
  244. node.alias,
  245. node.lft,
  246. node.rgt,
  247. (COUNT(parent.id) - 1) AS depth
  248. FROM
  249. shop_cats AS node,
  250. shop_cats AS parent
  251. WHERE
  252. node.lft BETWEEN parent.lft AND parent.rgt
  253. GROUP BY
  254. node.id
  255. ORDER BY
  256. node.lft ASC
  257. ) AS main
  258. LEFT JOIN (
  259. SELECT
  260. node.id,
  261. node.user,
  262. node.name,
  263. node.alias,
  264. node.lft,
  265. node.rgt,
  266. (COUNT(parent.id) - 0) AS depth
  267. FROM
  268. shop_cats AS node,
  269. shop_cats AS parent
  270. WHERE
  271. node.lft BETWEEN parent.lft AND parent.rgt
  272. GROUP BY
  273. node.id
  274. ORDER BY
  275. node.lft ASC
  276. ) AS parent ON
  277. parent.depth = main.depth AND
  278. main.lft > parent.lft AND
  279. main.rgt < parent.rgt
  280. WHERE
  281. main.id > 1
  282. ORDER BY
  283. main.lft ASC
  284. ) AS cats ON cats.id = shop_products.category
  285. WHERE
  286. shop_products.active = 1 AND
  287. shop_cat_product_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
  288. GROUP BY
  289. shop_products.id,
  290. cats.parent_id
  291. ORDER BY
  292. shop_products.id DESC
  293. LIMIT ?, ?;
  294. `
  295. }
  296. product_ids := []string{}
  297. if err := this.wrap.DB.QueryRow(sql_nums).Scan(&this.productsCount); err == nil {
  298. if this.category == nil {
  299. this.productsPerPage = (*this.wrap.Config).Shop.Pagination.Index
  300. } else {
  301. this.productsPerPage = (*this.wrap.Config).Shop.Pagination.Category
  302. }
  303. this.productsMaxPage = int(math.Ceil(float64(this.productsCount) / float64(this.productsPerPage)))
  304. this.productsCurrPage = this.wrap.GetCurrentPage(this.productsMaxPage)
  305. offset := this.productsCurrPage*this.productsPerPage - this.productsPerPage
  306. if rows, err := this.wrap.DB.Query(sql_rows, offset, this.productsPerPage); err == nil {
  307. defer rows.Close()
  308. for rows.Next() {
  309. rp := utils.MySql_shop_product{}
  310. ru := utils.MySql_user{}
  311. rc := utils.MySql_shop_currency{}
  312. ro := utils.MySql_shop_category{}
  313. if err := rows.Scan(
  314. &rp.A_id,
  315. &rp.A_user,
  316. &rp.A_currency,
  317. &rp.A_price,
  318. &rp.A_name,
  319. &rp.A_alias,
  320. &rp.A_vendor,
  321. &rp.A_quantity,
  322. &rp.A_category,
  323. &rp.A_briefly,
  324. &rp.A_content,
  325. &rp.A_datetime,
  326. &rp.A_active,
  327. &ru.A_id,
  328. &ru.A_first_name,
  329. &ru.A_last_name,
  330. &ru.A_email,
  331. &ru.A_admin,
  332. &ru.A_active,
  333. &rc.A_id,
  334. &rc.A_name,
  335. &rc.A_coefficient,
  336. &rc.A_code,
  337. &rc.A_symbol,
  338. &ro.A_id,
  339. &ro.A_user,
  340. &ro.A_name,
  341. &ro.A_alias,
  342. &ro.A_lft,
  343. &ro.A_rgt,
  344. &ro.A_depth,
  345. &ro.A_parent,
  346. ); err == nil {
  347. product_ids = append(product_ids, utils.IntToStr(rp.A_id))
  348. this.products = append(this.products, &ShopProduct{
  349. wrap: this.wrap,
  350. object: &rp,
  351. user: &User{wrap: this.wrap, object: &ru},
  352. currency: &ShopCurrency{wrap: this.wrap, object: &rc},
  353. category: &ShopCategory{wrap: this.wrap, object: &ro},
  354. })
  355. }
  356. }
  357. }
  358. }
  359. // Product images
  360. product_images := map[int][]*ShopProductImage{}
  361. if len(product_ids) > 0 {
  362. if rows, err := this.wrap.DB.Query(
  363. `SELECT
  364. shop_product_images.product_id,
  365. shop_product_images.filename
  366. FROM
  367. shop_product_images
  368. WHERE
  369. shop_product_images.product_id IN (` + strings.Join(product_ids, ", ") + `)
  370. ORDER BY
  371. shop_product_images.ord ASC
  372. ;`,
  373. ); err == nil {
  374. defer rows.Close()
  375. for rows.Next() {
  376. img := utils.MySql_shop_product_image{}
  377. if err := rows.Scan(
  378. &img.A_product_id,
  379. &img.A_filename,
  380. ); err == nil {
  381. product_images[img.A_product_id] = append(product_images[img.A_product_id], &ShopProductImage{wrap: this.wrap, object: &img})
  382. }
  383. }
  384. }
  385. }
  386. for index, product := range this.products {
  387. if pimgs, ok := product_images[product.Id()]; ok {
  388. this.products[index].images = pimgs
  389. }
  390. }
  391. // Build pagination
  392. if true {
  393. for i := 1; i < this.productsCurrPage; i++ {
  394. if this.productsCurrPage >= 5 && i > 1 && i < this.productsCurrPage-1 {
  395. continue
  396. }
  397. if this.productsCurrPage >= 5 && i > 1 && i < this.productsCurrPage {
  398. this.pagination = append(this.pagination, &ShopPagination{
  399. Dots: true,
  400. })
  401. }
  402. link := this.wrap.R.URL.Path
  403. if i > 1 {
  404. link = link + "?p=" + utils.IntToStr(i)
  405. }
  406. this.pagination = append(this.pagination, &ShopPagination{
  407. Num: utils.IntToStr(i),
  408. Link: link,
  409. Current: false,
  410. })
  411. }
  412. // Current page
  413. link := this.wrap.R.URL.Path
  414. if this.productsCurrPage > 1 {
  415. link = link + "?p=" + utils.IntToStr(this.productsCurrPage)
  416. }
  417. this.pagination = append(this.pagination, &ShopPagination{
  418. Num: utils.IntToStr(this.productsCurrPage),
  419. Link: link,
  420. Current: true,
  421. })
  422. for i := this.productsCurrPage + 1; i <= this.productsMaxPage; i++ {
  423. if this.productsCurrPage < this.productsMaxPage-3 && i == this.productsCurrPage+3 {
  424. this.pagination = append(this.pagination, &ShopPagination{
  425. Dots: true,
  426. })
  427. }
  428. if this.productsCurrPage < this.productsMaxPage-3 && i > this.productsCurrPage+1 && i <= this.productsMaxPage-1 {
  429. continue
  430. }
  431. link := this.wrap.R.URL.Path
  432. if i > 1 {
  433. link = link + "?p=" + utils.IntToStr(i)
  434. }
  435. this.pagination = append(this.pagination, &ShopPagination{
  436. Num: utils.IntToStr(i),
  437. Link: link,
  438. Current: false,
  439. })
  440. }
  441. } else {
  442. for i := 1; i <= this.productsMaxPage; i++ {
  443. link := this.wrap.R.URL.Path
  444. if i > 1 {
  445. link = link + "?p=" + utils.IntToStr(i)
  446. }
  447. this.pagination = append(this.pagination, &ShopPagination{
  448. Num: utils.IntToStr(i),
  449. Link: link,
  450. Current: i == this.productsCurrPage,
  451. })
  452. }
  453. }
  454. // Pagination prev/next
  455. if this.productsMaxPage > 1 {
  456. link := this.wrap.R.URL.Path
  457. if this.productsCurrPage-1 > 1 {
  458. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.productsCurrPage-1)
  459. }
  460. this.paginationPrev = &ShopPagination{
  461. Num: utils.IntToStr(this.productsCurrPage - 1),
  462. Link: link,
  463. Current: this.productsCurrPage <= 1,
  464. }
  465. if this.productsCurrPage >= 1 && this.productsCurrPage < this.productsMaxPage {
  466. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.productsCurrPage+1)
  467. } else {
  468. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.productsMaxPage)
  469. }
  470. this.paginationNext = &ShopPagination{
  471. Num: utils.IntToStr(this.productsCurrPage + 1),
  472. Link: link,
  473. Current: this.productsCurrPage >= this.productsMaxPage,
  474. }
  475. }
  476. return this
  477. }
  478. func (this *Shop) preload_cats() {
  479. if this.bufferCats == nil {
  480. this.bufferCats = map[int]*utils.MySql_shop_category{}
  481. if rows, err := this.wrap.DB.Query(`
  482. SELECT
  483. main.id,
  484. main.user,
  485. main.name,
  486. main.alias,
  487. main.lft,
  488. main.rgt,
  489. main.depth,
  490. parent.id AS parent_id
  491. FROM
  492. (
  493. SELECT
  494. node.id,
  495. node.user,
  496. node.name,
  497. node.alias,
  498. node.lft,
  499. node.rgt,
  500. (COUNT(parent.id) - 1) AS depth
  501. FROM
  502. shop_cats AS node,
  503. shop_cats AS parent
  504. WHERE
  505. node.lft BETWEEN parent.lft AND parent.rgt
  506. GROUP BY
  507. node.id
  508. ORDER BY
  509. node.lft ASC
  510. ) AS main
  511. LEFT JOIN (
  512. SELECT
  513. node.id,
  514. node.user,
  515. node.name,
  516. node.alias,
  517. node.lft,
  518. node.rgt,
  519. (COUNT(parent.id) - 0) AS depth
  520. FROM
  521. shop_cats AS node,
  522. shop_cats AS parent
  523. WHERE
  524. node.lft BETWEEN parent.lft AND parent.rgt
  525. GROUP BY
  526. node.id
  527. ORDER BY
  528. node.lft ASC
  529. ) AS parent ON
  530. parent.depth = main.depth AND
  531. main.lft > parent.lft AND
  532. main.rgt < parent.rgt
  533. WHERE
  534. main.id > 1
  535. ORDER BY
  536. main.lft ASC
  537. ;
  538. `); err == nil {
  539. defer rows.Close()
  540. for rows.Next() {
  541. row := utils.MySql_shop_category{}
  542. if err := rows.Scan(
  543. &row.A_id,
  544. &row.A_user,
  545. &row.A_name,
  546. &row.A_alias,
  547. &row.A_lft,
  548. &row.A_rgt,
  549. &row.A_depth,
  550. &row.A_parent,
  551. ); err == nil {
  552. this.bufferCats[row.A_id] = &row
  553. if _, ok := this.bufferCats[row.A_parent]; ok {
  554. this.bufferCats[row.A_parent].A_childs = true
  555. }
  556. }
  557. }
  558. }
  559. }
  560. }
  561. func (this *Shop) Category() *ShopCategory {
  562. if this == nil {
  563. return nil
  564. }
  565. return this.category
  566. }
  567. func (this *Shop) Product() *ShopProduct {
  568. if this == nil {
  569. return nil
  570. }
  571. return this.product
  572. }
  573. func (this *Shop) HaveProducts() bool {
  574. if this == nil {
  575. return false
  576. }
  577. if len(this.products) <= 0 {
  578. return false
  579. }
  580. return true
  581. }
  582. func (this *Shop) Products() []*ShopProduct {
  583. if this == nil {
  584. return []*ShopProduct{}
  585. }
  586. return this.products
  587. }
  588. func (this *Shop) ProductsCount() int {
  589. if this == nil {
  590. return 0
  591. }
  592. return this.productsCount
  593. }
  594. func (this *Shop) ProductsPerPage() int {
  595. if this == nil {
  596. return 0
  597. }
  598. return this.productsPerPage
  599. }
  600. func (this *Shop) ProductsMaxPage() int {
  601. if this == nil {
  602. return 0
  603. }
  604. return this.productsMaxPage
  605. }
  606. func (this *Shop) ProductsCurrPage() int {
  607. if this == nil {
  608. return 0
  609. }
  610. return this.productsCurrPage
  611. }
  612. func (this *Shop) Pagination() []*ShopPagination {
  613. if this == nil {
  614. return []*ShopPagination{}
  615. }
  616. return this.pagination
  617. }
  618. func (this *Shop) PaginationPrev() *ShopPagination {
  619. if this == nil {
  620. return nil
  621. }
  622. return this.paginationPrev
  623. }
  624. func (this *Shop) PaginationNext() *ShopPagination {
  625. if this == nil {
  626. return nil
  627. }
  628. return this.paginationNext
  629. }
  630. func (this *Shop) Categories(parent, depth int) []*ShopCategory {
  631. this.preload_cats()
  632. depth_tmp := 0
  633. result := []*ShopCategory{}
  634. for _, cat := range this.bufferCats {
  635. if parent <= 1 {
  636. if depth <= 0 {
  637. result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
  638. } else {
  639. if cat.A_depth <= depth {
  640. result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
  641. }
  642. }
  643. } else {
  644. if cat.A_parent == parent {
  645. if depth_tmp == 0 {
  646. depth_tmp = cat.A_depth
  647. }
  648. if depth <= 0 {
  649. result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
  650. } else {
  651. if (cat.A_depth - depth_tmp + 1) <= depth {
  652. result = append(result, (&ShopCategory{wrap: this.wrap, object: cat}).load(&this.bufferCats))
  653. }
  654. }
  655. }
  656. }
  657. }
  658. sort.Slice(result, func(i, j int) bool { return result[i].Left() < result[j].Left() })
  659. return result
  660. }