123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package dao
- import (
- "context"
- "fmt"
- "strconv"
- "time"
- "go-common/app/service/openplatform/ticket-item/conf"
- "go-common/app/service/openplatform/ticket-item/model"
- "go-common/library/cache/redis"
- "go-common/library/database/orm"
- "go-common/library/log"
- "go-common/library/database/elastic"
- "go-common/library/sync/pipeline/fanout"
- "github.com/jinzhu/gorm"
- )
- // Expire time
- const (
- CacheTimeout = 120
- _expireHalfhour = 1800 // 半小时过期
- )
- // Dao dao
- type Dao struct {
- c *conf.Config
- redis *redis.Pool
- cache *fanout.Fanout
- // DB
- db *gorm.DB
- expire int32
- es *elastic.Elastic
- }
- func keyItem(id int64) string {
- return "open_item_" + strconv.FormatInt(id, 10)
- }
- func keyItemDetail(id int64) string {
- return "open_item_detail_" + strconv.FormatInt(id, 10)
- }
- func keyItemTicket(id int64) string {
- return "open_itemticket_" + strconv.FormatInt(id, 10)
- }
- func keyTicket(id int64) string {
- return "open_ticket_" + strconv.FormatInt(id, 10)
- }
- func keyVenue(id int64) string {
- return "open_venue_" + strconv.FormatInt(id, 10)
- }
- func keyPlace(id int64) string {
- return "open_place_" + strconv.FormatInt(id, 10)
- }
- func keyItemScreen(id int64) string {
- return "open_itemscreen_" + strconv.FormatInt(id, 10)
- }
- func keyScreen(id int64) string {
- return "open_screen_" + strconv.FormatInt(id, 10)
- }
- func keyBannerList(order int32, districtID string, position int32, subPosition int32) string {
- return fmt.Sprintf("BANNERLISTV3:%d:%s:%d:%d", order, districtID, position, subPosition)
- }
- func keyBannerInfo(bannerID int64) string {
- return fmt.Sprintf("%d:BANNERINFOV2", bannerID)
- }
- //go:generate $GOPATH/src/go-common/app/tool/cache/gen
- type _cache interface {
- // cache: -nullcache=&model.Item{ID:-1} -check_null_code=$!=nil&&$.ID==-1
- Items(c context.Context, pid []int64) (info map[int64]*model.Item, err error)
- // cache: -nullcache=&model.ItemDetail{ProjectID:-1} -check_null_code=$!=nil&&$.ProjectID==-1
- ItemDetails(c context.Context, pid []int64) (details map[int64]*model.ItemDetail, err error)
- // cache: -nullcache=[]*model.TicketInfo{{TicketPrice:model.TicketPrice{ProjectID:-1}}} -check_null_code=len($)==1&&$[0].ProjectID==-1
- TkListByItem(c context.Context, pid []int64) (info map[int64][]*model.TicketInfo, err error)
- // cache: -nullcache=&model.Venue{ID:-1} -check_null_code=$!=nil&&$.ID==-1
- Venues(c context.Context, id []int64) (venues map[int64]*model.Venue, err error)
- // cache: -nullcache=&model.Place{ID:-1} -check_null_code=$!=nil&&$.ID==-1
- Place(c context.Context, id int64) (place *model.Place, err error)
- // cache: -nullcache=[]*model.Screen{{ProjectID:-1}} -check_null_code=len($)==1&&$[0].ProjectID==-1
- ScListByItem(c context.Context, pid []int64) (info map[int64][]*model.Screen, err error)
- // cache: -nullcache=&model.Screen{ProjectID:-1} -check_null_code=$!=nil&&$.ProjectID==-1
- ScList(c context.Context, sids []int64) (info map[int64]*model.Screen, err error)
- // cache: -nullcache=&model.TicketInfo{TicketPrice:model.TicketPrice{ProjectID:-1}} -check_null_code=$!=nil&&$.ProjectID==-1
- TkList(c context.Context, tids []int64) (info map[int64]*model.TicketInfo, err error)
- }
- // New init mysql db
- func New(c *conf.Config) (dao *Dao) {
- dao = &Dao{
- c: c,
- // orm
- db: orm.NewMySQL(c.ORM),
- redis: redis.NewPool(c.Redis.Master),
- expire: int32(time.Duration(c.Redis.Expire) / time.Second),
- cache: fanout.New("cache", fanout.Worker(1), fanout.Buffer(1024)),
- es: elastic.NewElastic(&elastic.Config{
- Host: c.URL.ElasticHost,
- HTTPClient: c.HTTPClient.Read,
- }),
- }
- return
- }
- // Ping ping 方法
- func (d *Dao) Ping(c context.Context) (err error) {
- conn := d.redis.Get(c)
- defer conn.Close()
- _, err = conn.Do("PING")
- if err != nil {
- return
- }
- return d.db.DB().PingContext(c)
- }
- // Close 关闭redis 和 db 连接
- func (d *Dao) Close() (err error) {
- d.redis.Close()
- d.db.Close()
- return
- }
- // BeginTran 开启事务
- func (d *Dao) BeginTran(c context.Context) (tx *gorm.DB, err error) {
- tx = d.db.Begin()
- if tx.Error != nil {
- err = tx.Error
- tx = nil
- log.Error("开启事务失败:%s", err)
- }
- return
- }
- // CommitTran 提交事务
- func (d *Dao) CommitTran(c context.Context, tx *gorm.DB) (err error) {
- if err = tx.Commit().Error; err != nil {
- tx = nil
- log.Error("提交事务失败:%s", err)
- }
- return
- }
|