av_charge.go 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package charge
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. model "go-common/app/job/main/growup/model/charge"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _layout = "2006-01-02"
  12. _avDailyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_daily_charge_%s WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
  13. _avWeeklyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_weekly_charge WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
  14. _avMonthlyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_monthly_charge WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
  15. _inAvChargeTableSQL = "INSERT INTO %s(av_id,mid,tag_id,is_original,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,upload_time) VALUES %s ON DUPLICATE KEY UPDATE danmaku_count=VALUES(danmaku_count),comment_count=VALUES(comment_count),collect_count=VALUES(collect_count),coin_count=VALUES(coin_count),share_count=VALUES(share_count),elec_pay_count=VALUES(elec_pay_count),total_play_count=VALUES(total_play_count),web_play_count=VALUES(web_play_count),app_play_count=VALUES(app_play_count),h5_play_count=VALUES(h5_play_count),lv_unknown=VALUES(lv_unknown),lv_0=VALUES(lv_0),lv_1=VALUES(lv_1),lv_2=VALUES(lv_2),lv_3=VALUES(lv_3),lv_4=VALUES(lv_4),lv_5=VALUES(lv_5),lv_6=VALUES(lv_6),v_score=VALUES(v_score),inc_charge=VALUES(inc_charge),total_charge=VALUES(total_charge)"
  16. )
  17. // IAvCharge av_charge_weekly monthly
  18. type IAvCharge func(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error)
  19. // AvDailyCharge av_daily_charge
  20. func (d *Dao) AvDailyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
  21. month := date.Month()
  22. monthStr := strconv.Itoa(int(month))
  23. if month < 10 {
  24. monthStr = "0" + monthStr
  25. }
  26. return d.GetAvCharge(c, fmt.Sprintf(_avDailyChargeSQL, monthStr), date.Format(_layout), id, limit)
  27. }
  28. // AvWeeklyCharge av_weekly_charge
  29. func (d *Dao) AvWeeklyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
  30. return d.GetAvCharge(c, _avWeeklyChargeSQL, date.Format(_layout), id, limit)
  31. }
  32. // AvMonthlyCharge av_monthly_charge
  33. func (d *Dao) AvMonthlyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
  34. return d.GetAvCharge(c, _avMonthlyChargeSQL, date.Format(_layout), id, limit)
  35. }
  36. // GetAvCharge get av_charge
  37. func (d *Dao) GetAvCharge(c context.Context, sql string, date string, id int64, limit int) (data []*model.AvCharge, err error) {
  38. rows, err := d.db.Query(c, sql, id, date, limit)
  39. if err != nil {
  40. log.Error("d.Query av_charge(%s) error(%v)", sql, err)
  41. return
  42. }
  43. defer rows.Close()
  44. for rows.Next() {
  45. adc := &model.AvCharge{}
  46. err = rows.Scan(&adc.ID, &adc.AvID, &adc.MID, &adc.TagID, &adc.IsOriginal, &adc.UploadTime, &adc.DanmakuCount, &adc.CommentCount, &adc.CollectCount, &adc.CoinCount, &adc.ShareCount, &adc.ElecPayCount, &adc.TotalPlayCount, &adc.WebPlayCount, &adc.AppPlayCount, &adc.H5PlayCount, &adc.LvUnknown, &adc.Lv0, &adc.Lv1, &adc.Lv2, &adc.Lv3, &adc.Lv4, &adc.Lv5, &adc.Lv6, &adc.VScore, &adc.IncCharge, &adc.TotalCharge, &adc.Date, &adc.IsDeleted, &adc.CTime, &adc.MTime)
  47. if err != nil {
  48. log.Error("rows scan error(%v)", err)
  49. return
  50. }
  51. data = append(data, adc)
  52. }
  53. return
  54. }
  55. // InsertAvChargeTable add av charge batch
  56. func (d *Dao) InsertAvChargeTable(c context.Context, vals, table string) (rows int64, err error) {
  57. if vals == "" {
  58. return
  59. }
  60. res, err := d.db.Exec(c, fmt.Sprintf(_inAvChargeTableSQL, table, vals))
  61. if err != nil {
  62. log.Error("InsertAvChargeTable(%s) tx.Exec error(%v)", table, err)
  63. return
  64. }
  65. return res.RowsAffected()
  66. }