region.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package region
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-channel/conf"
  5. "go-common/app/interface/main/app-channel/model/channel"
  6. "go-common/library/database/sql"
  7. )
  8. const (
  9. _allSQL2 = "SELECT r.id,r.rid,r.reid,r.name,r.logo,r.param,r.plat,r.area,r.uri,r.type,l.name FROM region_copy AS r, language AS l WHERE r.state=1 AND l.id=r.lang_id ORDER BY r.rank DESC"
  10. _limitSQL = "SELECT l.id,l.rid,l.build,l.conditions FROM region_limit AS l,region_copy AS r WHERE l.rid=r.id"
  11. _configSQL = "SELECT c.id,c.rid,c.is_rank FROM region_rank_config AS c,region_copy AS r WHERE c.rid=r.id"
  12. )
  13. type Dao struct {
  14. db *sql.DB
  15. }
  16. func New(c *conf.Config) (d *Dao) {
  17. d = &Dao{
  18. db: sql.NewMySQL(c.MySQL.Show),
  19. }
  20. return
  21. }
  22. // AllList get all region.
  23. func (d *Dao) AllList(ctx context.Context) (apps []*channel.Region, err error) {
  24. rows, err := d.db.Query(ctx, _allSQL2)
  25. if err != nil {
  26. return
  27. }
  28. defer rows.Close()
  29. apps = []*channel.Region{}
  30. for rows.Next() {
  31. a := &channel.Region{}
  32. if err = rows.Scan(&a.ID, &a.RID, &a.ReID, &a.Name, &a.Logo, &a.Param, &a.Plat, &a.Area, &a.URI, &a.Type, &a.Language); err != nil {
  33. return
  34. }
  35. apps = append(apps, a)
  36. }
  37. return
  38. }
  39. // Limit region limits
  40. func (d *Dao) Limit(ctx context.Context) (limits map[int64][]*channel.RegionLimit, err error) {
  41. rows, err := d.db.Query(ctx, _limitSQL)
  42. if err != nil {
  43. return
  44. }
  45. defer rows.Close()
  46. limits = map[int64][]*channel.RegionLimit{}
  47. for rows.Next() {
  48. a := &channel.RegionLimit{}
  49. if err = rows.Scan(&a.ID, &a.Rid, &a.Build, &a.Condition); err != nil {
  50. return
  51. }
  52. limits[a.Rid] = append(limits[a.Rid], a)
  53. }
  54. return
  55. }
  56. // Config region configs
  57. func (d *Dao) Config(ctx context.Context) (configs map[int64][]*channel.RegionConfig, err error) {
  58. rows, err := d.db.Query(ctx, _configSQL)
  59. if err != nil {
  60. return
  61. }
  62. defer rows.Close()
  63. configs = map[int64][]*channel.RegionConfig{}
  64. for rows.Next() {
  65. a := &channel.RegionConfig{}
  66. if err = rows.Scan(&a.ID, &a.Rid, &a.ScenesID); err != nil {
  67. return
  68. }
  69. configs[a.Rid] = append(configs[a.Rid], a)
  70. }
  71. return
  72. }
  73. // Close close memcache resource.
  74. func (dao *Dao) Close() {
  75. if dao.db != nil {
  76. dao.db.Close()
  77. }
  78. }