report_grant.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. tx := global.DmSQL["rddp"].Begin()
  31. defer func() {
  32. if err != nil {
  33. _ = tx.Rollback()
  34. return
  35. }
  36. _ = tx.Commit()
  37. }()
  38. sql := "DELETE from report_grant where report_id=?"
  39. err = tx.Exec(sql, reportId).Error
  40. if err != nil {
  41. return
  42. }
  43. // 新增授权记录
  44. if len(list) > 0 {
  45. e := tx.CreateInBatches(list, utils.MultiAddNum).Error
  46. if e != nil {
  47. err = e
  48. return
  49. }
  50. }
  51. return
  52. }
  53. // GetGrantListById
  54. // @Description: 根据id获取授权列表
  55. // @author: Roc
  56. // @receiver m
  57. // @datetime 2024-06-04 15:33:58
  58. // @param reportId int
  59. // @return list []*ReportGrant
  60. // @return err error
  61. func (m ReportGrant) GetGrantListById(reportId int) (list []*ReportGrant, err error) {
  62. sql := `SELECT * FROM report_grant WHERE report_id=? `
  63. err = global.DmSQL["rddp"].Raw(sql, reportId).Find(&list).Error
  64. return
  65. }
  66. // GetGrantListByIdList
  67. // @Description: 根据id列表获取授权列表
  68. // @author: Roc
  69. // @receiver m
  70. // @datetime 2024-06-04 15:33:58
  71. // @param reportIdList []int
  72. // @return list []*ReportGrant
  73. // @return err error
  74. func (m ReportGrant) GetGrantListByIdList(reportIdList []int) (list []*ReportGrant, err error) {
  75. num := len(reportIdList)
  76. if num <= 0 {
  77. return
  78. }
  79. sql := `SELECT * FROM report_grant WHERE report_id in (` + utils.GetOrmInReplace(num) + `) `
  80. err = global.DmSQL["rddp"].Raw(sql, reportIdList).Find(&list).Error
  81. return
  82. }
  83. // GetGrantByIdAndAdmin
  84. // @Description: 根据reportId和操作人获取报告权限配置
  85. // @author: Roc
  86. // @receiver m
  87. // @datetime 2024-06-04 15:49:59
  88. // @param reportId int
  89. // @param sysUserId int
  90. // @return item *ReportGrant
  91. // @return err error
  92. func (m ReportGrant) GetGrantByIdAndAdmin(reportId, sysUserId int) (item *ReportGrant, err error) {
  93. sql := `SELECT * FROM report_grant WHERE report_id = ? AND admin_id = ? `
  94. err = global.DmSQL["rddp"].Raw(sql, reportId, sysUserId).First(&item).Error
  95. return
  96. }
  97. // GetGrantListByAdminId
  98. // @Description: 根据id获取授权列表
  99. // @author: Roc
  100. // @receiver m
  101. // @datetime 2024-06-04 15:33:58
  102. // @param adminId int
  103. // @return list []*ReportGrant
  104. // @return err error
  105. func (m ReportGrant) GetGrantListByAdminId(adminId int) (list []*ReportGrant, err error) {
  106. sql := `SELECT * FROM report_grant WHERE admin_id=? `
  107. err = global.DmSQL["rddp"].Raw(sql, adminId).Find(&list).Error
  108. return
  109. }