shop_product.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package fetdata
  2. import (
  3. "html/template"
  4. "strings"
  5. "time"
  6. "golang-fave/engine/wrapper"
  7. "golang-fave/utils"
  8. )
  9. type ShopProduct struct {
  10. wrap *wrapper.Wrapper
  11. object *utils.MySql_shop_product
  12. user *User
  13. currency *ShopCurrency
  14. category *ShopCategory
  15. images []*ShopProductImage
  16. specs []*ShopProductSpec
  17. }
  18. func (this *ShopProduct) load() *ShopProduct {
  19. if this == nil {
  20. return this
  21. }
  22. if rows, err := this.wrap.DB.Query(
  23. `SELECT
  24. shop_product_images.product_id,
  25. shop_product_images.filename
  26. FROM
  27. shop_product_images
  28. WHERE
  29. shop_product_images.product_id = ?
  30. ORDER BY
  31. shop_product_images.ord ASC
  32. ;`,
  33. this.object.A_id,
  34. ); err == nil {
  35. defer rows.Close()
  36. for rows.Next() {
  37. img := utils.MySql_shop_product_image{}
  38. if err := rows.Scan(
  39. &img.A_product_id,
  40. &img.A_filename,
  41. ); err == nil {
  42. this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
  43. }
  44. }
  45. }
  46. filter_ids := []int{}
  47. filter_names := map[int]string{}
  48. filter_values := map[int][]string{}
  49. if rows, err := this.wrap.DB.Query(
  50. `SELECT
  51. shop_filters.id,
  52. shop_filters.filter,
  53. shop_filters_values.name
  54. FROM
  55. shop_filter_product_values
  56. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  57. LEFT JOIN shop_filters ON shop_filters.id = shop_filters_values.filter_id
  58. WHERE
  59. shop_filter_product_values.product_id = ?
  60. ORDER BY
  61. shop_filters.filter ASC,
  62. shop_filters_values.name ASC
  63. ;`,
  64. this.object.A_id,
  65. ); err == nil {
  66. defer rows.Close()
  67. values := make([]string, 3)
  68. scan := make([]interface{}, len(values))
  69. for i := range values {
  70. scan[i] = &values[i]
  71. }
  72. for rows.Next() {
  73. err = rows.Scan(scan...)
  74. if err == nil {
  75. if !utils.InArrayInt(filter_ids, utils.StrToInt(string(values[0]))) {
  76. filter_ids = append(filter_ids, utils.StrToInt(string(values[0])))
  77. }
  78. filter_names[utils.StrToInt(string(values[0]))] = string(values[1])
  79. filter_values[utils.StrToInt(string(values[0]))] = append(filter_values[utils.StrToInt(string(values[0]))], string(values[2]))
  80. }
  81. }
  82. }
  83. for _, filter_id := range filter_ids {
  84. this.specs = append(this.specs, &ShopProductSpec{wrap: this.wrap, object: &utils.MySql_shop_product_spec{
  85. A_product_id: this.object.A_id,
  86. A_filter_id: filter_id,
  87. A_filter_name: filter_names[filter_id],
  88. A_filter_value: strings.Join(filter_values[filter_id], ", "),
  89. }})
  90. }
  91. return this
  92. }
  93. func (this *ShopProduct) Id() int {
  94. if this == nil {
  95. return 0
  96. }
  97. return this.object.A_id
  98. }
  99. func (this *ShopProduct) User() *User {
  100. if this == nil {
  101. return nil
  102. }
  103. if this.user != nil {
  104. return this.user
  105. }
  106. this.user = (&User{wrap: this.wrap}).load()
  107. this.user.loadById(this.object.A_user)
  108. return this.user
  109. }
  110. func (this *ShopProduct) Currency() *ShopCurrency {
  111. if this == nil {
  112. return nil
  113. }
  114. if this.currency != nil {
  115. return this.currency
  116. }
  117. this.currency = (&ShopCurrency{wrap: this.wrap}).load()
  118. this.currency.loadById(this.object.A_currency)
  119. return this.currency
  120. }
  121. func (this *ShopProduct) Price() float64 {
  122. if this == nil {
  123. return 0
  124. }
  125. return this.object.A_price
  126. }
  127. func (this *ShopProduct) PriceFormat(format string) string {
  128. return utils.Float64ToStrF(this.Price(), format)
  129. }
  130. func (this *ShopProduct) Name() string {
  131. if this == nil {
  132. return ""
  133. }
  134. return this.object.A_name
  135. }
  136. func (this *ShopProduct) Alias() string {
  137. if this == nil {
  138. return ""
  139. }
  140. return this.object.A_alias
  141. }
  142. func (this *ShopProduct) Vendor() string {
  143. if this == nil {
  144. return ""
  145. }
  146. return this.object.A_vendor
  147. }
  148. func (this *ShopProduct) Quantity() int {
  149. if this == nil {
  150. return 0
  151. }
  152. return this.object.A_quantity
  153. }
  154. func (this *ShopProduct) Category() *ShopCategory {
  155. if this == nil {
  156. return nil
  157. }
  158. if this.category != nil {
  159. return this.category
  160. }
  161. this.category = (&ShopCategory{wrap: this.wrap}).load(nil)
  162. this.category.loadById(this.object.A_category)
  163. return this.category
  164. }
  165. func (this *ShopProduct) Briefly() template.HTML {
  166. if this == nil {
  167. return template.HTML("")
  168. }
  169. return template.HTML(this.object.A_briefly)
  170. }
  171. func (this *ShopProduct) Content() template.HTML {
  172. if this == nil {
  173. return template.HTML("")
  174. }
  175. return template.HTML(this.object.A_content)
  176. }
  177. func (this *ShopProduct) DateTimeUnix() int {
  178. if this == nil {
  179. return 0
  180. }
  181. return this.object.A_datetime
  182. }
  183. func (this *ShopProduct) DateTimeFormat(format string) string {
  184. if this == nil {
  185. return ""
  186. }
  187. return time.Unix(int64(this.object.A_datetime), 0).Format(format)
  188. }
  189. func (this *ShopProduct) Active() bool {
  190. if this == nil {
  191. return false
  192. }
  193. return this.object.A_active > 0
  194. }
  195. func (this *ShopProduct) Permalink() string {
  196. if this == nil {
  197. return ""
  198. }
  199. return "/shop/" + this.object.A_alias + "/"
  200. }
  201. func (this *ShopProduct) Image() *ShopProductImage {
  202. if this == nil {
  203. return nil
  204. }
  205. if len(this.images) <= 0 {
  206. return nil
  207. }
  208. return this.images[0]
  209. }
  210. func (this *ShopProduct) HaveImages() bool {
  211. if this == nil {
  212. return false
  213. }
  214. if len(this.images) <= 0 {
  215. return false
  216. }
  217. return true
  218. }
  219. func (this *ShopProduct) Images() []*ShopProductImage {
  220. if this == nil {
  221. return []*ShopProductImage{}
  222. }
  223. return this.images
  224. }
  225. func (this *ShopProduct) ImagesCount() int {
  226. if this == nil {
  227. return 0
  228. }
  229. return len(this.images)
  230. }
  231. func (this *ShopProduct) HaveSpecs() bool {
  232. if this == nil {
  233. return false
  234. }
  235. if len(this.specs) <= 0 {
  236. return false
  237. }
  238. return true
  239. }
  240. func (this *ShopProduct) Specs() []*ShopProductSpec {
  241. if this == nil {
  242. return []*ShopProductSpec{}
  243. }
  244. return this.specs
  245. }
  246. func (this *ShopProduct) SpecsCount() int {
  247. if this == nil {
  248. return 0
  249. }
  250. return len(this.specs)
  251. }