edb_info_no_permission_admin.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package data_manage
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // EdbInfoNoPermissionAdmin 指标不可见用户配置表
  7. type EdbInfoNoPermissionAdmin struct {
  8. Id int `orm:"column(id);pk"`
  9. EdbInfoId int `orm:"column(edb_info_id);"`
  10. AdminId int `orm:"column(admin_id);"`
  11. CreateTime time.Time
  12. }
  13. func AddEdbInfoNoPermissionAdmin(item *EdbInfoNoPermissionAdmin) (lastId int64, err error) {
  14. o := orm.NewOrmUsingDB("data")
  15. lastId, err = o.Insert(item)
  16. return
  17. }
  18. // GetAllListByEdbInfoId 根据指标id获取所有的配置
  19. func (item EdbInfoNoPermissionAdmin) GetAllListByEdbInfoId(edbInfoId int) (items []*EdbInfoNoPermissionAdmin, err error) {
  20. o := orm.NewOrmUsingDB("data")
  21. sql := `SELECT * FROM edb_info_no_permission_admin WHERE edb_info_id = ? ORDER BY create_time desc`
  22. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  23. return
  24. }
  25. // GetAllListByAdminId 根据用户id获取该用户的所有配置
  26. func (item EdbInfoNoPermissionAdmin) GetAllListByAdminId(adminId int) (items []*EdbInfoNoPermissionAdmin, err error) {
  27. o := orm.NewOrmUsingDB("data")
  28. sql := `SELECT * FROM edb_info_no_permission_admin WHERE admin_id = ? ORDER BY create_time desc`
  29. _, err = o.Raw(sql, adminId).QueryRows(&items)
  30. return
  31. }
  32. // GetByEdbInfoIdAndAdminId 根据用户id和指标id获取配置
  33. func (item EdbInfoNoPermissionAdmin) GetByEdbInfoIdAndAdminId(adminId, edbInfoId int) (info *EdbInfoNoPermissionAdmin, err error) {
  34. o := orm.NewOrmUsingDB("data")
  35. sql := `SELECT * FROM edb_info_no_permission_admin WHERE admin_id = ? AND edb_info_id = ? ORDER BY create_time desc`
  36. err = o.Raw(sql, adminId, edbInfoId).QueryRow(&info)
  37. return
  38. }
  39. // DeleteAllByEdbInfoId 根据指标id删除所有的配置
  40. func (item EdbInfoNoPermissionAdmin) DeleteAllByEdbInfoId(edbInfoId int) (err error) {
  41. o := orm.NewOrmUsingDB("data")
  42. sql := `DELETE FROM edb_info_no_permission_admin WHERE edb_info_id = ? `
  43. _, err = o.Raw(sql, edbInfoId).Exec()
  44. return
  45. }
  46. // DeleteByEdbInfoIdAndAdminId 根据指标id和用户id删除配置
  47. func (item EdbInfoNoPermissionAdmin) DeleteByEdbInfoIdAndAdminId(edbInfoId, adminId int) (err error) {
  48. o := orm.NewOrmUsingDB("data")
  49. sql := `DELETE FROM edb_info_no_permission_admin WHERE edb_info_id = ? AND admin_id = ? `
  50. _, err = o.Raw(sql, edbInfoId, adminId).Exec()
  51. return
  52. }
  53. // ModifyByEdbInfoId 根据指标id和用户ID列表 重新配置
  54. func (item EdbInfoNoPermissionAdmin) ModifyByEdbInfoId(edbInfoId int, adminIdList []int) (err error) {
  55. to, err := orm.NewOrmUsingDB("data").Begin()
  56. if err != nil {
  57. return
  58. }
  59. defer func() {
  60. if err != nil {
  61. _ = to.Rollback()
  62. } else {
  63. _ = to.Commit()
  64. }
  65. }()
  66. sql := `DELETE FROM edb_info_no_permission_admin WHERE edb_info_id = ? `
  67. _, err = to.Raw(sql, edbInfoId).Exec()
  68. if err != nil {
  69. return
  70. }
  71. insertArr := make([]*EdbInfoNoPermissionAdmin, 0)
  72. for _, adminId := range adminIdList {
  73. insertArr = append(insertArr, &EdbInfoNoPermissionAdmin{
  74. //Id: 0,
  75. EdbInfoId: edbInfoId,
  76. AdminId: adminId,
  77. CreateTime: time.Now(),
  78. })
  79. }
  80. if len(insertArr) > 0 {
  81. _, err = to.InsertMulti(len(insertArr), insertArr)
  82. }
  83. return
  84. }
  85. // AddByEdbInfoId 根据指标id和用户ID列表 添加配置
  86. func (item EdbInfoNoPermissionAdmin) AddByEdbInfoId(edbInfoId int, adminIdList []int) (err error) {
  87. to, err := orm.NewOrmUsingDB("data").Begin()
  88. if err != nil {
  89. return
  90. }
  91. defer func() {
  92. if err != nil {
  93. _ = to.Rollback()
  94. } else {
  95. _ = to.Commit()
  96. }
  97. }()
  98. var items []*EdbInfoNoPermissionAdmin
  99. sql := `SELECT * FROM edb_info_no_permission_admin WHERE edb_info_id = ? ORDER BY create_time desc`
  100. _, err = to.Raw(sql, edbInfoId).QueryRows(&items)
  101. if err != nil {
  102. return
  103. }
  104. hasMap := make(map[int]int)
  105. for _, v := range items {
  106. hasMap[v.AdminId] = v.EdbInfoId
  107. }
  108. insertArr := make([]*EdbInfoNoPermissionAdmin, 0)
  109. for _, adminId := range adminIdList {
  110. if _, ok := hasMap[adminId]; !ok {
  111. insertArr = append(insertArr, &EdbInfoNoPermissionAdmin{
  112. //Id: 0,
  113. EdbInfoId: edbInfoId,
  114. AdminId: adminId,
  115. CreateTime: time.Now(),
  116. })
  117. }
  118. }
  119. if len(insertArr) > 0 {
  120. _, err = to.InsertMulti(len(insertArr), insertArr)
  121. }
  122. return
  123. }
  124. type NoPermissionChart struct {
  125. AdminId int
  126. ChartInfoId int
  127. }
  128. // GetAllChartListByAdminId 根据用户id获取该用户的所有配置
  129. func (item EdbInfoNoPermissionAdmin) GetAllChartListByAdminId(adminId int) (items []*NoPermissionChart, err error) {
  130. o := orm.NewOrmUsingDB("data")
  131. sql := `SELECT a.admin_id,b.chart_info_id FROM edb_info_no_permission_admin AS a
  132. JOIN chart_edb_mapping AS b ON a.edb_info_id=b. edb_info_id
  133. WHERE a.admin_id = ? ORDER BY a.create_time desc `
  134. _, err = o.Raw(sql, adminId).QueryRows(&items)
  135. return
  136. }
  137. // GetChartByEdbInfoIdAndAdminId 根据用户id和指标id获取配置
  138. func (item EdbInfoNoPermissionAdmin) GetChartByEdbInfoIdAndAdminId(adminId, edbInfoId int) (info *NoPermissionChart, err error) {
  139. o := orm.NewOrmUsingDB("data")
  140. sql := `SELECT a.admin_id,b.chart_info_id FROM edb_info_no_permission_admin AS a
  141. JOIN chart_edb_mapping AS b ON a.edb_info_id=b. edb_info_id
  142. WHERE a.admin_id = ? AND a.edb_info_id = ? ORDER BY a.create_time desc`
  143. err = o.Raw(sql, adminId, edbInfoId).QueryRow(&info)
  144. return
  145. }