|
@@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
|
|
|
import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
import org.springblade.core.tool.utils.CollectionUtil;
|
|
|
import org.springblade.core.tool.utils.Func;
|
|
|
+import org.springblade.manager.dto.ElementData;
|
|
|
import org.springblade.manager.dto.FormData;
|
|
|
import org.springblade.manager.entity.Formula;
|
|
|
import org.springblade.manager.entity.WbsParam;
|
|
@@ -39,7 +40,7 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
public final static String FC="FC.";
|
|
|
|
|
|
|
|
|
- public static final String POLY_REG= "(avg|min|max|sum)\\([^)]+\\)";
|
|
|
+ public static final String POLY_REG= "(avg|min|max|sum)\\(([^)]+)\\)";
|
|
|
public static final Pattern POLY = Pattern.compile(POLY_REG);
|
|
|
public void execute( List<FormData> list ){
|
|
|
Map<Boolean,List<FormData>> map = list.stream().collect(Collectors.partitioningBy(e->e!=null&&e.getFormula().getFormula().contains("E[")));
|
|
@@ -79,19 +80,6 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
constantMap.put(WP,wpMap);
|
|
|
constantMap.put(CHAIN,trees.stream().map(WbsTree::getDeptName).collect(Collectors.toList()));
|
|
|
for(FormData fd:total){
|
|
|
- /*存入数据*/
|
|
|
- if(CollectionUtil.isNotEmpty(fd.getValues())){
|
|
|
- Map<String,Object> tmpMap = new HashMap<>();
|
|
|
- for(int i=0;i<fd.getValues().size();i++){
|
|
|
- tmpMap.put("p"+i,fd.getValues().get(i));
|
|
|
- }
|
|
|
- Map<String,Object> tableMap = (Map<String, Object>) variables.get(fd.getCode());
|
|
|
- if(tableMap==null){
|
|
|
- tableMap= new HashMap<>();
|
|
|
- variables.put(fd.getCode(),tableMap);
|
|
|
- }
|
|
|
- tableMap.put(fd.getGroupId().toString(),tmpMap);
|
|
|
- }
|
|
|
/*预处理公式脚本*/
|
|
|
String tmp =fd.getFormula().getFormula();
|
|
|
tmp = tmp.replace(FC, CustomFunction.CLASS_CALL);
|
|
@@ -102,78 +90,96 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
Matcher m2 = P.matcher(tmp);
|
|
|
while (m2.find()){
|
|
|
tmp= tmp.replace(m2.group(),"'"+m2.group()+"'");
|
|
|
- fd.getRely().add(m2.group());
|
|
|
}
|
|
|
fd.getFormula().setFormula(tmp);
|
|
|
}
|
|
|
- /*执行公式*/
|
|
|
- /*集合公式预处理*/
|
|
|
+
|
|
|
for(FormData fd:total){
|
|
|
- String formula=fd.getFormula().getFormula();
|
|
|
- Map<String,Object> currentMap = new HashMap<>(constantMap);
|
|
|
- currentMap.put("E",variables);
|
|
|
- if(formula.contains(CustomFunction.CLASS_CALL+"avg(")||formula.contains(CustomFunction.CLASS_CALL+"min(")||formula.contains(CustomFunction.CLASS_CALL+"max(")||formula.contains(CustomFunction.CLASS_CALL+"sum(")){
|
|
|
- Matcher m = POLY.matcher(formula);
|
|
|
- while (m.find()){
|
|
|
- Object data = Expression.parse(CustomFunction.CLASS_CALL+m.group()).calculate(currentMap);
|
|
|
- formula = formula.replace(CustomFunction.CLASS_CALL+m.group(),data.toString());
|
|
|
- fd.getFormula().setFormula(formula);
|
|
|
+ Formula formula= fd.getFormula();
|
|
|
+ if(formula!=null){
|
|
|
+ String f=formula.getFormula();
|
|
|
+ if(Func.isNotBlank(f)){
|
|
|
+ Map<String, Object> currentMap = new HashMap<>(constantMap);
|
|
|
+ Map<String,Object> E = getMap(currentMap,"E");
|
|
|
+ List<String> relyList = fd.getFormula().getRelyList();
|
|
|
+ if(CollectionUtil.isNotEmpty(relyList)){
|
|
|
+ List<FormData> ele = total.stream().filter(e->relyList.contains(e.getCode())).collect(Collectors.toList());
|
|
|
+ if(f.contains(CustomFunction.CLASS_CALL+"avg(")||f.contains(CustomFunction.CLASS_CALL+"min(")||f.contains(CustomFunction.CLASS_CALL+"max(")||f.contains(CustomFunction.CLASS_CALL+"sum(")){
|
|
|
+ Matcher m = POLY.matcher(f);
|
|
|
+ while (m.find()){
|
|
|
+ List<String> elementCodes=CustomFunction.parseElementCode(m.group(2));
|
|
|
+ List<FormData> tmp = ele.stream().filter(e->elementCodes.contains(e.getCode())).collect(Collectors.toList());
|
|
|
+ ele.removeAll(tmp);
|
|
|
+ tmp.forEach(e-> E.put(e.getCode(),e.getValues().stream().map(ElementData::getValue).collect(Collectors.toList())));
|
|
|
+ Object data = Expression.parse(CustomFunction.CLASS_CALL+m.group()).calculate(currentMap);
|
|
|
+ f = f.replace(CustomFunction.CLASS_CALL+m.group(),data.toString());
|
|
|
+ fd.getFormula().setFormula(f);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(CollectionUtil.isNotEmpty(ele)){
|
|
|
+ Map<Integer,Map<String,List<Object>>> dataSource = new HashMap<>();
|
|
|
+ for(FormData e:ele){
|
|
|
+ Map<Integer,List<ElementData>> m = e.getValues().stream().collect(Collectors.groupingBy(ElementData::getGroupId));
|
|
|
+ for(Map.Entry<Integer,List<ElementData>> entry:m.entrySet()){
|
|
|
+ Map<String,List<Object>> t = dataSource.computeIfAbsent(entry.getKey(), k -> new HashMap());
|
|
|
+ t.put(e.getCode(),entry.getValue().stream().map(ElementData::getValue).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(dataSource.size()>0){
|
|
|
+ for(Map.Entry<Integer,Map<String,List<Object>>> entry:dataSource.entrySet()){
|
|
|
+ if(entry.getValue().keySet().size()>=ele.size()){
|
|
|
+ List<ElementData> eds = slice(entry.getValue(),constantMap,f,entry.getKey());
|
|
|
+ System.out.println(eds);
|
|
|
+ fd.setValues(eds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ fd.getValues().add(new ElementData(0,0,Expression.parse(formula.getFormula()).calculate(currentMap)));
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ fd.getValues().add(new ElementData(0,0,Expression.parse(formula.getFormula()).calculate(currentMap)));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- for(FormData fd:total){
|
|
|
- if(Func.isNotBlank(fd.getFormula().getFormula())){
|
|
|
- Map<String, Object> currentMap = new HashMap<>();
|
|
|
- Map<String,Object> elementMap = getMap(variables,"E");
|
|
|
- Map<String,Object> tmp = getMap(currentMap,"E");
|
|
|
- for(String key:fd.getRely()){
|
|
|
- tmp.put(key,elementMap.get(key));
|
|
|
- }
|
|
|
- System.out.println(slice(currentMap,constantMap,fd.getFormula().getFormula()));
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
- System.out.println("执行结束");
|
|
|
+// total.stream().filter(e->Func.isNotBlank(e.getFormula().getFormula())).map(FormData::getValues).forEach(e->e.stream().map(ElementData::getValue).forEach(System.out::println));
|
|
|
+ total.stream().filter(e->Func.isNotBlank(e.getFormula().getFormula())).map(FormData::getValues).forEach(System.out::println);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- public Map<String,Object> getMap(Map<String,Object> variables,String key){
|
|
|
- Map<String,Object> CNDMap = (Map<String, Object>) variables.get(key);
|
|
|
+ public Map<String,Object> getMap(Map<String,Object> main,String key){
|
|
|
+ Map<String,Object> CNDMap = (Map<String, Object>) main.get(key);
|
|
|
if(CNDMap==null){
|
|
|
CNDMap = new HashMap<>();
|
|
|
- variables.put(key,CNDMap);
|
|
|
+ main.put(key,CNDMap);
|
|
|
}
|
|
|
- CNDMap.put("A",100);
|
|
|
return CNDMap;
|
|
|
}
|
|
|
|
|
|
- public List<Object> slice(Map<String,Object> variables,Map<String,Object> constantMap,String formula){
|
|
|
- int min =1;
|
|
|
- List<Object> result = new ArrayList<>();
|
|
|
+ public List<ElementData> slice(Map<String,List<Object>> elementMap,Map<String,Object> constantMap,String formula,Integer groupId){
|
|
|
+ int min =0;
|
|
|
+ List<ElementData> result = new ArrayList<>();
|
|
|
Map<String,Object> currentMap = new HashMap<>();
|
|
|
- Map<String,List<Object>> massMap = new HashMap<>();
|
|
|
- Map<String,Object> E=getMap(variables,"E");
|
|
|
- Map<String,Object> E2=getMap(currentMap,"E");
|
|
|
-
|
|
|
- for(Map.Entry<String,Object> entry:E.entrySet()){
|
|
|
- if(entry.getValue() instanceof List){
|
|
|
- List<Object> tmp = (List<Object>) entry.getValue();
|
|
|
- if(min==1){
|
|
|
- min= Math.max(tmp.size(), 1);
|
|
|
- }else{
|
|
|
- min= Math.min(min,tmp.size());
|
|
|
- }
|
|
|
- massMap.put(entry.getKey(),tmp);
|
|
|
- }else{
|
|
|
- E2.put(entry.getKey(),entry.getValue());
|
|
|
- }
|
|
|
+ Map<String,Object> E=getMap(currentMap,"E");
|
|
|
+ for(Map.Entry<String,List<Object>> entry:elementMap.entrySet()){
|
|
|
+ List<Object> tmp = entry.getValue();
|
|
|
+ if(min==0){
|
|
|
+ min=tmp.size();
|
|
|
+ }else{
|
|
|
+ min= Math.min(min,tmp.size());
|
|
|
+ }
|
|
|
}
|
|
|
- currentMap.putAll(constantMap);
|
|
|
- for(int i=0;i<min;i++){
|
|
|
- for(Map.Entry<String,List<Object>> e:massMap.entrySet()){
|
|
|
- E2.put(e.getKey(),e.getValue().get(i));
|
|
|
+ if(min>0){
|
|
|
+ currentMap.putAll(constantMap);
|
|
|
+ for(int i=0;i<min;i++){
|
|
|
+ for(Map.Entry<String,List<Object>> e:elementMap.entrySet()){
|
|
|
+ E.put(e.getKey(),e.getValue().get(i));
|
|
|
+ }
|
|
|
+ Object data = Expression.parse(formula).calculate(currentMap);
|
|
|
+ result.add(new ElementData(i,groupId,data));
|
|
|
}
|
|
|
- Object data = Expression.parse(formula).calculate(currentMap);
|
|
|
- result.add(data);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
@@ -215,6 +221,25 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * @Description 元素依赖解析
|
|
|
+ * @Param [f]
|
|
|
+ * @return void
|
|
|
+ * @Author yangyj
|
|
|
+ * @Date 2022.07.04 16:10
|
|
|
+ **/
|
|
|
+ public void relyParse(Formula f){
|
|
|
+ if(Func.isNotBlank(f.getFormula())){
|
|
|
+ List<String> l = new ArrayList<>();
|
|
|
+ Matcher m = P.matcher(f.getFormula());
|
|
|
+ while (m.find()){
|
|
|
+ l.add(m.group());
|
|
|
+ }
|
|
|
+ if(l.size()>0){
|
|
|
+ f.setRely(String.join(",", l));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|