shop.go 16 KB

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