sys_role_admin.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package crm
  2. import (
  3. "hongze/hz_crm_eta/global"
  4. "time"
  5. )
  6. // SysRoleAdmin 管理员和角色映射表
  7. type SysRoleAdmin struct {
  8. Id int `gorm:"primaryKey;column:id;type:int(11);not null" json:"id"`
  9. RoleId int `gorm:"column:role_id;type:int(11);not null" json:"role_id"` // 角色Id
  10. AdminId int `gorm:"column:admin_id;type:bigint(20);not null" json:"admin_id"` // 管理后台账号Id
  11. CreateTime time.Time `gorm:"column:create_time;type:datetime;not null;default:CURRENT_TIMESTAMP" json:"create_time"` // 创建时间
  12. }
  13. func (m *SysRoleAdmin) TableName() string {
  14. return "sys_role_admin"
  15. }
  16. func (m *SysRoleAdmin) DeleteAndCreate(adminId int, roleIds []int, items []*SysRoleAdmin) (err error) {
  17. tx := global.MYSQL["hz_crm"].Begin()
  18. defer func() {
  19. if err != nil {
  20. tx.Rollback()
  21. } else {
  22. tx.Commit()
  23. }
  24. }()
  25. sql := `DELETE FROM sys_role_admin WHERE admin_id = ? AND role_id IN (?)`
  26. tx.Exec(sql, adminId, roleIds)
  27. if len(items) > 0 {
  28. err = tx.CreateInBatches(items, len(items)).Error
  29. if err != nil {
  30. return
  31. }
  32. }
  33. return
  34. }