shop_product.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. if this == nil {
  129. return ""
  130. }
  131. return utils.Float64ToStrF(this.object.A_price, format)
  132. }
  133. func (this *ShopProduct) Name() string {
  134. if this == nil {
  135. return ""
  136. }
  137. return this.object.A_name
  138. }
  139. func (this *ShopProduct) Alias() string {
  140. if this == nil {
  141. return ""
  142. }
  143. return this.object.A_alias
  144. }
  145. func (this *ShopProduct) Vendor() string {
  146. if this == nil {
  147. return ""
  148. }
  149. return this.object.A_vendor
  150. }
  151. func (this *ShopProduct) Quantity() int {
  152. if this == nil {
  153. return 0
  154. }
  155. return this.object.A_quantity
  156. }
  157. func (this *ShopProduct) Category() *ShopCategory {
  158. if this == nil {
  159. return nil
  160. }
  161. if this.category != nil {
  162. return this.category
  163. }
  164. this.category = (&ShopCategory{wrap: this.wrap}).load(nil)
  165. this.category.loadById(this.object.A_category)
  166. return this.category
  167. }
  168. func (this *ShopProduct) Briefly() template.HTML {
  169. if this == nil {
  170. return template.HTML("")
  171. }
  172. return template.HTML(this.object.A_briefly)
  173. }
  174. func (this *ShopProduct) Content() template.HTML {
  175. if this == nil {
  176. return template.HTML("")
  177. }
  178. return template.HTML(this.object.A_content)
  179. }
  180. func (this *ShopProduct) DateTimeUnix() int {
  181. if this == nil {
  182. return 0
  183. }
  184. return this.object.A_datetime
  185. }
  186. func (this *ShopProduct) DateTimeFormat(format string) string {
  187. if this == nil {
  188. return ""
  189. }
  190. return time.Unix(int64(this.object.A_datetime), 0).Format(format)
  191. }
  192. func (this *ShopProduct) Active() bool {
  193. if this == nil {
  194. return false
  195. }
  196. return this.object.A_active > 0
  197. }
  198. func (this *ShopProduct) Permalink() string {
  199. if this == nil {
  200. return ""
  201. }
  202. return "/shop/" + this.object.A_alias + "/"
  203. }
  204. func (this *ShopProduct) Image() *ShopProductImage {
  205. if this == nil {
  206. return nil
  207. }
  208. if len(this.images) <= 0 {
  209. return nil
  210. }
  211. return this.images[0]
  212. }
  213. func (this *ShopProduct) HaveImages() bool {
  214. if this == nil {
  215. return false
  216. }
  217. if len(this.images) <= 0 {
  218. return false
  219. }
  220. return true
  221. }
  222. func (this *ShopProduct) Images() []*ShopProductImage {
  223. if this == nil {
  224. return []*ShopProductImage{}
  225. }
  226. return this.images
  227. }
  228. func (this *ShopProduct) ImagesCount() int {
  229. if this == nil {
  230. return 0
  231. }
  232. return len(this.images)
  233. }
  234. func (this *ShopProduct) HaveSpecs() bool {
  235. if this == nil {
  236. return false
  237. }
  238. if len(this.specs) <= 0 {
  239. return false
  240. }
  241. return true
  242. }
  243. func (this *ShopProduct) Specs() []*ShopProductSpec {
  244. if this == nil {
  245. return []*ShopProductSpec{}
  246. }
  247. return this.specs
  248. }
  249. func (this *ShopProduct) SpecsCount() int {
  250. if this == nil {
  251. return 0
  252. }
  253. return len(this.specs)
  254. }