shop_product.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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) PriceNice() string {
  225. return utils.FormatProductPrice(
  226. this.Price(),
  227. (*this.wrap.Config).Shop.Price.Format,
  228. (*this.wrap.Config).Shop.Price.Round,
  229. )
  230. }
  231. func (this *ShopProduct) PriceFormat(format string) string {
  232. return utils.Float64ToStrF(this.Price(), format)
  233. }
  234. func (this *ShopProduct) Group() string {
  235. if this == nil {
  236. return ""
  237. }
  238. return this.object.A_gname
  239. }
  240. func (this *ShopProduct) Name() string {
  241. if this == nil {
  242. return ""
  243. }
  244. return this.object.A_name
  245. }
  246. func (this *ShopProduct) Alias() string {
  247. if this == nil {
  248. return ""
  249. }
  250. return this.object.A_alias
  251. }
  252. func (this *ShopProduct) Vendor() string {
  253. if this == nil {
  254. return ""
  255. }
  256. return this.object.A_vendor
  257. }
  258. func (this *ShopProduct) Quantity() int {
  259. if this == nil {
  260. return 0
  261. }
  262. return this.object.A_quantity
  263. }
  264. func (this *ShopProduct) Category() *ShopCategory {
  265. if this == nil {
  266. return nil
  267. }
  268. if this.category != nil {
  269. return this.category
  270. }
  271. this.category = (&ShopCategory{wrap: this.wrap}).load(nil)
  272. this.category.loadById(this.object.A_category)
  273. return this.category
  274. }
  275. func (this *ShopProduct) Briefly() template.HTML {
  276. if this == nil {
  277. return template.HTML("")
  278. }
  279. return template.HTML(this.object.A_briefly)
  280. }
  281. func (this *ShopProduct) Content() template.HTML {
  282. if this == nil {
  283. return template.HTML("")
  284. }
  285. return template.HTML(this.object.A_content)
  286. }
  287. func (this *ShopProduct) DateTimeUnix() int {
  288. if this == nil {
  289. return 0
  290. }
  291. return this.object.A_datetime
  292. }
  293. func (this *ShopProduct) DateTimeFormat(format string) string {
  294. if this == nil {
  295. return ""
  296. }
  297. return time.Unix(int64(this.object.A_datetime), 0).Format(format)
  298. }
  299. func (this *ShopProduct) Active() bool {
  300. if this == nil {
  301. return false
  302. }
  303. return this.object.A_active > 0
  304. }
  305. func (this *ShopProduct) Permalink() string {
  306. if this == nil {
  307. return ""
  308. }
  309. return "/shop/" + this.object.A_alias + "/"
  310. }
  311. func (this *ShopProduct) Image() *ShopProductImage {
  312. if this == nil {
  313. return nil
  314. }
  315. if len(this.images) <= 0 {
  316. return nil
  317. }
  318. return this.images[0]
  319. }
  320. func (this *ShopProduct) HaveImages() bool {
  321. if this == nil {
  322. return false
  323. }
  324. if len(this.images) <= 0 {
  325. return false
  326. }
  327. return true
  328. }
  329. func (this *ShopProduct) Images() []*ShopProductImage {
  330. if this == nil {
  331. return []*ShopProductImage{}
  332. }
  333. return this.images
  334. }
  335. func (this *ShopProduct) ImagesCount() int {
  336. if this == nil {
  337. return 0
  338. }
  339. return len(this.images)
  340. }
  341. func (this *ShopProduct) HaveSpecs() bool {
  342. if this == nil {
  343. return false
  344. }
  345. if len(this.specs) <= 0 {
  346. return false
  347. }
  348. return true
  349. }
  350. func (this *ShopProduct) Specs() []*ShopProductSpec {
  351. if this == nil {
  352. return []*ShopProductSpec{}
  353. }
  354. return this.specs
  355. }
  356. func (this *ShopProduct) SpecsCount() int {
  357. if this == nil {
  358. return 0
  359. }
  360. return len(this.specs)
  361. }
  362. func (this *ShopProduct) HaveVariations() bool {
  363. if this == nil {
  364. return false
  365. }
  366. if len(this.vars) <= 1 {
  367. return false
  368. }
  369. return true
  370. }
  371. func (this *ShopProduct) Variations() []*ShopProductVarItem {
  372. if this == nil {
  373. return []*ShopProductVarItem{}
  374. }
  375. return this.vars
  376. }
  377. func (this *ShopProduct) VariationsCount() int {
  378. if this == nil {
  379. return 0
  380. }
  381. return len(this.vars)
  382. }