av_breach.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/admin/main/growup/model/income"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. // insert
  12. _inAvBreachSQL = "INSERT INTO av_breach_record(av_id,mid,cdate,money,ctype,reason,upload_time) VALUES %s"
  13. // select
  14. _avBreachByMIDsSQL = "SELECT av_id, mid, cdate, money FROM av_breach_record WHERE mid in (%s) AND ctype in (%s)"
  15. _breachSQL = "SELECT av_id,mid,cdate,money,ctype,reason,upload_time FROM av_breach_record WHERE %s"
  16. _breachCountSQL = "SELECT count(*) FROM av_breach_record WHERE %s"
  17. // update
  18. _upAvBreachPreSQL = "UPDATE av_breach_pre SET state = 2 WHERE aid IN (%s) AND ctype = 0 AND cdate <= '%s'"
  19. )
  20. // BreachCount breach count
  21. func (d *Dao) BreachCount(c context.Context, query string) (total int, err error) {
  22. err = d.db.QueryRow(c, fmt.Sprintf(_breachCountSQL, query)).Scan(&total)
  23. if err == sql.ErrNoRows {
  24. err = nil
  25. }
  26. return
  27. }
  28. // ListArchiveBreach list av_breach_record by query
  29. func (d *Dao) ListArchiveBreach(c context.Context, query string) (breachs []*model.AvBreach, err error) {
  30. breachs = make([]*model.AvBreach, 0)
  31. rows, err := d.db.Query(c, fmt.Sprintf(_breachSQL, query))
  32. if err != nil {
  33. log.Error("ListArchiveBreach d.db.Query error(%v)", err)
  34. return
  35. }
  36. defer rows.Close()
  37. for rows.Next() {
  38. b := &model.AvBreach{}
  39. err = rows.Scan(&b.AvID, &b.MID, &b.CDate, &b.Money, &b.CType, &b.Reason, &b.UploadTime)
  40. if err != nil {
  41. log.Error("ListArchiveBreach rows.Scan error(%v)", err)
  42. return
  43. }
  44. breachs = append(breachs, b)
  45. }
  46. err = rows.Err()
  47. return
  48. }
  49. // TxInsertAvBreach insert av_breach_record
  50. func (d *Dao) TxInsertAvBreach(tx *sql.Tx, val string) (rows int64, err error) {
  51. if val == "" {
  52. return
  53. }
  54. res, err := tx.Exec(fmt.Sprintf(_inAvBreachSQL, val))
  55. if err != nil {
  56. return
  57. }
  58. return res.RowsAffected()
  59. }
  60. // GetAvBreachByMIDs get av_breach_record by mids
  61. func (d *Dao) GetAvBreachByMIDs(c context.Context, mids []int64, types []int64) (breachs []*model.AvBreach, err error) {
  62. if len(mids) == 0 {
  63. return
  64. }
  65. rows, err := d.db.Query(c, fmt.Sprintf(_avBreachByMIDsSQL, xstr.JoinInts(mids), xstr.JoinInts(types)))
  66. if err != nil {
  67. log.Error("GetAvBreachByMIDs d.db.Query error(%v)", err)
  68. return
  69. }
  70. defer rows.Close()
  71. for rows.Next() {
  72. b := &model.AvBreach{}
  73. err = rows.Scan(&b.AvID, &b.MID, &b.CDate, &b.Money)
  74. if err != nil {
  75. log.Error("GetAvBreachByMIDs rows scan error(%v)", err)
  76. return
  77. }
  78. breachs = append(breachs, b)
  79. }
  80. err = rows.Err()
  81. return
  82. }
  83. // TxUpdateBreachPre update av_breach_pre state = 2
  84. func (d *Dao) TxUpdateBreachPre(tx *sql.Tx, aids []int64, cdate string) (rows int64, err error) {
  85. if len(aids) == 0 {
  86. return
  87. }
  88. res, err := tx.Exec(fmt.Sprintf(_upAvBreachPreSQL, xstr.JoinInts(aids), cdate))
  89. if err != nil {
  90. return
  91. }
  92. return res.RowsAffected()
  93. }