From 3bb1aafd6836a3672c2315c19a3c29e16a7c6574 Mon Sep 17 00:00:00 2001 From: taichu110 <1477638737@qq.com> Date: Fri, 28 Jun 2024 19:16:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=8F=9C=E5=8D=95=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MenuServiceImpl.java | 160 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 MenuServiceImpl.java diff --git a/MenuServiceImpl.java b/MenuServiceImpl.java new file mode 100644 index 0000000..a19a78e --- /dev/null +++ b/MenuServiceImpl.java @@ -0,0 +1,160 @@ +package com.intelligentHealthCare.service.impl; + +import com.intelligentHealthCare.config.CacheNameConfig; +import com.intelligentHealthCare.dto.MenuDto; +import com.intelligentHealthCare.entity.*; +import com.intelligentHealthCare.exception.BizException; +import com.intelligentHealthCare.interceptor.AccountInfoInterceptor; +import com.intelligentHealthCare.repository.AccountRepository; +import com.intelligentHealthCare.repository.MenuRepository; +import com.intelligentHealthCare.repository.RoleMenuPermissionRepository; +import com.intelligentHealthCare.service.MenuService; +import com.intelligentHealthCare.service.RoleMenuPermissionService; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import java.util.*; +import java.util.stream.Collectors; + +@Service +@CacheConfig(cacheNames = {CacheNameConfig.PLATFORM_MENU_ACCOUNT, CacheNameConfig.PLATFORM_MENU}) +public class MenuServiceImpl extends AbstractSimpleJpaService implements MenuService { + + @Autowired + private RoleMenuPermissionService roleMenuPermissionService; + + @Autowired + private RoleMenuPermissionRepository roleMenuPermissionRepository; + + @Autowired + private AccountRepository accountRepository; + + @Override + @Cacheable(cacheNames = CacheNameConfig.PLATFORM_MENU_ACCOUNT, key = "#accountId", unless = "#result==null") + public List getMenusByAccountId(String accountId) { + //通过账号获取菜单 + QAccountRolePermission accountRolePermission = QAccountRolePermission.accountRolePermission; + QRoleMenuPermission roleMenuPermission = QRoleMenuPermission.roleMenuPermission; + return queryFactory.select(roleMenuPermission).from(roleMenuPermission).leftJoin(accountRolePermission).on(roleMenuPermission.role.id + .eq(accountRolePermission.role.id)).where(accountRolePermission.account.id.eq(accountId)).fetch() + .stream().map(r -> r.getMenu().getMenuPath()).collect(Collectors.toList()); + } + + @Override + @Cacheable(cacheNames = CacheNameConfig.PLATFORM_MENU_ACCOUNT, key = "#accountId", unless = "#result==null") + public List getMenuTreeByAccountId(String accountId) { + if (accountRepository.getOne(AccountInfoInterceptor.userId.get()).getSystemManager()) { + return this.getMenuTree(); + } + QAccountRolePermission accountRolePermission = QAccountRolePermission.accountRolePermission; + QRoleMenuPermission roleMenuPermission = QRoleMenuPermission.roleMenuPermission; + LinkedHashMap menuDtoMap = queryFactory.select(roleMenuPermission).from(roleMenuPermission).leftJoin(accountRolePermission).on(roleMenuPermission.role.id + .eq(accountRolePermission.role.id)).where(accountRolePermission.account.id.eq(accountId)).orderBy(roleMenuPermission.menu.menuSort.asc()).fetch() + .stream().map(RoleMenuPermission::getMenu).collect(Collectors.toMap(Menu::getId, v ->{ + MenuDto menuDto = new MenuDto(); + BeanUtils.copyProperties(v, menuDto); + return menuDto; + }, (key1, key2) -> key1, LinkedHashMap::new)); + LinkedList temp = new LinkedList<>(); + menuDtoMap.forEach((k, v) -> { + if (v.getMenuParent().equals("0")) { + temp.add(v); + return; + } + MenuDto menuDto = menuDtoMap.get(v.getMenuParent()); + if (menuDto != null) { + if (menuDto.getChildrenList() == null) { + menuDto.setChildrenList(new LinkedList<>()); + } + menuDto.getChildrenList().add(v); + }else { + temp.add(v); + } + }); + return temp; + } + + @Override + public List getMenuTree() { + Map menuDtoMap = this.getAll().stream().sorted(Comparator.comparingInt(Menu::getMenuSort)).collect(Collectors.toMap(Menu::getId, v ->{ + MenuDto menuDto = new MenuDto(); + BeanUtils.copyProperties(v, menuDto); + return menuDto; + }, (key1, key2) -> key1, LinkedHashMap::new)); + LinkedList temp = new LinkedList<>(); + menuDtoMap.forEach((k, v) -> { + if (v.getMenuParent().equals("0")) { + temp.add(v); + } else { + MenuDto menuDto = menuDtoMap.get(v.getMenuParent()); + if (menuDto != null) { + if (menuDto.getChildrenList() == null) { + menuDto.setChildrenList(new LinkedList<>()); + } + menuDto.getChildrenList().add(v); + } + } + }); + return temp; + } + + @Override + public List getParentMenu() { + return repository.getByMenuParent("0"); + } + + @Override + public Boolean existSameMenuTitle(String menuTitle) { + List menus = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuTitle), menuTitle))); + return !menus.isEmpty(); + } + + @Override + public Boolean existSameMenuPath(String menuPath) { + List menus = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuPath), menuPath))); + return !menus.isEmpty(); + } + + + @Override + @CacheEvict(allEntries = true) + public boolean delete(String id) { + List menus = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuParent), id))); + if (!menus.isEmpty()) { + throw new BizException("请先删除子级菜单"); + } + return super.delete(id); + } + + /** + * 父级菜单路径修改,子级对应修改 + * + * @param menu + * @return + */ + @Override + @CacheEvict(allEntries = true) + public Menu update(Menu menu) { + Menu queryMenu = this.getById(menu.getId()); + if (menu.getMenuParent().equals("0")) { + if (!queryMenu.getMenuPath().equals(menu.getMenuPath())) { + List childrenList = this.getByFilter(((root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get(Menu_.menuParent), menu.getId()))); + childrenList.forEach(children -> { + children.setMenuPath(children.getMenuPath().replace(queryMenu.getMenuPath(), menu.getMenuPath())); + }); + super.saveAll(childrenList); + } + } + return super.update(menu); + } + + @Override + @CacheEvict(allEntries = true) + public Menu save(Menu menu) { + return super.save(menu); + } +} From 38ef791f4711a8964e2e1e1e02f9eb83ef1a2947 Mon Sep 17 00:00:00 2001 From: 1545558448 <1545558448@qq.com> Date: Fri, 28 Jun 2024 20:43:31 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=81=8C=E5=8A=A1?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OfficeServiceImpl.java | 148 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 OfficeServiceImpl.java diff --git a/OfficeServiceImpl.java b/OfficeServiceImpl.java new file mode 100644 index 0000000..3097b90 --- /dev/null +++ b/OfficeServiceImpl.java @@ -0,0 +1,148 @@ +package com.intelligentHealthCare.service.impl; + +import com.intelligentHealthCare.dto.OfficeDto; +import com.intelligentHealthCare.entity.Department; +import com.intelligentHealthCare.entity.DepartmentOffice; +import com.intelligentHealthCare.entity.Office; +import com.intelligentHealthCare.exception.BizException; +import com.intelligentHealthCare.repository.DepartmentOfficeRepository; +import com.intelligentHealthCare.repository.OfficeRepository; +import com.intelligentHealthCare.service.DepartmentOfficeService; +import com.intelligentHealthCare.service.OfficeService; +import com.intelligentHealthCare.service.OfficeStaffService; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + + +@Service +@RequiredArgsConstructor(onConstructor = @__({@Autowired})) +public class OfficeServiceImpl extends AbstractSimpleJpaService implements OfficeService { + + private final DepartmentOfficeService departmentOfficeService; + + private final DepartmentOfficeRepository departmentOfficeRepository; + + @Override + public List getTree() { + List officeList = repository.findAll(); + ConvertTree(officeList); + List officeIds = officeList.stream().map(Office::getId).collect(Collectors.toList()); + List departmentOffices = departmentOfficeRepository.getDepartmentOfficeByOfficeIdIn(officeIds); + departmentOffices.forEach(d -> { + officeList.forEach(o -> { + if(o.getId().equals(d.getOffice().getId())){ + if(o.getDepartments() == null){ + o.setDepartments(new LinkedList<>()); + } + o.getDepartments().add(d.getDepartment()); + } + }); + }); + return ConvertTree(officeList); + } + + @Override + @Transactional + public boolean delete(String id) { + List childrenList = repository.findAllByOfficeParent(id); + if(!childrenList.isEmpty()){ + throw new BizException("请先删除子级职务"); + } + //删除和部门之间的关系 + int i = departmentOfficeRepository.deleteAllByOfficeId(id); + repository.deleteById(id); + return true; + } + + @Override + @Transactional + public OfficeDto saveOfficeAndDepartmentOffice(OfficeDto officeDto) { + Office office = new Office(); + BeanUtils.copyProperties(officeDto, office); + //保存职务 + Office saved = repository.saveAndFlush(office); + saved.setOfficeTree(StringUtils.hasText(saved.getOfficeTree()) ? saved.getOfficeTree() + "-" + saved.getId() + : saved.getId()); + //保存职务与部门关系,并且过滤出数据库没有的数据 + List allDepartmentOffice = departmentOfficeService.getAll(); + List departmentOffices = officeDto.getDepartmentIds().stream().filter(p -> allDepartmentOffice.stream().noneMatch(a ->{ + return p.equals(a.getDepartment().getId()) && a.getOffice().getId().equals(saved.getId()); + })).map(i -> { + return DepartmentOffice.builder().office(saved).department(Department.builder().id(i).build()).build(); + }).collect(Collectors.toList()); + List savedDepartmentOffices = departmentOfficeService.saveAll(departmentOffices); + OfficeDto returnOfficeDto = new OfficeDto(); + BeanUtils.copyProperties(saved, returnOfficeDto); + returnOfficeDto.setDepartmentOffices(savedDepartmentOffices); + return returnOfficeDto; + } + + @Override + @Transactional + public OfficeDto updateDto(OfficeDto officeDto) { + if(officeDto.getId() != null && officeDto.getOfficeParent() != null && officeDto.getId().equals(officeDto.getOfficeParent())){ + throw new BizException("上级职务不能是自己"); + } + String oldOfficeId = this.getById(officeDto.getId()).getId(); + //删除职务和部门关系 + departmentOfficeRepository.deleteAllByOfficeId(officeDto.getId()); + //修改数据 + OfficeDto savedOfficeDto = this.saveOfficeAndDepartmentOffice(officeDto); + List childrenOffice = repository.findAllByOfficeParent(officeDto.getId()); + if(!childrenOffice.isEmpty() && !oldOfficeId.equals(savedOfficeDto.getOfficeParent())){ + //更新所有子级的officeTree + List allOffice = this.getAll(); + Office parent = new Office(); + BeanUtils.copyProperties(savedOfficeDto, parent); + List updateOffices = new LinkedList<>(); + updateChildrenTree(parent, allOffice, updateOffices); + this.saveAll(updateOffices); + } + return savedOfficeDto; + } + + private List ConvertTree(List officeList) { + ArrayList list = new ArrayList<>(); + for (Office office : officeList) { + if (office.getOfficeParent().equals("0")) { + list.add(childrenTree(office, officeList)); + } + } + return list; + } + + private Office childrenTree(Office office, List officeList) { + office.setChildren(new ArrayList<>()); // 创建一个新的子节点列表 + for (Office item : officeList) { + if (office.getId().equals(item.getOfficeParent())){ + office.getChildren().add(childrenTree(item, officeList)); + } + } + return office; + } + + private void updateChildrenTree(Office parent, List allOffice, List updateOffices){ + //递归终止条件,集合中没有子级为止 + List childrenList = allOffice.stream().filter(office -> office.getOfficeParent().equals(parent.getId())).collect(Collectors.toList()); + if(childrenList.isEmpty()){ + return; + } + //更新所有子级tree + childrenList.forEach(i -> { + i.setOfficeTree(parent.getOfficeTree() + "-" + i.getId()); + updateOffices.add(i); + //递归更新 + updateChildrenTree(i, allOffice, updateOffices); + }); + } + +} From 966160abb3b712c5a8c266a381abe2b6cf1fea9c Mon Sep 17 00:00:00 2001 From: 1875510253 <1875510253@qq.com> Date: Fri, 28 Jun 2024 20:45:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=81=8C=E5=8A=A1?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/api.iml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 api/api.iml diff --git a/api/api.iml b/api/api.iml new file mode 100644 index 0000000..49e7d1b --- /dev/null +++ b/api/api.iml @@ -0,0 +1,18 @@ + + + + + + + + + + \ No newline at end of file