sys_department.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. func GetDepartmentListByIds(departmentIds string) (items []*SysDepartmentList, err error) {
  77. sql := `SELECT * FROM sys_department where 1 = 1 `
  78. if departmentIds != "" {
  79. sql += `and department_id in(` + departmentIds + `) `
  80. }
  81. sql += ` ORDER BY sort ASC, create_time ASC `
  82. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  83. return
  84. }
  85. type SysDepartmentListResp struct {
  86. List []*SysDepartmentList
  87. }
  88. func GetSysDepartmentAll() (item []*SysDepartment, err error) {
  89. sql := `SELECT * FROM sys_department ORDER BY sort ASC, department_id ASC `
  90. err = global.DEFAULT_DmSQL.Raw(sql).Find(&item).Error
  91. return
  92. }
  93. type DepartmentSort struct {
  94. DepartmentId int
  95. Sort int
  96. }
  97. func MultiUpdateDepartmentSort(items []*DepartmentSort) (err error) {
  98. if len(items) == 0 {
  99. return
  100. }
  101. sql := "UPDATE sys_department SET sort = ? WHERE department_id = ?"
  102. for _, v := range items {
  103. err = global.DEFAULT_DmSQL.Exec(sql, v.Sort, v.DepartmentId).Error
  104. if err != nil {
  105. return
  106. }
  107. }
  108. return
  109. }
  110. type DepartmentUserTree struct {
  111. NodeId int `description:"节点ID"`
  112. NodeType int `description:"节点类型:1-部门;2-分组;3-用户"`
  113. NodeName string `description:"节点名称"`
  114. Children []*DepartmentUserTree `gorm:"-" description:"子节点"`
  115. }