shop_product.go 9.4 KB

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