package wallet_coin_relation_service

import (
	"bwallet/models"
)

type WalletCoinRelation struct {
	PlatformId    int
	CoinId        int
	CoinCondition CoinCondition

	PageNum  int
	PageSize int
}

type CoinCondition struct {
	Name     string
	Chain    string
	Platform string
}

func (w *WalletCoinRelation) GetAll() ([]*models.Coin, error) {
	var wallet_coin_relations []*models.WalletCoinRelation

	wallet_coin_relations, err := models.GetWalletCoinRelations(w.PageNum, w.PageSize, w.getMaps())
	if err != nil {
		return nil, err
	}
	var coins []*models.Coin
	for _, value := range wallet_coin_relations {
		if nil != value.Coin {
			coins = append(coins, value.Coin)
		}
	}

	return coins, nil
}

func (w *WalletCoinRelation) Sum() ([]*models.Coin, error) {
	var wallet_coin_relations []*models.WalletCoinRelation

	wallet_coin_relations, err := models.GetWalletCoinRelationsSum(w.getMaps())
	if err != nil {
		return nil, err
	}
	var coins []*models.Coin
	for _, value := range wallet_coin_relations {
		if nil != value.Coin {
			coins = append(coins, value.Coin)
		}
	}

	return coins, nil
}

func (w *WalletCoinRelation) Count() (int, error) {
	return models.GetWalletCoinRelationTotal(w.getMaps())
}

/**
 * 添加币种钱包对应关系
 * @param
 * @return
 */
func (c *WalletCoinRelation) Add() error {
	relation := map[string]interface{}{
		"coin_id":     c.CoinId,
		"platform_id": c.PlatformId,
	}

	if err := models.AddWalletCoinRelation(relation); err != nil {
		return err
	}

	return nil
}

/**
 * 删除币种钱包对应关系
 * @param
 * @return
 */
func (c *WalletCoinRelation) Delete() error {
	return models.DeleteWalletCoinRelation(c.PlatformId, c.CoinId)
}

type Condition struct {
}

func (c *WalletCoinRelation) getMaps() (map[string]interface{}) {
	maps := make(map[string]interface{})

	if c.PlatformId != 0 {
		maps["platform_id"] = c.PlatformId
	}

	if c.CoinId != 0 {
		maps["coin_id"] = c.CoinId
	}

	var condition = make(map[string]interface{})
	if c.CoinCondition.Name != "" {
		condition["name"] = c.CoinCondition.Name
	}

	if c.CoinCondition.Chain != "" {
		condition["chain"] = c.CoinCondition.Chain
	}

	if c.CoinCondition.Platform != "" {
		condition["platform"] = c.CoinCondition.Platform
	}
	maps["condition"] = condition
	return maps
}
