sys_department.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package wx_crm
  2. import (
  3. "eta/eta_bridge/global"
  4. "gorm.io/gorm/clause"
  5. )
  6. type DepartmentType string
  7. const (
  8. SubCompany DepartmentType = "sub"
  9. Department DepartmentType = "department"
  10. )
  11. type SysDepartment struct {
  12. SysDepartmentId int `gorm:"primaryKey;column:sys_department_id;type:int(11);not null" json:"department_id"`
  13. SysDepartmentName string `gorm:"unique;column:sys_department_name;type:varchar(255);default:''" json:"department_name"` // 部门名称
  14. OutId int `gorm:"column:out_id;type:int(11);not null" json:"out_id"`
  15. Type DepartmentType `gorm:"column:type;type:varchar(50);not null" json:"type"`
  16. Sort int `gorm:"column:sort;type:int(10);not null;default:0" json:"sort"` // 排序
  17. Level int `gorm:"column:level" json:"level"` // 外部id
  18. ParentId int `gorm:"column:parent_id;type:int(11);not null" json:"parent_id"`
  19. }
  20. func (m *SysDepartment) TableName() string {
  21. return "sys_department"
  22. }
  23. func BatchInsertOrUpdate(departments []SysDepartment) (err error) {
  24. db := global.MYSQL["ht_crm"]
  25. OnConflictFunc := clause.OnConflict{
  26. Columns: []clause.Column{{Name: "out_id"}, {Name: "type"}},
  27. DoUpdates: clause.AssignmentColumns([]string{"sys_department_name", "level", "sort", "parent_id"}),
  28. }
  29. // 执行批量插入或更新操作
  30. err = db.Clauses(OnConflictFunc).Create(&departments).Error
  31. return
  32. }
  33. func GetDepartmentList() (list []SysDepartment, err error) {
  34. db := global.MYSQL["ht_crm"]
  35. err = db.Select("*").Find(&list).Error
  36. if err != nil {
  37. global.FILE_LOG.Error("获取CRM部门列表失败", err.Error())
  38. }
  39. return
  40. }