AppFeatureService.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.yihu.ehr.basic.apps.service;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.ehr.basic.apps.dao.AppFeatureDao;
  5. import com.yihu.ehr.basic.apps.model.AppFeature;
  6. import com.yihu.ehr.query.BaseJpaService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.data.domain.PageRequest;
  10. import org.springframework.data.domain.Pageable;
  11. import org.springframework.jdbc.core.JdbcTemplate;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * @author linz
  21. * @version 1.0
  22. * @created 2016年7月7日21:04:33
  23. */
  24. @Service
  25. @Transactional
  26. public class AppFeatureService extends BaseJpaService<AppFeature, AppFeatureDao> {
  27. @Autowired
  28. private AppFeatureDao xAppFeatureRepository;
  29. @Autowired
  30. private JdbcTemplate jdbcTemplate;
  31. @Autowired
  32. ObjectMapper objectMapper;
  33. public Page<AppFeature> getAppFeatureList(String sorts, int page, int size){
  34. AppFeatureDao repo = (AppFeatureDao)getJpaRepository();
  35. Pageable pageable = new PageRequest(page, size, parseSorts(sorts));
  36. return repo.findAll(pageable);
  37. }
  38. public AppFeature createAppFeature(AppFeature appFeature) {
  39. xAppFeatureRepository.save(appFeature);
  40. return appFeature;
  41. }
  42. public AppFeature updateAppFeature(AppFeature appFeature){
  43. xAppFeatureRepository.save(appFeature);
  44. return appFeature;
  45. }
  46. public void deleteAppFeature(String id){
  47. xAppFeatureRepository.delete(id);
  48. }
  49. /**
  50. * 生成菜单JSON对象字符串
  51. * @return AppFeature
  52. */
  53. public AppFeature joinMenuItemJsonStr(AppFeature appFeature) throws JsonProcessingException {
  54. ObjectMapper objectMapper = new ObjectMapper();
  55. Map<String, Object> contentMap = new HashMap<>();
  56. contentMap.put("id", appFeature.getId());
  57. contentMap.put("code", appFeature.getCode());
  58. contentMap.put("level", appFeature.getLevel());
  59. contentMap.put("text", appFeature.getName());
  60. contentMap.put("type", appFeature.getType());
  61. contentMap.put("iconUrl", appFeature.getIconUrl());
  62. if (appFeature.getLevel() != 1) {
  63. contentMap.put("pid", appFeature.getParentId());
  64. }
  65. if (appFeature.getUrl().startsWith("/")) {
  66. contentMap.put("url", appFeature.getPrefixUrl() + appFeature.getUrl());
  67. }
  68. appFeature.setContent(objectMapper.writeValueAsString(contentMap));
  69. return appFeature;
  70. }
  71. /**
  72. * 根据权限,获取应用菜单
  73. *
  74. * @param appId 应用ID
  75. * @param userId 用户ID
  76. * @return 菜单JSON字符串集合
  77. */
  78. public List<Map<String, Object>> findAppMenus(String appId, String userId, int parentId) throws IOException {
  79. List<Map<String, Object>> menuList = new ArrayList<>();
  80. String sql = " SELECT DISTINCT af.id AS id, af.content AS content FROM apps_feature af " +
  81. " JOIN role_user ru ON ru.user_id = '" + userId + "' " +
  82. " JOIN role_feature_relation rfr ON rfr.feature_id = af.id AND rfr.role_id = ru.role_id " +
  83. " WHERE af.app_id = '" + appId + "' AND af.parent_id = " + parentId + " AND af.type <> 3 " +
  84. " ORDER BY af.sort ";
  85. List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
  86. for (Map<String, Object> item : list) {
  87. Map<String, Object> conMap = objectMapper.readValue(item.get("content").toString(), Map.class);
  88. menuList.add(conMap);
  89. menuList.addAll(findAppMenus(appId, userId, (int) item.get("id")));
  90. }
  91. return menuList;
  92. }
  93. }