department.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package admin
  2. import (
  3. "errors"
  4. "server/dao"
  5. )
  6. type DepartmentService struct {
  7. }
  8. func (ds *DepartmentService) QueryAllDep() ([]dao.Department, error) {
  9. deps, err := dao.QueryAllDep()
  10. if err != nil {
  11. return nil, err
  12. }
  13. for i, dep := range deps {
  14. users, err := dao.QueryUsersByDepId(dep.Id)
  15. if err != nil {
  16. return nil, err
  17. }
  18. deps[i].Users = users
  19. }
  20. return deps, nil
  21. }
  22. func (ds *DepartmentService) QueryDepByStart() ([]dao.Department, error) {
  23. deps, err := dao.QueryDepByStart()
  24. if err != nil {
  25. return nil, err
  26. }
  27. return deps, nil
  28. }
  29. func (ds *DepartmentService) CreateDep(department dao.Department) error {
  30. user, _ := dao.QueryUserByUserId(uint(department.PrincipalId))
  31. department.PrincipalName = user.NickName
  32. return department.CreateDep()
  33. }
  34. func (ds *DepartmentService) UpdateDep(department dao.Department) error {
  35. return department.UpdateDep()
  36. }
  37. func (ds *DepartmentService) UpdateDepStatus(depId int, isStart bool) error {
  38. return dao.UpdateDepStatus(depId, isStart)
  39. }
  40. func (ds *DepartmentService) UpdateUserDep(userIds []int, depId int) error {
  41. return dao.UpdateUserDep(userIds, depId)
  42. }
  43. func (ds *DepartmentService) DeleteDep(depId int) error {
  44. users, err := dao.QueryUsersByDepId(depId)
  45. if err != nil {
  46. return err
  47. }
  48. if len(users) > 0 {
  49. return errors.New("当前部门下存在用户")
  50. }
  51. return dao.DeleteDep(depId)
  52. }