report_chapter_grant.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package report
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "time"
  6. )
  7. type ReportChapterGrant struct {
  8. GrantId int `gorm:"primaryKey;column:grant_id;type:int(9) unsigned;not null"` // 授权id
  9. ReportChapterId int `gorm:"index:idx_report_chapterid;column:report_chapter_id;type:int(9) unsigned;not null;default:0"` // 报告章节id
  10. AdminId int `gorm:"column:admin_id;type:int(9) unsigned;default:0"` // 授权的用户id
  11. CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 授权时间
  12. }
  13. func (m ReportChapterGrant) MultiAddReportChapterGrantGrant(reportChapterId int, list []*ReportChapterGrant) (err error) {
  14. tx := global.DmSQL["rddp"].Begin()
  15. defer func() {
  16. if err != nil {
  17. _ = tx.Rollback()
  18. return
  19. }
  20. _ = tx.Commit()
  21. }()
  22. sql := "DELETE from report_chapter_grant where report_chapter_id=?"
  23. err = tx.Exec(sql, reportChapterId).Error
  24. if err != nil {
  25. return
  26. }
  27. if len(list) > 0 {
  28. e := tx.CreateInBatches(list, utils.MultiAddNum).Error
  29. if e != nil {
  30. err = e
  31. return
  32. }
  33. }
  34. return
  35. }
  36. func (m ReportChapterGrant) GetGrantListById(reportChapterId int) (list []*ReportChapterGrant, err error) {
  37. sql := `SELECT * FROM report_chapter_grant WHERE report_chapter_id=? `
  38. err = global.DmSQL["rddp"].Raw(sql, reportChapterId).Find(&list).Error
  39. return
  40. }
  41. func (m ReportChapterGrant) GetGrantListByIdList(reportChapterIdList []int) (list []*ReportChapterGrant, err error) {
  42. num := len(reportChapterIdList)
  43. if num <= 0 {
  44. return
  45. }
  46. sql := `SELECT * FROM report_chapter_grant WHERE report_chapter_id in (` + utils.GetOrmInReplace(num) + `) `
  47. err = global.DmSQL["rddp"].Raw(sql, reportChapterIdList).Find(&list).Error
  48. return
  49. }
  50. func (m ReportChapterGrant) GetGrantByIdAndAdmin(reportChapterId, sysUserId int) (item *ReportGrant, err error) {
  51. sql := `SELECT * FROM report_chapter_grant WHERE report_chapter_id = ? AND admin_id = ? `
  52. err = global.DmSQL["rddp"].Raw(sql, reportChapterId, sysUserId).First(&item).Error
  53. return
  54. }