package models

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

type Chain struct {
	Model

	Id         int     `json:"primary_key"`
	Platform   string  `json:"platform"`    //平行链名称
	Address    string  `json:"address"`     //代扣地址
	PrivateKey string  `json:"private_key"` //代扣私钥
	Execer     string  `json:"execer"`      //执行器
	Token      string  `json:"token"`       //基础代币
	Fee        float32 `json:"fee"`         //手续费（基础代币）
	BrowerUrl  string  `json:"brower_url"`  //浏览器链接
}

type UserChain struct {
	Model

	Id       int    `json:"primary_key,omitempty"`
	Platform string `json:"platform"` //平行链名称
}

func (c Chain) TableName() string {
	return setting.DatabaseSetting.Name_Sources + ".wallet_chain"
}

func (c UserChain) TableName() string {
	return setting.DatabaseSetting.Name_Sources + ".wallet_chain"
}

/**
 * 通过id检查链是否存在
 * @param id
 * @return
 */
func ExistChainById(id int) (bool, error) {
	var chain Chain
	err := db.Select("id").Where("id = ?", id).First(&chain).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}

	if chain.ID > 0 {
		return true, nil
	}

	return false, nil
}

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

	return count, nil
}

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

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

	return chains, nil
}

func GetUserChains(maps interface{}) ([]*UserChain, error) {
	var chains []*UserChain

	err := db.Select("id, platform").Table("wallet_chain").Where(maps).Scan(&chains).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return chains, nil
}

/**
 * 通过id获取指定链信息
 * @param id
 * @return
 */
func GetChain(id int) (*Chain, error) {
	var chain Chain
	err := db.Where("id = ?", id).First(&chain).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	err = db.Model(&chain).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return &chain, nil
}

/**
 * 添加链信息
 * @param
 * @return
 */
func AddChain(data map[string]interface{}) (error) {
	chain := Chain{
		Platform:   data["platform"].(string),
		Address:    data["address"].(string),
		PrivateKey: data["private_key"].(string),
		Execer:     data["execer"].(string),
		Token:      data["token"].(string),
		Fee:        data["fee"].(float32),
		BrowerUrl:  data["brower_url"].(string),
	}
	if err := db.Create(&chain).Error; err != nil {
		return err
	}

	return nil
}

/**
 * 更新链信息
 * @param
 * @return
 */
func EditChain(id int, data interface{}) error {
	if err := db.Model(&Chain{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return err
	}

	return nil
}

/**
 * 删除链信息
 * @param
 * @return
 */
func DeleteChain(id int) error {
	if err := db.Where("id = ?", id).Delete(Chain{}).Error; err != nil {
		return err
	}

	return nil
}
