CommonService.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 filePath = fileUtils.getPath("/11.docx");
  44. File file = new File(rootPath + fileName);
  45. Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
  46. int ch = 0;
  47. StringBuffer buffer = new StringBuffer();
  48. while ((ch = reader.read()) != -1) {
  49. buffer.append((char) ch);
  50. }
  51. reader.close();
  52. json = buffer.toString();
  53. return json;
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. return null;
  57. }
  58. }
  59. public List<QueryFieldsVO> getQueryConditions(List<String> tableNames) {
  60. List<QueryFieldsVO> entityVOS = new ArrayList<>();
  61. tableNames.forEach(item -> {
  62. String json = null;
  63. try {
  64. json = CommonService.readJsonFile(item + ".json");
  65. } catch (Exception e) {
  66. }
  67. if (json != null) {
  68. QueryFieldsVO entityVO = new QueryFieldsVO();
  69. entityVO.setTableName(item);
  70. List<QueryFiledVO> queryConditions = JSON.parseArray(json, QueryFiledVO.class);
  71. entityVO.setConditionDTOList(queryConditions);
  72. entityVOS.add(entityVO);
  73. }
  74. });
  75. return entityVOS;
  76. }
  77. public List<Integer> getEventOrders(Integer orderType){
  78. LambdaQueryWrapper<Event> queryWrapper = new LambdaQueryWrapper<>();
  79. queryWrapper.select(Event::getId);
  80. if (orderType.equals(0)) {
  81. queryWrapper.orderByAsc(Event::getName);
  82. } else {
  83. queryWrapper.orderByDesc(Event::getName);
  84. }
  85. java.util.function.Function<Object, Integer> f = (o -> Integer.parseInt(o.toString()));
  86. List<Integer> ids = eventService.listObjs(queryWrapper, f);
  87. return ids;
  88. }
  89. public List<Map<String, Object>> getExcelConfig() {
  90. List<Map<String,Object>> maps =new ArrayList<>();
  91. //创建map用于装载:1.自定义字段(标引/非树类型字段、分类/树类型字段) 2.文件夹 3.数据来源,和返回结果
  92. String json = CommonService.readJsonFile("uploadSetting.json");
  93. List<UploadSettingVO> uploadSettingVOs = JsonUtils.jsonToList(json, UploadSettingVO.class);
  94. uploadSettingVOs.forEach(item->{
  95. Map<String, Object> map =new HashMap<>();
  96. map.put("name",item.getName());
  97. map.put("id",item.getSourceId());
  98. maps.add(map);
  99. });
  100. return maps;
  101. }
  102. public static String readFile(String fileName) {
  103. String json = "";
  104. try {
  105. //获取 applicationHome 内的路径 ...\target\classes 到这一层级下
  106. //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
  107. // String filePath = fileUtils.getPath("/11.docx");
  108. File file = new File(fileName);
  109. Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
  110. int ch = 0;
  111. StringBuffer buffer = new StringBuffer();
  112. while ((ch = reader.read()) != -1) {
  113. buffer.append((char) ch);
  114. }
  115. reader.close();
  116. json = buffer.toString();
  117. return json;
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. return null;
  121. }
  122. }
  123. }