couponmgr.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package coupon
  2. import (
  3. "fmt"
  4. "sort"
  5. "time"
  6. "bet24.com/log"
  7. )
  8. var mgr *couponManager
  9. type couponManager struct {
  10. day_list map[string]*statInfo
  11. hour_list map[string][]*statInfo
  12. }
  13. func Run() {
  14. mgr = new(couponManager)
  15. mgr.day_list = make(map[string]*statInfo)
  16. mgr.hour_list = make(map[string][]*statInfo)
  17. }
  18. func (this *couponManager) dayStat(beginTime, endTime string) []*statInfo {
  19. var list []*statInfo
  20. end, _ := time.Parse(dateFormat, endTime)
  21. for begin, _ := time.Parse(dateFormat, beginTime); !begin.After(end); begin = begin.AddDate(0, 0, 1) {
  22. dateStr := begin.Format(dateFormat)
  23. key := fmt.Sprintf("%s", dateStr)
  24. v, ok := this.day_list[key]
  25. if ok {
  26. list = append(list, v)
  27. continue
  28. }
  29. for _, v := range statByDay(dateStr) {
  30. log.Debug("couponmgr.dayStat ==> %+v", v)
  31. if begin.Format(dateFormat) == time.Now().Format(dateFormat) {
  32. list = append(list, v)
  33. continue
  34. }
  35. key = fmt.Sprintf("%s", v.DateFlag)
  36. this.day_list[key] = v
  37. list = append(list, v)
  38. }
  39. }
  40. sort.SliceStable(list, func(i, j int) bool {
  41. return list[i].DateFlag > list[j].DateFlag
  42. })
  43. return list
  44. }
  45. func (this *couponManager) hourStat(beginTime, endTime string) []*statInfo {
  46. var list []*statInfo
  47. end, _ := time.Parse(dateFormat, endTime)
  48. for begin, _ := time.Parse(dateFormat, beginTime); !begin.After(end); begin = begin.AddDate(0, 0, 1) {
  49. dateStr := begin.Format(dateFormat)
  50. key := fmt.Sprintf("%s", dateStr)
  51. v, ok := this.hour_list[key]
  52. if ok {
  53. list = append(list, v...)
  54. continue
  55. }
  56. for _, v := range statByHour(dateStr) {
  57. log.Debug("couponmgr.hourStat ==> %+v", v)
  58. if begin.Format(dateFormat) == time.Now().Format(dateFormat) {
  59. list = append(list, v)
  60. continue
  61. }
  62. key = fmt.Sprintf("%s", v.DateFlag)
  63. this.hour_list[key] = append(this.hour_list[key], v)
  64. list = append(list, v)
  65. }
  66. }
  67. sort.SliceStable(list, func(i, j int) bool {
  68. return list[i].DateFlag > list[j].DateFlag
  69. })
  70. return list
  71. }