budget.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/growup/model"
  6. "go-common/library/log"
  7. )
  8. const (
  9. // get expense_daily_info total_expense
  10. _expenseDailyTotalSQL = "SELECT total_expense,ctype FROM expense_daily_info WHERE date = '%s'"
  11. // insert
  12. _insertDailyExpenseSQL = "INSERT INTO expense_daily_info(day_expense, up_count, av_count, up_avg_expense, av_avg_expense, total_expense, date, ctype) VALUES (%d,%d,%d,%d,%d,%d,'%s',%d) ON DUPLICATE KEY UPDATE day_expense = VALUES(day_expense), up_count = VALUES(up_count), av_count = VALUES(av_count), up_avg_expense = values(up_avg_expense), av_avg_expense = values(av_avg_expense), total_expense = values(total_expense), date = values(date)"
  13. _insertMonthlyExpenseSQL = "INSERT INTO expense_monthly_info(month_expense, up_count, av_count, up_avg_expense, av_avg_expense, total_expense, date, month, ctype) VALUES (%d,%d,%d,%d,%d,%d,'%s','%s',%d) ON DUPLICATE KEY UPDATE month_expense = VALUES(month_expense), up_count = VALUES(up_count), av_count = VALUES(av_count), up_avg_expense = values(up_avg_expense), av_avg_expense = values(av_avg_expense), total_expense = values(total_expense), date = values(date)"
  14. )
  15. // GetTotalExpenseByDate get expense_daily_info by date
  16. func (d *Dao) GetTotalExpenseByDate(c context.Context, date string) (expense map[int64]int64, err error) {
  17. expense = make(map[int64]int64)
  18. rows, err := d.db.Query(c, fmt.Sprintf(_expenseDailyTotalSQL, date))
  19. if err != nil {
  20. log.Error("GetTotalExpenseByDate d.db.Query error(%v)", err)
  21. return
  22. }
  23. defer rows.Close()
  24. for rows.Next() {
  25. var ctype, totalExpense int64
  26. err = rows.Scan(&totalExpense, &ctype)
  27. if err != nil {
  28. log.Error("GetTotalExpenseByDate rows.Scan error(%v)", err)
  29. return
  30. }
  31. expense[ctype] = totalExpense
  32. }
  33. err = rows.Err()
  34. return
  35. }
  36. // InsertDailyExpense insert expense_daily_info
  37. func (d *Dao) InsertDailyExpense(c context.Context, e *model.BudgetExpense) (rows int64, err error) {
  38. res, err := d.db.Exec(c, fmt.Sprintf(_insertDailyExpenseSQL, e.Expense, e.UpCount, e.AvCount, e.UpAvgExpense, e.AvAvgExpense, e.TotalExpense, e.Date, e.CType))
  39. if err != nil {
  40. return
  41. }
  42. return res.RowsAffected()
  43. }
  44. // InsertMonthlyExpense insert expense_monthly_info
  45. func (d *Dao) InsertMonthlyExpense(c context.Context, e *model.BudgetExpense) (rows int64, err error) {
  46. res, err := d.db.Exec(c, fmt.Sprintf(_insertMonthlyExpenseSQL, e.Expense, e.UpCount, e.AvCount, e.UpAvgExpense, e.AvAvgExpense, e.TotalExpense, e.Date, e.Date.Format("2006-01"), e.CType))
  47. if err != nil {
  48. return
  49. }
  50. return res.RowsAffected()
  51. }