dao.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/figure/conf"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/hbase.v2"
  8. "go-common/library/database/sql"
  9. )
  10. // Dao figure job dao
  11. type Dao struct {
  12. c *conf.Config
  13. hbase *hbase.Client
  14. db *sql.DB
  15. redis *redis.Pool
  16. redisExpire int
  17. waiteMidExpire int
  18. }
  19. // New new a figure DAO
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. c: c,
  23. hbase: hbase.NewClient(c.HBase.Config),
  24. db: sql.NewMySQL(c.Mysql),
  25. redis: redis.NewPool(c.Redis.Config),
  26. redisExpire: int(time.Duration(c.Redis.Expire) / time.Second),
  27. waiteMidExpire: int(time.Duration(c.Redis.WaiteMidExpire) / time.Second),
  28. }
  29. return
  30. }
  31. // Ping check service health
  32. func (d *Dao) Ping(c context.Context) (err error) {
  33. return d.PingRedis(c)
  34. }
  35. // Close close all dao.
  36. func (d *Dao) Close() {
  37. if d.hbase != nil {
  38. d.hbase.Close()
  39. }
  40. }
  41. //Version get ever monday start time ts.
  42. func (d *Dao) Version(now time.Time) (ts int64) {
  43. var (
  44. n int8
  45. )
  46. y, m, day := now.Date()
  47. w := now.Weekday()
  48. switch w {
  49. case time.Sunday:
  50. n = 6
  51. default:
  52. n = int8(w) - 1
  53. }
  54. t := time.Date(y, m, day, 0, 0, 0, 0, time.Local).Add(-time.Duration(n) * 24 * time.Hour)
  55. return t.Unix()
  56. }