SourceServiceImpl.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package com.fuint.common.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.fuint.common.domain.TreeSelect;
  4. import com.fuint.common.enums.StatusEnum;
  5. import com.fuint.common.service.SourceService;
  6. import com.fuint.common.vo.MetaVo;
  7. import com.fuint.common.vo.RouterVo;
  8. import com.fuint.framework.annoation.OperationServiceLog;
  9. import com.fuint.repository.mapper.TSourceMapper;
  10. import com.fuint.repository.model.TSource;
  11. import com.fuint.common.domain.TreeNode;
  12. import com.fuint.framework.exception.BusinessCheckException;
  13. import com.fuint.utils.ArrayUtil;
  14. import com.fuint.utils.StringUtil;
  15. import lombok.AllArgsConstructor;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.util.*;
  19. import java.util.stream.Collectors;
  20. /**
  21. * 菜单管理接口实现类
  22. *
  23. * Created by FSQ
  24. * CopyRight https://www.fuint.cn
  25. */
  26. @Service
  27. @AllArgsConstructor
  28. public class SourceServiceImpl extends ServiceImpl<TSourceMapper, TSource> implements SourceService {
  29. private TSourceMapper tSourceMapper;
  30. /**
  31. * 获取有效的角色集合
  32. *
  33. * @param merchantId 商户ID
  34. * @return
  35. */
  36. @Override
  37. public List<TSource> getAvailableSources(Integer merchantId) {
  38. return tSourceMapper.findByStatus(merchantId, StatusEnum.ENABLED.getKey());
  39. }
  40. /**
  41. * 获取菜单的属性结构
  42. *
  43. * @param merchantId 商户IF
  44. * @return
  45. */
  46. @Override
  47. public List<TreeNode> getSourceTree(Integer merchantId) {
  48. List<TSource> tSources = getAvailableSources(merchantId);
  49. List<TreeNode> trees = new ArrayList<>();
  50. if (tSources != null && tSources.size() > 0) {
  51. TreeNode sourceTreeNode;
  52. for (TSource tSource : tSources) {
  53. sourceTreeNode = new TreeNode();
  54. sourceTreeNode.setName(tSource.getSourceName());
  55. sourceTreeNode.setId(tSource.getSourceId());
  56. sourceTreeNode.setLevel(tSource.getSourceLevel());
  57. sourceTreeNode.setSort((tSource.getSourceStyle() == null || StringUtil.isEmpty(tSource.getSourceStyle())) ? 0 : Integer.parseInt(tSource.getSourceStyle()));
  58. sourceTreeNode.setPath(tSource.getPath());
  59. sourceTreeNode.setIcon(tSource.getNewIcon());
  60. sourceTreeNode.setIsMenu(tSource.getIsMenu());
  61. sourceTreeNode.setStatus(tSource.getStatus());
  62. sourceTreeNode.setPerms(tSource.getPath().replaceAll("/", ":"));
  63. if (tSource.getParentId() != null) {
  64. sourceTreeNode.setPId(tSource.getParentId());
  65. } else {
  66. sourceTreeNode.setPId(0);
  67. }
  68. trees.add(sourceTreeNode);
  69. }
  70. }
  71. return trees;
  72. }
  73. /**
  74. * 根据菜单ID集合查询菜单列表信息
  75. *
  76. * @param ids 菜单ID
  77. * @return
  78. */
  79. @Override
  80. public List<TSource> findDatasByIds(String[] ids) {
  81. Long[] arrays = new Long[ids.length];
  82. for (int i = 0; i < ids.length; i++) {
  83. arrays[i] = Long.parseLong(ids[i]);
  84. }
  85. return tSourceMapper.findByIdIn(ArrayUtil.toList(arrays));
  86. }
  87. /**
  88. * 根据账号ID获取菜单列表
  89. *
  90. * @param merchantId 商户ID
  91. * @param accountId 账号ID
  92. * @throws BusinessCheckException
  93. */
  94. @Override
  95. public List<TSource> getMenuListByUserId(Integer merchantId, Integer accountId) {
  96. if (merchantId == null) {
  97. merchantId = 0;
  98. }
  99. List<TSource> sourceList = tSourceMapper.findSourcesByAccountId(merchantId, accountId);
  100. return delRepeated(sourceList);
  101. }
  102. /**
  103. * 构建前端路由所需要的菜单
  104. *
  105. * @param treeNodes 菜单列表
  106. * @return 路由列表
  107. */
  108. @Override
  109. public List<RouterVo> buildMenus(List<TreeNode> treeNodes) {
  110. List<RouterVo> routers = new LinkedList<>();
  111. for (TreeNode menu : treeNodes) {
  112. RouterVo router = new RouterVo();
  113. if (menu.getIsMenu() == 0) {
  114. router.setHidden(true);
  115. } else {
  116. router.setHidden(false);
  117. }
  118. router.setName(menu.getEname());
  119. if (menu.getLevel() == 1) {
  120. router.setComponent("Layout");
  121. router.setPath("/" + menu.getEname().toLowerCase());
  122. router.setRedirect("noRedirect");
  123. router.setAlwaysShow(true);
  124. } else {
  125. if (menu.getIsMenu() == 2) {
  126. router.setAlwaysShow(true);
  127. } else {
  128. router.setAlwaysShow(false);
  129. }
  130. router.setComponent(menu.getPath());
  131. router.setPath('/' + menu.getPath());
  132. }
  133. router.setMeta(new MetaVo(menu.getName(), menu.getNewIcon(), false, null));
  134. List<TreeNode> cMenus = menu.getChildrens();
  135. if (cMenus != null && !cMenus.isEmpty() && cMenus.size() > 0) {
  136. router.setChildren(buildMenus(cMenus));
  137. }
  138. routers.add(router);
  139. }
  140. return routers;
  141. }
  142. /**
  143. * 构建前端所需要下拉树结构
  144. *
  145. * @param menus 菜单列表
  146. * @return 下拉树结构列表
  147. */
  148. @Override
  149. public List<TreeSelect> buildMenuTreeSelect(List<TreeNode> menus) {
  150. List<TreeNode> menuTrees = buildMenuTree(menus);
  151. return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  152. }
  153. /**
  154. * 构建前端所需要树结构
  155. *
  156. * @param menus 菜单列表
  157. * @return 树结构列表
  158. */
  159. @Override
  160. public List<TreeNode> buildMenuTree(List<TreeNode> menus) {
  161. List<TreeNode> returnList = new ArrayList<TreeNode>();
  162. List<Long> tempList = new ArrayList<Long>();
  163. for (TreeNode dept : menus) {
  164. tempList.add(dept.getId());
  165. }
  166. for (Iterator<TreeNode> iterator = menus.iterator(); iterator.hasNext();) {
  167. TreeNode menu = (TreeNode) iterator.next();
  168. // 如果是顶级节点, 遍历该父节点的所有子节点
  169. if (!tempList.contains(menu.getPId())) {
  170. recursionFn(menus, menu);
  171. returnList.add(menu);
  172. }
  173. }
  174. if (returnList.isEmpty()) {
  175. returnList = menus;
  176. }
  177. return returnList;
  178. }
  179. /**
  180. * 添加菜单
  181. *
  182. * @param tSource 菜单参数
  183. * @return
  184. */
  185. @Override
  186. @Transactional(rollbackFor = Exception.class)
  187. @OperationServiceLog(description = "新增后台菜单")
  188. public void addSource(TSource tSource) {
  189. this.save(tSource);
  190. }
  191. /**
  192. * 修改菜单
  193. *
  194. * @param source 菜单参数
  195. * @return
  196. */
  197. @Override
  198. @Transactional(rollbackFor = Exception.class)
  199. @OperationServiceLog(description = "修改后台菜单")
  200. public void editSource(TSource source) {
  201. tSourceMapper.updateById(source);
  202. }
  203. /**
  204. * 删除菜单
  205. *
  206. * @param source
  207. * @return
  208. * */
  209. @Override
  210. @Transactional(rollbackFor = Exception.class)
  211. @OperationServiceLog(description = "删除后台菜单")
  212. public void deleteSource(TSource source) {
  213. source.setStatus(StatusEnum.DISABLE.getKey());
  214. editSource(source);
  215. Map<String, Object> param = new HashMap<>();
  216. param.put("STATUS", StatusEnum.ENABLED.getKey());
  217. param.put("PARENT_ID", source.getSourceId());
  218. List<TSource> dataList = tSourceMapper.selectByMap(param);
  219. if (dataList != null && dataList.size() > 0) {
  220. for (TSource tSource : dataList) {
  221. deleteSource(tSource);
  222. }
  223. }
  224. }
  225. /**
  226. * 菜单去重
  227. *
  228. * @param sources
  229. * @return
  230. */
  231. private List<TSource> delRepeated(List<TSource> sources) {
  232. List<TSource> distinct = new ArrayList<>();
  233. if (sources != null) {
  234. Map<Long, Boolean> sourceMap = new HashMap<>();
  235. for (TSource tSource : sources) {
  236. if (sourceMap.get(tSource.getSourceId()) == null) {
  237. sourceMap.put(tSource.getSourceId().longValue(), true);
  238. distinct.add(tSource);
  239. }
  240. }
  241. }
  242. return distinct;
  243. }
  244. /**
  245. * 递归列表
  246. *
  247. * @param list
  248. * @param t
  249. */
  250. private void recursionFn(List<TreeNode> list, TreeNode t) {
  251. // 得到子节点列表
  252. List<TreeNode> childList = getChildList(list, t);
  253. t.setChildrens(childList);
  254. for (TreeNode tChild : childList) {
  255. if (hasChild(list, tChild)) {
  256. recursionFn(list, tChild);
  257. }
  258. }
  259. }
  260. /**
  261. * 得到子节点列表
  262. *
  263. * @param list 菜单列表
  264. * @param t 当前节点
  265. * @return
  266. */
  267. private List<TreeNode> getChildList(List<TreeNode> list, TreeNode t) {
  268. List<TreeNode> tList = new ArrayList<TreeNode>();
  269. Iterator<TreeNode> it = list.iterator();
  270. while (it.hasNext()) {
  271. TreeNode n = it.next();
  272. if (n.getPId() == t.getId()) {
  273. tList.add(n);
  274. }
  275. }
  276. return tList;
  277. }
  278. /**
  279. * 判断是否有子节点
  280. *
  281. * @param list 菜单列表
  282. * @param t 当前节点
  283. * @return
  284. */
  285. private boolean hasChild(List<TreeNode> list, TreeNode t) {
  286. return getChildList(list, t).size() > 0;
  287. }
  288. }