av_income.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package income
  2. import (
  3. "bytes"
  4. "context"
  5. "strconv"
  6. incomeD "go-common/app/job/main/growup/dao/income"
  7. model "go-common/app/job/main/growup/model/income"
  8. )
  9. // AvIncomeSvr Av income service
  10. type AvIncomeSvr struct {
  11. batchSize int
  12. dao *incomeD.Dao
  13. }
  14. // NewAvIncomeSvr new av income service
  15. func NewAvIncomeSvr(dao *incomeD.Dao, batchSize int) (svr *AvIncomeSvr) {
  16. return &AvIncomeSvr{
  17. batchSize: batchSize,
  18. dao: dao,
  19. }
  20. }
  21. // BatchInsertAvIncome batch insert av income
  22. func (p *AvIncomeSvr) BatchInsertAvIncome(c context.Context, am map[int64][]*model.AvIncome) (err error) {
  23. var (
  24. buff = make([]*model.AvIncome, p.batchSize)
  25. buffEnd = 0
  26. )
  27. for _, as := range am {
  28. for _, a := range as {
  29. buff[buffEnd] = a
  30. buffEnd++
  31. if buffEnd >= p.batchSize {
  32. values := avIncomeValues(buff[:buffEnd])
  33. buffEnd = 0
  34. _, err = p.dao.InsertAvIncome(c, values)
  35. if err != nil {
  36. return
  37. }
  38. }
  39. }
  40. }
  41. if buffEnd > 0 {
  42. values := avIncomeValues(buff[:buffEnd])
  43. buffEnd = 0
  44. _, err = p.dao.InsertAvIncome(c, values)
  45. }
  46. return
  47. }
  48. func avIncomeValues(as []*model.AvIncome) (values string) {
  49. var buf bytes.Buffer
  50. for _, a := range as {
  51. buf.WriteString("(")
  52. buf.WriteString(strconv.FormatInt(a.AvID, 10))
  53. buf.WriteByte(',')
  54. buf.WriteString(strconv.FormatInt(a.MID, 10))
  55. buf.WriteByte(',')
  56. buf.WriteString(strconv.FormatInt(a.TagID, 10))
  57. buf.WriteByte(',')
  58. buf.WriteString(strconv.Itoa(a.IsOriginal))
  59. buf.WriteByte(',')
  60. buf.WriteString("'" + a.UploadTime.Time().Format(_layoutSec) + "'")
  61. buf.WriteByte(',')
  62. buf.WriteString(strconv.FormatInt(a.PlayCount, 10))
  63. buf.WriteByte(',')
  64. buf.WriteString(strconv.FormatInt(a.TotalIncome, 10))
  65. buf.WriteByte(',')
  66. buf.WriteString(strconv.FormatInt(a.Income, 10))
  67. buf.WriteByte(',')
  68. buf.WriteString(strconv.FormatInt(a.TaxMoney, 10))
  69. buf.WriteByte(',')
  70. buf.WriteString("'" + a.Date.Time().Format(_layout) + "'")
  71. buf.WriteByte(',')
  72. buf.WriteString(strconv.FormatInt(a.BaseIncome, 10))
  73. buf.WriteString(")")
  74. buf.WriteByte(',')
  75. }
  76. if buf.Len() > 0 {
  77. buf.Truncate(buf.Len() - 1)
  78. }
  79. values = buf.String()
  80. buf.Reset()
  81. return
  82. }