sys_group.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package eta
  2. import (
  3. "eta_gn/eta_bridge/global"
  4. "fmt"
  5. "strings"
  6. "time"
  7. )
  8. type SysGroup struct {
  9. GroupId int `gorm:"primaryKey;column:group_id;type:int(11);not null" json:"group_id"`
  10. DepartmentId int `gorm:"index:idx_department_id;column:department_id;type:int(11)" json:"department_id"` // 部门id
  11. GroupName string `gorm:"column:group_name;type:varchar(255);default:''" json:"group_name"` // 分组名称
  12. CreateTime time.Time `gorm:"column:create_time;type:datetime" json:"create_time"`
  13. ParentId int `gorm:"column:parent_id;type:int(11);default:0" json:"parent_id"`
  14. Sort int `gorm:"column:sort;type:int(10);not null;default:0" json:"sort"` // 排序
  15. OutID string `gorm:"column:out_id" json:"out_id"` // 外部id
  16. }
  17. func (m *SysGroup) TableName() string {
  18. return "sys_group"
  19. }
  20. // Create 新增分组
  21. func (m *SysGroup) Create() (err error) {
  22. err = global.MYSQL["hz_eta"].Create(m).Error
  23. return
  24. }
  25. // Update 更新分组
  26. func (m *SysGroup) Update(cols []string) (err error) {
  27. err = global.MYSQL["hz_eta"].Model(m).Select(cols).Updates(m).Error
  28. return
  29. }
  30. // GetSysGroupByGroupId 获取分组-根据主键
  31. func GetSysGroupByGroupId(groupId int) (item *SysGroup, err error) {
  32. err = global.MYSQL["hz_eta"].Where("group_id = ?", groupId).First(&item).Error
  33. return
  34. }
  35. // GetAllSysGroup 获取所有的分组
  36. func GetAllSysGroup() (items []*SysGroup, err error) {
  37. sql := `SELECT * FROM sys_group `
  38. err = global.MYSQL["hz_eta"].Raw(sql).Find(&items).Error
  39. return
  40. }
  41. // DeleteGroupByGroupId 删除分组-根据主键
  42. func DeleteGroupByGroupId(groupId int) (err error) {
  43. sql := `DELETE FROM sys_group WHERE group_id = ? LIMIT 1`
  44. err = global.MYSQL["hz_eta"].Exec(sql, groupId).Error
  45. if err != nil {
  46. return
  47. }
  48. // 清除关联
  49. sql = `UPDATE admin SET group_id = 0, group_name = '' WHERE group_id = ? `
  50. err = global.MYSQL["hz_eta"].Exec(sql, groupId).Error
  51. return
  52. }
  53. func (m *SysGroup) GetItemByCondition(condition string, pars []interface{}) (item *SysGroup, err error) {
  54. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  55. err = global.MYSQL["hz_eta"].Raw(sql, pars...).First(&item).Error
  56. return
  57. }
  58. func (m *SysGroup) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysGroup, err error) {
  59. fields := strings.Join(fieldArr, ",")
  60. if len(fieldArr) == 0 {
  61. fields = `*`
  62. }
  63. order := ``
  64. if orderRule != "" {
  65. order = ` ORDER BY ` + orderRule
  66. }
  67. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  68. err = global.MYSQL["hz_eta"].Raw(sql, pars...).Find(&items).Error
  69. return
  70. }
  71. // GetSysGroupByOutId 获取分组-根据OutId
  72. func GetSysGroupByOutId(outId string) (item *SysGroup, err error) {
  73. err = global.MYSQL["hz_eta"].Where("out_id = ?", outId).First(&item).Error
  74. return
  75. }