Commit 60fe0217 by 庞善天

平均房价计算逻辑修改

1 parent 4045dc4e
...@@ -19,6 +19,9 @@ import org.springframework.core.env.Environment; ...@@ -19,6 +19,9 @@ import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.stream.Stream;
/** /**
* 每月1号上午10点30分定时任务推送美团消费公司月度报告 * 每月1号上午10点30分定时任务推送美团消费公司月度报告
* *
...@@ -75,6 +78,15 @@ public class MeiTuanCompanyMonthlyReportJob implements SimpleJob { ...@@ -75,6 +78,15 @@ public class MeiTuanCompanyMonthlyReportJob implements SimpleJob {
log.info("公司月度报告推送测试电话,testPhone:{}", testPhone); log.info("公司月度报告推送测试电话,testPhone:{}", testPhone);
String[] persons = personList.split(","); String[] persons = personList.split(",");
String personHrList = env.getProperty("staff.meituan.order.send.hr.person", String.class);
log.info("推送hr人员名单:{}", personHrList);
if (!StringUtils.isEmpty(personHrList)){
String[] hrPersons = personHrList.split(",");
persons = Stream.concat(Arrays.stream(persons), Arrays.stream(hrPersons))
.toArray(String[]::new);
}
for (String person : persons) { for (String person : persons) {
String[] nameAndPhone = person.split(":"); String[] nameAndPhone = person.split(":");
if (nameAndPhone.length > 1) { if (nameAndPhone.length > 1) {
......
...@@ -77,6 +77,9 @@ public class MeiTuanDeptMonthlyReportJob implements SimpleJob { ...@@ -77,6 +77,9 @@ public class MeiTuanDeptMonthlyReportJob implements SimpleJob {
String testPhone = env.getProperty("staff.meituan.order.send.qw.test.phone", String.class); String testPhone = env.getProperty("staff.meituan.order.send.qw.test.phone", String.class);
log.info("部门月度报告推送测试电话,testPhone:{}", testPhone); log.info("部门月度报告推送测试电话,testPhone:{}", testPhone);
String personHrList = env.getProperty("staff.meituan.order.send.hr.person", String.class);
log.info("推送hr人员名单:{}", personHrList);
int pushCount = 0; int pushCount = 0;
for (DeptMonthlyReportDTO dto : reportList) { for (DeptMonthlyReportDTO dto : reportList) {
Long directOrgId = dto.getDirectOrgId(); Long directOrgId = dto.getDirectOrgId();
...@@ -87,6 +90,22 @@ public class MeiTuanDeptMonthlyReportJob implements SimpleJob { ...@@ -87,6 +90,22 @@ public class MeiTuanDeptMonthlyReportJob implements SimpleJob {
continue; continue;
} }
if (!StringUtils.isEmpty(personHrList)){
String[] persons = personHrList.split(",");
for (String person : persons) {
String[] nameAndPhone = person.split(":");
if (nameAndPhone.length > 1) {
StaffUniqueMarkInfo markInfo = new StaffUniqueMarkInfo();
String name = nameAndPhone[0];
String mobile = nameAndPhone[1];
markInfo.setStaffName(name);
markInfo.setStaffIdentifier(mobile);
staffUniqueMarkInfos.add(markInfo);
}
}
}
for (StaffUniqueMarkInfo staffUniqueMarkInfo : staffUniqueMarkInfos) { for (StaffUniqueMarkInfo staffUniqueMarkInfo : staffUniqueMarkInfos) {
Long staffId = staffUniqueMarkInfo.getStaffId(); Long staffId = staffUniqueMarkInfo.getStaffId();
String name = staffUniqueMarkInfo.getStaffName(); String name = staffUniqueMarkInfo.getStaffName();
......
...@@ -494,12 +494,12 @@ public class StaffMeituanOrderService { ...@@ -494,12 +494,12 @@ public class StaffMeituanOrderService {
} }
// 部门总消费环比 // 部门总消费环比
BigDecimal deptDinnerRing = calculateMonthOnMonth(deptDinnerAmount, deptDinnerAmountPrev); BigDecimal deptDinnerRing = calculateMonthOnMonth(deptDinnerAmount, deptDinnerAmountPrev);
// 部门总消费人数
Integer consumeCountTotal = curList.stream().map(o -> o.getConsumeCount() == null ? 0 : o.getConsumeCount()).reduce(0, Integer::sum);
// 人均招待费 // 人均招待费
BigDecimal avgConsumeAmountTotal = sumAmount(curList, StaffMeituanOrder::getAvgConsumeAmount);
int cnt = curList.size();
BigDecimal avgConsumeAmount = BigDecimal.ZERO; BigDecimal avgConsumeAmount = BigDecimal.ZERO;
if (cnt > 0) { if (consumeCountTotal!=null && consumeCountTotal > 0) {
avgConsumeAmount = avgConsumeAmountTotal.divide(BigDecimal.valueOf(cnt), 2, RoundingMode.HALF_UP); avgConsumeAmount = deptDinnerAmount.divide(BigDecimal.valueOf(consumeCountTotal), 2, RoundingMode.HALF_UP);
} }
// 部门超标人次 // 部门超标人次
int exceedCount = (int) curList.stream().filter(o -> Objects.equals(o.getIsExceed(), MeituanExceedEnum.EXCEED.getCode())).count(); int exceedCount = (int) curList.stream().filter(o -> Objects.equals(o.getIsExceed(), MeituanExceedEnum.EXCEED.getCode())).count();
...@@ -535,19 +535,22 @@ public class StaffMeituanOrderService { ...@@ -535,19 +535,22 @@ public class StaffMeituanOrderService {
} }
// 部门总消费环比 // 部门总消费环比
BigDecimal deptTravelRing = calculateMonthOnMonth(deptTravelAmount, deptTravelAmountPrev); BigDecimal deptTravelRing = calculateMonthOnMonth(deptTravelAmount, deptTravelAmountPrev);
// 上个月部门总住宿天数
Integer consumeCountTotal = curList.stream().map(o -> o.getConsumeCount() == null ? 0 : o.getConsumeCount()).reduce(0, Integer::sum);
// 上个月部门平均房价 // 上个月部门平均房价
BigDecimal totalAvg = sumAmount(curList, StaffMeituanOrder::getAvgConsumeAmount);
int cnt = curList.size();
BigDecimal avgPrice = BigDecimal.ZERO; BigDecimal avgPrice = BigDecimal.ZERO;
if (cnt > 0) { if (consumeCountTotal!=null && consumeCountTotal > 0) {
avgPrice = totalAvg.divide(BigDecimal.valueOf(cnt), 2, RoundingMode.HALF_UP); avgPrice = deptTravelAmount.divide(BigDecimal.valueOf(consumeCountTotal), 2, RoundingMode.HALF_UP);
}
// 上个月部门总住宿天数
Integer consumeCountTotalPrev = 0;
if (CollectionUtils.isNotEmpty(prevList)){
consumeCountTotalPrev = prevList.stream().map(o -> o.getConsumeCount() == null ? 0 : o.getConsumeCount()).reduce(0, Integer::sum);
} }
// 上上个月部门平均房价 // 上上个月部门平均房价
BigDecimal totalAvgPrevDept = sumAmount(prevList, StaffMeituanOrder::getAvgConsumeAmount);
int cntPrev = prevList.size();
BigDecimal avgPricePrev = BigDecimal.ZERO; BigDecimal avgPricePrev = BigDecimal.ZERO;
if (cntPrev > 0) { if (consumeCountTotalPrev!=null && consumeCountTotalPrev > 0) {
avgPricePrev = totalAvgPrevDept.divide(BigDecimal.valueOf(cntPrev), 2, RoundingMode.HALF_UP); avgPricePrev = deptTravelAmountPrev.divide(BigDecimal.valueOf(consumeCountTotalPrev), 2, RoundingMode.HALF_UP);
} }
// 部门平均房价环比 // 部门平均房价环比
BigDecimal avgPriceRing = calculateMonthOnMonth(avgPrice, avgPricePrev); BigDecimal avgPriceRing = calculateMonthOnMonth(avgPrice, avgPricePrev);
...@@ -642,6 +645,7 @@ public class StaffMeituanOrderService { ...@@ -642,6 +645,7 @@ public class StaffMeituanOrderService {
// 入住城市数为0 差旅住宿隐藏 // 入住城市数为0 差旅住宿隐藏
int cityCount = 0; int cityCount = 0;
BigDecimal travelTotalAmount = BigDecimal.ZERO; BigDecimal travelTotalAmount = BigDecimal.ZERO;
Integer travelRoomAmount = 0;
BigDecimal avgRoomPrice = BigDecimal.ZERO; BigDecimal avgRoomPrice = BigDecimal.ZERO;
String travelCityName = ""; String travelCityName = "";
String travelShopName = ""; String travelShopName = "";
...@@ -653,11 +657,11 @@ public class StaffMeituanOrderService { ...@@ -653,11 +657,11 @@ public class StaffMeituanOrderService {
cityCount = citySet.size(); cityCount = citySet.size();
// 住宿总金额 // 住宿总金额
travelTotalAmount = sumAmount(travelOrders, StaffMeituanOrder::getTotalRealAmount); travelTotalAmount = sumAmount(travelOrders, StaffMeituanOrder::getTotalRealAmount);
// 住宿总天数
travelRoomAmount = travelOrders.stream().map(o -> o.getConsumeCount() == null ? 0 : o.getConsumeCount()).reduce(0, Integer::sum);
// 平均房价 // 平均房价
BigDecimal totalAvgRoom = sumAmount(travelOrders, StaffMeituanOrder::getAvgConsumeAmount); if (travelRoomAmount != null && travelRoomAmount > 0) {
int travelSize = travelOrders.size(); avgRoomPrice = travelTotalAmount.divide(BigDecimal.valueOf(travelRoomAmount), 2, RoundingMode.HALF_UP);
if (travelSize > 0) {
avgRoomPrice = totalAvgRoom.divide(BigDecimal.valueOf(travelSize), 2, RoundingMode.HALF_UP);
} }
// 差旅消费最多的记录 // 差旅消费最多的记录
StaffMeituanOrder maxTravelOrder = travelOrders.stream().filter(o -> o.getAvgConsumeAmount() != null) StaffMeituanOrder maxTravelOrder = travelOrders.stream().filter(o -> o.getAvgConsumeAmount() != null)
...@@ -801,17 +805,23 @@ public class StaffMeituanOrderService { ...@@ -801,17 +805,23 @@ public class StaffMeituanOrderService {
.filter(o -> Objects.equals(o.getSceneType(), MeituanSceneTypeEnum.BUSINESS_TRAVEL.getCode())) .filter(o -> Objects.equals(o.getSceneType(), MeituanSceneTypeEnum.BUSINESS_TRAVEL.getCode()))
.collect(Collectors.toList()); .collect(Collectors.toList());
// 上个月差旅住宿全部天数
int consumeTotalCount = travelLast.stream().map(o -> o.getConsumeCount() == null ? 0 : o.getConsumeCount()).reduce(0, Integer::sum);
// 上个月差旅住宿总天数 // 上个月差旅住宿总天数
int travelDayCount = travelLast.size(); int travelDayCount = travelLast.size();
// 上上个月差旅住宿 // 上上个月差旅住宿
List<StaffMeituanOrder> travelPrev = new ArrayList<>(); List<StaffMeituanOrder> travelPrev = new ArrayList<>();
int travelDayCountPrev = 0; int travelDayCountPrev = 0;
int consumeTotalCountPrev = 0;
if (CollectionUtils.isNotEmpty(prevDeptOrders)){ if (CollectionUtils.isNotEmpty(prevDeptOrders)){
travelPrev = prevDeptOrders.stream().filter(o -> Objects.equals(o.getSceneType(), MeituanSceneTypeEnum.BUSINESS_TRAVEL.getCode())) travelPrev = prevDeptOrders.stream().filter(o -> Objects.equals(o.getSceneType(), MeituanSceneTypeEnum.BUSINESS_TRAVEL.getCode()))
.collect(Collectors.toList()); .collect(Collectors.toList());
// 上上个月差旅住宿总天数 // 上上个月差旅住宿总天数
travelDayCountPrev = travelPrev.size(); travelDayCountPrev = travelPrev.size();
// 上上个月差旅住宿全部天数
consumeTotalCountPrev = travelPrev.stream().map(o -> o.getConsumeCount() == null ? 0 : o.getConsumeCount()).reduce(0, Integer::sum);
} }
// 差旅住宿总消费 // 差旅住宿总消费
...@@ -823,18 +833,14 @@ public class StaffMeituanOrderService { ...@@ -823,18 +833,14 @@ public class StaffMeituanOrderService {
BigDecimal travelPersonCountRing = calculateMonthOnMonth(BigDecimal.valueOf(travelDayCount), BigDecimal.valueOf(travelDayCountPrev)); BigDecimal travelPersonCountRing = calculateMonthOnMonth(BigDecimal.valueOf(travelDayCount), BigDecimal.valueOf(travelDayCountPrev));
// 上个月差旅平均房价 // 上个月差旅平均房价
BigDecimal totalTravelAvg = sumAmount(travelLast, StaffMeituanOrder::getAvgConsumeAmount);
int travelRecordCount = travelLast.size();
BigDecimal avgTravelRoomPrice = BigDecimal.ZERO; BigDecimal avgTravelRoomPrice = BigDecimal.ZERO;
if (travelRecordCount > 0) { if (consumeTotalCount > 0) {
avgTravelRoomPrice = totalTravelAvg.divide(BigDecimal.valueOf(travelRecordCount), 2, RoundingMode.HALF_UP); avgTravelRoomPrice = travelAmount.divide(BigDecimal.valueOf(consumeTotalCount), 2, RoundingMode.HALF_UP);
} }
// 上上个月差旅平均房价 // 上上个月差旅平均房价
BigDecimal totalTravelAvgPrev = sumAmount(travelPrev, StaffMeituanOrder::getAvgConsumeAmount);
int travelRecordCountPrev = travelPrev.size();
BigDecimal avgTravelRoomPricePrev = BigDecimal.ZERO; BigDecimal avgTravelRoomPricePrev = BigDecimal.ZERO;
if (travelRecordCountPrev > 0) { if (consumeTotalCountPrev > 0) {
avgTravelRoomPricePrev = totalTravelAvgPrev.divide(BigDecimal.valueOf(travelRecordCountPrev), 2, RoundingMode.HALF_UP); avgTravelRoomPricePrev = travelAmountPrev.divide(BigDecimal.valueOf(consumeTotalCountPrev), 2, RoundingMode.HALF_UP);
} }
// 差旅平均房价环比 // 差旅平均房价环比
BigDecimal avgTravelRoomPriceRing = calculateMonthOnMonth(avgTravelRoomPrice, avgTravelRoomPricePrev); BigDecimal avgTravelRoomPriceRing = calculateMonthOnMonth(avgTravelRoomPrice, avgTravelRoomPricePrev);
......
  • @wangxw1
    commented
    Master

    你好。我是资深的软件开发工程师,很高兴为你进行代码审查。本次提交主要涉及美团月度报告推送逻辑的增强(增加 HR 抄送名单)以及平均消费计算算法的优化。

    以下是针对本次代码变更的审查反馈:

    一、关键问题与优化建议(Top 3)

    1. 循环内重复解析与潜在的逻辑风险 (Performance & Robustness)

    • 问题描述:在 MeiTuanDeptMonthlyReportJob.java 中,personHrList 的解析(split)和 HR 人员信息的构建被放在了 reportList 的 for 循环内部。
    • 影响分析:
      1. 性能损耗:如果部门较多,程序会反复对同一个环境变量字符串进行分割和遍历,产生大量重复的对象,浪费 CPU 和内存。
      2. 逻辑风险:虽然代码片段未展示 staffUniqueMarkInfos 的初始化位置,但如果该集合在循环外定义且未在循环内清理,会导致 HR 人员被重复添加,从而收到成百上千条重复推送。
    • 优化建议:将 HR 名单的解析逻辑提取到 reportList 循环之外,预先构建好 HR 的 StaffUniqueMarkInfo 列表,在循环内直接 addAll 即可。

    2. 代码高度重复与维护性问题 (Best Practices)

    • 问题描述:在 StaffMeituanOrderService.java 中,计算总消费次数(consumeCount 的求和)的 Stream 流逻辑在多个方法(avgConsumeAmount、avgPrice、avgPricePrev 等计算处)中重复出现了 5 次。
    • 影响分析:违反了 DRY(Don't Repeat Yourself)原则。一旦未来 consumeCount 的取值逻辑发生变化(例如需要处理特定的负值或偏移),开发者必须修改所有 5 处地方,极易遗漏,增加维护成本。
    • 优化建议:封装一个私有的辅助方法用于计算列表中的总消费次数。 java private int sumConsumeCount(List<StaffMeituanOrder> orders) { if (CollectionUtils.isEmpty(orders)) return 0; return orders.stream() .map(o -> o.getConsumeCount() == null ? 0 : o.getConsumeCount()) .reduce(0, Integer::sum); }

    3. 提交信息与实际变更不符 (Commit Message Quality)

    • 问题描述:提交信息仅为“平均房价计算逻辑修改”,但代码实际包含了两个 Job 类中关于“新增 HR 推送名单”的重要功能变更。
    • 影响分析:代码回溯和审计时,通过 git log 无法感知到 HR 推送功能的上线,增加了排查问题的难度。
    • 优化建议:应准确描述所有关键变更,例如:“feat: 增加美团月度报告HR抄送功能,并优化平均房价计算逻辑”。

    二、评分明细

    评分维度 分数 理由
    功能正确性与健壮性 36/40 平均值算法由“平均值的平均”改为“总额/总次数”,准确性大幅提升。但 Job 中的循环逻辑存在重复推送风险。
    安全性与潜在风险 30/30 未发现明显的安全漏洞。配置项读取使用了 env.getProperty 并做了非空校验。
    最佳实践与可维护性 12/20 存在明显的代码重复(Stream 求和逻辑);Job 中的逻辑分层不理想,解析逻辑应与业务循环分离。
    性能与资源利用 4/5 循环内重复 split 字符串属于不必要的资源浪费,但在低频 Job 场景下影响尚可接受。
    提交信息质量 2/5 描述极不完整,遗漏了核心的功能变更说明。

    总分:84分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!