sys_department.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package eta
  2. import (
  3. "eta/eta_bridge/global"
  4. "gorm.io/gorm/clause"
  5. "time"
  6. )
  7. type SysDepartment struct {
  8. DepartmentId int `gorm:"primaryKey;column:department_id;type:int(11);not null" json:"department_id"`
  9. DepartmentName string `gorm:"unique;column:department_name;type:varchar(255);default:''" json:"department_name"` // 部门名称
  10. OutID string `gorm:"column:out_id" json:"out_id"` // 外部id
  11. CreateTime time.Time `gorm:"column:create_time;type:datetime" json:"create_time"` // 创建时间
  12. Sort int `gorm:"column:sort;type:int(10);not null;default:0" json:"sort"` // 排序
  13. }
  14. func (m *SysDepartment) TableName() string {
  15. return "sys_department"
  16. }
  17. // Create 新增部门
  18. func (m *SysDepartment) Create() (err error) {
  19. err = global.MYSQL["hz_eta"].Create(m).Error
  20. return
  21. }
  22. // Update 更新部门
  23. func (m *SysDepartment) Update(cols []string) (err error) {
  24. err = global.MYSQL["hz_eta"].Model(m).Select(cols).Updates(m).Error
  25. return
  26. }
  27. // GetDepartmentById 主键获取部门
  28. func GetDepartmentById(departmentId int) (item *SysDepartment, err error) {
  29. err = global.MYSQL["hz_eta"].Where("department_id = ?", departmentId).First(&item).Error
  30. return
  31. }
  32. // GetAllDepartment 获取所有部门
  33. func GetAllDepartment() (items []*SysDepartment, err error) {
  34. err = global.MYSQL["hz_eta"].Find(&items).Error
  35. return
  36. }
  37. // DeleteDepartmentById 主键删除部门
  38. func DeleteDepartmentById(departmentId int) (err error) {
  39. sql := `DELETE FROM sys_department WHERE department_id = ? LIMIT 1`
  40. err = global.MYSQL["hz_eta"].Exec(sql, departmentId).Error
  41. return
  42. }
  43. func BatchInsertOrUpdate(departments []SysDepartment) (err error) {
  44. db := global.MYSQL["hz_eta"]
  45. OnConflictFunc := clause.OnConflict{
  46. Columns: []clause.Column{{Name: "department_id"}},
  47. DoUpdates: clause.AssignmentColumns([]string{"department_name", "sort"}),
  48. }
  49. // 执行批量插入或更新操作
  50. err = db.Clauses(OnConflictFunc).Create(&departments).Error
  51. return
  52. }