package admin

import (
	"errors"
	"server/dao"
)

type DepartmentService struct {
}

func (ds *DepartmentService) QueryAllDep() ([]dao.Department, error) {
	deps, err := dao.QueryAllDep()
	if err != nil {
		return nil, err
	}
	for i, dep := range deps {
		users, err := dao.QueryUsersByDepId(dep.Id)
		if err != nil {
			return nil, err
		}
		deps[i].Users = users
	}

	return deps, nil
}

func (ds *DepartmentService) QueryDepByStart() ([]dao.Department, error) {
	deps, err := dao.QueryDepByStart()
	if err != nil {
		return nil, err
	}
	return deps, nil
}

func (ds *DepartmentService) CreateDep(department dao.Department) error {
	return department.CreateDep()
}

func (ds *DepartmentService) UpdateDep(department dao.Department) error {
	return department.UpdateDep()
}

func (ds *DepartmentService) UpdateDepStatus(depId int, isStart bool) error {
	return dao.UpdateDepStatus(depId, isStart)
}

func (ds *DepartmentService) UpdateUserDep(userIds []int, depId int) error {
	return dao.UpdateUserDep(userIds, depId)
}

func (ds *DepartmentService) DeleteDep(depId int) error {
	users, err := dao.QueryUsersByDepId(depId)
	if err != nil {
		return err
	}
	if len(users) > 0 {
		return errors.New("当前部门下存在用户")
	}
	return dao.DeleteDep(depId)
}
