AppPageDesignServiceImpl.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.xjrsoft.module.app.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.lang.TypeReference;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import cn.hutool.json.JSON;
  7. import cn.hutool.json.JSONArray;
  8. import cn.hutool.json.JSONObject;
  9. import cn.hutool.json.JSONUtil;
  10. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  11. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import com.xjrsoft.common.constant.GlobalConstant;
  14. import com.xjrsoft.common.enums.YesOrNoEnum;
  15. import com.xjrsoft.common.exception.MyException;
  16. import com.xjrsoft.common.utils.RedisUtil;
  17. import com.xjrsoft.config.GeneratePathConfig;
  18. import com.xjrsoft.module.app.dto.AddAppPageDesignDto;
  19. import com.xjrsoft.module.app.dto.UpdateAppPageDesignDto;
  20. import com.xjrsoft.module.app.entity.AppMenu;
  21. import com.xjrsoft.module.app.entity.AppPageDesign;
  22. import com.xjrsoft.module.app.mapper.AppMenuMapper;
  23. import com.xjrsoft.module.app.mapper.AppPageDesignMapper;
  24. import com.xjrsoft.module.app.service.IAppPageDesignService;
  25. import com.xjrsoft.module.generator.utils.GeneratorUtil;
  26. import com.xjrsoft.module.system.entity.DictionaryDetail;
  27. import lombok.AllArgsConstructor;
  28. import lombok.SneakyThrows;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import java.io.File;
  32. import java.nio.charset.StandardCharsets;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Optional;
  37. /**
  38. * <p>
  39. * xjr_data_display【数据展示表】 服务实现类
  40. * </p>
  41. *
  42. * @author hnyyzy
  43. * @since 2023-06-25
  44. */
  45. @Service
  46. @AllArgsConstructor
  47. public class AppPageDesignServiceImpl extends ServiceImpl<AppPageDesignMapper, AppPageDesign> implements IAppPageDesignService {
  48. private final AppMenuMapper appMenuMapper;
  49. private final GeneratePathConfig generatePathConfig;
  50. private final RedisUtil redisUtil;
  51. @Override
  52. @Transactional(rollbackFor = Exception.class)
  53. @SneakyThrows
  54. public Boolean add(AddAppPageDesignDto dto) {
  55. long count = count(Wrappers.<AppPageDesign>query().lambda().eq(AppPageDesign::getName, dto.getName()));
  56. if (count > 0) {
  57. throw new MyException("此数据展示页已经存在!");
  58. }
  59. AppPageDesign appPageDesign = BeanUtil.toBean(dto, AppPageDesign.class);
  60. List<DictionaryDetail> detailList = redisUtil.get(GlobalConstant.DIC_DETAIL_CACHE_KEY, new TypeReference<List<DictionaryDetail>>() {
  61. });
  62. if (dto.getIsMenu() == YesOrNoEnum.YES.getCode()) {
  63. Optional<DictionaryDetail> first = detailList.stream().filter(x -> x.getId().equals(dto.getCategoryId())).findFirst();
  64. if (!first.isPresent()) {
  65. throw new MyException("找不到当前功能类别信息!");
  66. }
  67. AppMenu appMenu = new AppMenu();
  68. appMenu.setCode(dto.getCode());
  69. appMenu.setName(dto.getName());
  70. appMenu.setIcon(dto.getIcon());
  71. appMenu.setCategoryId(dto.getCategoryId());
  72. appMenu.setRemark(dto.getRemark());
  73. appMenu.setUrl(StringPool.SLASH+"pages"+StringPool.SLASH +first.get().getValue().toLowerCase() + StringPool.SLASH + dto.getCode().toLowerCase());
  74. appMenuMapper.insert(appMenu);
  75. appPageDesign.setAppMenuId(appMenu.getId());
  76. save(appPageDesign);
  77. //保存成功之后 生成代码
  78. GeneratorUtil.writeFile(getPageOutputDir(first.get().getValue()), dto.getCode().toLowerCase() + StringPool.DOT + "vue", dto.getPageCode());
  79. File tempFile = FileUtil.file(generatePathConfig.getAppPath() + StringPool.SLASH + "pages.json");
  80. JSON jsonContent = JSONUtil.readJSON(tempFile, StandardCharsets.UTF_8);
  81. Map<String, Object> pageJsonMap = new HashMap<>();
  82. pageJsonMap.put("path", "pages/" + first.get().getValue().toLowerCase() + StringPool.SLASH + dto.getCode().toLowerCase());
  83. Map<String, Object> styleMap = new HashMap<>();
  84. styleMap.put("navigationBarTitleText", dto.getName());
  85. pageJsonMap.put("style", styleMap);
  86. JSONArray pages = JSONUtil.parseArray(jsonContent.getByPath("pages").toString());
  87. pages.add(pageJsonMap);
  88. jsonContent.putByPath("pages", pages);
  89. FileUtil.writeString(jsonContent.toJSONString(4), tempFile, StandardCharsets.UTF_8);
  90. } else {
  91. save(appPageDesign);
  92. return Boolean.TRUE;
  93. }
  94. return Boolean.TRUE;
  95. }
  96. @Override
  97. @Transactional(rollbackFor = Exception.class)
  98. @SneakyThrows
  99. public Boolean modify(UpdateAppPageDesignDto dto) {
  100. AppPageDesign appPageDesign = BeanUtil.toBean(dto, AppPageDesign.class);
  101. if (dto.getIsMenu() == YesOrNoEnum.YES.getCode()) {
  102. List<DictionaryDetail> detailList = redisUtil.get(GlobalConstant.DIC_DETAIL_CACHE_KEY, new TypeReference<List<DictionaryDetail>>() {
  103. });
  104. Optional<DictionaryDetail> first = detailList.stream().filter(x -> x.getId().equals(dto.getCategoryId())).findFirst();
  105. if (!first.isPresent()) {
  106. throw new MyException("找不到当前功能类别信息!");
  107. }
  108. GeneratorUtil.writeFile(getPageOutputDir(first.get().getValue().toLowerCase()), dto.getCode().toLowerCase() + StringPool.DOT + "vue", dto.getPageCode());
  109. AppMenu appMenu = new AppMenu();
  110. appMenu.setCode(dto.getCode());
  111. appMenu.setName(dto.getName());
  112. appMenu.setIcon(dto.getIcon());
  113. appMenu.setCategoryId(dto.getCategoryId());
  114. appMenu.setUrl(StringPool.SLASH+"pages"+StringPool.SLASH +first.get().getValue().toLowerCase() + StringPool.SLASH + dto.getCode().toLowerCase());
  115. appMenu.setRemark(dto.getRemark());
  116. //如果发布菜单 menuid 不为空 则需要修改
  117. if (ObjectUtil.isNotNull(dto.getAppMenuId())) {
  118. appMenu.setId(dto.getAppMenuId());
  119. appMenuMapper.updateById(appMenu);
  120. } else {
  121. appMenuMapper.insert(appMenu);
  122. }
  123. // //保存成功之后 生成代码
  124. // GeneratorUtil.writeFile(getPageOutputDir(first.get().getValue()), dto.getCode().toLowerCase() + StringPool.DOT + "vue", dto.getPageCode());
  125. File tempFile = FileUtil.file(generatePathConfig.getAppPath() + StringPool.SLASH + "pages.json");
  126. JSON jsonContent = JSONUtil.readJSON(tempFile, StandardCharsets.UTF_8);
  127. //判断page.json里面是否有对应的路径,没有就进行生成
  128. String path = "pages/" + first.get().getValue().toLowerCase() + StringPool.SLASH + dto.getCode().toLowerCase();
  129. if (((JSONObject) jsonContent).get("pages").toString().contains(path)){
  130. JSONArray pages = JSONUtil.parseArray(jsonContent.getByPath("pages").toString());
  131. for (Object page : pages) {
  132. if (page.toString().contains(path) && !page.toString().contains(dto.getName())){//找到这个path路径的值,再判断页面名称是否改变,改变则先移除,再新增
  133. pages.remove(page);
  134. Map<String, Object> pageJsonMap = new HashMap<>();
  135. pageJsonMap.put("path", path);
  136. Map<String, Object> styleMap = new HashMap<>();
  137. styleMap.put("navigationBarTitleText", dto.getName());
  138. pageJsonMap.put("style", styleMap);
  139. pages.add(pageJsonMap);
  140. jsonContent.putByPath("pages", pages);
  141. FileUtil.writeString(jsonContent.toJSONString(4), tempFile, StandardCharsets.UTF_8);
  142. }
  143. }
  144. }else {
  145. Map<String, Object> pageJsonMap = new HashMap<>();
  146. pageJsonMap.put("path", path);
  147. Map<String, Object> styleMap = new HashMap<>();
  148. styleMap.put("navigationBarTitleText", dto.getName());
  149. pageJsonMap.put("style", styleMap);
  150. JSONArray pages = JSONUtil.parseArray(jsonContent.getByPath("pages").toString());
  151. pages.add(pageJsonMap);
  152. jsonContent.putByPath("pages", pages);
  153. FileUtil.writeString(jsonContent.toJSONString(4), tempFile, StandardCharsets.UTF_8);
  154. }
  155. appPageDesign.setAppMenuId(appMenu.getId());
  156. } else {
  157. //如果不发布菜单 并且 appmenuId又在 需要删除菜单
  158. if (ObjectUtil.isNotNull(dto.getAppMenuId())) {
  159. appMenuMapper.deleteById(dto.getAppMenuId());
  160. }
  161. }
  162. updateById(appPageDesign);
  163. return Boolean.TRUE;
  164. }
  165. /**
  166. * 获取实体类生成目录
  167. *
  168. * @param outputArea
  169. * @return
  170. */
  171. private String getPageOutputDir(String outputArea) {
  172. return generatePathConfig.getAppPath() + StringPool.SLASH + "pages" + StringPool.SLASH + outputArea.toLowerCase();
  173. }
  174. }