service.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package data
  2. import (
  3. "go-common/app/admin/main/up/conf"
  4. "go-common/app/admin/main/up/dao/data"
  5. "go-common/app/admin/main/up/dao/tag"
  6. "time"
  7. )
  8. //Service data service
  9. type Service struct {
  10. c *conf.Config
  11. data *data.Dao
  12. dtag *tag.Dao
  13. }
  14. //New get service
  15. func New(c *conf.Config) *Service {
  16. s := &Service{
  17. c: c,
  18. data: data.New(c),
  19. dtag: tag.New(c),
  20. }
  21. return s
  22. }
  23. func beginningOfDay(t time.Time) time.Time {
  24. d := time.Duration(-t.Hour()) * time.Hour
  25. return t.Truncate(time.Hour).Add(d)
  26. }
  27. func getTuesday(now time.Time) time.Time {
  28. t := beginningOfDay(now)
  29. weekday := int(t.Weekday())
  30. if weekday == 0 {
  31. weekday = 7
  32. }
  33. d := time.Duration(-weekday+2) * 24 * time.Hour
  34. return t.Truncate(time.Hour).Add(d)
  35. }
  36. func getSunday(now time.Time) time.Time {
  37. t := beginningOfDay(now)
  38. weekday := int(t.Weekday())
  39. if weekday == 0 {
  40. return t
  41. }
  42. d := time.Duration(7-weekday) * 24 * time.Hour
  43. return t.Truncate(time.Hour).Add(d)
  44. }
  45. func getDateLastSunday() (date time.Time) {
  46. t := time.Now()
  47. td := getTuesday(t).Add(12 * time.Hour)
  48. if t.Before(td) { //当前时间在本周二12点之前,则取上上周日的数据,否则取上周日的数据
  49. date = getSunday(t.AddDate(0, 0, -14))
  50. } else {
  51. date = getSunday(t.AddDate(0, 0, -7))
  52. }
  53. //log.Info("current time (%s) tuesday (%s) sunday (%s)", t.Format("2006-01-02 15:04:05"), td, sd)
  54. return
  55. }