shop_product.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package fetdata
  2. import (
  3. "html/template"
  4. "strings"
  5. "time"
  6. "golang-fave/engine/utils"
  7. "golang-fave/engine/wrapper"
  8. )
  9. type ShopProductVarItem struct {
  10. Link string
  11. Name string
  12. Selected bool
  13. }
  14. type ShopProduct struct {
  15. wrap *wrapper.Wrapper
  16. object *utils.MySql_shop_product
  17. user *User
  18. currency *ShopCurrency
  19. category *ShopCategory
  20. images []*ShopProductImage
  21. specs []*ShopProductSpec
  22. vars []*ShopProductVarItem
  23. }
  24. func (this *ShopProduct) load() *ShopProduct {
  25. if this == nil {
  26. return this
  27. }
  28. if rows, err := this.wrap.DB.Query(
  29. this.wrap.R.Context(),
  30. `SELECT
  31. fave_shop_product_images.product_id,
  32. fave_shop_product_images.filename
  33. FROM
  34. fave_shop_product_images
  35. WHERE
  36. fave_shop_product_images.product_id = ?
  37. ORDER BY
  38. fave_shop_product_images.ord ASC
  39. ;`,
  40. this.object.A_id,
  41. ); err == nil {
  42. defer rows.Close()
  43. for rows.Next() {
  44. img := utils.MySql_shop_product_image{}
  45. if err := rows.Scan(
  46. &img.A_product_id,
  47. &img.A_filename,
  48. ); *this.wrap.LogCpError(&err) == nil {
  49. this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
  50. }
  51. }
  52. }
  53. // Get images from parent
  54. if len(this.images) <= 0 && this.object.A_parent_id() > 0 {
  55. if rows, err := this.wrap.DB.Query(
  56. this.wrap.R.Context(),
  57. `SELECT
  58. fave_shop_product_images.product_id,
  59. fave_shop_product_images.filename
  60. FROM
  61. fave_shop_product_images
  62. WHERE
  63. fave_shop_product_images.product_id = ?
  64. ORDER BY
  65. fave_shop_product_images.ord ASC
  66. ;`,
  67. this.object.A_parent_id(),
  68. ); err == nil {
  69. defer rows.Close()
  70. for rows.Next() {
  71. img := utils.MySql_shop_product_image{}
  72. if err := rows.Scan(
  73. &img.A_product_id,
  74. &img.A_filename,
  75. ); *this.wrap.LogCpError(&err) == nil {
  76. this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
  77. }
  78. }
  79. }
  80. }
  81. filter_ids := []int{}
  82. filter_names := map[int]string{}
  83. filter_values := map[int][]string{}
  84. if rows, err := this.wrap.DB.Query(
  85. this.wrap.R.Context(),
  86. `SELECT
  87. fave_shop_filters.id,
  88. fave_shop_filters.filter,
  89. fave_shop_filters_values.name
  90. FROM
  91. fave_shop_filter_product_values
  92. LEFT JOIN fave_shop_filters_values ON fave_shop_filters_values.id = fave_shop_filter_product_values.filter_value_id
  93. LEFT JOIN fave_shop_filters ON fave_shop_filters.id = fave_shop_filters_values.filter_id
  94. WHERE
  95. fave_shop_filter_product_values.product_id = ?
  96. ORDER BY
  97. fave_shop_filters.filter ASC,
  98. fave_shop_filters_values.name ASC
  99. ;`,
  100. this.object.A_id,
  101. ); err == nil {
  102. defer rows.Close()
  103. values := make([]string, 3)
  104. scan := make([]interface{}, len(values))
  105. for i := range values {
  106. scan[i] = &values[i]
  107. }
  108. for rows.Next() {
  109. err = rows.Scan(scan...)
  110. if *this.wrap.LogCpError(&err) == nil {
  111. if !utils.InArrayInt(filter_ids, utils.StrToInt(string(values[0]))) {
  112. filter_ids = append(filter_ids, utils.StrToInt(string(values[0])))
  113. }
  114. filter_names[utils.StrToInt(string(values[0]))] = string(values[1])
  115. filter_values[utils.StrToInt(string(values[0]))] = append(filter_values[utils.StrToInt(string(values[0]))], string(values[2]))
  116. }
  117. }
  118. }
  119. for _, filter_id := range filter_ids {
  120. this.specs = append(this.specs, &ShopProductSpec{wrap: this.wrap, object: &utils.MySql_shop_product_spec{
  121. A_product_id: this.object.A_id,
  122. A_filter_id: filter_id,
  123. A_filter_name: filter_names[filter_id],
  124. A_filter_value: strings.Join(filter_values[filter_id], ", "),
  125. }})
  126. }
  127. // Variations
  128. if rows, err := this.wrap.DB.Query(
  129. this.wrap.R.Context(),
  130. `SELECT
  131. fave_shop_products.id,
  132. fave_shop_products.name,
  133. fave_shop_products.alias
  134. FROM
  135. fave_shop_products
  136. WHERE
  137. fave_shop_products.active = 1 AND
  138. (
  139. (fave_shop_products.id = ? OR fave_shop_products.parent_id = ?) OR
  140. (
  141. (fave_shop_products.id = ?) OR
  142. (fave_shop_products.parent_id IS NOT NULL AND fave_shop_products.parent_id = ?)
  143. )
  144. )
  145. ORDER BY
  146. fave_shop_products.name ASC
  147. ;`,
  148. this.object.A_id,
  149. this.object.A_id,
  150. this.object.A_parent_id(),
  151. this.object.A_parent_id(),
  152. ); err == nil {
  153. defer rows.Close()
  154. for rows.Next() {
  155. var tmp_id int
  156. var tmp_name string
  157. var tmp_alias string
  158. if err := rows.Scan(
  159. &tmp_id,
  160. &tmp_name,
  161. &tmp_alias,
  162. ); *this.wrap.LogCpError(&err) == nil {
  163. selected := false
  164. if tmp_id == this.object.A_id {
  165. selected = true
  166. }
  167. this.vars = append(this.vars, &ShopProductVarItem{
  168. Link: "/shop/" + tmp_alias + "/",
  169. Name: tmp_name + " " + utils.IntToStr(tmp_id),
  170. Selected: selected,
  171. })
  172. }
  173. }
  174. }
  175. return this
  176. }
  177. func (this *ShopProduct) Id() int {
  178. if this == nil {
  179. return 0
  180. }
  181. return this.object.A_id
  182. }
  183. func (this *ShopProduct) User() *User {
  184. if this == nil {
  185. return nil
  186. }
  187. if this.user != nil {
  188. return this.user
  189. }
  190. this.user = (&User{wrap: this.wrap}).load()
  191. this.user.loadById(this.object.A_user)
  192. return this.user
  193. }
  194. func (this *ShopProduct) Currency() *ShopCurrency {
  195. if this == nil {
  196. return nil
  197. }
  198. if this.currency != nil {
  199. return this.currency
  200. }
  201. this.currency = (&ShopCurrency{wrap: this.wrap}).load()
  202. this.currency.loadById(this.object.A_currency)
  203. return this.currency
  204. }
  205. func (this *ShopProduct) Price() float64 {
  206. if this == nil {
  207. return 0
  208. }
  209. if this.Currency() == nil {
  210. return this.object.A_price
  211. }
  212. if this.wrap.ShopGetCurrentCurrency() == nil {
  213. return this.object.A_price
  214. }
  215. if this.wrap.ShopGetCurrentCurrency().A_id == this.Currency().Id() {
  216. return this.object.A_price
  217. }
  218. if this.Currency().Id() == 1 {
  219. return this.object.A_price * this.wrap.ShopGetCurrentCurrency().A_coefficient
  220. } else {
  221. if c, ok := (*this.wrap.ShopGetAllCurrencies())[this.Currency().Id()]; ok == true {
  222. return this.object.A_price / c.A_coefficient
  223. } else {
  224. return this.object.A_price
  225. }
  226. }
  227. }
  228. func (this *ShopProduct) PriceOld() float64 {
  229. if this == nil {
  230. return 0
  231. }
  232. if this.Currency() == nil {
  233. return this.object.A_price_old
  234. }
  235. if this.wrap.ShopGetCurrentCurrency() == nil {
  236. return this.object.A_price_old
  237. }
  238. if this.wrap.ShopGetCurrentCurrency().A_id == this.Currency().Id() {
  239. return this.object.A_price_old
  240. }
  241. if this.Currency().Id() == 1 {
  242. return this.object.A_price_old * this.wrap.ShopGetCurrentCurrency().A_coefficient
  243. } else {
  244. if c, ok := (*this.wrap.ShopGetAllCurrencies())[this.Currency().Id()]; ok == true {
  245. return this.object.A_price_old / c.A_coefficient
  246. } else {
  247. return this.object.A_price_old
  248. }
  249. }
  250. }
  251. func (this *ShopProduct) PriceNice() string {
  252. return utils.FormatProductPrice(
  253. this.Price(),
  254. (*this.wrap.Config).Shop.Price.Format,
  255. (*this.wrap.Config).Shop.Price.Round,
  256. )
  257. }
  258. func (this *ShopProduct) PriceOldNice() string {
  259. return utils.FormatProductPrice(
  260. this.PriceOld(),
  261. (*this.wrap.Config).Shop.Price.Format,
  262. (*this.wrap.Config).Shop.Price.Round,
  263. )
  264. }
  265. func (this *ShopProduct) PriceFormat(format string) string {
  266. return utils.Float64ToStrF(this.Price(), format)
  267. }
  268. func (this *ShopProduct) Group() string {
  269. if this == nil {
  270. return ""
  271. }
  272. return this.object.A_gname
  273. }
  274. func (this *ShopProduct) Name() string {
  275. if this == nil {
  276. return ""
  277. }
  278. return this.object.A_name
  279. }
  280. func (this *ShopProduct) Alias() string {
  281. if this == nil {
  282. return ""
  283. }
  284. return this.object.A_alias
  285. }
  286. func (this *ShopProduct) Vendor() string {
  287. if this == nil {
  288. return ""
  289. }
  290. return this.object.A_vendor
  291. }
  292. func (this *ShopProduct) Quantity() int {
  293. if this == nil {
  294. return 0
  295. }
  296. return this.object.A_quantity
  297. }
  298. func (this *ShopProduct) Category() *ShopCategory {
  299. if this == nil {
  300. return nil
  301. }
  302. if this.category != nil {
  303. return this.category
  304. }
  305. this.category = (&ShopCategory{wrap: this.wrap}).load(nil)
  306. this.category.loadById(this.object.A_category)
  307. return this.category
  308. }
  309. func (this *ShopProduct) Briefly() template.HTML {
  310. if this == nil {
  311. return template.HTML("")
  312. }
  313. return template.HTML(this.object.A_briefly)
  314. }
  315. func (this *ShopProduct) Content() template.HTML {
  316. if this == nil {
  317. return template.HTML("")
  318. }
  319. return template.HTML(this.object.A_content)
  320. }
  321. func (this *ShopProduct) DateTimeUnix() int {
  322. if this == nil {
  323. return 0
  324. }
  325. return this.object.A_datetime
  326. }
  327. func (this *ShopProduct) DateTimeFormat(format string) string {
  328. if this == nil {
  329. return ""
  330. }
  331. return time.Unix(int64(this.object.A_datetime), 0).Format(format)
  332. }
  333. func (this *ShopProduct) Active() bool {
  334. if this == nil {
  335. return false
  336. }
  337. return this.object.A_active > 0
  338. }
  339. func (this *ShopProduct) Permalink() string {
  340. if this == nil {
  341. return ""
  342. }
  343. return "/shop/" + this.object.A_alias + "/"
  344. }
  345. func (this *ShopProduct) Image() *ShopProductImage {
  346. if this == nil {
  347. return nil
  348. }
  349. if len(this.images) <= 0 {
  350. return nil
  351. }
  352. return this.images[0]
  353. }
  354. func (this *ShopProduct) HaveImages() bool {
  355. if this == nil {
  356. return false
  357. }
  358. if len(this.images) <= 0 {
  359. return false
  360. }
  361. return true
  362. }
  363. func (this *ShopProduct) Images() []*ShopProductImage {
  364. if this == nil {
  365. return []*ShopProductImage{}
  366. }
  367. return this.images
  368. }
  369. func (this *ShopProduct) ImagesCount() int {
  370. if this == nil {
  371. return 0
  372. }
  373. return len(this.images)
  374. }
  375. func (this *ShopProduct) HaveSpecs() bool {
  376. if this == nil {
  377. return false
  378. }
  379. if len(this.specs) <= 0 {
  380. return false
  381. }
  382. return true
  383. }
  384. func (this *ShopProduct) Specs() []*ShopProductSpec {
  385. if this == nil {
  386. return []*ShopProductSpec{}
  387. }
  388. return this.specs
  389. }
  390. func (this *ShopProduct) SpecsCount() int {
  391. if this == nil {
  392. return 0
  393. }
  394. return len(this.specs)
  395. }
  396. func (this *ShopProduct) HaveVariations() bool {
  397. if this == nil {
  398. return false
  399. }
  400. if len(this.vars) <= 1 {
  401. return false
  402. }
  403. return true
  404. }
  405. func (this *ShopProduct) Variations() []*ShopProductVarItem {
  406. if this == nil {
  407. return []*ShopProductVarItem{}
  408. }
  409. return this.vars
  410. }
  411. func (this *ShopProduct) VariationsCount() int {
  412. if this == nil {
  413. return 0
  414. }
  415. return len(this.vars)
  416. }