shop_product.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. ); *this.wrap.LogCpError(&err) == nil {
  42. this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
  43. }
  44. }
  45. }
  46. // Get images from parent
  47. if len(this.images) <= 0 && this.object.A_parent_id() > 0 {
  48. if rows, err := this.wrap.DB.Query(
  49. `SELECT
  50. shop_product_images.product_id,
  51. shop_product_images.filename
  52. FROM
  53. shop_product_images
  54. WHERE
  55. shop_product_images.product_id = ?
  56. ORDER BY
  57. shop_product_images.ord ASC
  58. ;`,
  59. this.object.A_parent_id(),
  60. ); err == nil {
  61. defer rows.Close()
  62. for rows.Next() {
  63. img := utils.MySql_shop_product_image{}
  64. if err := rows.Scan(
  65. &img.A_product_id,
  66. &img.A_filename,
  67. ); *this.wrap.LogCpError(&err) == nil {
  68. this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
  69. }
  70. }
  71. }
  72. }
  73. filter_ids := []int{}
  74. filter_names := map[int]string{}
  75. filter_values := map[int][]string{}
  76. if rows, err := this.wrap.DB.Query(
  77. `SELECT
  78. shop_filters.id,
  79. shop_filters.filter,
  80. shop_filters_values.name
  81. FROM
  82. shop_filter_product_values
  83. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  84. LEFT JOIN shop_filters ON shop_filters.id = shop_filters_values.filter_id
  85. WHERE
  86. shop_filter_product_values.product_id = ?
  87. ORDER BY
  88. shop_filters.filter ASC,
  89. shop_filters_values.name ASC
  90. ;`,
  91. this.object.A_id,
  92. ); err == nil {
  93. defer rows.Close()
  94. values := make([]string, 3)
  95. scan := make([]interface{}, len(values))
  96. for i := range values {
  97. scan[i] = &values[i]
  98. }
  99. for rows.Next() {
  100. err = rows.Scan(scan...)
  101. if *this.wrap.LogCpError(&err) == nil {
  102. if !utils.InArrayInt(filter_ids, utils.StrToInt(string(values[0]))) {
  103. filter_ids = append(filter_ids, utils.StrToInt(string(values[0])))
  104. }
  105. filter_names[utils.StrToInt(string(values[0]))] = string(values[1])
  106. filter_values[utils.StrToInt(string(values[0]))] = append(filter_values[utils.StrToInt(string(values[0]))], string(values[2]))
  107. }
  108. }
  109. }
  110. for _, filter_id := range filter_ids {
  111. this.specs = append(this.specs, &ShopProductSpec{wrap: this.wrap, object: &utils.MySql_shop_product_spec{
  112. A_product_id: this.object.A_id,
  113. A_filter_id: filter_id,
  114. A_filter_name: filter_names[filter_id],
  115. A_filter_value: strings.Join(filter_values[filter_id], ", "),
  116. }})
  117. }
  118. return this
  119. }
  120. func (this *ShopProduct) Id() int {
  121. if this == nil {
  122. return 0
  123. }
  124. return this.object.A_id
  125. }
  126. func (this *ShopProduct) User() *User {
  127. if this == nil {
  128. return nil
  129. }
  130. if this.user != nil {
  131. return this.user
  132. }
  133. this.user = (&User{wrap: this.wrap}).load()
  134. this.user.loadById(this.object.A_user)
  135. return this.user
  136. }
  137. func (this *ShopProduct) Currency() *ShopCurrency {
  138. if this == nil {
  139. return nil
  140. }
  141. if this.currency != nil {
  142. return this.currency
  143. }
  144. this.currency = (&ShopCurrency{wrap: this.wrap}).load()
  145. this.currency.loadById(this.object.A_currency)
  146. return this.currency
  147. }
  148. func (this *ShopProduct) Price() float64 {
  149. if this == nil {
  150. return 0
  151. }
  152. // TODO: read currency from session?
  153. // this.object.A_price * this.Currency().Coefficient()
  154. return this.object.A_price
  155. }
  156. func (this *ShopProduct) PriceFormat(format string) string {
  157. return utils.Float64ToStrF(this.Price(), format)
  158. }
  159. func (this *ShopProduct) Group() string {
  160. if this == nil {
  161. return ""
  162. }
  163. return this.object.A_gname
  164. }
  165. func (this *ShopProduct) Name() string {
  166. if this == nil {
  167. return ""
  168. }
  169. return this.object.A_name
  170. }
  171. func (this *ShopProduct) Alias() string {
  172. if this == nil {
  173. return ""
  174. }
  175. return this.object.A_alias
  176. }
  177. func (this *ShopProduct) Vendor() string {
  178. if this == nil {
  179. return ""
  180. }
  181. return this.object.A_vendor
  182. }
  183. func (this *ShopProduct) Quantity() int {
  184. if this == nil {
  185. return 0
  186. }
  187. return this.object.A_quantity
  188. }
  189. func (this *ShopProduct) Category() *ShopCategory {
  190. if this == nil {
  191. return nil
  192. }
  193. if this.category != nil {
  194. return this.category
  195. }
  196. this.category = (&ShopCategory{wrap: this.wrap}).load(nil)
  197. this.category.loadById(this.object.A_category)
  198. return this.category
  199. }
  200. func (this *ShopProduct) Briefly() template.HTML {
  201. if this == nil {
  202. return template.HTML("")
  203. }
  204. return template.HTML(this.object.A_briefly)
  205. }
  206. func (this *ShopProduct) Content() template.HTML {
  207. if this == nil {
  208. return template.HTML("")
  209. }
  210. return template.HTML(this.object.A_content)
  211. }
  212. func (this *ShopProduct) DateTimeUnix() int {
  213. if this == nil {
  214. return 0
  215. }
  216. return this.object.A_datetime
  217. }
  218. func (this *ShopProduct) DateTimeFormat(format string) string {
  219. if this == nil {
  220. return ""
  221. }
  222. return time.Unix(int64(this.object.A_datetime), 0).Format(format)
  223. }
  224. func (this *ShopProduct) Active() bool {
  225. if this == nil {
  226. return false
  227. }
  228. return this.object.A_active > 0
  229. }
  230. func (this *ShopProduct) Permalink() string {
  231. if this == nil {
  232. return ""
  233. }
  234. return "/shop/" + this.object.A_alias + "/"
  235. }
  236. func (this *ShopProduct) Image() *ShopProductImage {
  237. if this == nil {
  238. return nil
  239. }
  240. if len(this.images) <= 0 {
  241. return nil
  242. }
  243. return this.images[0]
  244. }
  245. func (this *ShopProduct) HaveImages() bool {
  246. if this == nil {
  247. return false
  248. }
  249. if len(this.images) <= 0 {
  250. return false
  251. }
  252. return true
  253. }
  254. func (this *ShopProduct) Images() []*ShopProductImage {
  255. if this == nil {
  256. return []*ShopProductImage{}
  257. }
  258. return this.images
  259. }
  260. func (this *ShopProduct) ImagesCount() int {
  261. if this == nil {
  262. return 0
  263. }
  264. return len(this.images)
  265. }
  266. func (this *ShopProduct) HaveSpecs() bool {
  267. if this == nil {
  268. return false
  269. }
  270. if len(this.specs) <= 0 {
  271. return false
  272. }
  273. return true
  274. }
  275. func (this *ShopProduct) Specs() []*ShopProductSpec {
  276. if this == nil {
  277. return []*ShopProductSpec{}
  278. }
  279. return this.specs
  280. }
  281. func (this *ShopProduct) SpecsCount() int {
  282. if this == nil {
  283. return 0
  284. }
  285. return len(this.specs)
  286. }