shop_product.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package fetdata
  2. import (
  3. "html/template"
  4. "time"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. type ShopProduct struct {
  9. wrap *wrapper.Wrapper
  10. object *utils.MySql_shop_product
  11. user *User
  12. currency *Currency
  13. category *ShopCategory
  14. images []*ShopProductImage
  15. }
  16. func (this *ShopProduct) load() *ShopProduct {
  17. if this == nil {
  18. return this
  19. }
  20. if rows, err := this.wrap.DB.Query(
  21. `SELECT
  22. shop_product_images.product_id,
  23. shop_product_images.filename
  24. FROM
  25. shop_product_images
  26. WHERE
  27. shop_product_images.product_id = ?
  28. ORDER BY
  29. shop_product_images.filename ASC
  30. ;`,
  31. this.object.A_id,
  32. ); err == nil {
  33. defer rows.Close()
  34. for rows.Next() {
  35. img := utils.MySql_shop_product_image{}
  36. if err := rows.Scan(
  37. &img.A_product_id,
  38. &img.A_filename,
  39. ); err == nil {
  40. this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
  41. }
  42. }
  43. }
  44. return this
  45. }
  46. func (this *ShopProduct) Id() int {
  47. if this == nil {
  48. return 0
  49. }
  50. return this.object.A_id
  51. }
  52. func (this *ShopProduct) User() *User {
  53. if this == nil {
  54. return nil
  55. }
  56. if this.user != nil {
  57. return this.user
  58. }
  59. this.user = &User{wrap: this.wrap}
  60. this.user.loadById(this.object.A_user)
  61. return this.user
  62. }
  63. func (this *ShopProduct) Currency() *Currency {
  64. if this == nil {
  65. return nil
  66. }
  67. if this.currency != nil {
  68. return this.currency
  69. }
  70. this.currency = &Currency{wrap: this.wrap}
  71. this.currency.loadById(this.object.A_currency)
  72. return this.currency
  73. }
  74. func (this *ShopProduct) Price() float64 {
  75. if this == nil {
  76. return 0
  77. }
  78. return this.object.A_price
  79. }
  80. func (this *ShopProduct) PriceFormat(format string) string {
  81. if this == nil {
  82. return ""
  83. }
  84. return utils.Float64ToStrF(this.object.A_price, format)
  85. }
  86. func (this *ShopProduct) Name() string {
  87. if this == nil {
  88. return ""
  89. }
  90. return this.object.A_name
  91. }
  92. func (this *ShopProduct) Alias() string {
  93. if this == nil {
  94. return ""
  95. }
  96. return this.object.A_alias
  97. }
  98. func (this *ShopProduct) Vendor() string {
  99. if this == nil {
  100. return ""
  101. }
  102. return this.object.A_vendor
  103. }
  104. func (this *ShopProduct) Quantity() int {
  105. if this == nil {
  106. return 0
  107. }
  108. return this.object.A_quantity
  109. }
  110. func (this *ShopProduct) Category() *ShopCategory {
  111. if this == nil {
  112. return nil
  113. }
  114. if this.category != nil {
  115. return this.category
  116. }
  117. this.category = &ShopCategory{wrap: this.wrap}
  118. this.category.loadById(this.object.A_category)
  119. return this.category
  120. }
  121. func (this *ShopProduct) Briefly() template.HTML {
  122. if this == nil {
  123. return template.HTML("")
  124. }
  125. return template.HTML(this.object.A_briefly)
  126. }
  127. func (this *ShopProduct) Content() template.HTML {
  128. if this == nil {
  129. return template.HTML("")
  130. }
  131. return template.HTML(this.object.A_content)
  132. }
  133. func (this *ShopProduct) DateTimeUnix() int {
  134. if this == nil {
  135. return 0
  136. }
  137. return this.object.A_datetime
  138. }
  139. func (this *ShopProduct) DateTimeFormat(format string) string {
  140. if this == nil {
  141. return ""
  142. }
  143. return time.Unix(int64(this.object.A_datetime), 0).Format(format)
  144. }
  145. func (this *ShopProduct) Active() bool {
  146. if this == nil {
  147. return false
  148. }
  149. return this.object.A_active > 0
  150. }
  151. func (this *ShopProduct) Permalink() string {
  152. if this == nil {
  153. return ""
  154. }
  155. return "/shop/" + this.object.A_alias + "/"
  156. }
  157. func (this *ShopProduct) HaveImages() bool {
  158. if this == nil {
  159. return false
  160. }
  161. if len(this.images) <= 0 {
  162. return false
  163. }
  164. return true
  165. }
  166. func (this *ShopProduct) Image() *ShopProductImage {
  167. if this == nil {
  168. return nil
  169. }
  170. if len(this.images) <= 0 {
  171. return nil
  172. }
  173. return this.images[0]
  174. }
  175. func (this *ShopProduct) Images() []*ShopProductImage {
  176. if this == nil {
  177. return []*ShopProductImage{}
  178. }
  179. return this.images
  180. }
  181. func (this *ShopProduct) ImagesCount() int {
  182. if this == nil {
  183. return 0
  184. }
  185. return len(this.images)
  186. }