1
This commit is contained in:
+14
@@ -42,6 +42,20 @@ public class OrderProfitAnalysisController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出连续若干个月的利润和单量纯文本。不调用大模型。
|
||||
*/
|
||||
@GetMapping("/export-text")
|
||||
public AjaxResult exportText(@RequestParam(value = "months", required = false) Integer months,
|
||||
@RequestParam(value = "date", required = false) String date) {
|
||||
try {
|
||||
return AjaxResult.success(orderProfitAnalysisService.exportMonthsText(months, date));
|
||||
} catch (Exception e) {
|
||||
logger.error("导出利润文本失败", e);
|
||||
return AjaxResult.error("导出失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 套固定提示词调用已配置大模型;push=true 时推企微。
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,14 @@ public interface IOrderProfitAnalysisService {
|
||||
*/
|
||||
Map<String, Object> buildStats(String periodType, String date);
|
||||
|
||||
/**
|
||||
* 导出连续若干个自然月的利润和单量纯文本,给本地助手看。不调用大模型,不含文案提示词。
|
||||
*
|
||||
* @param months 月数,1-24,空则 6
|
||||
* @param date 结束月锚点 yyyy-MM-dd,空则本月
|
||||
*/
|
||||
Map<String, Object> exportMonthsText(Integer months, String date);
|
||||
|
||||
/**
|
||||
* 提交一次 AI 分析后立即返回。模型调用在后台进行,结果写入调用记录。
|
||||
*
|
||||
|
||||
+203
@@ -165,6 +165,199 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi
|
||||
return stats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> exportMonthsText(Integer months, String date) {
|
||||
int n = months == null ? 6 : months;
|
||||
if (n < 1) {
|
||||
n = 1;
|
||||
} else if (n > 24) {
|
||||
n = 24;
|
||||
}
|
||||
LocalDate anchor = parseDate(date);
|
||||
if (anchor == null) {
|
||||
anchor = LocalDate.now(ZONE);
|
||||
}
|
||||
LocalDate endMonth = anchor.with(TemporalAdjusters.firstDayOfMonth());
|
||||
LocalDate startMonth = endMonth.minusMonths(n - 1L);
|
||||
LocalDate rangeEnd = endMonth.with(TemporalAdjusters.lastDayOfMonth());
|
||||
|
||||
List<MonthAcc> series = new ArrayList<>();
|
||||
Map<String, MonthAcc> index = new LinkedHashMap<>();
|
||||
DateTimeFormatter ym = DateTimeFormatter.ofPattern("yyyy-MM");
|
||||
for (int i = 0; i < n; i++) {
|
||||
LocalDate m = startMonth.plusMonths(i);
|
||||
MonthAcc acc = new MonthAcc();
|
||||
acc.key = m.format(ym);
|
||||
acc.label = m.getYear() + "年" + m.getMonthValue() + "月";
|
||||
series.add(acc);
|
||||
index.put(acc.key, acc);
|
||||
}
|
||||
|
||||
JDOrder query = new JDOrder();
|
||||
query.getParams().put("beginTime", startMonth.format(DAY));
|
||||
query.getParams().put("endTime", rangeEnd.format(DAY));
|
||||
List<JDOrder> rows = jdOrderService.selectJDOrderList(query);
|
||||
if (rows == null) {
|
||||
rows = Collections.emptyList();
|
||||
}
|
||||
int used = 0;
|
||||
for (JDOrder o : rows) {
|
||||
if (!isCountEnabled(o) || o.getOrderTime() == null) {
|
||||
continue;
|
||||
}
|
||||
String key = o.getOrderTime().toInstant().atZone(ZONE).format(ym);
|
||||
MonthAcc acc = index.get(key);
|
||||
if (acc == null) {
|
||||
continue;
|
||||
}
|
||||
used++;
|
||||
acc.orderCount++;
|
||||
String kind = markKind(blankTo(o.getDistributionMark(), "未标记"));
|
||||
KindAcc ka = acc.byKind.computeIfAbsent(kind, k -> new KindAcc());
|
||||
ka.orderCount++;
|
||||
boolean refunded = o.getIsRefunded() != null && o.getIsRefunded() == 1;
|
||||
if (refunded) {
|
||||
acc.refundCount++;
|
||||
ka.refundCount++;
|
||||
continue;
|
||||
}
|
||||
if (o.getProfit() == null) {
|
||||
acc.missingProfit++;
|
||||
ka.missingProfit++;
|
||||
} else {
|
||||
BigDecimal pft = money(o.getProfit());
|
||||
acc.profit = acc.profit.add(pft);
|
||||
ka.profit = ka.profit.add(pft);
|
||||
}
|
||||
}
|
||||
String text = renderMonthsText(series, LocalDate.now(ZONE));
|
||||
log.info("导出利润文本 months={} rows={} used={} chars={}", n, rows.size(), used, text.length());
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
out.put("text", text);
|
||||
out.put("chars", text.length());
|
||||
out.put("months", n);
|
||||
out.put("begin", series.get(0).label);
|
||||
out.put("end", series.get(series.size() - 1).label);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static String renderMonthsText(List<MonthAcc> series, LocalDate today) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
MonthAcc first = series.get(0);
|
||||
MonthAcc last = series.get(series.size() - 1);
|
||||
String thisMonth = today.format(DateTimeFormatter.ofPattern("yyyy-MM"));
|
||||
sb.append("慢单月度利润与单量\n");
|
||||
sb.append("区间:").append(first.label).append(" 至 ").append(last.label);
|
||||
if (thisMonth.equals(last.key)) {
|
||||
sb.append("(").append(last.label).append("截至 ").append(today.format(DAY)).append(")");
|
||||
}
|
||||
sb.append("\n");
|
||||
sb.append("口径:只含参与统计的单。退款计入总单量,不计入利润。利润是订单上已经算好的 profit,单位元。\n");
|
||||
sb.append("类型:自有单=F开头且除去F-W、F-H;代下单=仅F-W、F-H;代下单单=其余前缀。三类分开,不要合并。后返不是收入。\n\n");
|
||||
|
||||
int sumOrder = 0;
|
||||
int sumRefund = 0;
|
||||
int sumMissing = 0;
|
||||
BigDecimal sumProfit = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
||||
Map<String, KindAcc> sumKind = new LinkedHashMap<>();
|
||||
MonthAcc prev = null;
|
||||
for (MonthAcc m : series) {
|
||||
int valid = m.orderCount - m.refundCount;
|
||||
sb.append(m.label).append('\n');
|
||||
sb.append("总单 ").append(m.orderCount)
|
||||
.append(" 退款 ").append(m.refundCount)
|
||||
.append(" 有效 ").append(valid)
|
||||
.append(" 缺利润 ").append(m.missingProfit)
|
||||
.append(" 利润 ").append(plain(m.profit))
|
||||
.append(" 单均 ").append(plain(avgOf(m.profit, valid)))
|
||||
.append('\n');
|
||||
appendKindLine(sb, "自有单", m.byKind.get("自有单"));
|
||||
appendKindLine(sb, "代下单", m.byKind.get("代下单"));
|
||||
appendKindLine(sb, "代下单单", m.byKind.get("代下单单"));
|
||||
KindAcc unmarked = m.byKind.get("未标记");
|
||||
if (unmarked != null && unmarked.orderCount > 0) {
|
||||
appendKindLine(sb, "未标记", unmarked);
|
||||
}
|
||||
if (prev == null) {
|
||||
sb.append("较上月 —\n");
|
||||
} else {
|
||||
int prevValid = prev.orderCount - prev.refundCount;
|
||||
sb.append("较上月 有效 ").append(signedInt(valid - prevValid))
|
||||
.append(" 利润 ").append(signedMoney(m.profit.subtract(prev.profit)))
|
||||
.append('\n');
|
||||
}
|
||||
sb.append('\n');
|
||||
sumOrder += m.orderCount;
|
||||
sumRefund += m.refundCount;
|
||||
sumMissing += m.missingProfit;
|
||||
sumProfit = sumProfit.add(m.profit);
|
||||
mergeKind(sumKind, m.byKind);
|
||||
prev = m;
|
||||
}
|
||||
int sumValid = sumOrder - sumRefund;
|
||||
sb.append("合计\n");
|
||||
sb.append("总单 ").append(sumOrder)
|
||||
.append(" 退款 ").append(sumRefund)
|
||||
.append(" 有效 ").append(sumValid)
|
||||
.append(" 缺利润 ").append(sumMissing)
|
||||
.append(" 利润 ").append(plain(sumProfit))
|
||||
.append(" 单均 ").append(plain(avgOf(sumProfit, sumValid)))
|
||||
.append('\n');
|
||||
appendKindLine(sb, "自有单", sumKind.get("自有单"));
|
||||
appendKindLine(sb, "代下单", sumKind.get("代下单"));
|
||||
appendKindLine(sb, "代下单单", sumKind.get("代下单单"));
|
||||
KindAcc unmarkedSum = sumKind.get("未标记");
|
||||
if (unmarkedSum != null && unmarkedSum.orderCount > 0) {
|
||||
appendKindLine(sb, "未标记", unmarkedSum);
|
||||
}
|
||||
sb.append('\n');
|
||||
sb.append("请根据以上各月利润和单量分析收入情况:趋势、三类各自的单量和利润、哪几个月更好或更差。不要编造上面没有的数字。\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static void appendKindLine(StringBuilder sb, String name, KindAcc src) {
|
||||
KindAcc a = src == null ? new KindAcc() : src;
|
||||
int valid = a.orderCount - a.refundCount;
|
||||
sb.append(name).append(" 有效 ").append(valid).append(" 利润 ").append(plain(a.profit)).append('\n');
|
||||
}
|
||||
|
||||
private static void mergeKind(Map<String, KindAcc> sum, Map<String, KindAcc> part) {
|
||||
for (Map.Entry<String, KindAcc> e : part.entrySet()) {
|
||||
KindAcc dst = sum.computeIfAbsent(e.getKey(), k -> new KindAcc());
|
||||
KindAcc src = e.getValue();
|
||||
dst.orderCount += src.orderCount;
|
||||
dst.refundCount += src.refundCount;
|
||||
dst.missingProfit += src.missingProfit;
|
||||
dst.profit = dst.profit.add(src.profit);
|
||||
}
|
||||
}
|
||||
|
||||
private static BigDecimal avgOf(BigDecimal profit, int valid) {
|
||||
if (valid <= 0) {
|
||||
return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
return profit.divide(BigDecimal.valueOf(valid), 2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
private static String plain(BigDecimal v) {
|
||||
if (v == null) {
|
||||
return "0.00";
|
||||
}
|
||||
return v.setScale(2, RoundingMode.HALF_UP).toPlainString();
|
||||
}
|
||||
|
||||
private static String signedInt(int n) {
|
||||
return n > 0 ? "+" + n : String.valueOf(n);
|
||||
}
|
||||
|
||||
private static String signedMoney(BigDecimal v) {
|
||||
BigDecimal n = v == null ? BigDecimal.ZERO : v.setScale(2, RoundingMode.HALF_UP);
|
||||
if (n.compareTo(BigDecimal.ZERO) > 0) {
|
||||
return "+" + n.toPlainString();
|
||||
}
|
||||
return n.toPlainString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> analyze(String periodType, String date, boolean push, String profileId, String trigger) {
|
||||
PeriodRange current = resolvePeriod(periodType, date, false);
|
||||
@@ -1479,6 +1672,16 @@ public class OrderProfitAnalysisServiceImpl implements IOrderProfitAnalysisServi
|
||||
BigDecimal profit = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
private static final class MonthAcc {
|
||||
String key;
|
||||
String label;
|
||||
int orderCount;
|
||||
int refundCount;
|
||||
int missingProfit;
|
||||
BigDecimal profit = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
|
||||
Map<String, KindAcc> byKind = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
private static final class KindAcc {
|
||||
int orderCount;
|
||||
int refundCount;
|
||||
|
||||
Reference in New Issue
Block a user