up_income.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/admin/main/growup/model/income"
  6. "go-common/library/log"
  7. )
  8. const (
  9. // select
  10. _upIncomeTableSQL = "SELECT id,mid,%s,date FROM %s WHERE id > ? AND %s LIMIT ?"
  11. _upIncomeTableSortSQL = "SELECT mid,av_count,column_count,bgm_count,%s,date FROM %s WHERE %s ORDER BY date desc,%s desc LIMIT ?,? "
  12. _upIncomeCountSQL = "SELECT count(*) FROM %s WHERE %s"
  13. _upDailyStatisSQL = "SELECT ups,income,cdate FROM %s WHERE cdate >= '%s' AND cdate <= '%s'"
  14. )
  15. // UpIncomeCount count
  16. func (d *Dao) UpIncomeCount(c context.Context, table, query string) (count int, err error) {
  17. if table == "" || query == "" {
  18. return 0, fmt.Errorf("error args table(%s), query(%s)", table, query)
  19. }
  20. err = d.db.QueryRow(c, fmt.Sprintf(_upIncomeCountSQL, table, query)).Scan(&count)
  21. return
  22. }
  23. // GetUpIncome get up_income_(weekly/monthly) from table and query
  24. func (d *Dao) GetUpIncome(c context.Context, table, incomeType, query string, id int64, limit int) (upIncome []*model.UpIncome, err error) {
  25. upIncome = make([]*model.UpIncome, 0)
  26. if table == "" || query == "" {
  27. return nil, fmt.Errorf("error args table(%s), query(%s)", table, query)
  28. }
  29. rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeTableSQL, incomeType, table, query), id, limit)
  30. if err != nil {
  31. log.Error("GetUpIncome d.db.Query error(%v)", err)
  32. return
  33. }
  34. defer rows.Close()
  35. for rows.Next() {
  36. list := &model.UpIncome{}
  37. err = rows.Scan(&list.ID, &list.MID, &list.Income, &list.Date)
  38. if err != nil {
  39. log.Error("GetUpIncome rows scan error(%v)", err)
  40. return
  41. }
  42. upIncome = append(upIncome, list)
  43. }
  44. err = rows.Err()
  45. return
  46. }
  47. // GetUpIncomeBySort get up_income by query
  48. func (d *Dao) GetUpIncomeBySort(c context.Context, table, typeField, sort, query string, from, limit int) (upIncome []*model.UpIncome, err error) {
  49. upIncome = make([]*model.UpIncome, 0)
  50. if table == "" || query == "" || typeField == "" {
  51. return nil, fmt.Errorf("error args table(%s), typeField(%s),query(%s)", table, typeField, query)
  52. }
  53. rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeTableSortSQL, typeField, table, query, sort), from, limit)
  54. if err != nil {
  55. log.Error("GetUpIncomeBySort d.db.Query error(%v)", err)
  56. return
  57. }
  58. defer rows.Close()
  59. for rows.Next() {
  60. up := &model.UpIncome{}
  61. err = rows.Scan(&up.MID, &up.AvCount, &up.ColumnCount, &up.BgmCount, &up.Income, &up.TaxMoney, &up.BaseIncome, &up.TotalIncome, &up.Date)
  62. if err != nil {
  63. log.Error("GetUpIncome rows scan error(%v)", err)
  64. return
  65. }
  66. upIncome = append(upIncome, up)
  67. }
  68. err = rows.Err()
  69. return
  70. }
  71. // GetUpDailyStatis get up income daily statis
  72. func (d *Dao) GetUpDailyStatis(c context.Context, table, fromTime, toTime string) (s []*model.UpDailyStatis, err error) {
  73. if table == "" {
  74. return nil, fmt.Errorf("error args table(%s)", table)
  75. }
  76. s = make([]*model.UpDailyStatis, 0)
  77. rows, err := d.db.Query(c, fmt.Sprintf(_upDailyStatisSQL, table, fromTime, toTime))
  78. if err != nil {
  79. log.Error("GetUpDailyStatis d.db.Query error(%v)", err)
  80. return
  81. }
  82. defer rows.Close()
  83. for rows.Next() {
  84. list := &model.UpDailyStatis{}
  85. err = rows.Scan(&list.Ups, &list.Income, &list.Date)
  86. if err != nil {
  87. log.Error("GetUpIncome rows scan error(%v)", err)
  88. return
  89. }
  90. s = append(s, list)
  91. }
  92. err = rows.Err()
  93. return
  94. }