dao.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package region
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-view/conf"
  5. "go-common/app/interface/main/app-view/model/region"
  6. xsql "go-common/library/database/sql"
  7. )
  8. const (
  9. // region
  10. _secSQL = "SELECT rid,name,logo,rank,goto,param,plat,area FROM region_copy WHERE state=1 AND reid!=0"
  11. )
  12. type Dao struct {
  13. db *xsql.DB
  14. get *xsql.Stmt
  15. }
  16. func New(c *conf.Config) (d *Dao) {
  17. d = &Dao{
  18. db: xsql.NewMySQL(c.MySQL.Show),
  19. }
  20. // prepare
  21. d.get = d.db.Prepared(_secSQL)
  22. return
  23. }
  24. // Seconds get all second region.
  25. func (d *Dao) Seconds(ctx context.Context) (rm map[int8]map[int]*region.Region, err error) {
  26. rows, err := d.get.Query(ctx)
  27. if err != nil {
  28. return
  29. }
  30. defer rows.Close()
  31. rm = map[int8]map[int]*region.Region{}
  32. for rows.Next() {
  33. a := &region.Region{}
  34. if err = rows.Scan(&a.Rid, &a.Name, &a.Logo, &a.Rank, &a.Goto, &a.Param, &a.Plat, &a.Area); err != nil {
  35. return
  36. }
  37. if rs, ok := rm[a.Plat]; ok {
  38. rs[a.Rid] = a
  39. } else {
  40. rm[a.Plat] = map[int]*region.Region{a.Rid: a}
  41. }
  42. }
  43. return
  44. }