Commit c52848de by zhouhaibao

[+]日报的生态预警门店日期处理以及流失门店的模糊查询过滤

1 parent 19eff771
package cn.com.glsx.dj.peanalysis.dto.res.output;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import lombok.Data;
@ApiModel(description = "流失门店明细信息")
@Data
public class LostStoreDetailResponse implements Serializable {
private static final long serialVersionUID = -1;
// 门店基础信息
@ApiModelProperty(notes = "门店名称", example = "我是门店名称")
private String storeName;
@ApiModelProperty(notes = "门店ID", example = "4401011")
private Integer storeId;
// 整体经营概览
@ApiModelProperty(notes = "最后开单日期", example = "2025-01-02")
private String loatOrderTime;
}
\ No newline at end of file
package cn.com.glsx.dj.peanalysis.dto.res.output;
import java.math.BigDecimal;
import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StorePerformanceDTO {
/**
* 产值 (建议保留两位小数,使用 BigDecimal 避免精度丢失)
* 对应: xxxxxx.xx
*/
private BigDecimal outputValue;
/**
* 出单门店数
* 对应: xx
*/
private Integer activeStoreCount;
/**
* 本周生态系数预警门店数
* 对应: xx
*/
private Integer ecosystemWarningCount;
/**
* 本周兜底预警门店数
* 对应: xx
*/
private Integer floorProtectionWarningCount;
/**
* 【新增】多品类门店占比
* 建议格式:字符串 "xx.xx%" 或 BigDecimal (0.xx)
* 此处沿用之前的字符串格式以保持一致性
*/
private BigDecimal multiCategoryStoreRatio;
/**
* 本月产值达成率 (当前值)
* 对应: xx.xx% (存储时建议存小数 0.xx 或字符串 "xx.xx%")
*/
private BigDecimal currentAchievementRate;
/**
* 本月产值达成率 (预期值)
* 对应: 预期:xx.xx%
*/
private BigDecimal expectedAchievementRate;
}
\ No newline at end of file
......@@ -9,7 +9,9 @@ import cn.com.glsx.dj.peanalysis.service.BusinessAnalysisService;
import cn.com.glsx.dj.peanalysis.service.ProvinceRganizationService;
import cn.com.glsx.dj.peanalysis.service.UpdateMerchantService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
......@@ -144,6 +146,13 @@ public class BusinessAnalysisRemoteImpl implements BusinessAnalysisRemote{
@Override
public RpcResponse<StorePerformanceDTO> staffSearch(BaseReq req) {
try {
RpcAssert.assertIsNull(req, DefaultErrorEnum.DATA_NULL, "req must not ne null");
RpcAssert.assertNotEmpty(req.getStartTime(), DefaultErrorEnum.DATA_NULL, "startTime must not ne null");
RpcAssert.assertNotEmpty(req.getEndTime(), DefaultErrorEnum.DATA_NULL, "endTime must not ne null");
RpcAssert.assertIsNull(req.getCycleType(), DefaultErrorEnum.DATA_NULL, "cycleType must not ne null");
RpcAssert.assertIsNull(req.getLoginRole(), DefaultErrorEnum.DATA_NULL, "loginRole must not ne null");
StorePerformanceDTO staffSearch=businessAnalysisService.staffSearch(req);
return RpcResponse.success(staffSearch);
} catch (RpcServiceException e) {
......
......@@ -47,7 +47,9 @@ import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
......@@ -177,6 +179,11 @@ public class BusinessAnalysisService {
public static final Integer MONTH_CYCLE_TYPE = 2;
public static final Integer DATE_CYCLE_TYPE = 0;
@Autowired
private BiAnalyseBackgroundService biAnalyseBackgroundService;
......@@ -2206,14 +2213,41 @@ public class BusinessAnalysisService {
// 获取兜底预警门店
SceneBaseReq sceneBaseReq = new SceneBaseReq();
sceneBaseReq.setMerchantIds(storeList);
sceneBaseReq.setStartTime(request.getStartTime());
sceneBaseReq.setEndTime(request.getEndTime());
// 当日期是日的时候,获取的是本周的生态预警
if (request.getCycleType() == DATE_CYCLE_TYPE){
// 1. 定义格式化器 (根据实际入参格式调整,通常是 yyyy-MM-dd)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 2. 解析输入字符串为 LocalDate
LocalDate inputDate = LocalDate.parse(request.getStartTime(), formatter);
// 3. 计算本周周一 (本周起始)
LocalDate monday = inputDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
// 4. 计算本周周日 (本周结束)
LocalDate sunday = inputDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
// 5. 转换为 LocalDateTime 以包含具体时间,确保查询范围完整
// 开始时间:周一 00:00:00
LocalDateTime startDateTime = monday.atStartOfDay();
// 结束时间:周日 23:59:59 (重要:如果数据库查询是 <=,必须包含周日全天)
LocalDateTime endDateTime = sunday.atTime(23, 59, 59);
// 6. 格式化回 String (如果 sceneBaseReq 接收的是 String)
// 如果 sceneBaseReq 的 startTime/endTime 是 Date/LocalDateTime 类型,请直接 set 对象,无需 format
sceneBaseReq.setStartTime(startDateTime.format(formatter));
sceneBaseReq.setEndTime(endDateTime.format(formatter));
}else {
sceneBaseReq.setStartTime(request.getStartTime());
sceneBaseReq.setEndTime(request.getEndTime());
}
List<Integer> backUpStores = businessSceneAnalysisService.findBackUpStores(sceneBaseReq);
resp.setFloorProtectionWarningCount(backUpStores.size());
log.info("兜底预警门店数:{}", backUpStores.size());
// 获取生态预警门店
sceneBaseReq.setBizs(1);
List<Integer> ybEcosystemWarningStores = businessSceneAnalysisService.getDelayWarningStore(sceneBaseReq);
sceneBaseReq.setBizs(2);
List<Integer> qgEcosystemWarningStores = businessSceneAnalysisService.getDelayWarningStore(sceneBaseReq);
......@@ -2231,11 +2265,10 @@ public class BusinessAnalysisService {
sceneBaseReq1.setMerchantIds(storeList);
sceneBaseReq1.setStartTime(request.getStartTime());
sceneBaseReq1.setEndTime(request.getEndTime());
sceneBaseReq1.setCycleType(2);
sceneBaseReq1.setCycleType(MONTH_CYCLE_TYPE);
Map<String, BigDecimal> stringBigDecimalMap = businessSceneAnalysisService.queryStoreTargetOutputList(
sceneBaseReq1);
log.info("目标值:{}", stringBigDecimalMap);
BigDecimal tagetOutput = new BigDecimal("0.0");
// 3. 填充 allOutputGroupByMerchantRegionId(本期数据)
for (DataBusinessDTO dto : totalOutputGroupByMerchant) {
......@@ -2268,11 +2301,11 @@ public class BusinessAnalysisService {
resp.setExpectedAchievementRate(rate);
if (request.getCycleType()==0) {
if (request.getCycleType()==DATE_CYCLE_TYPE) {
// 获取当月份产值
request.setStartTime(request.getStartTime().substring(0, 7) + "-01");
request.setEndTime(request.getEndTime().substring(0, 7) + "-" + endDate.lengthOfMonth());
request.setCycleType(1);
request.setCycleType(MONTH_CYCLE_TYPE);
List<DataBusinessDTO> currentMonthOutPut = getOutputGroupByBiz(request);
tagetOutput= currentMonthOutPut.stream()
......
  • @wangxw1
    commented
    Master

    你好,我是负责本次代码审查的资深软件开发工程师。

    这段代码涉及 RPC 接口的入参校验、DTO 定义以及核心业务逻辑中关于日期周期的处理。代码中存在阻断性的逻辑错误以及数据准确性隐患,需要立即修正。

    以下是具体的审查报告:

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

    1. 严重逻辑错误:入参校验断言反向使用(Blocker)

    • 问题描述:在 BusinessAnalysisRemoteImpl.java 中,对于 cycleType 和 loginRole 字段的校验使用了 RpcAssert.assertIsNull。
    • 影响分析:assertIsNull 的语义是“断言该值必须为 Null,否则抛出异常”。结合错误提示信息 "must not ne null"(应为 be),开发者的本意显然是校验字段不能为空。当前的写法会导致所有传入了有效参数的合法请求全部被拦截报错,接口完全不可用。
    • 优化建议:
      1. 将 assertIsNull 修改为 assertNotNull。
      2. 修正错误提示信息中的拼写错误(ne -> be)。
    // 修改前
    RpcAssert.assertIsNull(req.getCycleType(), DefaultErrorEnum.DATA_NULL, "cycleType must not ne null");
    RpcAssert.assertIsNull(req.getLoginRole(), DefaultErrorEnum.DATA_NULL, "loginRole must not ne null");
    
    // 修改后
    RpcAssert.assertNotNull(req.getCycleType(), DefaultErrorEnum.DATA_NULL, "cycleType must not be null");
    RpcAssert.assertNotNull(req.getLoginRole(), DefaultErrorEnum.DATA_NULL, "loginRole must not be null");
    

    2. 数据准确性隐患:日期格式化导致时间精度丢失(Critical)

    • 问题描述:在 BusinessAnalysisService.java 中,计算本周结束时间时,先构建了包含时间的 LocalDateTime (sunday.atTime(23, 59, 59)),但在设置到 sceneBaseReq 时,使用了 yyyy-MM-dd 格式的 formatter 进行格式化。
    • 影响分析:startDateTime.format(formatter) 和 endDateTime.format(formatter) 会丢弃时间部分。
      • 结束时间 2025-01-05 23:59:59 格式化后变为字符串 "2025-01-05"。
      • 如果底层 SQL 使用字符串比较(如 BETWEEN 或 <=),数据库通常将 "2025-01-05" 解析为 "2025-01-05 00:00:00"。
      • 后果:这将导致周日全天的数据被遗漏,统计结果不准确。
    • 优化建议:
      1. 如果底层支持 yyyy-MM-dd HH:mm:ss 格式,请定义包含时间的 Formatter。
      2. 或者在 Service 层直接传递 Date 或 LocalDateTime 对象,由 DAO 层处理格式。
    // 建议修改
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    sceneBaseReq.setStartTime(startDateTime.format(timeFormatter)); 
    sceneBaseReq.setEndTime(endDateTime.format(timeFormatter));
    

    3. API 字段命名拼写错误与规范性(Major)

    • 问题描述:
      1. LostStoreDetailResponse.java 中字段名为 loatOrderTime,明显是 lastOrderTime 的拼写错误。
      2. BusinessAnalysisRemoteImpl.java 中错误提示 must not ne null。
      3. BusinessAnalysisService.java 中变量 tagetOutput 拼写错误(应为 target)。
    • 影响分析:
      • 对外暴露的 API 字段(DTO)一旦发布,客户端接入后将难以修改,永久保留这个拼写错误,极度影响接口的专业性和可维护性。
      • 拼写错误会误导后续维护人员,增加理解成本。
    • 优化建议:
      • 使用 IDE 的拼写检查插件。
      • 立即重构 DTO 字段名。
    // LostStoreDetailResponse.java
    @ApiModelProperty(notes = "最后开单日期", example = "2025-01-02")
    private String lastOrderTime; // 修正 loat -> last
    

    二、评分明细

    维度 评分 理由
    功能正确性 15/40 存在阻断性 Bug(校验逻辑反向),导致接口完全不可用;日期处理逻辑存在丢失数据的风险。
    安全性 28/30 使用了 RpcAssert 进行防御性编程,未见明显的注入或越权风险。
    最佳实践 12/20 存在多处拼写错误(API 字段、日志、错误提示);修改入参对象(request.setStartTime)产生副作用;serialVersionUID 设为 -1 不符合常规。
    性能与资源 4/5 日期计算逻辑基本合理,无明显性能瓶颈。
    提交信息 5/5 提交信息清晰,涵盖了主要变更点。

    总分:64分

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