package com.api.aiyh_pcn.patentWall.service; import aiyh.utils.Util; import aiyh.utils.zwl.common.ToolUtil; import com.api.aiyh_pcn.patentWall.dao.PatentWallMapping; import com.api.aiyh_pcn.patentWall.vo.PatentVO; import weaver.conn.RecordSet; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author EBU7-dev1-ayh * @create 2021/11/25 0025 15:23 */ public class PatentWallService { private final PatentWallMapping patentWallMapping = new PatentWallMapping(); private final ToolUtil toolUtil = new ToolUtil(); private Map patentWallConf; public List getList(Map map) { Map patentWallConf = getPatentWallConf(); List> dataList = patentWallMapping.getList(map); List list = new ArrayList<>(); List args = new ArrayList<>(); RecordSet rs = new RecordSet(); for (Map data : dataList) { for(Map.Entry entry : patentWallConf.entrySet()){ String patentVoField = entry.getKey(); String parsing = String.valueOf(entry.getValue()); // TODO 值解析 // 解析 ${}类型的参数,直接替换 String pattern = "\\$\\{(?(\\s\\S)+?)}"; Pattern compile = Pattern.compile(pattern); Matcher matcher = compile.matcher(parsing); while (matcher.find()){ parsing = parsing.replaceFirst(pattern,Util.null2String(data.get(patentVoField))); } // 解析#{}类型的参数,替换为?并按照顺序收集args pattern = "#\\{(?(\\s\\S)+?)}"; compile = Pattern.compile(pattern); matcher = compile.matcher(parsing); while (matcher.find()){ parsing = parsing.replaceFirst(pattern,"?"); args.add(Util.null2String(data.get(patentVoField))); } // 解析#sql{}类型的参数,并执行SQL,获取到SQL的值 pattern = "#sql\\{(?([\\s\\S])+?)}"; compile = Pattern.compile(pattern); matcher = compile.matcher(parsing); while (matcher.find()){ String sqlStr = matcher.group("sqlStr"); toolUtil.writeDebuggerLog(String.format("执行SQL: {%s} ---> 参数: {%s}",sqlStr,args)); rs.executeQuery(sqlStr,args); rs.next(); parsing = Util.null2String(rs.getString(1)); } // 清除参数信息 args.clear(); // 修改数据 patentWallConf.replace(patentVoField,parsing); } PatentVO patentVO = null; try { patentVO = Util.mapToObject(patentWallConf, PatentVO.class); } catch (Exception e) { e.printStackTrace(); toolUtil.writeErrorLog("map转为PatentVO失败!"); } list.add(patentVO); } return list; } /** * 获取单例的配置文件信息 * @return */ private Map getPatentWallConf() { synchronized (this){ if(this.patentWallConf == null){ this.patentWallConf = Util.getProperties2Map("PatentWall", "aiyh.patentWall"); } if(this.patentWallConf == null){ return new HashMap<>(0); } return new HashMap<>(this.patentWallConf); } } public void clearPatentWallConf(){ this.patentWallConf = null; } }