zlimit.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/main/location/model"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _getPolicySQL = "SELECT id, play_auth, down_auth, zone_id FROM policy_item WHERE zone_id <> '' AND state=1"
  12. _getRelationSQL = "SELECT policy_id FROM archive_relation WHERE aid=?"
  13. _getGolbalPolicySQL = "SELECT group_id,group_concat(id) FROM policy_item WHERE zone_id <> '' AND state=1 GROUP BY group_id"
  14. _getGroupZone = "SELECT a.group_id,a.play_auth,a.zone_id FROM policy_item AS a,policy_group AS b WHERE a.zone_id <> '' AND a.group_id=b.id AND b.type=2 AND a.state=1 AND b.state=1"
  15. )
  16. // Policies get policy data from db
  17. func (d *Dao) Policies(c context.Context) (res map[int64]map[int64]int64, err error) {
  18. var (
  19. tmpres map[int64]int64
  20. ok bool
  21. )
  22. rows, err := d.db.Query(c, _getPolicySQL)
  23. if err != nil {
  24. err = errors.WithStack(err)
  25. return
  26. }
  27. defer rows.Close()
  28. res = make(map[int64]map[int64]int64)
  29. for rows.Next() {
  30. var (
  31. pid, playAuth, downAuth int64
  32. zoneID string
  33. zoneIDs []int64
  34. )
  35. if err = rows.Scan(&pid, &playAuth, &downAuth, &zoneID); err != nil {
  36. err = errors.WithStack(err)
  37. return
  38. }
  39. if zoneIDs, err = xstr.SplitInts(zoneID); err != nil {
  40. log.Error("xstr.SplitInts(%s) error(%v)", zoneID, err)
  41. continue
  42. }
  43. for _, zoneid := range zoneIDs {
  44. if tmpres, ok = res[pid]; !ok {
  45. tmpres = make(map[int64]int64)
  46. res[pid] = tmpres
  47. }
  48. resCode := playAuth<<8 | downAuth
  49. tmpres[zoneid] = resCode
  50. }
  51. }
  52. err = errors.WithStack(err)
  53. return
  54. }
  55. // GroupPolicies get policy data from db group by group_id
  56. func (d *Dao) GroupPolicies(c context.Context) (res map[int64][]int64, err error) {
  57. rows, err := d.db.Query(c, _getGolbalPolicySQL)
  58. if err != nil {
  59. err = errors.WithStack(err)
  60. return
  61. }
  62. defer rows.Close()
  63. res = make(map[int64][]int64)
  64. for rows.Next() {
  65. var (
  66. groupID int64
  67. pids string
  68. zoneIDs []int64
  69. )
  70. if err = rows.Scan(&groupID, &pids); err != nil {
  71. err = errors.WithStack(err)
  72. return
  73. }
  74. if zoneIDs, err = xstr.SplitInts(pids); err != nil {
  75. log.Error("xstr.SplitInts(%s) error(%v)", pids, err)
  76. continue
  77. }
  78. res[groupID] = zoneIDs
  79. }
  80. err = errors.WithStack(err)
  81. return
  82. }
  83. // Groupid get gid from db by aid
  84. func (d *Dao) Groupid(c context.Context, aid int64) (gid int64, err error) {
  85. row := d.db.QueryRow(c, _getRelationSQL, aid)
  86. if err = row.Scan(&gid); err != nil {
  87. if err == sql.ErrNoRows {
  88. gid = 0
  89. err = nil
  90. } else {
  91. err = errors.WithStack(err)
  92. }
  93. }
  94. return
  95. }
  96. // GroupAuthZone zone_id by group_id.
  97. func (d *Dao) GroupAuthZone(c context.Context) (res map[int64]map[int64]map[int64]int64, err error) {
  98. var (
  99. tmpAres map[int64]map[int64]int64
  100. tmpZres map[int64]int64
  101. ok bool
  102. )
  103. rows, err := d.db.Query(c, _getGroupZone)
  104. if err != nil {
  105. err = errors.WithStack(err)
  106. return
  107. }
  108. defer rows.Close()
  109. res = make(map[int64]map[int64]map[int64]int64)
  110. for rows.Next() {
  111. var (
  112. gid, playAuth int64
  113. zoneID string
  114. zoneIDs []int64
  115. )
  116. if err = rows.Scan(&gid, &playAuth, &zoneID); err != nil {
  117. err = errors.WithStack(err)
  118. return
  119. }
  120. if playAuth != model.Forbidden && playAuth != model.Allow {
  121. playAuth = model.Allow
  122. }
  123. if zoneIDs, err = xstr.SplitInts(zoneID); err != nil {
  124. log.Error("xstr.SplitInts(%s) error(%v)", zoneID, err)
  125. continue
  126. }
  127. for _, zoneid := range zoneIDs {
  128. if tmpAres, ok = res[gid]; !ok {
  129. tmpAres = make(map[int64]map[int64]int64)
  130. res[gid] = tmpAres
  131. }
  132. if tmpZres, ok = tmpAres[playAuth]; !ok {
  133. tmpZres = make(map[int64]int64)
  134. tmpAres[playAuth] = tmpZres
  135. }
  136. if _, ok = tmpZres[zoneid]; !ok {
  137. tmpZres[zoneid] = zoneid
  138. }
  139. }
  140. }
  141. err = errors.WithStack(err)
  142. return
  143. }