sys_department.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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:out_id;type:int(11);not null" json:"out_id"`
  36. Type DepartmentType `gorm:"column:type;type:varchar(50);not null" json:"type"`
  37. Child []*SysDepartmentList `description:"分组"`
  38. Level int `description:"1:部门, 2|3:分组"`
  39. ParentId int `description:"父目录id"`
  40. }
  41. func DeleteSysDepartmentById(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. if level == 1 {
  52. sql = `DELETE FROM sys_user_department_mapping WHERE sys_department_id1=?`
  53. _, e = txOrm.Raw(sql, sysDepartmentIds[0]).Exec()
  54. if e != nil {
  55. return e
  56. }
  57. } else {
  58. sql = `UPDATE sys_user_department_mapping SET `
  59. for i := level; i <= utils.MaxDepartmentLevel; i++ {
  60. sql += fmt.Sprintf("sys_department_id%d=0 ", i)
  61. if i != utils.MaxDepartmentLevel {
  62. sql += ","
  63. }
  64. }
  65. sql += fmt.Sprintf("WHERE sys_department_id%d=?", level)
  66. _, e = txOrm.Raw(sql, sysDepartmentIds[0]).Exec()
  67. if e != nil {
  68. return e
  69. }
  70. }
  71. return nil
  72. })
  73. return
  74. }
  75. func DeleteSysDepartmentByIdV2(sysDepartmentIds []string, level int) (err error) {
  76. o := orm.NewOrm()
  77. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  78. // sql := `DELETE FROM sys_department WHERE sys_department_id IN (?) `
  79. _, e := txOrm.QueryTable(&SysDepartment{}).
  80. Filter("sys_department_id__in", sysDepartmentIds).
  81. Delete()
  82. if e != nil {
  83. return e
  84. }
  85. sql := `UPDATE sys_user SET `
  86. for i := level; i <= utils.MaxDepartmentLevel; i++ {
  87. sql += fmt.Sprintf("sys_department_id%d=0 ", i)
  88. if i != utils.MaxDepartmentLevel {
  89. sql += ","
  90. }
  91. }
  92. sql += fmt.Sprintf("WHERE sys_department_id%d=?", level)
  93. _, e = txOrm.Raw(sql, sysDepartmentIds[0]).Exec()
  94. if e != nil {
  95. return e
  96. }
  97. return nil
  98. })
  99. return
  100. }
  101. func UpdateDepartmentSortByIds(sysDepartmentIds []int) (err error) {
  102. o := orm.NewOrm()
  103. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  104. sql := `UPDATE sys_department SET sort=? WHERE sys_department_id=?`
  105. for i, depId := range sysDepartmentIds {
  106. _, e := txOrm.Raw(sql, i+1, depId).Exec()
  107. if e != nil {
  108. return e
  109. }
  110. }
  111. return nil
  112. })
  113. return
  114. }
  115. func GetSysDepartmentById(sysDepartmentId int) (item *SysDepartment, err error) {
  116. o := orm.NewOrm()
  117. sql := `SELECT * FROM sys_department WHERE sys_department_id=? `
  118. err = o.Raw(sql, sysDepartmentId).QueryRow(&item)
  119. return
  120. }
  121. func GetSysDepartments() (items []*SysDepartment, err error) {
  122. o := orm.NewOrm()
  123. sql := `SELECT * FROM sys_department `
  124. _, err = o.Raw(sql).QueryRows(&items)
  125. return
  126. }
  127. func GetSysDepartmentList() (items []*SysDepartmentList, err error) {
  128. o := orm.NewOrm()
  129. sql := `SELECT * FROM sys_department ORDER BY sort`
  130. _, err = o.Raw(sql).QueryRows(&items)
  131. return
  132. }
  133. func GetSysDepartmentCountById(sysDepartmentId int) (count int, err error) {
  134. o := orm.NewOrm()
  135. sql := `SELECT COUNT(1) AS count FROM sys_department WHERE sys_department_id=?`
  136. err = o.Raw(sql, sysDepartmentId).QueryRow(&count)
  137. return
  138. }
  139. func GetSysDepartmentCountByParentId(parentId int, sysDepartmentName string) (count int, err error) {
  140. o := orm.NewOrm()
  141. sql := `SELECT COUNT(1) AS count FROM sys_department WHERE parent_id=? AND sys_department_name=?`
  142. err = o.Raw(sql, parentId, sysDepartmentName).QueryRow(&count)
  143. return
  144. }
  145. func GetSysDepartmentCountByName(sysDepartmentName string) (count int, err error) {
  146. o := orm.NewOrm()
  147. sql := `SELECT COUNT(1) AS count FROM sys_department WHERE sys_department_name=?`
  148. err = o.Raw(sql, sysDepartmentName).QueryRow(&count)
  149. return
  150. }
  151. func GetSysDepartmentByName(sysDepartmentName string) (item *SysDepartment, err error) {
  152. o := orm.NewOrm()
  153. sql := `SELECT * FROM sys_department WHERE sys_department_name=?`
  154. err = o.Raw(sql, sysDepartmentName).QueryRow(&item)
  155. return
  156. }
  157. func GetSysDepartmentListByParentId(parentId int) (item SysDepartmentList, err error) {
  158. o := orm.NewOrm()
  159. sql := `SELECT * FROM sys_department WHERE sys_department_id=? `
  160. err = o.Raw(sql, parentId).QueryRow(&item)
  161. return
  162. }
  163. func GetChildSysDepartmentListById(sysDepartmentId int) (items []*SysDepartmentList, err error) {
  164. o := orm.NewOrm()
  165. sql := `SELECT * FROM sys_department WHERE parent_id=? `
  166. _, err = o.Raw(sql, sysDepartmentId).QueryRows(&items)
  167. return
  168. }
  169. func GetChildSysDepartmentListByIds(sysDepartmentIds string) (items []*SysDepartmentList, err error) {
  170. o := orm.NewOrm()
  171. sql := `SELECT * FROM sys_department WHERE parent_id in (?) `
  172. _, err = o.Raw(sql, sysDepartmentIds).QueryRows(&items)
  173. return
  174. }