av_breach.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/growup/model"
  6. "go-common/library/log"
  7. xtime "go-common/library/time"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. // select
  12. _avBreachSQL = "SELECT av_id, mid, cdate, money, ctype, reason FROM av_breach_record WHERE mid = ? AND cdate >= ? AND cdate <= ?"
  13. _avBreachByAvIDSQL = "SELECT av_id, mid, cdate, money, reason FROM av_breach_record WHERE av_id in (%s) AND ctype = ?"
  14. _avBreachByTypeSQL = "SELECT cdate, money FROM av_breach_record WHERE mid=? AND cdate >= ? AND cdate <= ? AND ctype in (%s)"
  15. )
  16. // ListAvBreach list av_breach_record by mid
  17. func (d *Dao) ListAvBreach(c context.Context, mid int64, startTime, endTime string) (records []*model.AvBreach, err error) {
  18. records = make([]*model.AvBreach, 0)
  19. rows, err := d.db.Query(c, _avBreachSQL, mid, startTime, endTime)
  20. if err != nil {
  21. log.Error("ListAvBreach d.db.Query error(%v)", err)
  22. return
  23. }
  24. defer rows.Close()
  25. for rows.Next() {
  26. r := &model.AvBreach{}
  27. err = rows.Scan(&r.AvID, &r.MID, &r.CDate, &r.Money, &r.CType, &r.Reason)
  28. if err != nil {
  29. log.Error("ListAvBreach rows.Scan error(%v)", err)
  30. return
  31. }
  32. records = append(records, r)
  33. }
  34. err = rows.Err()
  35. return
  36. }
  37. // GetAvBreachByType get av breach record
  38. func (d *Dao) GetAvBreachByType(c context.Context, mid int64, begin string, end string, typ []int64) (rs map[xtime.Time]int64, err error) {
  39. if len(typ) == 0 {
  40. typ = []int64{0, 1, 2, 3}
  41. }
  42. rows, err := d.db.Query(c, fmt.Sprintf(_avBreachByTypeSQL, xstr.JoinInts(typ)), mid, begin, end)
  43. if err != nil {
  44. log.Error("GetAvBreachByType d.db.Query error(%v)", err)
  45. return
  46. }
  47. rs = make(map[xtime.Time]int64)
  48. defer rows.Close()
  49. for rows.Next() {
  50. var cdate xtime.Time
  51. var money int64
  52. err = rows.Scan(&cdate, &money)
  53. if err != nil {
  54. log.Error("GetAvBreachByType rows.Scan error(%v)", err)
  55. return
  56. }
  57. rs[cdate] += money
  58. }
  59. return
  60. }
  61. // GetAvBreachs get av_breach map by avids
  62. func (d *Dao) GetAvBreachs(c context.Context, avs []int64, ctype int) (breachs map[int64]*model.AvBreach, err error) {
  63. breachs = make(map[int64]*model.AvBreach)
  64. rows, err := d.db.Query(c, fmt.Sprintf(_avBreachByAvIDSQL, xstr.JoinInts(avs)), ctype)
  65. if err != nil {
  66. log.Error("ListAvBreach d.db.Query error(%v)", err)
  67. return
  68. }
  69. defer rows.Close()
  70. for rows.Next() {
  71. r := &model.AvBreach{}
  72. if err = rows.Scan(&r.AvID, &r.MID, &r.CDate, &r.Money, &r.Reason); err != nil {
  73. log.Error("ListAvBreach rows.Scan error(%v)", err)
  74. return
  75. }
  76. breachs[r.AvID] = r
  77. }
  78. err = rows.Err()
  79. return
  80. }