blacklist.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package service
  2. import (
  3. "fmt"
  4. "time"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/log"
  7. "go-common/library/xstr"
  8. )
  9. // QueryBlacklist query blacklist
  10. func (s *Service) QueryBlacklist(fromTime, toTime int64, ctype, reason int, mid int64, nickname string, aid int64, from, limit int, sort string) (total int, blacklist []*model.Blacklist, err error) {
  11. blacklistQuery := buildBlacklistQuery(fromTime, toTime, ctype, reason, mid, nickname, aid)
  12. blacklist, total, err = s.dao.ListBlacklist(blacklistQuery, from, limit, sort)
  13. if err != nil {
  14. log.Error("s.dao.ListBlacklist error(%v)", err)
  15. return
  16. }
  17. if total == 0 {
  18. return
  19. }
  20. avIDs := make([]int64, len(blacklist))
  21. for i := 0; i < len(blacklist); i++ {
  22. avIDs[i] = blacklist[i].AvID
  23. }
  24. // get av total income
  25. avIncomeMap, err := s.getAvIncomeStatis(avIDs)
  26. if err != nil {
  27. log.Error("s.getAvIncomeStatis error(%v)", err)
  28. return
  29. }
  30. for i := 0; i < len(blacklist); i++ {
  31. blacklist[i].Income = avIncomeMap[blacklist[i].AvID]
  32. }
  33. return
  34. }
  35. func buildBlacklistQuery(fromTime, toTime int64, ctype, reason int, mid int64, nickname string, aid int64) (query string) {
  36. query += fmt.Sprintf("ctime >= '%s' AND ctime <= '%s'", time.Unix(fromTime, 0).Format("2006-01-02"), time.Unix(toTime, 0).Format("2006-01-02"))
  37. query += " AND "
  38. if aid <= 0 && mid <= 0 && nickname == "" {
  39. query += "has_signed = 1"
  40. query += " AND "
  41. }
  42. if mid > 0 {
  43. query += fmt.Sprintf("mid = %d", mid)
  44. query += " AND "
  45. }
  46. if nickname != "" {
  47. query += fmt.Sprintf("nickname = \"%s\"", nickname)
  48. query += " AND "
  49. }
  50. if aid > 0 {
  51. query += fmt.Sprintf("av_id = %d", aid)
  52. query += " AND "
  53. }
  54. if ctype != 4 {
  55. query += fmt.Sprintf("ctype = %d", ctype)
  56. query += " AND "
  57. }
  58. if reason > 0 {
  59. query += fmt.Sprintf("reason = %d", reason)
  60. query += " AND "
  61. }
  62. query += "is_delete = 0"
  63. return
  64. }
  65. func (s *Service) getAvIncomeStatis(avIDs []int64) (avIncomeMap map[int64]int64, err error) {
  66. avIncomeMap = make(map[int64]int64)
  67. query := fmt.Sprintf("av_id IN (%s) AND is_deleted = 0", xstr.JoinInts(avIDs))
  68. avIncomes, err := s.dao.GetAvIncomeStatis(query)
  69. if err != nil {
  70. log.Error("s.dao.GetAvIncomeStatis error(%v)", err)
  71. return
  72. }
  73. for _, avIncome := range avIncomes {
  74. avIncomeMap[avIncome.AvID] = avIncome.TotalIncome
  75. }
  76. return
  77. }
  78. // ExportBlacklist blacklist export csv
  79. func (s *Service) ExportBlacklist(fromTime, toTime int64, ctype, reason int, mid int64, nickname string, aid int64, from, limit int, sort string) (res []byte, err error) {
  80. _, blacklist, err := s.QueryBlacklist(fromTime, toTime, ctype, reason, mid, nickname, aid, from, limit, sort)
  81. if err != nil {
  82. log.Error("s.QueryBlacklist error(%v)", err)
  83. return
  84. }
  85. records := formatBlacklist(blacklist)
  86. res, err = FormatCSV(records)
  87. if err != nil {
  88. log.Error("FormatCSV error(%v)", err)
  89. }
  90. return
  91. }
  92. // RecoverBlacklist recover av from blacklist
  93. func (s *Service) RecoverBlacklist(aID int64, ctype int) (err error) {
  94. update := map[string]interface{}{
  95. "is_delete": 1,
  96. }
  97. err = s.dao.UpdateBlacklist(aID, ctype, update)
  98. if err != nil {
  99. log.Error("s.dao.UpdateBlacklist error(%v)", err)
  100. return
  101. }
  102. return
  103. }