dao.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/service/openplatform/ticket-item/conf"
  8. "go-common/app/service/openplatform/ticket-item/model"
  9. "go-common/library/cache/redis"
  10. "go-common/library/database/orm"
  11. "go-common/library/log"
  12. "go-common/library/database/elastic"
  13. "go-common/library/sync/pipeline/fanout"
  14. "github.com/jinzhu/gorm"
  15. )
  16. // Expire time
  17. const (
  18. CacheTimeout = 120
  19. _expireHalfhour = 1800 // 半小时过期
  20. )
  21. // Dao dao
  22. type Dao struct {
  23. c *conf.Config
  24. redis *redis.Pool
  25. cache *fanout.Fanout
  26. // DB
  27. db *gorm.DB
  28. expire int32
  29. es *elastic.Elastic
  30. }
  31. func keyItem(id int64) string {
  32. return "open_item_" + strconv.FormatInt(id, 10)
  33. }
  34. func keyItemDetail(id int64) string {
  35. return "open_item_detail_" + strconv.FormatInt(id, 10)
  36. }
  37. func keyItemTicket(id int64) string {
  38. return "open_itemticket_" + strconv.FormatInt(id, 10)
  39. }
  40. func keyTicket(id int64) string {
  41. return "open_ticket_" + strconv.FormatInt(id, 10)
  42. }
  43. func keyVenue(id int64) string {
  44. return "open_venue_" + strconv.FormatInt(id, 10)
  45. }
  46. func keyPlace(id int64) string {
  47. return "open_place_" + strconv.FormatInt(id, 10)
  48. }
  49. func keyItemScreen(id int64) string {
  50. return "open_itemscreen_" + strconv.FormatInt(id, 10)
  51. }
  52. func keyScreen(id int64) string {
  53. return "open_screen_" + strconv.FormatInt(id, 10)
  54. }
  55. func keyBannerList(order int32, districtID string, position int32, subPosition int32) string {
  56. return fmt.Sprintf("BANNERLISTV3:%d:%s:%d:%d", order, districtID, position, subPosition)
  57. }
  58. func keyBannerInfo(bannerID int64) string {
  59. return fmt.Sprintf("%d:BANNERINFOV2", bannerID)
  60. }
  61. //go:generate $GOPATH/src/go-common/app/tool/cache/gen
  62. type _cache interface {
  63. // cache: -nullcache=&model.Item{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  64. Items(c context.Context, pid []int64) (info map[int64]*model.Item, err error)
  65. // cache: -nullcache=&model.ItemDetail{ProjectID:-1} -check_null_code=$!=nil&&$.ProjectID==-1
  66. ItemDetails(c context.Context, pid []int64) (details map[int64]*model.ItemDetail, err error)
  67. // cache: -nullcache=[]*model.TicketInfo{{TicketPrice:model.TicketPrice{ProjectID:-1}}} -check_null_code=len($)==1&&$[0].ProjectID==-1
  68. TkListByItem(c context.Context, pid []int64) (info map[int64][]*model.TicketInfo, err error)
  69. // cache: -nullcache=&model.Venue{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  70. Venues(c context.Context, id []int64) (venues map[int64]*model.Venue, err error)
  71. // cache: -nullcache=&model.Place{ID:-1} -check_null_code=$!=nil&&$.ID==-1
  72. Place(c context.Context, id int64) (place *model.Place, err error)
  73. // cache: -nullcache=[]*model.Screen{{ProjectID:-1}} -check_null_code=len($)==1&&$[0].ProjectID==-1
  74. ScListByItem(c context.Context, pid []int64) (info map[int64][]*model.Screen, err error)
  75. // cache: -nullcache=&model.Screen{ProjectID:-1} -check_null_code=$!=nil&&$.ProjectID==-1
  76. ScList(c context.Context, sids []int64) (info map[int64]*model.Screen, err error)
  77. // cache: -nullcache=&model.TicketInfo{TicketPrice:model.TicketPrice{ProjectID:-1}} -check_null_code=$!=nil&&$.ProjectID==-1
  78. TkList(c context.Context, tids []int64) (info map[int64]*model.TicketInfo, err error)
  79. }
  80. // New init mysql db
  81. func New(c *conf.Config) (dao *Dao) {
  82. dao = &Dao{
  83. c: c,
  84. // orm
  85. db: orm.NewMySQL(c.ORM),
  86. redis: redis.NewPool(c.Redis.Master),
  87. expire: int32(time.Duration(c.Redis.Expire) / time.Second),
  88. cache: fanout.New("cache", fanout.Worker(1), fanout.Buffer(1024)),
  89. es: elastic.NewElastic(&elastic.Config{
  90. Host: c.URL.ElasticHost,
  91. HTTPClient: c.HTTPClient.Read,
  92. }),
  93. }
  94. return
  95. }
  96. // Ping ping 方法
  97. func (d *Dao) Ping(c context.Context) (err error) {
  98. conn := d.redis.Get(c)
  99. defer conn.Close()
  100. _, err = conn.Do("PING")
  101. if err != nil {
  102. return
  103. }
  104. return d.db.DB().PingContext(c)
  105. }
  106. // Close 关闭redis 和 db 连接
  107. func (d *Dao) Close() (err error) {
  108. d.redis.Close()
  109. d.db.Close()
  110. return
  111. }
  112. // BeginTran 开启事务
  113. func (d *Dao) BeginTran(c context.Context) (tx *gorm.DB, err error) {
  114. tx = d.db.Begin()
  115. if tx.Error != nil {
  116. err = tx.Error
  117. tx = nil
  118. log.Error("开启事务失败:%s", err)
  119. }
  120. return
  121. }
  122. // CommitTran 提交事务
  123. func (d *Dao) CommitTran(c context.Context, tx *gorm.DB) (err error) {
  124. if err = tx.Commit().Error; err != nil {
  125. tx = nil
  126. log.Error("提交事务失败:%s", err)
  127. }
  128. return
  129. }