sys_department.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package system
  2. import (
  3. "eta_gn/eta_api/global"
  4. "time"
  5. )
  6. type SysDepartmentAddReq struct {
  7. DepartmentName string `description:"部门名称"`
  8. }
  9. type SysDepartment struct {
  10. DepartmentId int `gorm:"primaryKey;" 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. err = global.DEFAULT_DmSQL.Raw(sql, departmentName).Scan(&count).Error
  18. return
  19. }
  20. func AddSysDepartment(item *SysDepartment) (lastId int64, err error) {
  21. err = global.DEFAULT_DmSQL.Create(item).Error
  22. return
  23. }
  24. type SysDepartmentEditReq struct {
  25. DepartmentId int `description:"部门Id"`
  26. DepartmentName string `description:"部门名称"`
  27. }
  28. func GetSysDepartmentById(departmentId int) (item *SysDepartment, err error) {
  29. sql := `SELECT * FROM sys_department WHERE department_id=? `
  30. err = global.DEFAULT_DmSQL.Raw(sql, departmentId).First(&item).Error
  31. return
  32. }
  33. func GetSysDepartmentByName(departmentName string) (item *SysDepartment, err error) {
  34. sql := `SELECT * FROM sys_department WHERE department_name=? `
  35. err = global.DEFAULT_DmSQL.Raw(sql, departmentName).First(&item).Error
  36. return
  37. }
  38. func ModifySysDepartment(departmentName string, departmentId int) (err error) {
  39. tx := global.DEFAULT_DmSQL.Begin()
  40. defer func() {
  41. if err != nil {
  42. _ = tx.Rollback()
  43. return
  44. }
  45. _ = tx.Commit()
  46. }()
  47. sql := `UPDATE sys_department SET department_name=? WHERE department_id=? `
  48. err = tx.Exec(sql, departmentName, departmentId).Error
  49. if err != nil {
  50. return
  51. }
  52. sql = `UPDATE "admin" SET department_name=? WHERE department_id=? `
  53. err = tx.Exec(sql, departmentName, departmentId).Error
  54. return
  55. }
  56. type SysDepartmentDeleteReq struct {
  57. DepartmentId int `description:"部门Id"`
  58. }
  59. func DeleteSysDepartment(departmentId int) (err error) {
  60. sql := `DELETE FROM sys_department WHERE department_id=? `
  61. err = global.DEFAULT_DmSQL.Exec(sql, departmentId).Error
  62. return
  63. }
  64. type SysDepartmentList struct {
  65. DepartmentId int `orm:"column(department_id);pk" description:"部门Id"`
  66. DepartmentName string `description:"部门名称"`
  67. CreateTime time.Time `description:"创建时间"`
  68. Child []*SysGroupList `gorm:"-" description:"分组"`
  69. IsDepartment bool `description:"true:部门,false:分组"`
  70. }
  71. func GetDepartmentList() (items []*SysDepartmentList, err error) {
  72. sql := `SELECT * FROM sys_department ORDER BY sort ASC, create_time ASC `
  73. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  74. return
  75. }
  76. // GetDepartmentListByIds 根据部门id集合获取部门列表信息
  77. func GetDepartmentListByIds(departmentIds string) (items []*SysDepartmentList, err error) {
  78. sql := `SELECT * FROM sys_department where 1 = 1 `
  79. if departmentIds != "" {
  80. sql += `and department_id in(` + departmentIds + `) `
  81. }
  82. sql += ` ORDER BY sort ASC, create_time ASC `
  83. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  84. return
  85. }
  86. type SysDepartmentListResp struct {
  87. List []*SysDepartmentList
  88. }
  89. func GetSysDepartmentAll() (item []*SysDepartment, err error) {
  90. sql := `SELECT * FROM sys_department ORDER BY sort ASC, department_id ASC `
  91. err = global.DEFAULT_DmSQL.Raw(sql).Find(&item).Error
  92. return
  93. }
  94. type DepartmentSort struct {
  95. DepartmentId int
  96. Sort int
  97. }
  98. func MultiUpdateDepartmentSort(items []*DepartmentSort) (err error) {
  99. if len(items) == 0 {
  100. return
  101. }
  102. sql := "UPDATE sys_department SET sort = ? WHERE department_id = ?"
  103. for _, v := range items {
  104. err = global.DEFAULT_DmSQL.Exec(sql, v.Sort, v.DepartmentId).Error
  105. if err != nil {
  106. return
  107. }
  108. }
  109. return
  110. }
  111. // DepartmentUserTree 部门用户树
  112. type DepartmentUserTree struct {
  113. NodeId int `description:"节点ID"`
  114. NodeType int `description:"节点类型:1-部门;2-分组;3-用户"`
  115. NodeName string `description:"节点名称"`
  116. Children []*DepartmentUserTree `gorm:"-" description:"子节点"`
  117. }