sys_department.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package system
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type SysDepartmentAddReq struct {
  7. DepartmentName string `description:"部门名称"`
  8. }
  9. type SysDepartment struct {
  10. DepartmentId int `orm:"column(department_id);pk" description:"部门Id"`
  11. DepartmentName string `description:"部门名称"`
  12. Sort int `description:"排序"`
  13. CreateTime time.Time `description:"创建时间"`
  14. }
  15. func GetSysDepartmentCount(departmentName string) (count int, err error) {
  16. sql := `SELECT COUNT(1) AS count FROM sys_department WHERE department_name=? `
  17. o := orm.NewOrm()
  18. err = o.Raw(sql, departmentName).QueryRow(&count)
  19. return
  20. }
  21. func AddSysDepartment(item *SysDepartment) (lastId int64, err error) {
  22. o := orm.NewOrm()
  23. lastId, err = o.Insert(item)
  24. return
  25. }
  26. type SysDepartmentEditReq struct {
  27. DepartmentId int `description:"部门Id"`
  28. DepartmentName string `description:"部门名称"`
  29. }
  30. func GetSysDepartmentById(departmentId int) (item *SysDepartment, err error) {
  31. sql := `SELECT * FROM sys_department WHERE department_id=? `
  32. o := orm.NewOrm()
  33. err = o.Raw(sql, departmentId).QueryRow(&item)
  34. return
  35. }
  36. func GetSysDepartmentByName(departmentName string) (item *SysDepartment, err error) {
  37. sql := `SELECT * FROM sys_department WHERE department_name=? `
  38. o := orm.NewOrm()
  39. err = o.Raw(sql, departmentName).QueryRow(&item)
  40. return
  41. }
  42. func ModifySysDepartment(departmentName string, departmentId int) (err error) {
  43. o := orm.NewOrm()
  44. to, err := o.Begin()
  45. if err != nil {
  46. return
  47. }
  48. defer func() {
  49. if err != nil {
  50. _ = to.Rollback()
  51. } else {
  52. _ = to.Commit()
  53. }
  54. }()
  55. sql := `UPDATE sys_department SET department_name=? WHERE department_id=? `
  56. _, err = to.Raw(sql, departmentName, departmentId).Exec()
  57. if err != nil {
  58. return
  59. }
  60. sql = `UPDATE admin SET department_name=? WHERE department_id=? `
  61. _, err = to.Raw(sql, departmentName, departmentId).Exec()
  62. return
  63. }
  64. type SysDepartmentDeleteReq struct {
  65. DepartmentId int `description:"部门Id"`
  66. }
  67. func DeleteSysDepartment(departmentId int) (err error) {
  68. sql := `DELETE FROM sys_department WHERE department_id=? `
  69. o := orm.NewOrm()
  70. _, err = o.Raw(sql, departmentId).Exec()
  71. return
  72. }
  73. type SysDepartmentList struct {
  74. DepartmentId int `orm:"column(department_id);pk" description:"部门Id"`
  75. DepartmentName string `description:"部门名称"`
  76. CreateTime time.Time `description:"创建时间"`
  77. Child []*SysGroupList `description:"分组"`
  78. IsDepartment bool `description:"true:部门,false:分组"`
  79. }
  80. func GetDepartmentList() (items []*SysDepartmentList, err error) {
  81. o := orm.NewOrm()
  82. sql := `SELECT * FROM sys_department ORDER BY sort ASC, create_time ASC `
  83. _, err = o.Raw(sql).QueryRows(&items)
  84. return
  85. }
  86. // GetDepartmentListByIds 根据部门id集合获取部门列表信息
  87. func GetDepartmentListByIds(departmentIds string) (items []*SysDepartmentList, err error) {
  88. o := orm.NewOrm()
  89. sql := `SELECT * FROM sys_department where 1 = 1 `
  90. if departmentIds != "" {
  91. sql += `and department_id in(` + departmentIds + `) `
  92. }
  93. sql += ` ORDER BY sort ASC, create_time ASC `
  94. _, err = o.Raw(sql).QueryRows(&items)
  95. return
  96. }
  97. type SysDepartmentListResp struct {
  98. List []*SysDepartmentList
  99. }
  100. func GetSysDepartmentAll() (item []*SysDepartment, err error) {
  101. sql := `SELECT * FROM sys_department ORDER BY sort ASC, department_id ASC `
  102. o := orm.NewOrm()
  103. _, err = o.Raw(sql).QueryRows(&item)
  104. return
  105. }
  106. type DepartmentSort struct {
  107. DepartmentId int
  108. Sort int
  109. }
  110. func MultiUpdateDepartmentSort(items []*DepartmentSort) (err error) {
  111. if len(items) == 0 {
  112. return
  113. }
  114. o := orm.NewOrm()
  115. p, err := o.Raw("UPDATE sys_department SET sort = ? WHERE department_id = ?").Prepare()
  116. if err != nil {
  117. return
  118. }
  119. defer func() {
  120. _ = p.Close()
  121. }()
  122. for _, v := range items {
  123. _, err = p.Exec(v.Sort, v.DepartmentId)
  124. if err != nil {
  125. return
  126. }
  127. }
  128. return
  129. }
  130. // DepartmentUserTree 部门用户树
  131. type DepartmentUserTree struct {
  132. NodeId int `description:"节点ID"`
  133. NodeType int `description:"节点类型:1-部门;2-分组;3-用户"`
  134. NodeName string `description:"节点名称"`
  135. Children []*DepartmentUserTree `description:"子节点"`
  136. }