report_grant.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package report
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "time"
  6. )
  7. // ReportGrant
  8. // @Description: 报告授权用户表
  9. //type ReportGrant struct {
  10. // GrantId int `orm:"column(grant_id)"` // 授权id
  11. // ReportId int `description:"报告id"`
  12. // AdminId int `description:"授权的用户id"`
  13. // CreateTime time.Time `description:"授权时间"`
  14. //}
  15. type ReportGrant struct {
  16. GrantId int `gorm:"primaryKey;column:grant_id;type:int(9) unsigned;not null"` // 授权id
  17. ReportId int `gorm:"index:idx_reportid;column:report_id;type:int(9) unsigned;not null;default:0"` // 报告id
  18. AdminId int `gorm:"column:admin_id;type:int(9) unsigned;default:0"` // 授权的用户id
  19. CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 授权时间
  20. }
  21. // MultiAddReportGrantGrant
  22. // @Description: 批量添加报告授权用户
  23. // @author: Roc
  24. // @receiver m
  25. // @datetime 2024-06-04 15:36:02
  26. // @param reportId int
  27. // @param list []*ReportGrant
  28. // @return err error
  29. func (m ReportGrant) MultiAddReportGrantGrant(reportId int, list []*ReportGrant) (err error) {
  30. //o := orm.NewOrmUsingDB("rddp")
  31. //to, err := o.Begin()
  32. //if err != nil {
  33. // return
  34. //}
  35. //defer func() {
  36. // if err != nil {
  37. // _ = to.Rollback()
  38. // } else {
  39. // _ = to.Commit()
  40. // }
  41. //}()
  42. //
  43. //sql := "DELETE from report_grant where report_id=?"
  44. //_, err = to.Raw(sql, reportId).Exec()
  45. //if err != nil {
  46. // return
  47. //}
  48. //
  49. //// 新增授权记录
  50. //if len(list) > 0 {
  51. // _, tmpErr := to.InsertMulti(500, list)
  52. // if tmpErr != nil {
  53. // err = tmpErr
  54. // return
  55. // }
  56. //}
  57. tx := global.DmSQL["rddp"].Begin()
  58. defer func() {
  59. if err != nil {
  60. _ = tx.Rollback()
  61. return
  62. }
  63. _ = tx.Commit()
  64. }()
  65. sql := "DELETE from report_grant where report_id=?"
  66. err = tx.Exec(sql, reportId).Error
  67. if err != nil {
  68. return
  69. }
  70. // 新增授权记录
  71. if len(list) > 0 {
  72. e := tx.CreateInBatches(list, utils.MultiAddNum).Error
  73. if e != nil {
  74. err = e
  75. return
  76. }
  77. }
  78. return
  79. }
  80. // GetGrantListById
  81. // @Description: 根据id获取授权列表
  82. // @author: Roc
  83. // @receiver m
  84. // @datetime 2024-06-04 15:33:58
  85. // @param reportId int
  86. // @return list []*ReportGrant
  87. // @return err error
  88. func (m ReportGrant) GetGrantListById(reportId int) (list []*ReportGrant, err error) {
  89. //o := orm.NewOrmUsingDB("rddp")
  90. sql := `SELECT * FROM report_grant WHERE report_id=? `
  91. //_, err = o.Raw(sql, reportId).QueryRows(&list)
  92. err = global.DmSQL["rddp"].Raw(sql, reportId).Find(&list).Error
  93. return
  94. }
  95. // GetGrantListByIdList
  96. // @Description: 根据id列表获取授权列表
  97. // @author: Roc
  98. // @receiver m
  99. // @datetime 2024-06-04 15:33:58
  100. // @param reportIdList []int
  101. // @return list []*ReportGrant
  102. // @return err error
  103. func (m ReportGrant) GetGrantListByIdList(reportIdList []int) (list []*ReportGrant, err error) {
  104. num := len(reportIdList)
  105. if num <= 0 {
  106. return
  107. }
  108. //o := orm.NewOrmUsingDB("rddp")
  109. sql := `SELECT * FROM report_grant WHERE report_id in (` + utils.GetOrmInReplace(num) + `) `
  110. //_, err = o.Raw(sql, reportIdList).QueryRows(&list)
  111. err = global.DmSQL["rddp"].Raw(sql, reportIdList).Find(&list).Error
  112. return
  113. }
  114. // GetGrantByIdAndAdmin
  115. // @Description: 根据reportId和操作人获取报告权限配置
  116. // @author: Roc
  117. // @receiver m
  118. // @datetime 2024-06-04 15:49:59
  119. // @param reportId int
  120. // @param sysUserId int
  121. // @return item *ReportGrant
  122. // @return err error
  123. func (m ReportGrant) GetGrantByIdAndAdmin(reportId, sysUserId int) (item *ReportGrant, err error) {
  124. //o := orm.NewOrmUsingDB("rddp")
  125. sql := `SELECT * FROM report_grant WHERE report_id = ? AND admin_id = ? `
  126. //err = o.Raw(sql, reportId, sysUserId).QueryRow(&item)
  127. err = global.DmSQL["rddp"].Raw(sql, reportId, sysUserId).First(&item).Error
  128. return
  129. }
  130. // GetGrantListByAdminId
  131. // @Description: 根据id获取授权列表
  132. // @author: Roc
  133. // @receiver m
  134. // @datetime 2024-06-04 15:33:58
  135. // @param adminId int
  136. // @return list []*ReportGrant
  137. // @return err error
  138. func (m ReportGrant) GetGrantListByAdminId(adminId int) (list []*ReportGrant, err error) {
  139. //o := orm.NewOrmUsingDB("rddp")
  140. sql := `SELECT * FROM report_grant WHERE admin_id=? `
  141. //_, err = o.Raw(sql, adminId).QueryRows(&list)
  142. err = global.DmSQL["rddp"].Raw(sql, adminId).Find(&list).Error
  143. return
  144. }