switch.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/live/push-live/model"
  6. "go-common/library/log"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _shard = 10 //分表十张
  11. _getMidsByTargetID = "SELECT uid FROM app_switch_config_%s WHERE target_id=? AND type=? AND switch=?"
  12. )
  13. // tableIndex return index by target_id
  14. func tableIndex(targetID int64) string {
  15. return fmt.Sprintf("%02d", targetID%_shard)
  16. }
  17. // GetFansBySwitch 获取直播开关数据
  18. func (d *Dao) GetFansBySwitch(c context.Context, targetID int64) (fans map[int64]bool, err error) {
  19. var mid int64
  20. fans = make(map[int64]bool)
  21. sql := fmt.Sprintf(_getMidsByTargetID, tableIndex(targetID))
  22. rows, err := d.db.Query(c, sql, targetID, model.LivePushType, model.LivePushSwitchOn)
  23. if err != nil {
  24. err = errors.WithStack(err)
  25. fmt.Printf("%v", err)
  26. log.Error("[dao.switch|GetSwitchMids] db.Query() error(%v)", err)
  27. return
  28. }
  29. for rows.Next() {
  30. if err = rows.Scan(&mid); err != nil {
  31. err = errors.WithStack(err)
  32. fmt.Printf("%v", err)
  33. log.Error("[dao.switch|GetSwitchMids] rows.Scan() error(%v)", err)
  34. return
  35. }
  36. fans[mid] = true
  37. }
  38. return
  39. }