sys_group.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package eta
  2. import (
  3. "eta/eta_bridge/global"
  4. "gorm.io/gorm/clause"
  5. "time"
  6. )
  7. type SysGroup struct {
  8. GroupId int `gorm:"primaryKey;column:group_id;type:int(11);not null" json:"group_id"`
  9. DepartmentId int `gorm:"index:idx_department_id;column:department_id;type:int(11)" json:"department_id"` // 部门id
  10. GroupName string `gorm:"column:group_name;type:varchar(255);default:''" json:"group_name"` // 分组名称
  11. CreateTime time.Time `gorm:"column:create_time;type:datetime" json:"create_time"`
  12. ParentId int `gorm:"column:parent_id;type:int(11);default:0" json:"parent_id"`
  13. Sort int `gorm:"column:sort;type:int(10);not null;default:0" json:"sort"` // 排序
  14. OutID string `gorm:"column:out_id" json:"out_id"` // 外部id
  15. }
  16. func (m *SysGroup) TableName() string {
  17. return "sys_group"
  18. }
  19. // Create 新增分组
  20. func (m *SysGroup) Create() (err error) {
  21. err = global.MYSQL["hz_eta"].Create(m).Error
  22. return
  23. }
  24. // Update 更新分组
  25. func (m *SysGroup) Update(cols []string) (err error) {
  26. err = global.MYSQL["hz_eta"].Model(m).Select(cols).Updates(m).Error
  27. return
  28. }
  29. // GetSysGroupByGroupId 获取分组-根据主键
  30. func GetSysGroupByGroupId(groupId int) (item *SysGroup, err error) {
  31. err = global.MYSQL["hz_eta"].Where("group_id = ?", groupId).First(&item).Error
  32. return
  33. }
  34. // GetAllSysGroup 获取所有的分组
  35. func GetAllSysGroup() (items []*SysGroup, err error) {
  36. err = global.MYSQL["hz_eta"].Find(&items).Error
  37. return
  38. }
  39. // DeleteGroupByGroupId 删除分组-根据主键
  40. func DeleteGroupByGroupId(groupId int) (err error) {
  41. sql := `DELETE FROM sys_group WHERE group_id = ? LIMIT 1`
  42. err = global.MYSQL["hz_eta"].Exec(sql, groupId).Error
  43. if err != nil {
  44. return
  45. }
  46. // 清除关联
  47. sql = `UPDATE admin SET group_id = 0, group_name = '' WHERE group_id = ? `
  48. err = global.MYSQL["hz_eta"].Exec(sql, groupId).Error
  49. return
  50. }
  51. func BatchInsertOrUpdateGroup(departments []SysGroup) (err error) {
  52. db := global.MYSQL["hz_eta"]
  53. OnConflictFunc := clause.OnConflict{
  54. Columns: []clause.Column{{Name: "group_id"}},
  55. DoUpdates: clause.AssignmentColumns([]string{"department_id", "group_name", "parent_id", "sort"}),
  56. }
  57. // 执行批量插入或更新操作
  58. err = db.Clauses(OnConflictFunc).Create(&departments).Error
  59. return
  60. }