package cn.cslg.pas.service.business; import cn.cslg.pas.Application; import cn.cslg.pas.common.utils.FileUtils; import cn.cslg.pas.common.utils.JsonUtils; import cn.cslg.pas.common.vo.QueryFiledVO; import cn.cslg.pas.common.vo.QueryFieldsVO; import cn.cslg.pas.common.vo.UploadSettingVO; import cn.cslg.pas.domain.business.Event; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.system.ApplicationHome; import org.springframework.stereotype.Service; import java.io.*; import java.util.*; /** * 公用服务类 * * @author lrj */ @Service @Slf4j public class CommonService { @Autowired private EventService eventService; @Autowired private FileUtils fileUtils; /** * 根据获得文件json * * @param fileName * @return */ public static String readJsonFile(String fileName) { String json = ""; try { ApplicationHome ah = new ApplicationHome(Application.class); //获取 applicationHome 内的路径 ...\target\classes 到这一层级下 File fileTem = ah.getSource(); //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root String rootPath = fileTem.getParentFile().toString() + FileUtils.FILE_SEPARATOR+"jsons/"; // String filePath = fileUtils.getPath("/11.docx"); File file = new File(rootPath + fileName); Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8"); int ch = 0; StringBuffer buffer = new StringBuffer(); while ((ch = reader.read()) != -1) { buffer.append((char) ch); } reader.close(); json = buffer.toString(); return json; } catch (IOException e) { e.printStackTrace(); return null; } } public List getQueryConditions(List tableNames) { List entityVOS = new ArrayList<>(); tableNames.forEach(item -> { String json = null; try { json = CommonService.readJsonFile(item + ".json"); } catch (Exception e) { } if (json != null) { QueryFieldsVO entityVO = new QueryFieldsVO(); entityVO.setTableName(item); List queryConditions = JSON.parseArray(json, QueryFiledVO.class); entityVO.setConditionDTOList(queryConditions); entityVOS.add(entityVO); } }); return entityVOS; } public List getEventOrders(Integer orderType){ LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.select(Event::getId); if (orderType.equals(0)) { queryWrapper.orderByAsc(Event::getName); } else { queryWrapper.orderByDesc(Event::getName); } java.util.function.Function f = (o -> Integer.parseInt(o.toString())); List ids = eventService.listObjs(queryWrapper, f); return ids; } public List> getExcelConfig() { List> maps =new ArrayList<>(); //创建map用于装载:1.自定义字段(标引/非树类型字段、分类/树类型字段) 2.文件夹 3.数据来源,和返回结果 String json = CommonService.readJsonFile("uploadSetting.json"); List uploadSettingVOs = JsonUtils.jsonToList(json, UploadSettingVO.class); uploadSettingVOs.forEach(item->{ Map map =new HashMap<>(); map.put("name",item.getName()); map.put("id",item.getSourceId()); maps.add(map); }); return maps; } public static String readFile(String fileName) { String json = ""; try { //获取 applicationHome 内的路径 ...\target\classes 到这一层级下 //获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root // String filePath = fileUtils.getPath("/11.docx"); File file = new File(fileName); Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8"); int ch = 0; StringBuffer buffer = new StringBuffer(); while ((ch = reader.read()) != -1) { buffer.append((char) ch); } reader.close(); json = buffer.toString(); return json; } catch (IOException e) { e.printStackTrace(); return null; } } }