package service import ( "hospital-admin-api/api/dao" "hospital-admin-api/api/responses/deptResponse" "strconv" ) type DeptService struct { } // GetDeptList 获取部门列表 func (r *DeptService) GetDeptList() ([]*deptResponse.GetDeptList, error) { // 获取全部部门 adminDeptDao := dao.AdminDeptDao{} adminDept, _ := adminDeptDao.GetAdminDeptList() if adminDept == nil { return nil, nil } deptMap := make(map[int64]*deptResponse.GetDeptList) var deptTree []*deptResponse.GetDeptList for _, dept := range adminDept { node := &deptResponse.GetDeptList{ DeptId: strconv.FormatInt(dept.DeptId, 10), ParentId: strconv.FormatInt(dept.ParentId, 10), DeptName: dept.DeptName, DeptStatus: dept.DeptStatus, CreatedAt: dept.CreatedAt, UpdatedAt: dept.UpdatedAt, Children: nil, } deptMap[dept.DeptId] = node } // 构建菜单树 for _, dept := range adminDept { if dept.ParentId == 0 { if deptMap[dept.DeptId] == nil { continue } deptTree = append(deptTree, deptMap[dept.DeptId]) } else if parent, ok := deptMap[dept.ParentId]; ok { parent.Children = append(parent.Children, deptMap[dept.DeptId]) } } return deptTree, nil } // AddDept 新增部门 // func (r *DeptService) AddDept(addDeptRequest requests.AddDept) (bool, error) { // adminDeptDao := dao.AdminDeptDao{} // // parentId, err := strconv.ParseInt(addDeptRequest.ParentId, 10, 64) // if err != nil { // return false, errors.New("新增失败") // } // // // 判断父级id // if parentId != 0 { // adminDept, _ := adminDeptDao.GetAdminDeptById(parentId) // if adminDept == nil { // return false, errors.New("上级部门错误") // } // } // // // 开始事务 // tx := global.Db.Begin() // defer func() { // if r := recover(); r != nil { // tx.Rollback() // } // }() // // // 新增菜单 // AdminMenuModel := &model.AdminMenu{ // MenuName: AddMenuRequest.MenuName, // MenuTitle: AddMenuRequest.MenuTitle, // ParentId: ParentId, // MenuStatus: AddMenuRequest.MenuStatus, // MenuType: AddMenuRequest.MenuType, // Permission: AddMenuRequest.Permission, // OrderNum: AddMenuRequest.OrderNum, // Icon: AddMenuRequest.Icon, // Path: AddMenuRequest.Path, // Component: AddMenuRequest.Component, // } // // adminMenu, _ := AdminMenuDao.AddAdminMenu(tx, AdminMenuModel) // if adminMenu == nil { // tx.Rollback() // return false, errors.New("新增失败") // } // // // 新增菜单api // if len(AddMenuRequest.ApiIds) > 0 { // for _, v := range AddMenuRequest.ApiIds { // // 将字符串转换为int64类型 // v, err := strconv.ParseInt(v, 10, 64) // if err != nil { // tx.Rollback() // return false, errors.New("新增失败") // } // // // 检测api // adminApi, _ := AdminApiDao.GetAdminApiById(v) // if adminApi == nil { // tx.Rollback() // return false, errors.New("接口不存在") // } // // AdminMenuApiModel := &model.AdminMenuApi{ // MenuId: adminMenu.MenuId, // ApiId: v, // } // // adminMenuApi, _ := AdminMenuApiDao.AddAdminMenuApi(tx, AdminMenuApiModel) // if adminMenuApi == nil { // tx.Rollback() // return false, errors.New("新增失败") // } // } // } // // tx.Commit() // // return true, nil // }