region.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package region
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-show/conf"
  5. "go-common/app/interface/main/app-show/model/region"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. // region
  11. _allSQL = "SELECT r.rid,r.reid,r.name,r.logo,r.rank,r.goto,r.param,r.plat,r.area,r.build,r.conditions,r.uri,r.is_logo,r.type,r.is_rank,l.name FROM region AS r, language AS l WHERE r.state=1 AND l.id=r.lang_id ORDER BY r.rank DESC"
  12. _allSQL2 = "SELECT r.id,r.rid,r.reid,r.name,r.logo,r.rank,r.goto,r.param,r.plat,r.area,r.uri,r.is_logo,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"
  13. _limitSQL = "SELECT l.id,l.rid,l.build,l.conditions FROM region_limit AS l,region_copy AS r WHERE l.rid=r.id"
  14. _configSQL = "SELECT c.id,c.rid,c.is_rank FROM region_rank_config AS c,region_copy AS r WHERE c.rid=r.id"
  15. //region android
  16. _regionPlatSQL = "SELECT r.rid,r.reid,r.name,r.logo,r.rank,r.goto,r.param,r.plat,r.area,l.name FROM region_copy AS r, language AS l WHERE r.plat=0 AND r.state=1 AND l.id=r.lang_id ORDER BY r.rank DESC"
  17. )
  18. type Dao struct {
  19. db *sql.DB
  20. get *sql.Stmt
  21. list *sql.Stmt
  22. limit *sql.Stmt
  23. config *sql.Stmt
  24. regionPlat *sql.Stmt
  25. }
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. db: sql.NewMySQL(c.MySQL.Show),
  29. }
  30. // prepare
  31. d.get = d.db.Prepared(_allSQL)
  32. d.list = d.db.Prepared(_allSQL2)
  33. d.limit = d.db.Prepared(_limitSQL)
  34. d.config = d.db.Prepared(_configSQL)
  35. d.regionPlat = d.db.Prepared(_regionPlatSQL)
  36. return
  37. }
  38. // GetAll get all region.
  39. func (d *Dao) All(ctx context.Context) ([]*region.Region, error) {
  40. rows, err := d.get.Query(ctx)
  41. if err != nil {
  42. log.Error("mysqlDB.Query error(%v)", err)
  43. return nil, err
  44. }
  45. defer rows.Close()
  46. apps := []*region.Region{}
  47. for rows.Next() {
  48. a := &region.Region{}
  49. if err = rows.Scan(&a.Rid, &a.Reid, &a.Name, &a.Logo, &a.Rank, &a.Goto, &a.Param, &a.Plat, &a.Area, &a.Build, &a.Condition, &a.URI, &a.Islogo, &a.Rtype, &a.Entrance, &a.Language); err != nil {
  50. log.Error("row.Scan error(%v)", err)
  51. return nil, err
  52. }
  53. apps = append(apps, a)
  54. }
  55. return apps, err
  56. }
  57. // RegionPlat get android
  58. func (d *Dao) RegionPlat(ctx context.Context) ([]*region.Region, error) {
  59. rows, err := d.regionPlat.Query(ctx)
  60. if err != nil {
  61. log.Error("mysqlDB.Query error(%v)", err)
  62. return nil, err
  63. }
  64. defer rows.Close()
  65. apps := []*region.Region{}
  66. for rows.Next() {
  67. a := &region.Region{}
  68. if err = rows.Scan(&a.Rid, &a.Reid, &a.Name, &a.Logo, &a.Rank, &a.Goto, &a.Param, &a.Plat, &a.Area, &a.Language); err != nil {
  69. log.Error("row.Scan error(%v)", err)
  70. return nil, err
  71. }
  72. apps = append(apps, a)
  73. }
  74. return apps, err
  75. }
  76. // AllList get all region.
  77. func (d *Dao) AllList(ctx context.Context) ([]*region.Region, error) {
  78. rows, err := d.list.Query(ctx)
  79. if err != nil {
  80. log.Error("mysqlDB.Query error(%v)", err)
  81. return nil, err
  82. }
  83. defer rows.Close()
  84. apps := []*region.Region{}
  85. for rows.Next() {
  86. a := &region.Region{}
  87. if err = rows.Scan(&a.ID, &a.Rid, &a.Reid, &a.Name, &a.Logo, &a.Rank, &a.Goto, &a.Param, &a.Plat, &a.Area, &a.URI, &a.Islogo, &a.Rtype, &a.Language); err != nil {
  88. log.Error("row.Scan error(%v)", err)
  89. return nil, err
  90. }
  91. apps = append(apps, a)
  92. }
  93. return apps, err
  94. }
  95. // Limit region limits
  96. func (d *Dao) Limit(ctx context.Context) (map[int64][]*region.Limit, error) {
  97. rows, err := d.limit.Query(ctx)
  98. if err != nil {
  99. log.Error("mysqlDB.Query error(%v)", err)
  100. return nil, err
  101. }
  102. defer rows.Close()
  103. limits := map[int64][]*region.Limit{}
  104. for rows.Next() {
  105. a := &region.Limit{}
  106. if err = rows.Scan(&a.ID, &a.Rid, &a.Build, &a.Condition); err != nil {
  107. log.Error("rows.Scan error(%v)", err)
  108. return nil, err
  109. }
  110. limits[a.Rid] = append(limits[a.Rid], a)
  111. }
  112. return limits, err
  113. }
  114. // Config region configs
  115. func (d *Dao) Config(ctx context.Context) (map[int64][]*region.Config, error) {
  116. rows, err := d.config.Query(ctx)
  117. if err != nil {
  118. log.Error("mysqlDB.Query error(%v)", err)
  119. return nil, err
  120. }
  121. defer rows.Close()
  122. configs := map[int64][]*region.Config{}
  123. for rows.Next() {
  124. a := &region.Config{}
  125. if err = rows.Scan(&a.ID, &a.Rid, &a.ScenesID); err != nil {
  126. log.Error("rows.Scan error(%v)", err)
  127. return nil, err
  128. }
  129. a.ConfigChange()
  130. configs[a.Rid] = append(configs[a.Rid], a)
  131. }
  132. return configs, err
  133. }
  134. // Close close memcache resource.
  135. func (dao *Dao) Close() {
  136. if dao.db != nil {
  137. dao.db.Close()
  138. }
  139. }