up_charge.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/job/main/growup/model/income"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _insertUpChargeSQL = "INSERT INTO %s (mid,inc_charge,total_charge,date) VALUES %s ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge),total_charge=VALUES(total_charge)"
  10. _upChargeSQL = "SELECT id,mid,inc_charge,total_charge,date FROM %s WHERE date=? AND id > ? ORDER BY id LIMIT ?"
  11. )
  12. // GetUpCharges get up charges
  13. func (d *Dao) GetUpCharges(c context.Context, table string, date string, offset, limit int64) (last int64, charges map[int64]*model.UpCharge, err error) {
  14. rows, err := d.db.Query(c, fmt.Sprintf(_upChargeSQL, table), date, offset, limit)
  15. if err != nil {
  16. log.Error("d.db.Query GetUpCharges error(%v)", err)
  17. return
  18. }
  19. charges = make(map[int64]*model.UpCharge)
  20. defer rows.Close()
  21. for rows.Next() {
  22. c := &model.UpCharge{}
  23. err = rows.Scan(&last, &c.MID, &c.IncCharge, &c.TotalCharge, &c.Date)
  24. if err != nil {
  25. log.Error("rows scan error(%v)", err)
  26. return
  27. }
  28. charges[c.MID] = c
  29. }
  30. return
  31. }
  32. // InsertUpCharge batch insert up charge
  33. func (d *Dao) InsertUpCharge(c context.Context, table string, values string) (rows int64, err error) {
  34. res, err := d.db.Exec(c, fmt.Sprintf(_insertUpChargeSQL, table, values))
  35. if err != nil {
  36. log.Error("d.db.Exec InsertUpCharge error(%v)", err)
  37. return
  38. }
  39. return res.RowsAffected()
  40. }