package models

import (
	"bwallet/pkg/setting"
	"github.com/jinzhu/gorm"
)

type WalletCoinRelation struct {
	Model
	Id         int `json:"id,omitempty"`
	PlatformId int `json:"platform_id"` //钱包ID
	CoinId     int `json:"coin_id"`     //币种ID

	Coin *Coin `gorm:"ForeignKey:CoinId"`
}

func (c WalletCoinRelation) TableName() string {
	return setting.DatabaseSetting.Name_Sources + ".wallet_coin_relation"
}

/**
 * 获取符合条件的信息总数
 * @param id
 * @return
 */
func GetWalletCoinRelationTotal(maps interface{}) (int, error) {
	var count int
	if err := db.Model(&WalletCoinRelation{}).Where(maps).Count(&count).Error; err != nil {
		return 0, err
	}

	return count, nil
}

/**
 * 获取符合条件的信息
 * @param id
 * @return
 */
func GetWalletCoinRelations(pageNum, pageSize int, maps interface{}) ([]*WalletCoinRelation, error) {
	var coins []*WalletCoinRelation

	err := db.Preload("Coin").Where(maps).Offset(pageNum).Limit(pageSize).Find(&coins).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return coins, nil
}

/**
 * 删除钱包币关系
 * @param
 * @return
 */
func DeleteWalletCoinRelation(platform_id, coin_id int) error {
	if err := db.Where("platform_id = ? and coin_id = ?", platform_id, coin_id).Delete(WalletCoinRelation{}).Error; err != nil {
		return err
	}

	return nil
}
