CommonService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package cn.cslg.pas.service.business;
  2. import cn.cslg.pas.Application;
  3. import cn.cslg.pas.common.utils.FileUtils;
  4. import cn.cslg.pas.common.utils.JsonUtils;
  5. import cn.cslg.pas.common.vo.QueryFiledVO;
  6. import cn.cslg.pas.common.vo.QueryFieldsVO;
  7. import cn.cslg.pas.common.vo.UploadSettingVO;
  8. import cn.cslg.pas.domain.business.Event;
  9. import com.alibaba.fastjson.JSON;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.boot.system.ApplicationHome;
  14. import org.springframework.stereotype.Service;
  15. import java.io.*;
  16. import java.util.*;
  17. /**
  18. * 公用服务类
  19. *
  20. * @author lrj
  21. */
  22. @Service
  23. @Slf4j
  24. public class CommonService {
  25. @Autowired
  26. private EventService eventService;
  27. @Autowired
  28. private FileUtils fileUtils;
  29. /**
  30. * 根据获得文件json
  31. *
  32. * @param fileName
  33. * @return
  34. */
  35. public static String readJsonFile(String fileName) {
  36. String json = "";
  37. try {
  38. ApplicationHome ah = new ApplicationHome(Application.class);
  39. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  40. File fileTem = ah.getSource();
  41. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  42. // String rootPath = fileTem.getParentFile().toString() + FileUtils.FILE_SEPARATOR+"jsons/";
  43. String rootPath = "D:/PAS/target/"+"jsons/";
  44. // String filePath = fileUtils.getPath("/11.docx");
  45. File file = new File(rootPath + fileName);
  46. Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
  47. int ch = 0;
  48. StringBuffer buffer = new StringBuffer();
  49. while ((ch = reader.read()) != -1) {
  50. buffer.append((char) ch);
  51. }
  52. reader.close();
  53. json = buffer.toString();
  54. return json;
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. return null;
  58. }
  59. }
  60. public List<QueryFieldsVO> getQueryConditions(List<String> tableNames) {
  61. List<QueryFieldsVO> entityVOS = new ArrayList<>();
  62. tableNames.forEach(item -> {
  63. String json = null;
  64. try {
  65. json = CommonService.readJsonFile(item + ".json");
  66. } catch (Exception e) {
  67. }
  68. if (json != null) {
  69. QueryFieldsVO entityVO = new QueryFieldsVO();
  70. entityVO.setTableName(item);
  71. List<QueryFiledVO> queryConditions = JSON.parseArray(json, QueryFiledVO.class);
  72. entityVO.setConditionDTOList(queryConditions);
  73. entityVOS.add(entityVO);
  74. }
  75. });
  76. return entityVOS;
  77. }
  78. public List<Integer> getEventOrders(Integer orderType){
  79. LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
  80. queryWrapper.select(Event::getId);
  81. if (orderType.equals(0)) {
  82. queryWrapper.orderByAsc(Event::getName);
  83. } else {
  84. queryWrapper.orderByDesc(Event::getName);
  85. }
  86. java.util.function.Function<Object, Integer> f = (o -> Integer.parseInt(o.toString()));
  87. List<Integer> ids = eventService.listObjs(queryWrapper, f);
  88. return ids;
  89. }
  90. public List<Map<String, Object>> getExcelConfig() {
  91. List<Map<String,Object>> maps =new ArrayList<>();
  92. //创建map用于装载:1.自定义字段(标引/非树类型字段、分类/树类型字段) 2.文件夹 3.数据来源,和返回结果
  93. String json = CommonService.readJsonFile("uploadSetting.json");
  94. List<UploadSettingVO> uploadSettingVOs = JsonUtils.jsonToList(json, UploadSettingVO.class);
  95. uploadSettingVOs.forEach(item->{
  96. Map<String, Object> map =new HashMap<>();
  97. map.put("name",item.getName());
  98. map.put("id",item.getSourceId());
  99. maps.add(map);
  100. });
  101. return maps;
  102. }
  103. public static String readFile(String fileName) {
  104. String json = "";
  105. try {
  106. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  107. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  108. // String filePath = fileUtils.getPath("/11.docx");
  109. File file = new File(fileName);
  110. Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
  111. int ch = 0;
  112. StringBuffer buffer = new StringBuffer();
  113. while ((ch = reader.read()) != -1) {
  114. buffer.append((char) ch);
  115. }
  116. reader.close();
  117. json = buffer.toString();
  118. return json;
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. return null;
  122. }
  123. }
  124. /**
  125. * 根据获得文件json
  126. *
  127. * @param file
  128. * @return
  129. */
  130. public static String readJsonFile(File file) {
  131. String json = "";
  132. try {
  133. Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
  134. int ch = 0;
  135. StringBuffer buffer = new StringBuffer();
  136. while ((ch = reader.read()) != -1) {
  137. buffer.append((char) ch);
  138. }
  139. reader.close();
  140. json = buffer.toString();
  141. return json;
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. return null;
  145. }
  146. }
  147. }