column_income.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/growup/model"
  6. "go-common/library/log"
  7. "go-common/library/xstr"
  8. )
  9. const (
  10. // select
  11. _columnIncomeByMIDSQL = "SELECT aid, income, total_income, date FROM column_income WHERE mid = ? AND date >= ? AND date <= ?"
  12. _columnIncomeByAvIDSQL = "SELECT income, date FROM column_income WHERE aid = ? AND date <= ?"
  13. _columnStatisTitleSQL = "SELECT aid, title FROM column_income_statis WHERE aid in (%s)"
  14. )
  15. // ListColumnIncome list column_income by mid
  16. func (d *Dao) ListColumnIncome(c context.Context, mid int64, startTime, endTime string) (columns []*model.ArchiveIncome, err error) {
  17. columns = make([]*model.ArchiveIncome, 0)
  18. rows, err := d.db.Query(c, _columnIncomeByMIDSQL, mid, startTime, endTime)
  19. if err != nil {
  20. log.Error("ListColumnIncome d.db.Query error(%v)", err)
  21. return
  22. }
  23. defer rows.Close()
  24. for rows.Next() {
  25. column := &model.ArchiveIncome{}
  26. err = rows.Scan(&column.ArchiveID, &column.Income, &column.TotalIncome, &column.Date)
  27. if err != nil {
  28. log.Error("ListColumnIncome rows.Scan error(%v)", err)
  29. return
  30. }
  31. columns = append(columns, column)
  32. }
  33. err = rows.Err()
  34. return
  35. }
  36. // ListColumnIncomeByID list column_income by aid
  37. func (d *Dao) ListColumnIncomeByID(c context.Context, id int64, endTime string) (columns []*model.ArchiveIncome, err error) {
  38. columns = make([]*model.ArchiveIncome, 0)
  39. rows, err := d.db.Query(c, _columnIncomeByAvIDSQL, id, endTime)
  40. if err != nil {
  41. log.Error("ListColumnIncomeByID d.db.Query error(%v)", err)
  42. return
  43. }
  44. defer rows.Close()
  45. for rows.Next() {
  46. column := &model.ArchiveIncome{}
  47. err = rows.Scan(&column.Income, &column.Date)
  48. if err != nil {
  49. log.Error("ListColumnIncomeByID rows.Scan error(%v)", err)
  50. return
  51. }
  52. columns = append(columns, column)
  53. }
  54. err = rows.Err()
  55. return
  56. }
  57. // GetColumnTitle get column title by id
  58. func (d *Dao) GetColumnTitle(c context.Context, ids []int64) (titles map[int64]string, err error) {
  59. titles = make(map[int64]string)
  60. rows, err := d.db.Query(c, fmt.Sprintf(_columnStatisTitleSQL, xstr.JoinInts(ids)))
  61. if err != nil {
  62. log.Error("GetColumnTitle d.db.Query error(%v)", err)
  63. return
  64. }
  65. defer rows.Close()
  66. for rows.Next() {
  67. var mid int64
  68. var title string
  69. err = rows.Scan(&mid, &title)
  70. if err != nil {
  71. log.Error("GetColumnTitle rows.Scan error(%v)", err)
  72. return
  73. }
  74. titles[mid] = title
  75. }
  76. err = rows.Err()
  77. return
  78. }