dao.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package vip
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/live/xuser/conf"
  6. "go-common/app/service/live/xuser/model"
  7. "go-common/library/cache/redis"
  8. xsql "go-common/library/database/sql"
  9. "strconv"
  10. )
  11. // Dao vip dao
  12. type Dao struct {
  13. c *conf.Config
  14. db *xsql.DB
  15. redis *redis.Pool
  16. }
  17. // New new vip dao
  18. func New(c *conf.Config) (dao *Dao) {
  19. dao = &Dao{
  20. c: c,
  21. db: xsql.NewMySQL(c.LiveUserMysql),
  22. redis: redis.NewPool(c.VipRedis),
  23. }
  24. return
  25. }
  26. // Close close the resource.
  27. func (d *Dao) Close() {
  28. d.db.Close()
  29. d.redis.Close()
  30. }
  31. // initInfo init info struct
  32. func (d *Dao) initInfo(info *model.VipInfo) *model.VipInfo {
  33. if info == nil {
  34. info = &model.VipInfo{Vip: 0, VipTime: model.TimeEmpty, Svip: 0, SvipTime: model.TimeEmpty}
  35. } else {
  36. if info.VipTime == "" {
  37. info.VipTime = model.TimeEmpty
  38. }
  39. if info.SvipTime == "" {
  40. info.SvipTime = model.TimeEmpty
  41. }
  42. }
  43. return info
  44. }
  45. // Ping dao ping
  46. func (d *Dao) Ping(ctx context.Context) error {
  47. // TODO: add mc,redis... if you use
  48. return nil
  49. }
  50. // toInt try trans interface input to int
  51. func toInt(in interface{}) (int, error) {
  52. switch in.(type) {
  53. case int:
  54. return in.(int), nil
  55. case int32:
  56. return int(in.(int32)), nil
  57. case int64:
  58. return int(in.(int64)), nil
  59. case float32:
  60. return int(in.(float32)), nil
  61. case float64:
  62. return int(in.(float64)), nil
  63. case string:
  64. i, err := strconv.Atoi(in.(string))
  65. if err != nil {
  66. return 0, err
  67. }
  68. return i, nil
  69. case []byte:
  70. i, err := strconv.Atoi(string(in.([]byte)))
  71. if err != nil {
  72. return 0, err
  73. }
  74. return i, nil
  75. }
  76. return 0, fmt.Errorf("invalid input(%v)", in)
  77. }