package models

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

type Chain struct {
	Model

	Platform   string  `json:"platform"`    //平行链名称
	Address    string  `json:"address"`     //代扣地址
	PrivateKey string  `json:"private_key"` //代扣私钥
	Exer       string  `json:"exer"`        //执行器
	Token      string  `json:"token"`       //基础代币
	Fee        float32 `json:"fee"`         //手续费（基础代币）
	BrowerUrl  string  `json:"brower_url"`  //浏览器链接
	Icon       string  `json:"icon"`        //图标
	Lock       int     `json:"lock"`        //是否允许转币
}

type UserChain struct {
	Model

	Platform string `json:"platform"` //平行链名称
}

type Explore struct {
	BrowerUrl string `json:"brower_url"` //浏览器链接
	Icon      string `json:"icon"`       //图标
}

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

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

/**
 * 通过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).Order("id desc").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("coin_platform_withhold").Where(maps).Scan(&chains).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return chains, nil
}

func GetExplore(platform string) (*Chain, error) {
	var explore Chain
	err := db.Where("platform = ?", platform).First(&explore).Error
	if err != nil {
		return nil, err
	}

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

	return &explore, 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),
		Exer:       data["exer"].(string),
		Token:      data["token"].(string),
		Fee:        data["fee"].(float32),
		BrowerUrl:  data["brower_url"].(string),
		Icon:       data["icon"].(string),
		Lock:       data["lock"].(int),
	}
	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
}
