165 lines
4.1 KiB
Go
165 lines
4.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"next-terminal/server/common/maps"
|
|
"next-terminal/server/model"
|
|
"next-terminal/server/repository"
|
|
"next-terminal/server/utils"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type DepartmentApi struct{}
|
|
|
|
func (api DepartmentApi) AllEndpoint(c echo.Context) error {
|
|
items, err := repository.DepartmentRepository.FindAll(context.TODO())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, items)
|
|
}
|
|
|
|
func (api DepartmentApi) PagingEndpoint(c echo.Context) error {
|
|
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
|
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
|
name := c.QueryParam("keyword")
|
|
order := c.QueryParam("order")
|
|
field := c.QueryParam("field")
|
|
|
|
items, total, err := repository.DepartmentRepository.Find(context.TODO(), pageIndex, pageSize, name, order, field)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
departmentMap := make(map[string]model.Department)
|
|
allDepts, _ := repository.DepartmentRepository.FindAll(context.TODO())
|
|
for _, dept := range allDepts {
|
|
departmentMap[dept.ID] = dept
|
|
}
|
|
|
|
for i := range items {
|
|
if items[i].ParentId != "" {
|
|
if parent, ok := departmentMap[items[i].ParentId]; ok {
|
|
items[i].ParentName = parent.Name
|
|
}
|
|
}
|
|
userCount, _ := repository.UserDepartmentRepository.CountByDepartmentId(context.TODO(), items[i].ID)
|
|
items[i].UserCount = userCount
|
|
}
|
|
|
|
return Success(c, maps.Map{
|
|
"items": items,
|
|
"total": total,
|
|
})
|
|
}
|
|
|
|
func (api DepartmentApi) CreateEndpoint(c echo.Context) error {
|
|
var item model.Department
|
|
if err := c.Bind(&item); err != nil {
|
|
return err
|
|
}
|
|
item.ID = utils.UUID()
|
|
|
|
if err := repository.DepartmentRepository.Create(context.TODO(), &item); err != nil {
|
|
return err
|
|
}
|
|
|
|
return Success(c, item)
|
|
}
|
|
|
|
func (api DepartmentApi) UpdateEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
var item model.Department
|
|
if err := c.Bind(&item); err != nil {
|
|
return err
|
|
}
|
|
item.ID = id
|
|
|
|
if err := repository.DepartmentRepository.UpdateById(context.TODO(), &item); err != nil {
|
|
return err
|
|
}
|
|
|
|
return Success(c, item)
|
|
}
|
|
|
|
func (api DepartmentApi) DeleteEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
|
|
count, err := repository.DepartmentRepository.CountByParentId(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return Fail(c, -1, "该部门下存在子部门,无法删除")
|
|
}
|
|
|
|
userCount, err := repository.UserDepartmentRepository.CountByDepartmentId(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if userCount > 0 {
|
|
return Fail(c, -1, "该部门下存在用户,无法删除")
|
|
}
|
|
|
|
if err := repository.DepartmentRepository.DeleteById(context.TODO(), id); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api DepartmentApi) GetEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
item, err := repository.DepartmentRepository.FindById(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, item)
|
|
}
|
|
|
|
func (api DepartmentApi) GetTreeEndpoint(c echo.Context) error {
|
|
departments, err := repository.DepartmentRepository.FindAll(context.TODO())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tree := repository.DepartmentRepository.BuildTree(departments)
|
|
return Success(c, tree)
|
|
}
|
|
|
|
func (api DepartmentApi) GetDepartmentUsersEndpoint(c echo.Context) error {
|
|
departmentId := c.Param("id")
|
|
users, err := repository.UserDepartmentRepository.FindUsersByDepartmentId(context.TODO(), departmentId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, users)
|
|
}
|
|
|
|
func (api DepartmentApi) SetDepartmentUsersEndpoint(c echo.Context) error {
|
|
departmentId := c.Param("id")
|
|
var userIds []string
|
|
if err := c.Bind(&userIds); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := repository.UserDepartmentRepository.SaveDepartmentUsers(context.TODO(), departmentId, userIds); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api DepartmentApi) RemoveUsersFromDepartmentEndpoint(c echo.Context) error {
|
|
departmentId := c.Param("id")
|
|
var userIds []string
|
|
if err := c.Bind(&userIds); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := repository.UserDepartmentRepository.RemoveUsersFromDepartment(context.TODO(), departmentId, userIds); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|