99 lines
3.7 KiB
Java
99 lines
3.7 KiB
Java
package com.yunhai.userhome.service.impl;
|
|
|
|
import com.yunhai.platform.apistd.BizException;
|
|
import com.yunhai.platform.utils.exception.ErrorCode;
|
|
import com.yunhai.userhome.entity.MenuBar;
|
|
import com.yunhai.userhome.entity.UserCurrentRole;
|
|
import com.yunhai.userhome.repository.MenuBarRepository;
|
|
import com.yunhai.userhome.service.IMenuBarService;
|
|
import com.yunhai.userhome.service.IUserCurrentRoleService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 菜单栏服务(对齐旧 UserHome service.impl.MenuBarService)。
|
|
*/
|
|
@Service
|
|
public class MenuBarServiceImpl implements IMenuBarService {
|
|
|
|
@Autowired
|
|
private IUserCurrentRoleService userCurrentRoleService;
|
|
|
|
@Autowired
|
|
private MenuBarRepository menuBarRepository;
|
|
|
|
@Override
|
|
public List<MenuBar> getMenuBarList(Integer schId) {
|
|
List<MenuBar> list;
|
|
if (schId == 0)
|
|
list = menuBarRepository.findBySchId(0);
|
|
else
|
|
list = menuBarRepository.findBySchIdIn(Arrays.asList(0, schId));
|
|
list.forEach(x -> {
|
|
Boolean isHide = x.getHideSchIds().contains(schId);
|
|
x.setIsShow(!isHide);
|
|
x.setIsEdit(x.getSchId().equals(schId));
|
|
});
|
|
list = list.stream().sorted(Comparator.comparing(MenuBar::getOrderNum, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
|
|
return list;
|
|
}
|
|
|
|
@Override
|
|
public List<MenuBar> findMenuBarById(Integer userId) {
|
|
UserCurrentRole userCurrentRole = userCurrentRoleService.findById(userId)
|
|
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
|
List<MenuBar> list = getMenuBarList(userCurrentRole.getSchId());
|
|
List<MenuBar> result = list.stream().filter(MenuBar::getIsShow).collect(Collectors.toList());
|
|
result = result.stream().sorted(Comparator.comparing(MenuBar::getOrderNum, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public void saveMenuBar(MenuBar menuBar) {
|
|
if (menuBar.getId() != null) {
|
|
Optional<MenuBar> optional = menuBarRepository.findById(menuBar.getId());
|
|
if (optional.isPresent()) {
|
|
menuBar.setHideSchIds(optional.get().getHideSchIds());
|
|
}
|
|
}
|
|
menuBarRepository.save(menuBar);
|
|
}
|
|
|
|
@Override
|
|
public void saveMenuBarOrderNum(Integer id, Integer orderNum) {
|
|
Optional<MenuBar> optionalMenuBar = menuBarRepository.findById(id);
|
|
if (!optionalMenuBar.isPresent())
|
|
throw new BizException(ErrorCode.INTERNAL_SERVER_ERROR);
|
|
MenuBar menuBar = optionalMenuBar.get();
|
|
menuBar.setOrderNum(orderNum);
|
|
menuBarRepository.save(menuBar);
|
|
}
|
|
|
|
@Override
|
|
public void toggleMenuBarShow(Integer id, Integer schId) {
|
|
Optional<MenuBar> optionalMenuBar = menuBarRepository.findById(id);
|
|
if (!optionalMenuBar.isPresent())
|
|
throw new BizException(ErrorCode.INTERNAL_SERVER_ERROR);
|
|
MenuBar menuBar = optionalMenuBar.get();
|
|
Integer index = menuBar.getHideSchIds().indexOf(schId);
|
|
List<Integer> schIds = menuBar.getHideSchIds();
|
|
if (index.equals(-1))
|
|
schIds.add(schId);
|
|
else
|
|
schIds.remove(Integer.valueOf(schId));
|
|
menuBar.setHideSchIds(schIds);
|
|
menuBarRepository.save(menuBar);
|
|
}
|
|
|
|
@Override
|
|
public void deleteMenuBar(Integer id) {
|
|
Optional<MenuBar> optionalMenuBar = menuBarRepository.findById(id);
|
|
if (!optionalMenuBar.isPresent())
|
|
throw new BizException(ErrorCode.INTERNAL_SERVER_ERROR);
|
|
menuBarRepository.delete(optionalMenuBar.get());
|
|
}
|
|
}
|