expense_info.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/growup/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. // select
  11. _getAllDayExpenseSQL = "SELECT day_expense, up_count, av_count, up_avg_expense, av_avg_expense, total_expense, date FROM expense_daily_info WHERE date >= ? AND ctype = ? ORDER BY date DESC limit ?,?"
  12. _getAllMonthExpenseSQL = "SELECT month_expense, up_count, av_count, up_avg_expense, av_avg_expense, total_expense, date, month FROM expense_monthly_info WHERE month <= ? AND month >=? AND ctype = ? ORDER BY month DESC LIMIT ?,?"
  13. _getDayTotalExpenseSQL = "SELECT total_expense FROM expense_daily_info WHERE date = ? AND ctype = ?"
  14. _getLatelyExpenseDateSQL = "SELECT date FROM expense_%s_info WHERE ctype = ? ORDER BY date DESC LIMIT 1"
  15. // count(*)
  16. _expenseDayCountSQL = "SELECT count(*) FROM expense_daily_info WHERE date >= ? AND ctype = ?"
  17. _expenseMonthCountSQL = "SELECT count(*) FROM expense_monthly_info WHERE month <= ? AND month >= ? AND ctype = ?"
  18. )
  19. // GetDayExpenseCount get expense_daily_info count
  20. func (d *Dao) GetDayExpenseCount(c context.Context, beginDate time.Time, ctype int) (total int, err error) {
  21. err = d.rddb.QueryRow(c, _expenseDayCountSQL, beginDate, ctype).Scan(&total)
  22. return
  23. }
  24. // GetAllDayExpenseInfo get year all day expense.
  25. func (d *Dao) GetAllDayExpenseInfo(c context.Context, beginDate time.Time, ctype, from, limit int) (infos []*model.BudgetDayStatistics, err error) {
  26. rows, err := d.rddb.Query(c, _getAllDayExpenseSQL, beginDate, ctype, from, limit)
  27. if err != nil {
  28. log.Error("dao.GetAllDayExpenseInfo query error(%v)", err)
  29. return
  30. }
  31. defer rows.Close()
  32. for rows.Next() {
  33. a := &model.BudgetDayStatistics{}
  34. if err = rows.Scan(&a.DayExpense, &a.UpCount, &a.AvCount, &a.UpAvgExpense, &a.AvAvgExpense, &a.TotalExpense, &a.Date); err != nil {
  35. log.Error("dao.GetAllDayExpenseInfo scan error(%v)", err)
  36. return
  37. }
  38. infos = append(infos, a)
  39. }
  40. err = rows.Err()
  41. return
  42. }
  43. // GetDayTotalExpenseInfo get one day total_expense.
  44. func (d *Dao) GetDayTotalExpenseInfo(c context.Context, date time.Time, ctype int) (totalExpense int64, err error) {
  45. err = d.rddb.QueryRow(c, _getDayTotalExpenseSQL, date, ctype).Scan(&totalExpense)
  46. return
  47. }
  48. // GetMonthExpenseCount get expense month count
  49. func (d *Dao) GetMonthExpenseCount(c context.Context, month, beginMonth string, ctype int) (total int, err error) {
  50. err = d.rddb.QueryRow(c, _expenseMonthCountSQL, month, beginMonth, ctype).Scan(&total)
  51. return
  52. }
  53. // GetAllMonthExpenseInfo get all month expense.
  54. func (d *Dao) GetAllMonthExpenseInfo(c context.Context, month, beginMonth string, ctype, from, limit int) (infos []*model.BudgetMonthStatistics, err error) {
  55. rows, err := d.rddb.Query(c, _getAllMonthExpenseSQL, month, beginMonth, ctype, from, limit)
  56. if err != nil {
  57. log.Error("dao.GetAllMonthExpenseInfo query error(%v)", err)
  58. return
  59. }
  60. defer rows.Close()
  61. for rows.Next() {
  62. a := &model.BudgetMonthStatistics{}
  63. if err = rows.Scan(&a.MonthExpense, &a.UpCount, &a.AvCount, &a.UpAvgExpense, &a.AvAvgExpense, &a.TotalExpense, &a.Date, &a.Month); err != nil {
  64. log.Error("dao.GetAllMonthExpenseInfo scan error(%v)", err)
  65. return
  66. }
  67. infos = append(infos, a)
  68. }
  69. err = rows.Err()
  70. return
  71. }
  72. // GetLatelyExpenseDate get lately date.
  73. func (d *Dao) GetLatelyExpenseDate(c context.Context, table string, ctype int) (date time.Time, err error) {
  74. err = d.rddb.QueryRow(c, fmt.Sprintf(_getLatelyExpenseDateSQL, table), ctype).Scan(&date)
  75. return
  76. }