common.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package view
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/metarare/metarare_api/common"
  8. "github.com/metarare/metarare_api/models"
  9. "gorm.io/gorm"
  10. )
  11. // filter
  12. func userIDCheck(id uint64) func(db *gorm.DB) *gorm.DB {
  13. return func(db *gorm.DB) *gorm.DB {
  14. return db.Where("user_id = ?", id)
  15. }
  16. }
  17. func filterByOwner(ownerAddress string) func(db *gorm.DB) *gorm.DB {
  18. return func(db *gorm.DB) *gorm.DB {
  19. return db.Where("owner_address = ?", ownerAddress)
  20. }
  21. }
  22. func filterByNetwork(network string) func(db *gorm.DB) *gorm.DB {
  23. return func(db *gorm.DB) *gorm.DB {
  24. if network == "" {
  25. fmt.Printf("invalid network(filterByNetwork!!! (%s)\n", network)
  26. return db
  27. } else {
  28. return db.Where("network = ?", network)
  29. }
  30. }
  31. }
  32. func filterByStatus(status string) func(db *gorm.DB) *gorm.DB {
  33. return func(db *gorm.DB) *gorm.DB {
  34. if status == "" {
  35. fmt.Printf("invalid status (filterByStatus)!!! (%s)\n", status)
  36. return db
  37. } else {
  38. return db.Where("status = ?", status)
  39. }
  40. }
  41. }
  42. func filterByTokenIDs(tokenIDs []uint64) func(db *gorm.DB) *gorm.DB {
  43. return func(db *gorm.DB) *gorm.DB {
  44. if len(tokenIDs) == 0 {
  45. fmt.Printf("invalid status (filterByTokenIDs)!!! len(: zero)\n")
  46. return db
  47. } else {
  48. return db.Where("token_id in (?)", tokenIDs)
  49. }
  50. }
  51. }
  52. func filterByUserID(user_id uint64) func(db *gorm.DB) *gorm.DB {
  53. return func(db *gorm.DB) *gorm.DB {
  54. if user_id == 0 {
  55. fmt.Printf("invalid status (filterByIDs)!!! len(: zero)\n")
  56. return db
  57. } else {
  58. return db.Where("user_id = (?)", user_id)
  59. }
  60. }
  61. }
  62. func filterByID(tokenIDs []uint64) func(db *gorm.DB) *gorm.DB {
  63. return func(db *gorm.DB) *gorm.DB {
  64. if len(tokenIDs) == 0 {
  65. fmt.Printf("invalid status (filterByIDs)!!! len(: zero)\n")
  66. return db
  67. } else {
  68. return db.Where("id in (?)", tokenIDs)
  69. }
  70. }
  71. }
  72. func appendCondition(origin string, data string) string {
  73. _str := origin
  74. if len(origin) != 0 {
  75. _str += " AND "
  76. }
  77. _str += data
  78. return _str
  79. }
  80. func PreloadCollectionWithFilter(_preloadTable string, _db *gorm.DB, _filter common.Filter) *gorm.DB {
  81. _whereCondition := ""
  82. var _whereParams []string
  83. if strings.Compare(_filter.Network, "") != 0 {
  84. _whereCondition = appendCondition(_whereCondition, "network = (?)")
  85. _whereParams = append(_whereParams, _filter.SaleType)
  86. }
  87. if strings.Compare(_filter.CollectionName, "") != 0 {
  88. _whereCondition = appendCondition(_whereCondition, "name = (?)")
  89. _whereParams = append(_whereParams, _filter.CollectionName)
  90. }
  91. if len(_whereParams) != 0 {
  92. return _db.Preload(_preloadTable, _whereCondition, _whereParams)
  93. } else {
  94. return _db.Preload(_preloadTable)
  95. }
  96. }
  97. func PreloadSaleWithFilter(_db *gorm.DB, _filter common.Filter) (string, string, []string) {
  98. _whereCondition := ""
  99. var _whereParams []string
  100. if strings.Compare(_filter.SaleType, "") != 0 {
  101. _whereCondition = appendCondition(_whereCondition, "sale_type in (?)")
  102. _whereParams = append(_whereParams, _filter.SaleType)
  103. }
  104. if strings.Compare(_filter.Currency, "") != 0 &&
  105. strings.Compare(_filter.CurrencyAmountStart, "") != 0 &&
  106. strings.Compare(_filter.CurrencyAmountEnd, "") != 0 {
  107. _whereCondition = appendCondition(_whereCondition, "currency = (?)")
  108. _whereParams = append(_whereParams, _filter.Currency)
  109. _whereCondition = appendCondition(_whereCondition, "price >= (?)")
  110. _whereParams = append(_whereParams, _filter.CurrencyAmountStart)
  111. _whereCondition = appendCondition(_whereCondition, "price < (?)")
  112. _whereParams = append(_whereParams, _filter.CurrencyAmountEnd)
  113. }
  114. if strings.Compare(_filter.SaleStatus, "") != 0 {
  115. _whereCondition = appendCondition(_whereCondition, "status = (?)")
  116. _whereParams = append(_whereParams, _filter.SaleStatus)
  117. }
  118. if len(_whereParams) != 0 {
  119. return "Sale", _whereCondition, _whereParams
  120. } else {
  121. return "Sale", "", nil
  122. }
  123. }
  124. // data extractor
  125. func getAddressByUserID(db *gorm.DB, id uint64) (error, string) {
  126. var _user models.User
  127. var ids []uint64
  128. ids = append(ids, id)
  129. err := db.Model(&_user).Preload("UserWallet").Scopes(filterByID(ids)).Find(&_user).Error
  130. if errors.Is(err, gorm.ErrRecordNotFound) {
  131. fmt.Println(err.Error())
  132. return err, ""
  133. }
  134. return nil, _user.UserWallet.Address
  135. }
  136. func Filtering(db *gorm.DB, filter common.Filter) *gorm.DB {
  137. db = db.Offset(filter.Offset)
  138. db = db.Limit(filter.Limit)
  139. if filter.Category != "" {
  140. }
  141. if filter.CollectionName != "" {
  142. db = db.Where("collection_profile.name = (?)", filter.CollectionName)
  143. }
  144. if filter.Currency != "" && filter.CurrencyAmountEnd != "" && filter.CurrencyAmountStart != "" {
  145. db = db.Where("(token.lastest_currency = (?) AND token.lastest_price >= (?) AND token.lastest_price <= (?)) OR (sale.currency = (?) AND if (sale.sale_type = 'fixed' , sale.price >= (?) AND sale.price <=(?) , if( sale.sale_type ='auction' or sale.sale_type = 'time' , (sale.start_price >= (?) AND sale.start_price <= (?)) or( bid_log.price >= (?) AND bid_log.price <=(?)), null)))",
  146. filter.Currency,
  147. filter.CurrencyAmountStart,
  148. filter.CurrencyAmountEnd,
  149. filter.Currency,
  150. filter.CurrencyAmountStart,
  151. filter.CurrencyAmountEnd,
  152. filter.CurrencyAmountStart,
  153. filter.CurrencyAmountEnd,
  154. filter.CurrencyAmountStart,
  155. filter.CurrencyAmountEnd,
  156. )
  157. }
  158. if filter.Network != "" {
  159. db = db.Where("collection.network in (?)", filter.Network)
  160. }
  161. if filter.SaleStatus != "" {
  162. db = db.Where("sale.status = (?)", filter.SaleStatus)
  163. }
  164. if filter.SaleType != "" {
  165. db = db.Where("sale.sale_type = (?)", filter.SaleType)
  166. }
  167. return db
  168. }
  169. func GetActivityItemQuery(db *gorm.DB) *gorm.DB {
  170. return db.Table("log_relation").
  171. Select(`
  172. log_relation.id as log_relation_id,
  173. token.content_url as token_content_url,
  174. token.name as token_name,
  175. log.type as log_type,
  176. log.sale_uid,
  177. log.to_address,
  178. log.from_address,
  179. sale.currency,
  180. sale.id as sale_id,
  181. token.id as token_id,
  182. log.price,
  183. log.is_cancel,
  184. log.tx,
  185. log_relation.created_at,
  186. from_user_info.thumbnail_image as from_user_profile,
  187. from_user_info.name as from_user_name,
  188. from_user_info.is_artist as from_user_is_artist,
  189. to_user_info.thumbnail_image as to_user_profile_user,
  190. to_user_info.name as to_user_name,
  191. to_user_info.is_artist as to_user_is_artist
  192. `).
  193. Joins("left join log on log_relation.id = log.log_relation_id").
  194. Joins("left join (select user_profile.*, user_wallet.address, if (artist_profile.id is null, false, true ) as is_artist from user left join user_profile on user.id = user_profile.user_id left join user_wallet on user.id = user_wallet.user_id left join artist_profile on user_profile.user_id = artist_profile.user_id) as from_user_info on log.from_address = from_user_info.address").
  195. Joins("left join (select user_profile.*, user_wallet.address, if (artist_profile.id is null, false, true ) as is_artist from user left join user_profile on user.id = user_profile.user_id left join user_wallet on user.id = user_wallet.user_id left join artist_profile on user_profile.user_id = artist_profile.user_id) as to_user_info on log.to_address = to_user_info.address").
  196. Joins("left join collection_profile on log_relation.collection_id = collection_profile.collection_id").
  197. Joins("left join token on log_relation.token_id = token.id").
  198. Joins("left join sale on log_relation.sale_id = sale.id").
  199. Order("log_relation.created_at desc").
  200. Where("log_relation.deleted_at is null")
  201. }
  202. func GetExploreItemQuery(db *gorm.DB, _filter common.Filter, authUserID uint64) *gorm.DB {
  203. query := Filtering(db, _filter)
  204. return query.Table("token").Select(`
  205. collection_profile.collection_id,
  206. collection_profile.thumbnail_image as collection_thumbnail_image,
  207. collection_profile.name as collection_name,
  208. collection.is_official as is_official_collection,
  209. user_info.user_id,
  210. user_info.thumbnail_image as owner_thumbnail_image,
  211. user_info.name as owner_name,
  212. user_info.is_artist,
  213. token.id as token_id,
  214. token.content_url,
  215. token.name as content_title,
  216. (select count(*) bid_log where bid_log.sale_id = sale_id) as bid_count,
  217. token.lastest_price,
  218. token.lastest_currency,
  219. token.index as item_index,
  220. token.total_count,
  221. user_like.is_like,
  222. sale.sale_type as sale_type,
  223. if(sale.status != "ongoing", null, sale.id) as sale_id,
  224. if(sale.status != "ongoing", null, sale.currency) as current_currency,
  225. if(sale.status != "ongoing", null, sale.price) as fixed_price,
  226. if(sale.status != "ongoing", null, sale.start_price) as start_price,
  227. if(sale.status != "ongoing", null, sale.end_at) as end_at,
  228. if(sale.status != "ongoing", null, bid_log.price) as current_price,
  229. if(sale.status != "ongoing", null, sale.created_at) as created_at ,
  230. sale.status as sale_status,
  231. token.like_count as likes
  232. `).
  233. Joins("left join (select * from sale where id in (select max(id) from sale group by token_id)) as sale on token.id = sale.token_id").
  234. Joins("left join collection on token.collection_id = collection.id").
  235. Joins("left join (select user_profile.*, user_wallet.address as address, if (artist_profile.id is not null, true, false) as is_artist from user left join user_profile as user_profile on user.id = user_profile.user_id left join user_wallet on user_wallet.user_id = user.id left join artist_profile on artist_profile.user_id = user.id) as user_info on token.owner_address = user_info.address").
  236. Joins("left join collection_profile on token.collection_id = collection_profile.collection_id").
  237. Joins("left join (select * from bid_log where is_cancel != true order by price desc ) as bid_log on sale.id = bid_log.sale_id").
  238. Joins("left join (select * from user_like where user_like.user_id =(?))as user_like on user_like.token_id = token.id", authUserID).
  239. Where("token.deleted_at is null")
  240. }
  241. func getCollectionQuery(db *gorm.DB, _filter common.Filter) *gorm.DB {
  242. query := Filtering(db, _filter)
  243. return query.Table("collection").Select(`
  244. collection.id as collection_id,
  245. collection_profile.cover_image,
  246. collection_profile.thumbnail_image,
  247. collection.is_official,
  248. collection_profile.name as collection_name,
  249. user_info.user_id,
  250. user_info.name as owner_name,
  251. collection_info.total_volume,
  252. collection_info.total_item
  253. `).
  254. Joins("left join collection_profile on collection.id = collection_profile.collection_id").
  255. Joins("left join (select user_profile.* , user_wallet.address from user_wallet left join user_profile on user_wallet.user_id = user_profile.user_id) as user_info on collection.owner_address = user_info.address").
  256. Joins("left join (select sum(lastest_price) as total_volume, count(*) as total_item , collection_id from token group by collection_id, lastest_currency) as collection_info on collection.id = collection_info.collection_id").
  257. Where("collection.deleted_at is null")
  258. }
  259. func GetSuggestionSearch(db *gorm.DB, _filter common.Filter) *gorm.DB {
  260. query := Filtering(db, _filter)
  261. return query.Table("collection").Select(`
  262. collection.id as collection_id,
  263. collection_profile.cover_image,
  264. collection_profile.thumbnail_image,
  265. collection.is_official,
  266. collection_profile.name as collection_name,
  267. user_info.user_id,
  268. user_info.name as owner_name,
  269. collection_info.total_volume,
  270. collection_info.total_item
  271. `).
  272. Joins("left join collection_profile on collection.id = collection_profile.collection_id").
  273. Joins("left join (select user_profile.* , user_wallet.address from user_wallet left join user_profile on user_wallet.user_id = user_profile.user_id) as user_info on collection.owner_address = user_info.address").
  274. Joins("left join (select sum(lastest_price) as total_volume, count(*) as total_item , collection_id from token group by collection_id, lastest_currency) as collection_info on collection.id = collection_info.collection_id").
  275. Where("collection.deleted_at is null")
  276. }
  277. func GetArtists(db *gorm.DB, limit int) []common.ArtistItem {
  278. _res := []common.ArtistItem{}
  279. e := db.Table("artist_profile").Select(`
  280. artist_profile.id,
  281. artist_profile.user_id,
  282. user_profile.name as team,
  283. artist_profile.category,
  284. user_profile.thumbnail_image`,
  285. ).
  286. Joins("join user_profile on artist_profile.user_id = user_profile.user_id").
  287. Joins("join user_wallet on user_wallet.user_id = user_profile.user_id").
  288. // Joins("join user on user_wallet.user_id = user.id").
  289. Limit(limit).
  290. Find(&_res).Error
  291. if e != nil {
  292. return nil
  293. }
  294. return _res
  295. }
  296. func GetRankingSaleByArtist(db *gorm.DB, limit int, timeLimit ...time.Time) []common.TopSales {
  297. _res := []common.TopSales{}
  298. // create View unpiviot price
  299. db.Exec(
  300. `Create OR REPLACE VIEW currency_view as
  301. SELECT 1 as type, (SELECT sum(eth) FROM currency_price) as symbol
  302. UNION
  303. SELECT 2 as type, (SELECT sum(mf) FROM currency_price) as symbol
  304. UNION
  305. SELECT 3 as type, (SELECT sum(mr) FROM currency_price) as symbol`,
  306. )
  307. db.Exec(`
  308. Create OR REPLACE VIEW sales_view as
  309. SELECT sale.id, sale.token_id as token_id, currency, price, currency_view.symbol, price * currency_view.symbol as amount from sale
  310. JOIN currency_view on currency_view.type = sale.currency
  311. ORDER BY price * currency_view.symbol DESC`,
  312. )
  313. query := db.Table("sales_view").
  314. Select("SUM(sales_view.amount) as amount_sales, user_profile.user_id, user_profile.cover_image, user_profile.thumbnail_image, user_profile.name, artist_profile.category").
  315. Joins("inner join sale on sale.id = sales_view.id")
  316. if len(timeLimit) >= 2 {
  317. start := timeLimit[0]
  318. end := timeLimit[1]
  319. query.Where("sale.created_at BETWEEN {ts ?} AND {ts ?}", start, end)
  320. }
  321. query.Joins("inner join token on token.id = sales_view.token_id").
  322. Joins("inner join user_wallet on token.creator_address = user_wallet.address").
  323. Joins("inner join collection on token.collection_id = collection.id").
  324. Joins("inner join collection_profile ON collection_profile.collection_id = collection.id").
  325. Joins("inner join user_profile ON user_profile.user_id = user_wallet.user_id").
  326. Joins("left join artist_profile ON artist_profile.user_id = user_profile.user_id").
  327. Group("user_profile.user_id, user_profile.thumbnail_image, user_profile.cover_image, artist_profile.team, user_profile.name, artist_profile.category").
  328. Having("amount_sales != 0").
  329. Order("amount_sales DESC")
  330. query.Limit(limit).Find(&_res)
  331. return _res
  332. }
  333. func GetHintExploreQuery(db *gorm.DB, _filter common.Filter, key string) *gorm.DB {
  334. query := Filtering(db, _filter)
  335. return query.Table("token").
  336. Select("name").
  337. Joins("join user_like on user_like.token_id = token.id").
  338. Group("user_like.token_id").
  339. Having("LOWER(token.name) like LOWER(?)", key).
  340. Order("COUNT(*)")
  341. }