collection.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package view
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/metarare/metarare_api/common"
  6. "github.com/metarare/metarare_api/models"
  7. "gorm.io/gorm"
  8. )
  9. type CollectionInfo struct {
  10. ID uint64 `json:"id"`
  11. Name string `json:"name"`
  12. Symbol string `json:"symbol"`
  13. Description string `json:"description"`
  14. ContractAddress string `json:"contract_address"`
  15. ThumbnailImage string `json:"thumbnail_image"`
  16. CoverImage string `json:"cover_image"`
  17. IsOfficial bool `json:"is_official"`
  18. Status string `json:"status"`
  19. CuratingNumber int `json:"curating_number"`
  20. UserName string `json:"user_name"`
  21. Items int64 `json:"items"`
  22. HighestSalePrice float64 `json:"highest_sale_price"`
  23. MarketCap float64 `json:"market_cap"`
  24. TotalVolume float64 `json:"total_volume"`
  25. TotalLikeCount uint32 `json:"total_like_count"`
  26. RecentTradingDay time.Time `json:"recent_trading_day"`
  27. IsOwner bool `json:"is_owner"`
  28. }
  29. func GetCollectionInfomation(db *gorm.DB, ids []uint64, ownerID uint64) ([]CollectionInfo, error) {
  30. _res := []CollectionInfo{}
  31. _err := GetCollectionInfo(db, ownerID).Where("collection.id in (?) AND collection.status = 'visible'", ids).Order("collection.id desc").Find(&_res).Error
  32. if errors.Is(_err, gorm.ErrRecordNotFound) {
  33. return nil, _err
  34. }
  35. err, _currency := common.UpdateCurrency(db)
  36. if err != nil {
  37. return nil, err
  38. }
  39. for idx, item := range _res {
  40. _res[idx].Items = GetCollectionItemCount(db, item.ID)
  41. _res[idx].HighestSalePrice = GetHighestPrice(db, _currency, item.ID)
  42. _res[idx].MarketCap = GetMarketCap(db, _currency, item.ID)
  43. _res[idx].TotalVolume = GetTotalVolume(db, _currency, item.ID)
  44. }
  45. return _res, nil
  46. }
  47. func GetCollectionInfo(db *gorm.DB, userID uint64) *gorm.DB {
  48. querySet := db.Select(`collection.id, collection.status, collection.symbol, collection_profile.name, collection_profile.thumbnail_image,
  49. collection_profile.cover_image, collection.is_official, user_profile.name AS user_name, collection.curating_number,
  50. collection_profile.description, collection.contract_address,
  51. IF(user_profile.user_id = ?, true, false) AS is_owner`, userID).Table("collection").
  52. Joins("INNER JOIN collection_profile ON collection_profile.collection_id = collection.id").
  53. Joins("INNER JOIN user_wallet ON user_wallet.address = collection.owner_address").
  54. Joins("INNER JOIN user_profile ON user_profile.user_id = user_wallet.user_id")
  55. return querySet
  56. }
  57. func GetCollectionItemCount(db *gorm.DB, collectionID uint64) int64 {
  58. var count int64
  59. db.Table("collection").
  60. Joins("INNER JOIN token ON token.collection_id = collection.id").
  61. Where("token.deleted_at IS NULL AND collection_id = ?", collectionID).Count(&count)
  62. return count
  63. }
  64. func TokenBaseCollectionQuery(db *gorm.DB, id uint64) *gorm.DB {
  65. querySet := db.Select("token.*").Table("collection").
  66. Joins("INNER JOIN token ON token.collection_id = collection.id").
  67. Where("token.lastest_price IS NOT NULL AND token.lastest_currency IS NOT NULL AND collection.id = ?", id)
  68. return querySet
  69. }
  70. func GetHighestPrice(db *gorm.DB, currencyPrice models.CurrencyPrice, collectionID uint64) float64 {
  71. token := []models.Token{}
  72. if err := TokenBaseCollectionQuery(db, collectionID).Find(&token).Error; err != nil {
  73. return 0
  74. } else if len(token) == 0 {
  75. return 0
  76. }
  77. highestPrice := 0.0
  78. for _, item := range token {
  79. _compare := 0.0
  80. if item.LastestCurrency.String == "eth" {
  81. _compare = currencyPrice.Eth
  82. } else if item.LastestCurrency.String == "mf" {
  83. _compare = currencyPrice.Mf
  84. } else if item.LastestCurrency.String == "mr" {
  85. _compare = currencyPrice.Mr
  86. }
  87. if highestPrice < (item.LastestPrice.Float64 * _compare) {
  88. highestPrice = (item.LastestPrice.Float64 * _compare)
  89. }
  90. }
  91. return highestPrice
  92. }
  93. func GetMarketCap(db *gorm.DB, currencyPrice models.CurrencyPrice, collectionID uint64) float64 {
  94. token := []models.Token{}
  95. if err := TokenBaseCollectionQuery(db, collectionID).Find(&token).Error; err != nil {
  96. return 0
  97. } else if len(token) == 0 {
  98. return 0
  99. }
  100. highestPrice := 0.0
  101. for _, item := range token {
  102. _compare := 0.0
  103. if item.LastestCurrency.String == "eth" {
  104. _compare = currencyPrice.Eth
  105. } else if item.LastestCurrency.String == "mf" {
  106. _compare = currencyPrice.Mf
  107. } else if item.LastestCurrency.String == "mr" {
  108. _compare = currencyPrice.Mr
  109. }
  110. highestPrice += (item.LastestPrice.Float64 * _compare)
  111. }
  112. return highestPrice
  113. }
  114. func GetTotalVolume(db *gorm.DB, currencyPrice models.CurrencyPrice, collectionID uint64) float64 {
  115. sale := []models.Sale{}
  116. if err := db.Select("sale.*").Table("collection").
  117. Joins("INNER JOIN token ON token.collection_id = collection.id").
  118. Joins("INNER JOIN sale ON sale.token_id = token.id").
  119. Where("collection.id = ? AND collection.status != 'cancel'", collectionID).Find(&sale).Error; err != nil {
  120. return 0
  121. } else if len(sale) == 0 {
  122. return 0
  123. }
  124. highestPrice := 0.0
  125. for _, item := range sale {
  126. _compare := 0.0
  127. if item.Currency == "eth" {
  128. _compare = currencyPrice.Eth
  129. } else if item.Currency == "mf" {
  130. _compare = currencyPrice.Mf
  131. } else if item.Currency == "mr" {
  132. _compare = currencyPrice.Mr
  133. }
  134. highestPrice += (item.Price.Float64 * _compare)
  135. }
  136. return highestPrice
  137. }
  138. func GetRecentTradingDate(db *gorm.DB, collectionID uint64) time.Time {
  139. _t := models.Token{}
  140. token := []models.Token{} //NOTE x update_at이 바뀌는건 판매 후 최근 가격에 값일 들어갈 때.
  141. err := db.Where("collection_id = ? AND lastest_price IS NOT NULL", collectionID).Find(&token).Error
  142. if errors.Is(err, gorm.ErrRecordNotFound) {
  143. return _t.CreatedAt
  144. } else if len(token) == 0 {
  145. return _t.CreatedAt
  146. }
  147. res := time.Date(2019, 1, 12, 0, 0, 0, 0, time.UTC)
  148. for idx, item := range token {
  149. if idx == 0 {
  150. res = item.UpdatedAt
  151. } else if res.Before(item.UpdatedAt) {
  152. res = item.UpdatedAt
  153. }
  154. }
  155. return res
  156. }
  157. func GetTotalLikeCount(db *gorm.DB, collectionID uint64) uint32 {
  158. token := []models.Token{}
  159. err := db.Where("collection_id = ?", collectionID).Find(&token).Error
  160. if errors.Is(err, gorm.ErrRecordNotFound) {
  161. return 0
  162. } else if len(token) == 0 {
  163. return 0
  164. }
  165. var totalCount uint32
  166. for _, item := range token {
  167. totalCount += item.LikeCount
  168. }
  169. return totalCount
  170. }