shop.go 10 KB

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