123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- package fetdata
- import (
- "html/template"
- "strings"
- "time"
- "golang-fave/engine/utils"
- "golang-fave/engine/wrapper"
- )
- type ShopProductVarItem struct {
- Link string
- Name string
- Selected bool
- }
- type ShopProduct struct {
- wrap *wrapper.Wrapper
- object *utils.MySql_shop_product
- user *User
- currency *ShopCurrency
- category *ShopCategory
- images []*ShopProductImage
- specs []*ShopProductSpec
- vars []*ShopProductVarItem
- }
- func (this *ShopProduct) load() *ShopProduct {
- if this == nil {
- return this
- }
- if rows, err := this.wrap.DB.Query(
- this.wrap.R.Context(),
- `SELECT
- fave_shop_product_images.product_id,
- fave_shop_product_images.filename
- FROM
- fave_shop_product_images
- WHERE
- fave_shop_product_images.product_id = ?
- ORDER BY
- fave_shop_product_images.ord ASC
- ;`,
- this.object.A_id,
- ); err == nil {
- defer rows.Close()
- for rows.Next() {
- img := utils.MySql_shop_product_image{}
- if err := rows.Scan(
- &img.A_product_id,
- &img.A_filename,
- ); *this.wrap.LogCpError(&err) == nil {
- this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
- }
- }
- }
- // Get images from parent
- if len(this.images) <= 0 && this.object.A_parent_id() > 0 {
- if rows, err := this.wrap.DB.Query(
- this.wrap.R.Context(),
- `SELECT
- fave_shop_product_images.product_id,
- fave_shop_product_images.filename
- FROM
- fave_shop_product_images
- WHERE
- fave_shop_product_images.product_id = ?
- ORDER BY
- fave_shop_product_images.ord ASC
- ;`,
- this.object.A_parent_id(),
- ); err == nil {
- defer rows.Close()
- for rows.Next() {
- img := utils.MySql_shop_product_image{}
- if err := rows.Scan(
- &img.A_product_id,
- &img.A_filename,
- ); *this.wrap.LogCpError(&err) == nil {
- this.images = append(this.images, &ShopProductImage{wrap: this.wrap, object: &img})
- }
- }
- }
- }
- filter_ids := []int{}
- filter_names := map[int]string{}
- filter_values := map[int][]string{}
- if rows, err := this.wrap.DB.Query(
- this.wrap.R.Context(),
- `SELECT
- fave_shop_filters.id,
- fave_shop_filters.filter,
- fave_shop_filters_values.name
- FROM
- fave_shop_filter_product_values
- LEFT JOIN fave_shop_filters_values ON fave_shop_filters_values.id = fave_shop_filter_product_values.filter_value_id
- LEFT JOIN fave_shop_filters ON fave_shop_filters.id = fave_shop_filters_values.filter_id
- WHERE
- fave_shop_filter_product_values.product_id = ?
- ORDER BY
- fave_shop_filters.filter ASC,
- fave_shop_filters_values.name ASC
- ;`,
- this.object.A_id,
- ); err == nil {
- defer rows.Close()
- values := make([]string, 3)
- scan := make([]interface{}, len(values))
- for i := range values {
- scan[i] = &values[i]
- }
- for rows.Next() {
- err = rows.Scan(scan...)
- if *this.wrap.LogCpError(&err) == nil {
- if !utils.InArrayInt(filter_ids, utils.StrToInt(string(values[0]))) {
- filter_ids = append(filter_ids, utils.StrToInt(string(values[0])))
- }
- filter_names[utils.StrToInt(string(values[0]))] = string(values[1])
- filter_values[utils.StrToInt(string(values[0]))] = append(filter_values[utils.StrToInt(string(values[0]))], string(values[2]))
- }
- }
- }
- for _, filter_id := range filter_ids {
- this.specs = append(this.specs, &ShopProductSpec{wrap: this.wrap, object: &utils.MySql_shop_product_spec{
- A_product_id: this.object.A_id,
- A_filter_id: filter_id,
- A_filter_name: filter_names[filter_id],
- A_filter_value: strings.Join(filter_values[filter_id], ", "),
- }})
- }
- // Variations
- if rows, err := this.wrap.DB.Query(
- this.wrap.R.Context(),
- `SELECT
- fave_shop_products.id,
- fave_shop_products.name,
- fave_shop_products.alias
- FROM
- fave_shop_products
- WHERE
- fave_shop_products.active = 1 AND
- (
- (fave_shop_products.id = ? OR fave_shop_products.parent_id = ?) OR
- (
- (fave_shop_products.id = ?) OR
- (fave_shop_products.parent_id IS NOT NULL AND fave_shop_products.parent_id = ?)
- )
- )
- ORDER BY
- fave_shop_products.name ASC
- ;`,
- this.object.A_id,
- this.object.A_id,
- this.object.A_parent_id(),
- this.object.A_parent_id(),
- ); err == nil {
- defer rows.Close()
- for rows.Next() {
- var tmp_id int
- var tmp_name string
- var tmp_alias string
- if err := rows.Scan(
- &tmp_id,
- &tmp_name,
- &tmp_alias,
- ); *this.wrap.LogCpError(&err) == nil {
- selected := false
- if tmp_id == this.object.A_id {
- selected = true
- }
- this.vars = append(this.vars, &ShopProductVarItem{
- Link: "/shop/" + tmp_alias + "/",
- Name: tmp_name + " " + utils.IntToStr(tmp_id),
- Selected: selected,
- })
- }
- }
- }
- return this
- }
- func (this *ShopProduct) Id() int {
- if this == nil {
- return 0
- }
- return this.object.A_id
- }
- func (this *ShopProduct) User() *User {
- if this == nil {
- return nil
- }
- if this.user != nil {
- return this.user
- }
- this.user = (&User{wrap: this.wrap}).load()
- this.user.loadById(this.object.A_user)
- return this.user
- }
- func (this *ShopProduct) Currency() *ShopCurrency {
- if this == nil {
- return nil
- }
- if this.currency != nil {
- return this.currency
- }
- this.currency = (&ShopCurrency{wrap: this.wrap}).load()
- this.currency.loadById(this.object.A_currency)
- return this.currency
- }
- func (this *ShopProduct) Price() float64 {
- if this == nil {
- return 0
- }
- if this.Currency() == nil {
- return this.object.A_price
- }
- if this.wrap.ShopGetCurrentCurrency() == nil {
- return this.object.A_price
- }
- if this.wrap.ShopGetCurrentCurrency().A_id == this.Currency().Id() {
- return this.object.A_price
- }
- if this.Currency().Id() == 1 {
- return this.object.A_price * this.wrap.ShopGetCurrentCurrency().A_coefficient
- } else {
- if c, ok := (*this.wrap.ShopGetAllCurrencies())[this.Currency().Id()]; ok == true {
- return this.object.A_price / c.A_coefficient
- } else {
- return this.object.A_price
- }
- }
- }
- func (this *ShopProduct) PriceOld() float64 {
- if this == nil {
- return 0
- }
- if this.Currency() == nil {
- return this.object.A_price_old
- }
- if this.wrap.ShopGetCurrentCurrency() == nil {
- return this.object.A_price_old
- }
- if this.wrap.ShopGetCurrentCurrency().A_id == this.Currency().Id() {
- return this.object.A_price_old
- }
- if this.Currency().Id() == 1 {
- return this.object.A_price_old * this.wrap.ShopGetCurrentCurrency().A_coefficient
- } else {
- if c, ok := (*this.wrap.ShopGetAllCurrencies())[this.Currency().Id()]; ok == true {
- return this.object.A_price_old / c.A_coefficient
- } else {
- return this.object.A_price_old
- }
- }
- }
- func (this *ShopProduct) PriceNice() string {
- return utils.FormatProductPrice(
- this.Price(),
- (*this.wrap.Config).Shop.Price.Format,
- (*this.wrap.Config).Shop.Price.Round,
- )
- }
- func (this *ShopProduct) PriceOldNice() string {
- return utils.FormatProductPrice(
- this.PriceOld(),
- (*this.wrap.Config).Shop.Price.Format,
- (*this.wrap.Config).Shop.Price.Round,
- )
- }
- func (this *ShopProduct) PriceFormat(format string) string {
- return utils.Float64ToStrF(this.Price(), format)
- }
- func (this *ShopProduct) Group() string {
- if this == nil {
- return ""
- }
- return this.object.A_gname
- }
- func (this *ShopProduct) Name() string {
- if this == nil {
- return ""
- }
- return this.object.A_name
- }
- func (this *ShopProduct) Alias() string {
- if this == nil {
- return ""
- }
- return this.object.A_alias
- }
- func (this *ShopProduct) Vendor() string {
- if this == nil {
- return ""
- }
- return this.object.A_vendor
- }
- func (this *ShopProduct) Quantity() int {
- if this == nil {
- return 0
- }
- return this.object.A_quantity
- }
- func (this *ShopProduct) Category() *ShopCategory {
- if this == nil {
- return nil
- }
- if this.category != nil {
- return this.category
- }
- this.category = (&ShopCategory{wrap: this.wrap}).load(nil)
- this.category.loadById(this.object.A_category)
- return this.category
- }
- func (this *ShopProduct) Briefly() template.HTML {
- if this == nil {
- return template.HTML("")
- }
- return template.HTML(this.object.A_briefly)
- }
- func (this *ShopProduct) Content() template.HTML {
- if this == nil {
- return template.HTML("")
- }
- return template.HTML(this.object.A_content)
- }
- func (this *ShopProduct) DateTimeUnix() int {
- if this == nil {
- return 0
- }
- return this.object.A_datetime
- }
- func (this *ShopProduct) DateTimeFormat(format string) string {
- if this == nil {
- return ""
- }
- return time.Unix(int64(this.object.A_datetime), 0).Format(format)
- }
- func (this *ShopProduct) Active() bool {
- if this == nil {
- return false
- }
- return this.object.A_active > 0
- }
- func (this *ShopProduct) Permalink() string {
- if this == nil {
- return ""
- }
- return "/shop/" + this.object.A_alias + "/"
- }
- func (this *ShopProduct) Image() *ShopProductImage {
- if this == nil {
- return nil
- }
- if len(this.images) <= 0 {
- return nil
- }
- return this.images[0]
- }
- func (this *ShopProduct) HaveImages() bool {
- if this == nil {
- return false
- }
- if len(this.images) <= 0 {
- return false
- }
- return true
- }
- func (this *ShopProduct) Images() []*ShopProductImage {
- if this == nil {
- return []*ShopProductImage{}
- }
- return this.images
- }
- func (this *ShopProduct) ImagesCount() int {
- if this == nil {
- return 0
- }
- return len(this.images)
- }
- func (this *ShopProduct) HaveSpecs() bool {
- if this == nil {
- return false
- }
- if len(this.specs) <= 0 {
- return false
- }
- return true
- }
- func (this *ShopProduct) Specs() []*ShopProductSpec {
- if this == nil {
- return []*ShopProductSpec{}
- }
- return this.specs
- }
- func (this *ShopProduct) SpecsCount() int {
- if this == nil {
- return 0
- }
- return len(this.specs)
- }
- func (this *ShopProduct) HaveVariations() bool {
- if this == nil {
- return false
- }
- if len(this.vars) <= 1 {
- return false
- }
- return true
- }
- func (this *ShopProduct) Variations() []*ShopProductVarItem {
- if this == nil {
- return []*ShopProductVarItem{}
- }
- return this.vars
- }
- func (this *ShopProduct) VariationsCount() int {
- if this == nil {
- return 0
- }
- return len(this.vars)
- }
|