dao.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package region
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-intl/conf"
  5. "go-common/app/interface/main/app-intl/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,build,conditions FROM region WHERE state=1 AND reid!=0"
  11. )
  12. // Dao is.
  13. type Dao struct {
  14. db *xsql.DB
  15. get *xsql.Stmt
  16. }
  17. // New a dao.
  18. func New(c *conf.Config) (d *Dao) {
  19. d = &Dao{
  20. db: xsql.NewMySQL(c.MySQL.Show),
  21. }
  22. // prepare
  23. d.get = d.db.Prepared(_secSQL)
  24. return
  25. }
  26. // Seconds get all second region.
  27. func (d *Dao) Seconds(c context.Context) (rm map[int8]map[int16]*region.Region, err error) {
  28. rows, err := d.get.Query(c)
  29. if err != nil {
  30. return
  31. }
  32. defer rows.Close()
  33. rm = map[int8]map[int16]*region.Region{}
  34. for rows.Next() {
  35. a := &region.Region{}
  36. if err = rows.Scan(&a.Rid, &a.Name, &a.Logo, &a.Rank, &a.Goto, &a.Param, &a.Plat, &a.Area, &a.Build, &a.Condition); err != nil {
  37. return
  38. }
  39. if rs, ok := rm[a.Plat]; ok {
  40. rs[a.Rid] = a
  41. } else {
  42. rm[a.Plat] = map[int16]*region.Region{a.Rid: a}
  43. }
  44. }
  45. return
  46. }
  47. // Close close db resource.
  48. func (d *Dao) Close() {
  49. if d.db != nil {
  50. d.db.Close()
  51. }
  52. }