Commit 298c6471 authored by shajiaiming's avatar shajiaiming

fix

parent 96504999
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bwallet/pkg/setting" "bwallet/pkg/setting"
"bwallet/pkg/util" "bwallet/pkg/util"
"encoding/json" "encoding/json"
"fmt"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
...@@ -120,6 +121,7 @@ func GetCoins(pageNum, pageSize int, maps interface{}) ([]*Coin, error) { ...@@ -120,6 +121,7 @@ func GetCoins(pageNum, pageSize int, maps interface{}) ([]*Coin, error) {
} }
var err error var err error
fmt.Println(platform_id)
if 0 == platform_id { if 0 == platform_id {
err = db.Where(term).Offset(pageNum).Limit(pageSize).Find(&coins).Error err = db.Where(term).Offset(pageNum).Limit(pageSize).Find(&coins).Error
} else { } else {
...@@ -193,7 +195,7 @@ func AddCoin(data map[string]interface{}) (error) { ...@@ -193,7 +195,7 @@ func AddCoin(data map[string]interface{}) (error) {
PlatformId: data["platform_id"].(string), PlatformId: data["platform_id"].(string),
Treaty: data["treaty"].(int), Treaty: data["treaty"].(int),
} }
if err := db.Create(&coin).Error; err != nil { if err := db.Debug().Create(&coin).Error; err != nil {
return err return err
} }
......
...@@ -11,6 +11,7 @@ type PlatformChain struct { ...@@ -11,6 +11,7 @@ type PlatformChain struct {
Id int `json:"primary_key,omitempty"` Id int `json:"primary_key,omitempty"`
PlatformId int `json:"platform_id"` PlatformId int `json:"platform_id"`
ChainId int `json:"chain_id"` ChainId int `json:"chain_id"`
IsPrimary int `json:"is_primary"`
ChainInfo *UserChain `gorm:"foreignkey:ChainId" json:"chain"` ChainInfo *UserChain `gorm:"foreignkey:ChainId" json:"chain"`
} }
......
...@@ -57,10 +57,10 @@ func GetWalletRecommendCategory(id int) (*WalletRecommendCategory, error) { ...@@ -57,10 +57,10 @@ func GetWalletRecommendCategory(id int) (*WalletRecommendCategory, error) {
* 获取符合条件的信息 * 获取符合条件的信息
* @return * @return
*/ */
func GetWalletRecommendCurrencies(pageNum, pageSize int, maps interface{}) ([]*WalletRecommendCategory, error) { func GetWalletRecommendCategories(pageNum, pageSize int, maps interface{}) ([]*WalletRecommendCategory, error) {
var categories []*WalletRecommendCategory var categories []*WalletRecommendCategory
err := db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&categories).Error err := db.Debug().Where(maps).Order("sort").Offset(pageNum).Limit(pageSize).Find(&categories).Error
if err != nil && err != gorm.ErrRecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
return nil, err return nil, err
} }
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"bwallet/pkg/util" "bwallet/pkg/util"
"bwallet/service/auth_service" "bwallet/service/auth_service"
"bwallet/service/coin_service" "bwallet/service/coin_service"
"bwallet/service/platform_chain_service"
"bwallet/service/recommend_coin_service" "bwallet/service/recommend_coin_service"
"bwallet/validate_service" "bwallet/validate_service"
"github.com/Unknwon/com" "github.com/Unknwon/com"
...@@ -83,10 +84,10 @@ func GetCoins(c *gin.Context) { ...@@ -83,10 +84,10 @@ func GetCoins(c *gin.Context) {
chain = c.Query("chain") chain = c.Query("chain")
} }
platform_id := "" //platform_id := ""
if arg := c.Query("platform_id"); arg != "" { //if arg := c.Query("platform_id"); arg != "" {
platform_id = c.DefaultQuery("platform_id", "1") // platform_id = c.DefaultQuery("platform_id", "1")
} //}
if valid.HasErrors() { if valid.HasErrors() {
handler.SendResponse(c, errno.ErrValidation, nil) handler.SendResponse(c, errno.ErrValidation, nil)
...@@ -94,12 +95,12 @@ func GetCoins(c *gin.Context) { ...@@ -94,12 +95,12 @@ func GetCoins(c *gin.Context) {
} }
coinService := coin_service.Coin{ coinService := coin_service.Coin{
Name: name, Name: name,
Chain: chain, Chain: chain,
Platform: platform, Platform: platform,
PlatformId: platform_id, //PlatformId: platform_id,
PageNum: util.GetPage(c), PageNum: util.GetPage(c),
PageSize: util.GetLimit(c), PageSize: util.GetLimit(c),
} }
total, err := coinService.Count() total, err := coinService.Count()
...@@ -158,7 +159,7 @@ func AddCoin(c *gin.Context) { ...@@ -158,7 +159,7 @@ func AddCoin(c *gin.Context) {
Decimals: coin.Decimals, Decimals: coin.Decimals,
Address: coin.Address, Address: coin.Address,
Treaty: coin.Treaty, Treaty: coin.Treaty,
PlatformId: strconv.Itoa(coin.PlatformId), PlatformId: "1",
} }
if err := coinService.Add(); err != nil { if err := coinService.Add(); err != nil {
...@@ -180,6 +181,48 @@ func EditCoin(c *gin.Context) { ...@@ -180,6 +181,48 @@ func EditCoin(c *gin.Context) {
} }
} }
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
coin_model := coin_service.Coin{Id: coin.Id}
exists, err := coin_model.ExistById()
if err != nil {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
if !exists {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
coin_exist, _ := coin_model.Get()
platformChainService := platform_chain_service.PlatformChain{
PlatformId: auth.PlatformId,
IsPrimary: 1,
}
platform_chain, err := platformChainService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
var platform string
for _, val := range platform_chain {
if nil != val.ChainInfo {
platform = val.ChainInfo.Platform
}
}
if ("administrator" != group) {
if (strings.ToUpper(coin_exist.Platform) != strings.ToUpper(platform)) {
handler.SendResponse(c, errno.ErrAuthCoin, nil)
return
}
}
coinService := coin_service.Coin{ coinService := coin_service.Coin{
Id: coin.Id, Id: coin.Id,
Name: coin.Name, Name: coin.Name,
...@@ -196,35 +239,10 @@ func EditCoin(c *gin.Context) { ...@@ -196,35 +239,10 @@ func EditCoin(c *gin.Context) {
CirculateCount: coin.CirculateCount, CirculateCount: coin.CirculateCount,
Decimals: coin.Decimals, Decimals: coin.Decimals,
Address: coin.Address, Address: coin.Address,
PlatformId: strconv.Itoa(coin.PlatformId), PlatformId: coin_exist.PlatformId,
Treaty: coin.Treaty, Treaty: coin.Treaty,
} }
exists, err := coinService.ExistById()
if err != nil {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
if !exists {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
//coin_model := coin_service.Coin{Id: coin.Id}
//coin_exist, _ := coin_model.Get()
if ("administrator" != group) {
//if (coin.PlatformId != auth.PlatformId) || (auth.PlatformId != coin_exist.PlatformId){
if (coin.PlatformId != auth.PlatformId) {
handler.SendResponse(c, errno.ErrAuthCoin, nil)
return
}
}
if err := coinService.Edit(); err != nil { if err := coinService.Edit(); err != nil {
handler.SendResponse(c, errno.ErrUpdateCoin, nil) handler.SendResponse(c, errno.ErrUpdateCoin, nil)
return return
......
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"bwallet/pkg/util" "bwallet/pkg/util"
"bwallet/service/auth_service" "bwallet/service/auth_service"
"bwallet/service/coin_service" "bwallet/service/coin_service"
"bwallet/service/wallet_coin_relation_service"
"bwallet/validate_service" "bwallet/validate_service"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
...@@ -15,16 +14,15 @@ import ( ...@@ -15,16 +14,15 @@ import (
) )
func GetWalletCoinRelationCoinRelations(c *gin.Context) { func GetWalletCoinRelationCoinRelations(c *gin.Context) {
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token} authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
var platform_id int var platform_id string
if ("administrator" == group) { if ("administrator" == group) {
platform_id = com.StrTo(c.DefaultQuery("platform_id", "1")).MustInt() platform_id = c.DefaultQuery("platform_id", "1")
} else { } else {
platform_id = auth.PlatformId platform_id = strconv.Itoa(auth.PlatformId)
} }
name := "" name := ""
if arg := c.Query("name"); arg != "" { if arg := c.Query("name"); arg != "" {
...@@ -41,24 +39,22 @@ func GetWalletCoinRelationCoinRelations(c *gin.Context) { ...@@ -41,24 +39,22 @@ func GetWalletCoinRelationCoinRelations(c *gin.Context) {
chain = c.Query("chain") chain = c.Query("chain")
} }
walletCoinRelateionService := wallet_coin_relation_service.WalletCoinRelation{ coinService := coin_service.Coin{
Name: name,
Chain: chain,
Platform: platform,
PlatformId: platform_id, PlatformId: platform_id,
CoinCondition: struct { PageNum: util.GetPage(c),
Name string PageSize: util.GetLimit(c),
Chain string
Platform string
}{Name: name, Chain: chain, Platform: platform},
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
} }
total, err := walletCoinRelateionService.Sum() total, err := coinService.Count()
if err != nil { if err != nil {
handler.SendResponse(c, errno.ErrCountCoin, nil) handler.SendResponse(c, errno.ErrCountCoin, nil)
return return
} }
coins, err := walletCoinRelateionService.GetAll() coins, err := coinService.GetAll()
if err != nil { if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil) handler.SendResponse(c, errno.InternalServerError, nil)
return return
...@@ -66,7 +62,7 @@ func GetWalletCoinRelationCoinRelations(c *gin.Context) { ...@@ -66,7 +62,7 @@ func GetWalletCoinRelationCoinRelations(c *gin.Context) {
data := make(map[string]interface{}) data := make(map[string]interface{})
data["items"] = coins data["items"] = coins
data["total"] = len(total) data["total"] = total
handler.SendResponse(c, nil, data) handler.SendResponse(c, nil, data)
} }
...@@ -114,56 +110,21 @@ func AddWalletCoinRelationCoinRelation(c *gin.Context) { ...@@ -114,56 +110,21 @@ func AddWalletCoinRelationCoinRelation(c *gin.Context) {
continue continue
} }
relationService := wallet_coin_relation_service.WalletCoinRelation{ coin_exist, _ := coinService.Get()
CoinId: coin_id, platform_id_arr := strings.Split(coin_exist.PlatformId, ",")
PlatformId: coin_relation.PlatformId, is_exist := false
for _, exist_platform_id := range platform_id_arr {
if util.ToInt(exist_platform_id) == coin_relation.PlatformId {
is_exist = true
}
} }
total, err := relationService.Count() if is_exist == false {
if err != nil || total > 0 { coinService.PlatformId = coin_exist.PlatformId + "," + strconv.Itoa(coin_relation.PlatformId)
continue coinService.UpdateCoinRelation()
} }
relationService.Add()
} }
handler.SendResponse(c, nil, nil) handler.SendResponse(c, nil, nil)
/*
coinService := coin_service.Coin{
Id: coin_relation.CoinId,
}
exists, err := coinService.ExistById()
if err != nil {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
if !exists {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
relationService := wallet_coin_relation_service.WalletCoinRelation{
CoinId: coin_relation.CoinId,
PlatformId: coin_relation.PlatformId,
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
coins, err := relationService.GetAll()
if err != nil {
handler.SendResponse(c, errno.InternalServerError, nil)
return
}
if 0 != len(coins) {
handler.SendResponse(c, errno.ErrExistCoin, nil)
return
}
if err := relationService.Add(); err != nil {
handler.SendResponse(c, errno.ErrAddCoin, nil)
return
}
handler.SendResponse(c, nil, nil)
*/
} }
/** /**
...@@ -196,27 +157,28 @@ func DeleteWalletCoinRelationCoinRelation(c *gin.Context) { ...@@ -196,27 +157,28 @@ func DeleteWalletCoinRelationCoinRelation(c *gin.Context) {
return return
} }
relationService := wallet_coin_relation_service.WalletCoinRelation{ coinService := coin_service.Coin{
PlatformId: platform_id, Id: coin_id,
CoinId: coin_id,
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
} }
coins, err := relationService.GetAll() exists, err := coinService.ExistById()
if err != nil { if err != nil || !exists {
handler.SendResponse(c, errno.InternalServerError, nil) handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
if 0 == len(coins) {
handler.SendResponse(c, errno.ErrNotExistCoin, nil)
return return
} }
err = relationService.Delete() coin_exist, _ := coinService.Get()
if err != nil { platform_id_arr := strings.Split(coin_exist.PlatformId, ",")
handler.SendResponse(c, errno.ErrDeleteCoin, nil)
return var new_platform_id string
for _, exist_platform_id := range platform_id_arr {
if util.ToInt(exist_platform_id) == platform_id {
continue
}
new_platform_id = strings.Join([]string{exist_platform_id, new_platform_id}, ",")
new_platform_id = strings.TrimRight(new_platform_id, ",")
} }
coinService.PlatformId = new_platform_id
coinService.UpdateCoinRelation()
handler.SendResponse(c, nil, nil) handler.SendResponse(c, nil, nil)
} }
...@@ -37,9 +37,9 @@ func InitRouter() *gin.Engine { ...@@ -37,9 +37,9 @@ func InitRouter() *gin.Engine {
api.GET("/primary-chains", v1.GetPrimaryChains) api.GET("/primary-chains", v1.GetPrimaryChains)
api.GET("/types", v1.GetTypes) api.GET("/types", v1.GetTypes)
//api.GET("/coin-relation", v1.GetWalletCoinRelationCoinRelations) api.GET("/coin-relation", v1.GetWalletCoinRelationCoinRelations)
//api.POST("/coin-relation", v1.AddWalletCoinRelationCoinRelation) api.POST("/coin-relation", v1.AddWalletCoinRelationCoinRelation)
//api.DELETE("/coin-relation", v1.DeleteWalletCoinRelationCoinRelation) api.DELETE("/coin-relation", v1.DeleteWalletCoinRelationCoinRelation)
//api.GET("/get-platform", v1.GetCoinsPlatform) //api.GET("/get-platform", v1.GetCoinsPlatform)
api.GET("/wallets", v1.GetWallets) api.GET("/wallets", v1.GetWallets)
......
...@@ -125,6 +125,17 @@ func (c *Coin) Add() error { ...@@ -125,6 +125,17 @@ func (c *Coin) Add() error {
} }
/** /**
* 添加币种信息
* @param
* @return
*/
func (c *Coin) UpdateCoinRelation() error {
return models.EditCoin(c.Id, map[string]interface{}{
"platform_id": c.PlatformId,
})
}
/**
* 更新币种信息 * 更新币种信息
* @param * @param
* @return * @return
......
...@@ -8,6 +8,7 @@ type PlatformChain struct { ...@@ -8,6 +8,7 @@ type PlatformChain struct {
Id int Id int
PlatformId int PlatformId int
ChainId int ChainId int
IsPrimary int
} }
func (c *PlatformChain) Get() (*models.PlatformChain, error) { func (c *PlatformChain) Get() (*models.PlatformChain, error) {
...@@ -54,5 +55,9 @@ func (c *PlatformChain) getMaps() (map[string]interface{}) { ...@@ -54,5 +55,9 @@ func (c *PlatformChain) getMaps() (map[string]interface{}) {
maps["chain_id"] = c.ChainId maps["chain_id"] = c.ChainId
} }
if c.IsPrimary != 0 {
maps["is_primary"] = c.IsPrimary
}
return maps return maps
} }
...@@ -29,7 +29,7 @@ func (c *WalletRecommendCategory) Get() (*models.WalletRecommendCategory, error) ...@@ -29,7 +29,7 @@ func (c *WalletRecommendCategory) Get() (*models.WalletRecommendCategory, error)
func (c *WalletRecommendCategory) GetAll() ([]*models.WalletRecommendCategory, error) { func (c *WalletRecommendCategory) GetAll() ([]*models.WalletRecommendCategory, error) {
var categories []*models.WalletRecommendCategory var categories []*models.WalletRecommendCategory
categories, err := models.GetWalletRecommendCurrencies(c.PageNum, c.PageSize, c.getMaps()) categories, err := models.GetWalletRecommendCategories(c.PageNum, c.PageSize, c.getMaps())
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -21,7 +21,7 @@ type Coin struct { ...@@ -21,7 +21,7 @@ type Coin struct {
Address string `json:"address" validate:"omitempty"` // Address string `json:"address" validate:"omitempty"` //
Treaty int `json:"treaty" validate:"required"` Treaty int `json:"treaty" validate:"required"`
PlatformId int `json:"platform_id" validate:"required"` //PlatformId int `json:"platform_id" validate:"required"`
} }
type EditCoin struct { type EditCoin struct {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment