column_charge.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package charge
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. model "go-common/app/job/main/growup/model/charge"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _columnChargeSQL = "SELECT id,aid,title,mid,tag_id,words,upload_time,inc_charge,view_c,date FROM %s WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
  11. _columnStatisSQL = "SELECT id,aid,total_charge FROM column_charge_statis WHERE id > ? ORDER BY id LIMIT ?"
  12. _countCmDailySQL = "SELECT COUNT(*) FROM column_daily_charge WHERE date = '%s'"
  13. _inCmChargeTableSQL = "INSERT INTO %s(aid,mid,tag_id,inc_charge,date,upload_time) VALUES %s ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge)"
  14. _inCmStatisSQL = "INSERT INTO column_charge_statis(aid,mid,tag_id,total_charge,upload_time) VALUES %s ON DUPLICATE KEY UPDATE total_charge=VALUES(total_charge)"
  15. )
  16. // ColumnCharge get column charge by date
  17. func (d *Dao) ColumnCharge(c context.Context, date time.Time, id int64, limit int, table string) (columns []*model.Column, err error) {
  18. columns = make([]*model.Column, 0)
  19. rows, err := d.db.Query(c, fmt.Sprintf(_columnChargeSQL, table), id, date, limit)
  20. if err != nil {
  21. log.Error("ColumnCharge d.db.Query error(%v)", err)
  22. return
  23. }
  24. defer rows.Close()
  25. for rows.Next() {
  26. column := &model.Column{}
  27. err = rows.Scan(&column.ID, &column.AID, &column.Title, &column.MID, &column.TagID, &column.Words, &column.UploadTime, &column.IncCharge, &column.IncViewCount, &column.Date)
  28. if err != nil {
  29. log.Error("ColumnCharge rows.Scan error(%v)", err)
  30. return
  31. }
  32. columns = append(columns, column)
  33. }
  34. return
  35. }
  36. // CmStatis column statis
  37. func (d *Dao) CmStatis(c context.Context, id int64, limit int) (columns []*model.ColumnStatis, err error) {
  38. columns = make([]*model.ColumnStatis, 0)
  39. rows, err := d.db.Query(c, _columnStatisSQL, id, limit)
  40. if err != nil {
  41. log.Error("CmStatis d.db.Query error(%v)", err)
  42. return
  43. }
  44. defer rows.Close()
  45. for rows.Next() {
  46. column := &model.ColumnStatis{}
  47. err = rows.Scan(&column.ID, &column.AID, &column.TotalCharge)
  48. if err != nil {
  49. log.Error("CmStatis rows.Scan error(%v)", err)
  50. return
  51. }
  52. columns = append(columns, column)
  53. }
  54. return
  55. }
  56. // InsertCmChargeTable insert column charge
  57. func (d *Dao) InsertCmChargeTable(c context.Context, vals, table string) (rows int64, err error) {
  58. if vals == "" {
  59. return
  60. }
  61. res, err := d.db.Exec(c, fmt.Sprintf(_inCmChargeTableSQL, table, vals))
  62. if err != nil {
  63. log.Error("InsertCmChargeTable(%s) tx.Exec error(%v)", table, err)
  64. return
  65. }
  66. return res.RowsAffected()
  67. }
  68. // InsertCmStatisBatch insert column statis
  69. func (d *Dao) InsertCmStatisBatch(c context.Context, vals string) (rows int64, err error) {
  70. if vals == "" {
  71. return
  72. }
  73. res, err := d.db.Exec(c, fmt.Sprintf(_inCmStatisSQL, vals))
  74. if err != nil {
  75. log.Error("InsertCmStatisBatch tx.Exec error(%v)", err)
  76. return
  77. }
  78. return res.RowsAffected()
  79. }
  80. // CountCmDailyCharge get column_daily_charge count
  81. func (d *Dao) CountCmDailyCharge(c context.Context, date string) (count int64, err error) {
  82. err = d.db.QueryRow(c, fmt.Sprintf(_countCmDailySQL, date)).Scan(&count)
  83. return
  84. }