package dao

import (
	"gorm.io/gorm"
)

type TronAddressInfo struct {
	gorm.Model
	Name        string `gorm:"column:name" json:"name"`
	TronIndex   int64  `gorm:"column:tron_index" json:"tron_index"`
	TronAddress string `gorm:"type:varchar(200);column:tron_address;uniqueIndex" json:"tron_address"`
}

func (tron *TronAddressInfo) GetName() string {
	return tron.Name
}
func (tron *TronAddressInfo) GetIndex() int64 {
	return tron.TronIndex
}
func (tron *TronAddressInfo) GetAddr() string {
	return tron.TronAddress
}

func (tron *TronAddressInfo) TableName() string {
	return "key_manager_tron"
}

func (dao *Dao) CreateTron(data *TronAddressInfo) error {
	return dao.db.Create(&data).Error
}

func (dao *Dao) GetTronFromAddress(addr string) (*TronAddressInfo, error) {
	var tronAddressInfo TronAddressInfo
	err := dao.db.Table(new(TronAddressInfo).TableName()).Where("tron_address=?", addr).First(&tronAddressInfo).Error
	return &tronAddressInfo, err
}

func (dao *Dao) GetTronFromIndex(name string, index int64) (*TronAddressInfo, error) {
	var tronAddressInfo TronAddressInfo
	err := dao.db.Table(new(TronAddressInfo).TableName()).Where("name=? AND tron_index=?", name, index).First(&tronAddressInfo).Error
	return &tronAddressInfo, err
}
