Просмотр исходного кода

试验-公式
1、求和调整按列或行求和

LHB 2 недель назад
Родитель
Сommit
6c0c088ffc

+ 88 - 1
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/FormulaServiceImpl.java

@@ -9,6 +9,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
+import com.alibaba.nacos.common.utils.NumberUtils;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
@@ -91,6 +92,8 @@ import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
 
+import static org.springblade.manager.utils.TableCoordinates.*;
+
 /**
  * @author yangyj
  * @Date 2022/6/9 14:29
@@ -1298,9 +1301,71 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
                             if (fd.getCoordsList().size() > 1 && f.split("[/+\\-*]").length > 1) {
                                 LinkedHashMap<String, FormData> fdMap = step(ele);
                                 if (fdMap != null) {
-                                    List<LocalVariable> local = slice2Local(f, fdMap, tec);
+                                    List<LocalVariable> local = null;
+                                    //求和公式 判断是否按坐标分组求和
+                                    if(f.contains("sum") && (f.contains(",x") || f.contains(",y"))){
+                                        FormData formData = fdMap.get(relyList.get(0));
+
+                                        Map<Integer, List<ElementData>> collect = new HashMap<>();
+                                        if(f.contains(",y")){
+                                            fd.getValues().sort(Comparator.comparing(ElementData::getY));
+                                            collect = formData.getValues().stream().collect(Collectors.groupingBy(ElementData::getY,TreeMap::new, Collectors.toList()));
+                                        }else{
+                                            //需要解析html获取当前key的对应的excel 坐标 通过excel坐标 统计相同列的数据
+                                            Long pKeyId = op.get().getPKeyId();
+                                            WbsTreePrivate wbsTreePrivate1 = wbsTreePrivateMapper.selectById(pKeyId);
+
+                                            String[] split = fd.getCode().split(":");
+                                            if(wbsTreePrivate1.getInitTableName().equals(split[0])){
+                                                //公式key
+                                                String dataKey = split[1];
+                                                //目标key
+                                                String rely = fd.getFormula().getRely().split(":")[1];
+                                                //获取html 解析html
+                                                InputStream fileInputStream = FileUtils.getInputStreamByUrl(wbsTreePrivate1.getHtmlUrl());
+                                                String htmlString = IoUtil.readToString(fileInputStream);
+                                                Document doc = Jsoup.parse(htmlString);
+                                                Elements select = doc.select("[id*="+dataKey+"]");
+                                                //通过excel坐标来确认同一列的单元格有哪些
+                                                HashMap<String, List<String>> stringListHashMap = new HashMap<>();
+                                                for (Element element : select) {
+                                                    String id = element.attr("id");
+                                                    String x1 = element.attr("x1");
+                                                    List<String> strings = new ArrayList<>();
+                                                    Elements select1 = doc.select("[id*=" + rely + "][x1=" + x1 + "]");
+                                                    for (Element element1 : select1) {
+                                                        strings.add(element1.attr("id"));
+                                                    }
+                                                    stringListHashMap.put(id,strings);
+                                                }
+                                                //对原始key排序 确保顺序以方便赋值
+                                                fd.getValues().sort(Comparator.comparing(ElementData::getX));
+                                                for (ElementData key : fd.getValues()) {
+                                                    String  newKey = dataKey + "__" + key.getY() + "_" + key.getX();
+                                                    List<String> strings = stringListHashMap.get(newKey);
+                                                    List<ElementData> values = formData.getValues();
+
+                                                    List<ElementData> objects = new ArrayList<>();
+                                                    for (String string : strings) {
+                                                        String[] split1 = string.split("__")[1].split("_");
+                                                        List<ElementData> collect1 = values.stream()
+                                                                .filter(y -> y.getX().equals(Integer.valueOf(split1[1])) && y.getY().equals(Integer.valueOf(split1[0])))
+                                                                .collect(Collectors.toList());
+                                                        objects.addAll(collect1);
+                                                    }
+                                                    collect.put(key.getX(),objects);
+                                                }
+                                            }
+                                        }
+                                        f = f.replaceAll(",x","").replaceAll(",y","");
+                                        formula.setFormula(f);
+                                        local = slice3Local(f, tec, collect, relyList.get(0));
+                                    }else{
+                                        local = slice2Local(f, fdMap, tec);
+                                    }
                                     if (local.size() > 0) {
                                         List<Object> values = FormulaUtils.slice(local, f);
+                                        values = values.stream().map(s -> StringUtils.isEmpty(s) ? "/" : s).collect(Collectors.toList());
                                         FormulaUtils.write(fd, values, !fd.getTableName().equals(checkTable));
                                     }
                                 } else {
@@ -1520,6 +1585,28 @@ public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula>
         return local;
     }
 
+    private List<LocalVariable> slice3Local(String f, TableElementConverter tec, Map<Integer, List<ElementData>> collect, String str) {
+        List<LocalVariable> local = new ArrayList<>();
+        collect.keySet().forEach(key -> {
+
+            Map<String, Object> variable = new HashMap<>(tec.constantMap);
+            Map<String, Object> em = (Map<String, Object>) variable.computeIfAbsent(E, k -> new HashMap<>());
+            List<ElementData> elementData = collect.get(key);
+            Integer index = elementData.get(0).getIndex();
+
+
+            List<Object> collect1 = elementData.stream().map(ElementData::getValue).collect(Collectors.toList());
+            List<Double> collect2 = collect1.stream()
+                    .filter(Objects::nonNull)
+                    .filter(StringUtils::isNumber)
+                    .map(obj -> NumberUtils.toDouble(obj.toString(), Double.NaN))
+                    .collect(Collectors.toList());
+            em.put(str,collect2);
+            local.add(new LocalVariable(index, f, variable));
+        });
+        return local;
+    }
+
     private void putEle(String f, List<FormData> ele, Map<String, Object> currentMap, FormData fd) {
         @SuppressWarnings("unchecked")
         Map<String, Object> em = (Map<String, Object>) currentMap.computeIfAbsent(E, (k) -> new HashMap<>());