package model

import (
	"crawler/config"
	"crawler/service"
	"database/sql"
)

type ShopConfigInterface interface {
	Configs()
}

type ShopConfigModel struct {
	ShopConfig
}

type ShopConfig struct {
	Key         string
	Value       *string
	Description *string
}

func (this *ShopConfigModel) Configs() ([]ShopConfig, error) {
	var (
		db    = service.DB_PLAYR
		conn  = db.SQLDB
		query = "SELECT `key`, `value`, `description` FROM tb_shop_config;"
		list  = make([]ShopConfig, 0)
	)

	rows, err := conn.Query(query)
	if err != nil && err != sql.ErrNoRows {
		db.SetErrorLog(err, query)
		return list, err
	}
	defer rows.Close()

	for rows.Next() {
		var row ShopConfig
		if err = rows.Scan(&row.Key, &row.Value, &row.Description); err != nil {
			return list, err
		}

		list = append(list, row)
	}

	if err = rows.Err(); err != nil {
		return list, err
	}

	db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select shop config")
	return list, nil
}

func (this *ShopConfigModel) G2AIsTest() bool {
	var (
		configs, err = this.Configs()
		isTest       = false
	)

	if err != nil {
		return isTest
	}

	for _, row := range configs {
		if row.Key == "shop_g2a_is_test" {
			if *row.Value != "0" && *row.Value != "" {
				isTest = true
			}
		}
	}

	return isTest
}
