oversea.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package manager
  2. import (
  3. "context"
  4. "go-common/app/admin/main/videoup/model/oversea"
  5. "go-common/library/log"
  6. )
  7. // UpPolicyRelation update or into archive_relation.
  8. func (d *Dao) UpPolicyRelation(c context.Context, aid, gid int64) (relation *oversea.ArchiveRelation, err error) {
  9. var assign = map[string]interface{}{
  10. "policy_id": gid,
  11. "aid": aid,
  12. }
  13. relation = &oversea.ArchiveRelation{}
  14. if err = d.OverseaDB.Where("aid=?", aid).Assign(assign).FirstOrCreate(&relation).Error; err != nil {
  15. log.Error("d.UpPolicyRelation.FirstOrCreate error(%v)", err)
  16. return
  17. }
  18. return
  19. }
  20. // PolicyRelation get archive policy group relation.
  21. func (d *Dao) PolicyRelation(c context.Context, aid int64) (relation *oversea.ArchiveRelation, err error) {
  22. relation = &oversea.ArchiveRelation{}
  23. res := d.OverseaDB.Where("aid=?", aid).Find(&relation)
  24. if res.RecordNotFound() {
  25. relation = nil
  26. return
  27. }
  28. err = res.Error
  29. return
  30. }
  31. // PolicyGroups get policy group
  32. func (d *Dao) PolicyGroups(c context.Context, uid, id int64, gType, state int8, count, page int64, order, sort string) (groups []*oversea.PolicyGroup, total int64, err error) {
  33. var (
  34. db = d.OverseaDB.Model(&groups)
  35. orders = map[string]int{
  36. "mtime": 1,
  37. }
  38. )
  39. db = db.Where("is_global=?", 1)
  40. if uid > 0 {
  41. db = db.Where("uid=?", uid)
  42. }
  43. if id > 0 {
  44. db = db.Where("id=?", id)
  45. }
  46. if gType > 0 {
  47. db = db.Where("type=?", gType)
  48. }
  49. if state >= 0 {
  50. db = db.Where("state=?", state)
  51. }
  52. if order != "" && sort != "" {
  53. if _, ok := orders[order]; ok {
  54. db = db.Order(order + " " + sort)
  55. }
  56. }
  57. if count <= 0 {
  58. count = 20
  59. }
  60. if page <= 0 {
  61. page = 1
  62. }
  63. db.Count(&total)
  64. db = db.Offset((page - 1) * count)
  65. db = db.Limit(count)
  66. if err = db.Find(&groups).Error; err != nil {
  67. log.Error("d.PolicyGroups.Find error(%v)", err)
  68. }
  69. err = d.ItemsByGroup(groups)
  70. return
  71. }
  72. // PolicyGroupsByIds get policy groups by ids
  73. func (d *Dao) PolicyGroupsByIds(c context.Context, ids []int64) (groups []*oversea.PolicyGroup, err error) {
  74. if err = d.OverseaDB.Where(ids).Find(&groups).Error; err != nil {
  75. log.Error("d.PolicyGroupsByIds.Find error(%v)", err)
  76. }
  77. return
  78. }
  79. // PolicyGroup get policy group by id
  80. func (d *Dao) PolicyGroup(c context.Context, id int64) (group *oversea.PolicyGroup, err error) {
  81. var (
  82. groups []*oversea.PolicyGroup
  83. )
  84. group = &oversea.PolicyGroup{}
  85. res := d.OverseaDB.Where("id=?", id).Find(&group)
  86. if res.RecordNotFound() {
  87. group = nil
  88. return
  89. }
  90. err = res.Error
  91. if err != nil {
  92. log.Error("d.PolicyGroup.Find error(%v)", err)
  93. return
  94. }
  95. groups = append(groups, group)
  96. if err = d.ItemsByGroup(groups); err != nil {
  97. log.Error("d.ItemsByGroup.Find error(%v)", err)
  98. return
  99. }
  100. if len(groups) != 0 {
  101. group = groups[0]
  102. }
  103. return
  104. }
  105. // ArchiveGroups get archive's policy groups
  106. func (d *Dao) ArchiveGroups(c context.Context, aid int64) (groups []*oversea.PolicyGroup, err error) {
  107. var (
  108. db = d.OverseaDB
  109. relations []*oversea.ArchiveRelation
  110. gids []int64
  111. )
  112. err = db.Where("aid=?", aid).Find(&relations).Error
  113. if err != nil {
  114. log.Error("d.ArchiveGroups.Find error(%v)", err)
  115. return
  116. }
  117. gids = make([]int64, len(relations))
  118. for i, v := range relations {
  119. gids[i] = v.GroupID
  120. }
  121. db = d.OverseaDB
  122. err = db.Where(gids).Find(&groups).Error
  123. if err != nil {
  124. log.Error("d.ArchiveGroups.Find error(%v)", err)
  125. return
  126. }
  127. err = d.ItemsByGroup(groups)
  128. return
  129. }
  130. // ItemsByGroup get policy items into group
  131. func (d *Dao) ItemsByGroup(groups []*oversea.PolicyGroup) (err error) {
  132. var (
  133. items []*oversea.PolicyItem
  134. itemMap = make(map[int64][]*oversea.PolicyItem)
  135. )
  136. gids := make([]int64, len(groups))
  137. for i, v := range groups {
  138. gids[i] = v.ID
  139. }
  140. db := d.OverseaDB
  141. err = db.Where("group_id in (?) and state=?", gids, oversea.StateOK).Find(&items).Error
  142. if err != nil {
  143. log.Error("d.ArchiveGroups.Find error(%v)", err)
  144. return
  145. }
  146. for _, v := range items {
  147. itemMap[v.GroupID] = append(itemMap[v.GroupID], v)
  148. }
  149. for i, g := range groups {
  150. if _, ok := itemMap[g.ID]; !ok {
  151. groups[i].Items = make([]*oversea.PolicyItem, 0)
  152. continue
  153. }
  154. groups[i].Items = itemMap[g.ID]
  155. }
  156. return
  157. }
  158. // AddPolicyGroup add policy group
  159. func (d *Dao) AddPolicyGroup(c context.Context, group *oversea.PolicyGroup) (err error) {
  160. var (
  161. db = d.OverseaDB
  162. )
  163. group.IsGlobal = 1
  164. group.Aid = 0
  165. group.State = 1
  166. err = db.Create(&group).Error
  167. if err != nil {
  168. group = &oversea.PolicyGroup{}
  169. log.Error("d.AddPolicyGroup.Create error(%v)", err)
  170. }
  171. return
  172. }
  173. // UpdatePolicyGroup update policy group
  174. func (d *Dao) UpdatePolicyGroup(c context.Context, id int64, attrs map[string]interface{}) (err error) {
  175. var (
  176. db = d.OverseaDB
  177. )
  178. err = db.Model(&oversea.PolicyGroup{}).Where("id=?", id).Update(attrs).Error
  179. if err != nil {
  180. log.Error("d.UpdatePolicyGroup.Update error(%v)", err)
  181. }
  182. return
  183. }
  184. // UpdatePolicyGroups multi update policy groups
  185. func (d *Dao) UpdatePolicyGroups(c context.Context, ids []int64, attrs map[string]interface{}) (err error) {
  186. var (
  187. db = d.OverseaDB
  188. )
  189. err = db.Model(&oversea.PolicyGroup{}).Where(ids).Update(attrs).Error
  190. if err != nil {
  191. log.Error("d.UpdatePolicyGroup.Update error(%v)", err)
  192. }
  193. return
  194. }
  195. // PolicyItems get policy items
  196. func (d *Dao) PolicyItems(c context.Context, gid int64) (items []*oversea.PolicyItem, err error) {
  197. err = d.OverseaDB.Where("group_id=? AND state=?", gid, oversea.StateOK).Find(&items).Error
  198. return
  199. }
  200. // ZoneIDs get zone ids by area ids
  201. func (d *Dao) ZoneIDs(c context.Context, aids []int64) (ids []int64, err error) {
  202. var items []*oversea.Zone
  203. if err = d.OverseaDB.Where(aids).Find(&items).Pluck("zone_id", &ids).Error; err != nil {
  204. log.Error("d.ZoneIDs.Find error(%v)", err)
  205. }
  206. return
  207. }
  208. // AddPolicies add policy items
  209. func (d *Dao) AddPolicies(c context.Context, policies []oversea.PolicyItem) (err error) {
  210. var assign = map[string]interface{}{
  211. "group_id": 0,
  212. "play_auth": 0,
  213. "down_auth": 0,
  214. "area_id": "",
  215. "zone_id": "",
  216. }
  217. for _, v := range policies {
  218. if v.ID > 0 {
  219. assign["group_id"] = v.GroupID
  220. assign["play_auth"] = v.PlayAuth
  221. assign["down_auth"] = v.DownAuth
  222. assign["area_id"] = v.AreaID
  223. assign["zone_id"] = v.ZoneID
  224. err = d.OverseaDB.Model(&v).Where("id=?", v.ID).Update(assign).Error
  225. } else {
  226. err = d.OverseaDB.Create(&v).Error
  227. }
  228. if err != nil {
  229. log.Error("d.AddPolicies.FirstOrCreate error(%v)", err)
  230. return
  231. }
  232. }
  233. return
  234. }
  235. // DelPolices soft delete policy items
  236. func (d *Dao) DelPolices(c context.Context, gid int64, ids []int64) (err error) {
  237. err = d.OverseaDB.Debug().Model(&oversea.PolicyItem{}).Where(ids).Where("group_id=?", gid).Update("state", oversea.StateDeleted).Error
  238. if err != nil {
  239. log.Error("d.DelPolices.Update error(%v)", err)
  240. }
  241. return
  242. }