diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java index d592960..a7f73f5 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/InstructionServiceImpl.java @@ -192,6 +192,8 @@ public class InstructionServiceImpl implements IInstructionService { if (kw.isEmpty()) return Collections.singletonList("请输入搜索关键词"); List list = jdOrderService.selectJDOrderList(new JDOrder()); if (list == null) list = Collections.emptyList(); + // 统一截取分销标记 + list.forEach(order -> order.setDistributionMark(truncateDistributionMark(order.getDistributionMark()))); String low = kw.toLowerCase(Locale.ROOT); List matched = list.stream().filter(o -> contains(o.getRemark(), low) || contains(o.getOrderId(), low) || contains(o.getModelNumber(), low) || contains(o.getAddress(), low) || contains(o.getBuyer(), low)).limit(50).collect(Collectors.toList()); if (matched.isEmpty()) return Collections.singletonList("未找到匹配的订单"); @@ -214,6 +216,8 @@ public class InstructionServiceImpl implements IInstructionService { Date end = Date.from(range.get(1).atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); List list = jdOrderService.selectJDOrderList(new JDOrder()); if (list == null) list = Collections.emptyList(); + // 统一截取分销标记 + list.forEach(order -> order.setDistributionMark(truncateDistributionMark(order.getDistributionMark()))); List filtered = list.stream().filter(o -> o.getOrderTime() != null && !o.getOrderTime().before(start) && !o.getOrderTime().after(end)).collect(Collectors.toList()); if (filtered.isEmpty()) return Collections.singletonList("今天没有订单。"); // 按 remark 排序 @@ -235,6 +239,8 @@ public class InstructionServiceImpl implements IInstructionService { Date end = Date.from(range.get(1).atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant()); List list = jdOrderService.selectJDOrderList(new JDOrder()); if (list == null) list = Collections.emptyList(); + // 统一截取分销标记 + list.forEach(order -> order.setDistributionMark(truncateDistributionMark(order.getDistributionMark()))); List filtered = list.stream().filter(o -> o.getOrderTime() != null && !o.getOrderTime().before(start) && !o.getOrderTime().after(end)).collect(Collectors.toList()); if (filtered.isEmpty()) return Collections.singletonList("今天没有订单。"); @@ -243,26 +249,21 @@ public class InstructionServiceImpl implements IInstructionService { List>> dmEntries = new ArrayList<>(byDM.entrySet()); dmEntries.sort(Comparator.comparing(en -> en.getKey() == null ? "" : en.getKey())); - - // 全局型号数量和金额统计 - Map globalModelCounts = new HashMap<>(); - Map globalPaymentAmounts = new HashMap<>(); - Map globalRebateAmounts = new HashMap<>(); - // 记录每个型号的最高单价订单 - Map globalMaxPriceOrders = new HashMap<>(); - for (Map.Entry> e : dmEntries) { String dm = e.getKey() != null ? e.getKey() : "未提供"; List orders = e.getValue(); Map byModel = orders.stream().collect(Collectors.groupingBy(JDOrder::getModelNumber, Collectors.counting())); - Map modelPaymentAmounts = orders.stream().collect(Collectors.groupingBy(JDOrder::getModelNumber, Collectors.summingDouble(o -> o.getPaymentAmount() != null ? o.getPaymentAmount() : 0.0))); - Map modelRebateAmounts = orders.stream().collect(Collectors.groupingBy(JDOrder::getModelNumber, Collectors.summingDouble(o -> o.getRebateAmount() != null ? o.getRebateAmount() : 0.0))); int totalCount = 0; StringBuilder summary = new StringBuilder(); List> modelEntries = new ArrayList<>(byModel.entrySet()); modelEntries.sort(Comparator.comparing(en -> en.getKey() == null ? "" : en.getKey())); + + // 记录该分组中每个型号的最高单价订单 + Map groupMaxPriceOrders = new HashMap<>(); + Map groupModelCounts = new HashMap<>(); + for (Map.Entry em : modelEntries) { int c = em.getValue().intValue(); totalCount += c; @@ -270,21 +271,17 @@ public class InstructionServiceImpl implements IInstructionService { summary.append("型号:").append(model).append(" 数量:").append(c).append("\n"); - // 累积到全局统计 - globalModelCounts.put(model, globalModelCounts.getOrDefault(model, 0) + c); - Double paymentAmount = modelPaymentAmounts.get(model); - Double rebateAmount = modelRebateAmounts.get(model); - globalPaymentAmounts.put(model, globalPaymentAmounts.getOrDefault(model, 0.0) + (paymentAmount != null ? paymentAmount : 0.0)); - globalRebateAmounts.put(model, globalRebateAmounts.getOrDefault(model, 0.0) + (rebateAmount != null ? rebateAmount : 0.0)); + // 记录该分组的型号数量 + groupModelCounts.put(model, c); // 找到该型号价格最高的订单 for (JDOrder order : orders) { if (model.equals(order.getModelNumber())) { - JDOrder currentMax = globalMaxPriceOrders.get(model); + JDOrder currentMax = groupMaxPriceOrders.get(model); if (currentMax == null || (order.getPaymentAmount() != null && (currentMax.getPaymentAmount() == null || order.getPaymentAmount() > currentMax.getPaymentAmount()))) { - globalMaxPriceOrders.put(model, order); + groupMaxPriceOrders.put(model, order); } } } @@ -303,102 +300,104 @@ public class InstructionServiceImpl implements IInstructionService { StringBuilder infoSingle = new StringBuilder(); infoSingle.append("分销标记:").append(dm).append("\n").append(summary).append(detail).append("\n"); outputs.add(infoSingle.toString().trim()); - } - // 添加算钱文本(全局统计版本) - if (!globalModelCounts.isEmpty()) { - StringBuilder priceText = new StringBuilder(); + // 为该分组添加算钱文本 + if (!groupModelCounts.isEmpty()) { + StringBuilder priceText = new StringBuilder(); + priceText.append("分销标记:").append(dm).append(" - 算钱\n"); - // 先输出型号和数量 - List> sortedEntries = globalModelCounts.entrySet().stream() - .sorted(Comparator.comparing(Map.Entry::getKey)) - .collect(Collectors.toList()); + // 先输出型号和数量 + List> sortedEntries = groupModelCounts.entrySet().stream() + .sorted(Comparator.comparing(Map.Entry::getKey)) + .collect(Collectors.toList()); - for (Map.Entry entry : sortedEntries) { - String model = entry.getKey(); - int count = entry.getValue(); - priceText.append("型号:").append(model).append(" 数量:").append(count).append("\n"); - } - - priceText.append("\n"); - - // 输出金额计算(单台价格-返现+佣金=净价×数量=总价) - double grandTotal = 0.0; - StringBuilder calculationLines = new StringBuilder(); - List totalPricesList = new ArrayList<>(); - - for (Map.Entry entry : sortedEntries) { - String model = entry.getKey(); - int count = entry.getValue(); - - // 获取该型号最高价格的订单 - JDOrder maxPriceOrder = globalMaxPriceOrders.get(model); - double singlePayment = 0.0; - double singleRebate = 0.0; - - if (maxPriceOrder != null) { - singlePayment = maxPriceOrder.getPaymentAmount() != null ? maxPriceOrder.getPaymentAmount() : 0.0; - singleRebate = maxPriceOrder.getRebateAmount() != null ? maxPriceOrder.getRebateAmount() : 0.0; + for (Map.Entry entry : sortedEntries) { + String model = entry.getKey(); + int count = entry.getValue(); + priceText.append("型号:").append(model).append(" 数量:").append(count).append("\n"); } - // 四舍五入到2位小数 - singlePayment = Math.round(singlePayment * 100.0) / 100.0; - singleRebate = Math.round(singleRebate * 100.0) / 100.0; - - // 从Redis获取佣金 - double commission = 0.0; - try { - com.ruoyi.jarvis.domain.ProductJdConfig config = productJdConfigService.selectProductJdConfigByModel(model); - if (config != null && config.getCommission() != null) { - commission = config.getCommission().doubleValue(); - } - } catch (Exception e) { - // 如果获取失败,佣金为0 - } - - // 计算净价(单台价格 - 返现 + 佣金) - double netPrice = singlePayment - singleRebate + commission; - // 四舍五入到2位小数 - netPrice = Math.round(netPrice * 100.0) / 100.0; - - // 计算总价 - double totalPrice = netPrice * count; - // 四舍五入到2位小数 - totalPrice = Math.round(totalPrice * 100.0) / 100.0; - totalPricesList.add(totalPrice); - grandTotal += totalPrice; - - // 格式:单台价格-返现+佣金=净价×数量=总价 - if (count == 1) { - // 数量为1时,不显示×1 - calculationLines.append(String.format("%.2f-%.2f+%.2f=%.2f\n", - singlePayment, singleRebate, commission, totalPrice)); - } else { - calculationLines.append(String.format("%.2f-%.2f+%.2f=%.2f×%d=%.2f\n", - singlePayment, singleRebate, commission, netPrice, count, totalPrice)); - } - } - - priceText.append(calculationLines); - - // 输出汇总(各型号总价相加) - if (sortedEntries.size() > 1) { priceText.append("\n"); - // 构建汇总公式 - for (int i = 0; i < totalPricesList.size(); i++) { - if (i > 0) { - priceText.append("+"); - } - priceText.append(String.format("%.2f", totalPricesList.get(i))); - } - // 四舍五入汇总到2位小数 - grandTotal = Math.round(grandTotal * 100.0) / 100.0; - priceText.append(String.format("=%.2f", grandTotal)); - } - outputs.add(priceText.toString().trim()); + // 输出金额计算(单台价格-返现+佣金=净价×数量=总价) + double grandTotal = 0.0; + StringBuilder calculationLines = new StringBuilder(); + List totalPricesList = new ArrayList<>(); + + for (Map.Entry entry : sortedEntries) { + String model = entry.getKey(); + int count = entry.getValue(); + + // 获取该型号最高价格的订单 + JDOrder maxPriceOrder = groupMaxPriceOrders.get(model); + double singlePayment = 0.0; + double singleRebate = 0.0; + + if (maxPriceOrder != null) { + singlePayment = maxPriceOrder.getPaymentAmount() != null ? maxPriceOrder.getPaymentAmount() : 0.0; + singleRebate = maxPriceOrder.getRebateAmount() != null ? maxPriceOrder.getRebateAmount() : 0.0; + } + + // 四舍五入到2位小数 + singlePayment = Math.round(singlePayment * 100.0) / 100.0; + singleRebate = Math.round(singleRebate * 100.0) / 100.0; + + // 从Redis获取佣金 + double commission = 0.0; + try { + com.ruoyi.jarvis.domain.ProductJdConfig config = productJdConfigService.selectProductJdConfigByModel(model); + if (config != null && config.getCommission() != null) { + commission = config.getCommission().doubleValue(); + } + } catch (Exception ex) { + // 如果获取失败,佣金为0 + } + + // 计算净价(单台价格 - 返现 + 佣金) + double netPrice = singlePayment - singleRebate + commission; + // 四舍五入到2位小数 + netPrice = Math.round(netPrice * 100.0) / 100.0; + + // 计算总价 + double totalPrice = netPrice * count; + // 四舍五入到2位小数 + totalPrice = Math.round(totalPrice * 100.0) / 100.0; + totalPricesList.add(totalPrice); + grandTotal += totalPrice; + + // 格式:单台价格-返现+佣金=净价×数量=总价 + if (count == 1) { + // 数量为1时,不显示×1 + calculationLines.append(String.format("%.2f-%.2f+%.2f=%.2f\n", + singlePayment, singleRebate, commission, totalPrice)); + } else { + calculationLines.append(String.format("%.2f-%.2f+%.2f=%.2f×%d=%.2f\n", + singlePayment, singleRebate, commission, netPrice, count, totalPrice)); + } + } + + priceText.append(calculationLines); + + // 输出汇总(各型号总价相加) + if (sortedEntries.size() > 1) { + priceText.append("\n"); + // 构建汇总公式 + for (int i = 0; i < totalPricesList.size(); i++) { + if (i > 0) { + priceText.append("+"); + } + priceText.append(String.format("%.2f", totalPricesList.get(i))); + } + // 四舍五入汇总到2位小数 + grandTotal = Math.round(grandTotal * 100.0) / 100.0; + priceText.append(String.format("=%.2f", grandTotal)); + } + + outputs.add(priceText.toString().trim()); + } } + return outputs.isEmpty() ? Collections.singletonList("无数据") : outputs; } if (input.startsWith("单")) { @@ -925,10 +924,37 @@ private String handleTF(String input) { return LocalDate.of(y, mo, d); } + /** + * 截取分销标记,如果长度大于4则进行截取 + * @param v 原始分销标记 + * @return 截取后的分销标记 + */ + private String truncateDistributionMark(String v) { + if (v == null) return ""; + + // 如果长度大于4,进行截取 + if (v.length() > 4) { + // 查找第一个左括号的位置 + int leftParenIndex = v.indexOf('('); + if (leftParenIndex > 0) { + // 截取到左括号之前的部分 + return v.substring(0, leftParenIndex); + } else { + // 如果没有括号,直接截取前4个字符 + return v.substring(0, 4); + } + } + + return v; + } + private String mapDistribution(String v) { if (v == null) return ""; - if (v.startsWith("H")) return "鸿"; - if (v.startsWith("F")) return "凡"; + + String processedV = truncateDistributionMark(v); + + if (processedV.startsWith("H")) return "鸿"; + if (processedV.startsWith("F")) return "凡"; return ""; } @@ -955,3 +981,4 @@ private String handleTF(String input) { } +