oversea.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package service
  2. import (
  3. "go-common/app/admin/main/videoup/model/oversea"
  4. "go-common/library/log"
  5. bm "go-common/library/net/http/blademaster"
  6. "go-common/library/xstr"
  7. )
  8. // PolicyGroups get policy group
  9. func (s *Service) PolicyGroups(c *bm.Context, uid, groupID int64, gType, state int8, count, page int64, order, sort string) (data *oversea.PolicyGroupData, err error) {
  10. groups, total, err := s.oversea.PolicyGroups(c, uid, groupID, gType, state, count, page, order, sort)
  11. if len(groups) != 0 {
  12. s.mulIDtoName(c, groups, s.mng.GetNameByUID, "UID", "UserName")
  13. }
  14. data = &oversea.PolicyGroupData{
  15. Items: groups,
  16. Pager: &oversea.Pager{
  17. Num: page,
  18. Size: count,
  19. Total: total,
  20. },
  21. }
  22. return
  23. }
  24. // ArchiveGroups get policy group by archive id
  25. func (s *Service) ArchiveGroups(c *bm.Context, aid int64) (groups []*oversea.PolicyGroup, err error) {
  26. groups, err = s.oversea.ArchiveGroups(c, aid)
  27. return
  28. }
  29. // AddPolicyGroup add policy group
  30. func (s *Service) AddPolicyGroup(c *bm.Context, group *oversea.PolicyGroup) (err error) {
  31. if err = s.oversea.AddPolicyGroup(c, group); err != nil {
  32. return
  33. }
  34. s.sendPolicyLog(c, &oversea.PolicyGroup{}, group)
  35. return
  36. }
  37. // UpdatePolicyGroup update policy group
  38. func (s *Service) UpdatePolicyGroup(c *bm.Context, id int64, attrs map[string]interface{}) (err error) {
  39. var (
  40. oldG = &oversea.PolicyGroup{}
  41. newG = &oversea.PolicyGroup{}
  42. )
  43. if oldG, err = s.oversea.PolicyGroup(c, id); err != nil {
  44. return
  45. }
  46. if err = s.oversea.UpdatePolicyGroup(c, id, attrs); err != nil {
  47. return
  48. }
  49. if newG, err = s.oversea.PolicyGroup(c, id); err != nil {
  50. log.Error("s.oversea.PolicyGroup(%d) err(%v)", id, err)
  51. err = nil
  52. } else {
  53. s.sendPolicyLog(c, oldG, newG)
  54. }
  55. return
  56. }
  57. // UpdatePolicyGroups multi update policy group
  58. func (s *Service) UpdatePolicyGroups(c *bm.Context, ids []int64, attrs map[string]interface{}) (err error) {
  59. var (
  60. oldGs []*oversea.PolicyGroup
  61. newGs []*oversea.PolicyGroup
  62. newMap = make(map[int64]*oversea.PolicyGroup)
  63. )
  64. if oldGs, err = s.oversea.PolicyGroupsByIds(c, ids); err != nil {
  65. return
  66. }
  67. if err = s.oversea.UpdatePolicyGroups(c, ids, attrs); err != nil {
  68. return
  69. }
  70. if newGs, err = s.oversea.PolicyGroupsByIds(c, ids); err != nil {
  71. log.Error("s.oversea.PolicyGroupsByIds(%d) err(%v)", ids, err)
  72. err = nil
  73. } else {
  74. for _, v := range newGs {
  75. newMap[v.ID] = v
  76. }
  77. for _, oldG := range oldGs {
  78. newG := &oversea.PolicyGroup{}
  79. if _, ok := newMap[oldG.ID]; ok {
  80. newG = newMap[oldG.ID]
  81. }
  82. s.sendPolicyLog(c, oldG, newG)
  83. }
  84. }
  85. return
  86. }
  87. // PolicyItems get polices by group id
  88. func (s *Service) PolicyItems(c *bm.Context, gid int64) (items []*oversea.PolicyItem, err error) {
  89. return s.oversea.PolicyItems(c, gid)
  90. }
  91. // AddPolicies add policies
  92. func (s *Service) AddPolicies(c *bm.Context, uid, gid int64, items []*oversea.PolicyParams) (err error) {
  93. var (
  94. zids []int64
  95. oldG = &oversea.PolicyGroup{}
  96. newG = &oversea.PolicyGroup{}
  97. )
  98. if oldG, err = s.oversea.PolicyGroup(c, gid); err != nil {
  99. return
  100. }
  101. policies := make([]oversea.PolicyItem, len(items))
  102. for i, v := range items {
  103. policies[i].ID = v.ID
  104. policies[i].GroupID = gid
  105. policies[i].PlayAuth = v.PlayAuth
  106. policies[i].DownAuth = v.DownAuth
  107. policies[i].AreaID = xstr.JoinInts(v.AreaIds)
  108. policies[i].State = oversea.StateOK
  109. zids, _ = s.oversea.ZoneIDs(c, v.AreaIds)
  110. policies[i].ZoneID = xstr.JoinInts(zids)
  111. }
  112. if err = s.oversea.AddPolicies(c, policies); err != nil {
  113. return
  114. }
  115. if err = s.oversea.UpdatePolicyGroup(c, gid, map[string]interface{}{"uid": uid}); err != nil {
  116. log.Error("s.oversea.UpdatePolicyGroup(%d) err(%v)", gid, err)
  117. err = nil
  118. }
  119. if newG, err = s.oversea.PolicyGroup(c, gid); err != nil {
  120. log.Error("s.oversea.PolicyGroup(%d) err(%v)", gid, err)
  121. err = nil
  122. } else {
  123. s.sendPolicyLog(c, oldG, newG)
  124. }
  125. return
  126. }
  127. // DelPolices soft delete policies
  128. func (s *Service) DelPolices(c *bm.Context, uid, gid int64, ids []int64) (err error) {
  129. var (
  130. oldG = &oversea.PolicyGroup{}
  131. newG = &oversea.PolicyGroup{}
  132. )
  133. if oldG, err = s.oversea.PolicyGroup(c, gid); err != nil {
  134. return
  135. }
  136. if err = s.oversea.DelPolices(c, gid, ids); err != nil {
  137. return
  138. }
  139. if err = s.oversea.UpdatePolicyGroup(c, gid, map[string]interface{}{"uid": uid}); err != nil {
  140. log.Error("s.oversea.UpdatePolicyGroup(%d) err(%v)", gid, err)
  141. err = nil
  142. }
  143. if newG, err = s.oversea.PolicyGroup(c, gid); err != nil {
  144. log.Error("s.oversea.PolicyGroup(%d) err(%v)", gid, err)
  145. err = nil
  146. } else {
  147. s.sendPolicyLog(c, oldG, newG)
  148. }
  149. return
  150. }