shop_product.go 9.1 KB

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