sys_department.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package models
  2. import (
  3. "context"
  4. "eta/eta_mini_crm_ht/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. )
  8. type DepartmentType string
  9. const (
  10. SubCompany DepartmentType = "sub"
  11. Department DepartmentType = "department"
  12. )
  13. type SysDepartment struct {
  14. SysDepartmentId int `orm:"pk" description:"部门id"`
  15. SysDepartmentName string `description:"部门名称"`
  16. OutId int `gorm:"column:out_id;type:int(11);not null" json:"out_id"`
  17. Type DepartmentType `gorm:"column:type;type:varchar(50);not null" json:"type"`
  18. Sort int `description:"排序"`
  19. Level int `description:"层级"`
  20. ParentId int `description:"父目录id"`
  21. }
  22. func (s *SysDepartment) Add() (err error) {
  23. o := orm.NewOrm()
  24. _, err = o.Insert(s)
  25. return
  26. }
  27. func (s *SysDepartment) Update(cols []string) (err error) {
  28. o := orm.NewOrm()
  29. _, err = o.Update(s, cols...)
  30. return
  31. }
  32. type SysDepartmentList struct {
  33. SysDepartmentId int `description:"部门id"`
  34. SysDepartmentName string `description:"部门名称"`
  35. OutId int `gorm:"column:outId;type:int(11);not null" `
  36. Type DepartmentType `gorm:"column:type;type:varchar(50);not null"`
  37. Child []*SysDepartmentList `description:"分组"`
  38. Level int `description:"1:部门, 2|3|4:分组"`
  39. ParentId int `description:"父目录id"`
  40. }
  41. func DeleteSysDepartmentByIdV2(sysDepartmentIds []string, level int) (err error) {
  42. o := orm.NewOrm()
  43. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  44. // sql := `DELETE FROM sys_department WHERE sys_department_id IN (?) `
  45. _, e := txOrm.QueryTable(&SysDepartment{}).
  46. Filter("sys_department_id__in", sysDepartmentIds).
  47. Delete()
  48. if e != nil {
  49. return e
  50. }
  51. sql := `UPDATE sys_user SET `
  52. for i := level; i <= utils.MaxDepartmentLevel; i++ {
  53. sql += fmt.Sprintf("sys_department_id%d=0 ", i)
  54. if i != utils.MaxDepartmentLevel {
  55. sql += ","
  56. }
  57. }
  58. sql += fmt.Sprintf("WHERE sys_department_id%d=?", level)
  59. _, e = txOrm.Raw(sql, sysDepartmentIds[0]).Exec()
  60. if e != nil {
  61. return e
  62. }
  63. return nil
  64. })
  65. return
  66. }
  67. func UpdateDepartmentSortByIds(sysDepartmentIds []int) (err error) {
  68. o := orm.NewOrm()
  69. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  70. sql := `UPDATE sys_department SET sort=? WHERE sys_department_id=?`
  71. for i, depId := range sysDepartmentIds {
  72. _, e := txOrm.Raw(sql, i+1, depId).Exec()
  73. if e != nil {
  74. return e
  75. }
  76. }
  77. return nil
  78. })
  79. return
  80. }
  81. func GetSysDepartmentById(sysDepartmentId int) (item *SysDepartment, err error) {
  82. o := orm.NewOrm()
  83. sql := `SELECT * FROM sys_department WHERE sys_department_id=? `
  84. err = o.Raw(sql, sysDepartmentId).QueryRow(&item)
  85. return
  86. }
  87. func GetSysDepartments() (items []*SysDepartment, err error) {
  88. o := orm.NewOrm()
  89. sql := `SELECT * FROM sys_department `
  90. _, err = o.Raw(sql).QueryRows(&items)
  91. return
  92. }
  93. func GetSysDepartmentList() (items []*SysDepartmentList, err error) {
  94. o := orm.NewOrm()
  95. sql := `SELECT * FROM sys_department ORDER BY sort`
  96. _, err = o.Raw(sql).QueryRows(&items)
  97. return
  98. }
  99. func GetSysDepartmentCountById(sysDepartmentId int) (count int, err error) {
  100. o := orm.NewOrm()
  101. sql := `SELECT COUNT(1) AS count FROM sys_department WHERE sys_department_id=?`
  102. err = o.Raw(sql, sysDepartmentId).QueryRow(&count)
  103. return
  104. }
  105. func GetSysDepartmentCountByParentId(parentId int, sysDepartmentName string) (count int, err error) {
  106. o := orm.NewOrm()
  107. sql := `SELECT COUNT(1) AS count FROM sys_department WHERE parent_id=? AND sys_department_name=?`
  108. err = o.Raw(sql, parentId, sysDepartmentName).QueryRow(&count)
  109. return
  110. }
  111. func GetSysDepartmentCountByName(sysDepartmentName string) (count int, err error) {
  112. o := orm.NewOrm()
  113. sql := `SELECT COUNT(1) AS count FROM sys_department WHERE sys_department_name=?`
  114. err = o.Raw(sql, sysDepartmentName).QueryRow(&count)
  115. return
  116. }
  117. func GetSysDepartmentByName(sysDepartmentName string) (item *SysDepartment, err error) {
  118. o := orm.NewOrm()
  119. sql := `SELECT * FROM sys_department WHERE sys_department_name=?`
  120. err = o.Raw(sql, sysDepartmentName).QueryRow(&item)
  121. return
  122. }
  123. func GetSysDepartmentListByParentId(parentId int) (item SysDepartmentList, err error) {
  124. o := orm.NewOrm()
  125. sql := `SELECT * FROM sys_department WHERE sys_department_id=? `
  126. err = o.Raw(sql, parentId).QueryRow(&item)
  127. return
  128. }
  129. func GetChildSysDepartmentListById(sysDepartmentId int) (items []*SysDepartmentList, err error) {
  130. o := orm.NewOrm()
  131. sql := `SELECT * FROM sys_department WHERE parent_id=? `
  132. _, err = o.Raw(sql, sysDepartmentId).QueryRows(&items)
  133. return
  134. }
  135. func GetChildSysDepartmentListByIds(sysDepartmentIds string) (items []*SysDepartmentList, err error) {
  136. o := orm.NewOrm()
  137. sql := `SELECT * FROM sys_department WHERE parent_id in (?) `
  138. _, err = o.Raw(sql, sysDepartmentIds).QueryRows(&items)
  139. return
  140. }