report_grant.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package models
  2. import (
  3. "eta_gn/eta_report/global"
  4. "eta_gn/eta_report/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. // ReportGrant 报告授权表
  10. type ReportGrant struct {
  11. GrantId int `gorm:"primaryKey;column:grant_id;type:int(9) unsigned;not null"` // 授权id
  12. ReportId int `gorm:"column:report_id;type:int(9) unsigned;not null;default:0"` // 报告id
  13. AdminId int `gorm:"column:admin_id;type:int(9) unsigned;default:0"` // 授权的用户id
  14. CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP"` // 授权时间
  15. }
  16. func (m *ReportGrant) TableName() string {
  17. return "report_grant"
  18. }
  19. type ReportGrantCols struct {
  20. PrimaryId string
  21. ReportId string
  22. AdminId string
  23. CreateTime string
  24. }
  25. func (m *ReportGrant) Cols() ReportGrantCols {
  26. return ReportGrantCols{
  27. PrimaryId: "grant_id",
  28. ReportId: "report_id",
  29. AdminId: "admin_id",
  30. CreateTime: "create_time",
  31. }
  32. }
  33. func (m *ReportGrant) Create() (err error) {
  34. err = global.DEFAULT_DmSQL.Create(m).Error
  35. return
  36. }
  37. func (m *ReportGrant) CreateMulti(items []*ReportGrant) (err error) {
  38. if len(items) == 0 {
  39. return
  40. }
  41. err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
  42. return
  43. }
  44. func (m *ReportGrant) Update(cols []string) (err error) {
  45. err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
  46. return
  47. }
  48. func (m *ReportGrant) Remove() (err error) {
  49. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  50. err = global.DEFAULT_DmSQL.Exec(sql, m.GrantId).Error
  51. return
  52. }
  53. func (m *ReportGrant) MultiRemove(ids []int) (err error) {
  54. if len(ids) == 0 {
  55. return
  56. }
  57. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  58. err = global.DEFAULT_DmSQL.Exec(sql, ids).Error
  59. return
  60. }
  61. func (m *ReportGrant) RemoveByCondition(condition string, pars []interface{}) (err error) {
  62. if condition == "" {
  63. return
  64. }
  65. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  66. err = global.DEFAULT_DmSQL.Exec(sql, pars...).Error
  67. return
  68. }
  69. func (m *ReportGrant) GetItemById(id int) (item *ReportGrant, err error) {
  70. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  71. err = global.DEFAULT_DmSQL.Raw(sql, id).First(&item).Error
  72. return
  73. }
  74. func (m *ReportGrant) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportGrant, err error) {
  75. order := ``
  76. if orderRule != "" {
  77. order = ` ORDER BY ` + orderRule
  78. }
  79. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  80. err = global.DEFAULT_DmSQL.Raw(sql, pars...).First(&item).Error
  81. return
  82. }
  83. func (m *ReportGrant) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  84. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  85. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  86. return
  87. }
  88. func (m *ReportGrant) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportGrant, err error) {
  89. fields := strings.Join(fieldArr, ",")
  90. if len(fieldArr) == 0 {
  91. fields = `*`
  92. }
  93. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  94. if orderRule != "" {
  95. order = ` ORDER BY ` + orderRule
  96. }
  97. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  98. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  99. return
  100. }