session.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package basket
  2. import (
  3. "encoding/json"
  4. "html"
  5. "math"
  6. "strings"
  7. "golang-fave/engine/sqlw"
  8. "golang-fave/utils"
  9. )
  10. type session struct {
  11. listCurrencies map[int]*currency
  12. totalSum float64
  13. Products map[int]*product `json:"products"`
  14. Currency *currency `json:"currency"`
  15. TotalSum string `json:"total_sum"`
  16. TotalCount int `json:"total_count"`
  17. }
  18. func (this *session) priceFormat(product_price float64, format, round int) string {
  19. price := product_price
  20. if round == 1 {
  21. price = math.Ceil(price)
  22. } else if round == 2 {
  23. price = math.Floor(price)
  24. }
  25. if format == 1 {
  26. return utils.Float64ToStrF(price, "%.1f")
  27. } else if format == 2 {
  28. return utils.Float64ToStrF(price, "%.2f")
  29. } else if format == 3 {
  30. return utils.Float64ToStrF(price, "%.3f")
  31. } else if format == 4 {
  32. return utils.Float64ToStrF(price, "%.4f")
  33. }
  34. return utils.Float64ToStrF(price, "%.0f")
  35. }
  36. func (this *session) makePrice(product_price float64, product_currency_id int) float64 {
  37. if this.Currency == nil {
  38. return product_price
  39. }
  40. if this.Currency.Id == product_currency_id {
  41. return product_price
  42. }
  43. if product_currency_id == 1 {
  44. return product_price * this.Currency.Coefficient
  45. } else {
  46. if c, ok := this.listCurrencies[product_currency_id]; ok == true {
  47. return product_price / c.Coefficient
  48. } else {
  49. return product_price
  50. }
  51. }
  52. }
  53. func (this *session) updateProducts(db *sqlw.DB) {
  54. products_ids := []int{}
  55. for _, product := range this.Products {
  56. products_ids = append(products_ids, product.Id)
  57. }
  58. if len(products_ids) > 0 {
  59. if rows, err := db.Query(
  60. `SELECT
  61. shop_products.id,
  62. shop_products.name,
  63. shop_products.price,
  64. shop_products.alias,
  65. shop_currencies.id,
  66. shop_currencies.name,
  67. shop_currencies.coefficient,
  68. shop_currencies.code,
  69. shop_currencies.symbol,
  70. IF(image_this.filename IS NULL, shop_products.parent_id, shop_products.id) as imgid,
  71. IFNULL(IFNULL(image_this.filename, image_parent.filename), '') as filename
  72. FROM
  73. shop_products
  74. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  75. LEFT JOIN (
  76. SELECT
  77. m.product_id,
  78. m.filename
  79. FROM
  80. shop_product_images as m
  81. LEFT JOIN (
  82. SELECT
  83. t.product_id,
  84. MIN(t.ord) as ordmin
  85. FROM
  86. shop_product_images as t
  87. GROUP BY
  88. t.product_id
  89. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  90. WHERE
  91. u.product_id IS NOT NULL
  92. ) as image_this ON image_this.product_id = shop_products.id
  93. LEFT JOIN (
  94. SELECT
  95. m.product_id,
  96. m.filename
  97. FROM
  98. shop_product_images as m
  99. LEFT JOIN (
  100. SELECT
  101. t.product_id,
  102. MIN(t.ord) as ordmin
  103. FROM
  104. shop_product_images as t
  105. GROUP BY
  106. t.product_id
  107. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  108. WHERE
  109. u.product_id IS NOT NULL
  110. ) as image_parent ON image_parent.product_id = shop_products.parent_id
  111. WHERE
  112. shop_products.active = 1 AND
  113. shop_products.id IN (` + strings.Join(utils.ArrayOfIntToArrayOfString(products_ids), ",") + `)
  114. ;`,
  115. ); err == nil {
  116. defer rows.Close()
  117. for rows.Next() {
  118. row := &utils.MySql_shop_product{}
  119. roc := &utils.MySql_shop_currency{}
  120. var img_product_id string
  121. var img_filename string
  122. if err = rows.Scan(
  123. &row.A_id,
  124. &row.A_name,
  125. &row.A_price,
  126. &row.A_alias,
  127. &roc.A_id,
  128. &roc.A_name,
  129. &roc.A_coefficient,
  130. &roc.A_code,
  131. &roc.A_symbol,
  132. &img_product_id,
  133. &img_filename,
  134. ); err == nil {
  135. if p, ok := this.Products[row.A_id]; ok == true {
  136. // Load product image here
  137. var product_image string
  138. if img_filename == "" {
  139. // TODO: Placeholder
  140. product_image = ""
  141. } else {
  142. product_image = "/products/images/" + img_product_id + "/thumb-0-" + img_filename
  143. }
  144. p.Name = html.EscapeString(row.A_name)
  145. p.Image = product_image
  146. p.Link = "/shop/" + row.A_alias + "/"
  147. p.price = row.A_price
  148. p.currency.Id = roc.A_id
  149. p.currency.Name = html.EscapeString(roc.A_name)
  150. p.currency.Coefficient = roc.A_coefficient
  151. p.currency.Code = html.EscapeString(roc.A_code)
  152. p.currency.Symbol = html.EscapeString(roc.A_symbol)
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. func (this *session) updateTotals(p *SBParam) {
  160. this.totalSum = 0
  161. this.TotalCount = 0
  162. for _, product := range this.Products {
  163. product.Price = this.priceFormat(this.makePrice(product.price, product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
  164. product.Sum = this.priceFormat(this.makePrice(product.price*float64(product.Quantity), product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
  165. this.totalSum += this.makePrice(product.price, product.currency.Id) * float64(product.Quantity)
  166. this.TotalCount += product.Quantity
  167. }
  168. this.TotalSum = this.priceFormat(this.totalSum, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
  169. }
  170. // Info, Plus, Minus
  171. func (this *session) Preload(p *SBParam) {
  172. user_currency := 1
  173. if cookie, err := p.R.Cookie("currency"); err == nil {
  174. user_currency = utils.StrToInt(cookie.Value)
  175. }
  176. // Clear list of currencies
  177. this.listCurrencies = map[int]*currency{}
  178. // Load currencies from database
  179. if rows, err := p.DB.Query(
  180. `SELECT
  181. id,
  182. name,
  183. coefficient,
  184. code,
  185. symbol
  186. FROM
  187. shop_currencies
  188. ORDER BY
  189. id ASC
  190. ;`,
  191. ); err == nil {
  192. defer rows.Close()
  193. for rows.Next() {
  194. roc := &utils.MySql_shop_currency{}
  195. if err = rows.Scan(
  196. &roc.A_id,
  197. &roc.A_name,
  198. &roc.A_coefficient,
  199. &roc.A_code,
  200. &roc.A_symbol,
  201. ); err == nil {
  202. this.listCurrencies[roc.A_id] = &currency{
  203. Id: roc.A_id,
  204. Name: html.EscapeString(roc.A_name),
  205. Coefficient: roc.A_coefficient,
  206. Code: html.EscapeString(roc.A_code),
  207. Symbol: html.EscapeString(roc.A_symbol),
  208. }
  209. }
  210. }
  211. }
  212. // Check if selected currency is exists
  213. if _, ok := this.listCurrencies[user_currency]; ok != true {
  214. user_currency = 1
  215. }
  216. // Save selected currency
  217. if c, ok := this.listCurrencies[user_currency]; ok == true {
  218. this.Currency = &currency{
  219. Id: c.Id,
  220. Name: c.Name,
  221. Coefficient: c.Coefficient,
  222. Code: c.Code,
  223. Symbol: c.Symbol,
  224. }
  225. }
  226. }
  227. func (this *session) String(p *SBParam) string {
  228. this.updateProducts(p.DB)
  229. this.updateTotals(p)
  230. json, err := json.Marshal(this)
  231. if err != nil {
  232. return `{"msg":"basket_engine_error","message":"` + err.Error() + `"}`
  233. }
  234. return string(json)
  235. }
  236. func (this *session) Plus(p *SBParam, product_id int) {
  237. if prod, ok := this.Products[product_id]; ok == true {
  238. prod.Quantity++
  239. this.updateProducts(p.DB)
  240. this.updateTotals(p)
  241. return
  242. }
  243. row := &utils.MySql_shop_product{}
  244. roc := &utils.MySql_shop_currency{}
  245. var img_product_id string
  246. var img_filename string
  247. if err := p.DB.QueryRow(`
  248. SELECT
  249. shop_products.id,
  250. shop_products.name,
  251. shop_products.price,
  252. shop_products.alias,
  253. shop_currencies.id,
  254. shop_currencies.name,
  255. shop_currencies.coefficient,
  256. shop_currencies.code,
  257. shop_currencies.symbol,
  258. IF(image_this.filename IS NULL, shop_products.parent_id, shop_products.id) as imgid,
  259. IFNULL(IFNULL(image_this.filename, image_parent.filename), '') as filename
  260. FROM
  261. shop_products
  262. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  263. LEFT JOIN (
  264. SELECT
  265. m.product_id,
  266. m.filename
  267. FROM
  268. shop_product_images as m
  269. LEFT JOIN (
  270. SELECT
  271. t.product_id,
  272. MIN(t.ord) as ordmin
  273. FROM
  274. shop_product_images as t
  275. GROUP BY
  276. t.product_id
  277. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  278. WHERE
  279. u.product_id IS NOT NULL
  280. ) as image_this ON image_this.product_id = shop_products.id
  281. LEFT JOIN (
  282. SELECT
  283. m.product_id,
  284. m.filename
  285. FROM
  286. shop_product_images as m
  287. LEFT JOIN (
  288. SELECT
  289. t.product_id,
  290. MIN(t.ord) as ordmin
  291. FROM
  292. shop_product_images as t
  293. GROUP BY
  294. t.product_id
  295. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  296. WHERE
  297. u.product_id IS NOT NULL
  298. ) as image_parent ON image_parent.product_id = shop_products.parent_id
  299. WHERE
  300. shop_products.active = 1 AND
  301. shop_products.id = ?
  302. LIMIT 1;`,
  303. product_id,
  304. ).Scan(
  305. &row.A_id,
  306. &row.A_name,
  307. &row.A_price,
  308. &row.A_alias,
  309. &roc.A_id,
  310. &roc.A_name,
  311. &roc.A_coefficient,
  312. &roc.A_code,
  313. &roc.A_symbol,
  314. &img_product_id,
  315. &img_filename,
  316. ); err == nil {
  317. // Load product image here
  318. var product_image string
  319. if img_filename == "" {
  320. // TODO: Placeholder
  321. product_image = ""
  322. } else {
  323. product_image = "/products/images/" + img_product_id + "/thumb-0-" + img_filename
  324. }
  325. this.Products[product_id] = &product{
  326. currency: &currency{Id: roc.A_id, Name: roc.A_name, Coefficient: roc.A_coefficient, Code: roc.A_code, Symbol: roc.A_symbol},
  327. Id: row.A_id,
  328. Name: html.EscapeString(row.A_name),
  329. Image: product_image,
  330. Link: "/shop/" + row.A_alias + "/",
  331. price: row.A_price,
  332. Quantity: 1,
  333. }
  334. this.updateProducts(p.DB)
  335. this.updateTotals(p)
  336. }
  337. }
  338. func (this *session) Minus(p *SBParam, product_id int) {
  339. if prod, ok := this.Products[product_id]; ok == true {
  340. if prod.Quantity > 1 {
  341. prod.Quantity--
  342. } else {
  343. delete(this.Products, product_id)
  344. }
  345. this.updateProducts(p.DB)
  346. this.updateTotals(p)
  347. }
  348. }
  349. func (this *session) Remove(p *SBParam, product_id int) {
  350. if _, ok := this.Products[product_id]; ok == true {
  351. delete(this.Products, product_id)
  352. this.updateProducts(p.DB)
  353. this.updateTotals(p)
  354. }
  355. }
  356. func (this *session) ProductsCount() int {
  357. return this.TotalCount
  358. }