Commit 6d133f0f by 林永健

feat(integral): 添加一键付款前限制门店校验功能

- 在 IntegralController 中新增 transferOutCheckAllPayHasLimitStore 接口
- 在 IntegralTransferOutMapper 中添加 existsLimitStore 方法和对应的 SQL 查询
- 在 IntegralTransferOutRemote 和实现类中新增 checkAllPayHasLimitStore 远程调用
- 在 IntegralTransferOutService 中实现限制门店校验业务逻辑
- 重构 IntegralTransferOutMapper.xml 中的查询条件为可复用的 sql 片段
- 添加专门用于限制门店校验的 existsLimitStore 查询,使用 select 1 limit 1 优化性能
1 parent 66335d48
...@@ -119,6 +119,11 @@ public interface IntegralTransferOutRemote { ...@@ -119,6 +119,11 @@ public interface IntegralTransferOutRemote {
@ApiResponse("结果") @ApiResponse("结果")
RpcResponse<Boolean> allPay(IntegralTransferOutDTO request); RpcResponse<Boolean> allPay(IntegralTransferOutDTO request);
@ApiMethod("一键付款前校验是否存在限制门店")
@ApiParam(name = "request", notes = "参数", dataType = ApiParam.DataTypeEnum.OBJECT, required = true)
@ApiResponse("是否存在限制门店")
RpcResponse<Boolean> checkAllPayHasLimitStore(IntegralTransferOutDTO request);
@ApiMethod("一键操作获取合计金额,校验") @ApiMethod("一键操作获取合计金额,校验")
@ApiParam(name = "request", notes = "参数", dataType = ApiParam.DataTypeEnum.OBJECT, required = true) @ApiParam(name = "request", notes = "参数", dataType = ApiParam.DataTypeEnum.OBJECT, required = true)
@ApiResponse("结果") @ApiResponse("结果")
......
...@@ -74,4 +74,12 @@ public interface IntegralTransferOutMapper extends OreMapper<IntegralTransferOut ...@@ -74,4 +74,12 @@ public interface IntegralTransferOutMapper extends OreMapper<IntegralTransferOut
* @return 去重后的省份快照行 * @return 去重后的省份快照行
*/ */
List<IntegralTransferOut> listDistinctProvinces(); List<IntegralTransferOut> listDistinctProvinces();
/**
* 判断是否存在限制门店(一键付款前校验,select 1 limit 1,不走 list 全量查询)。
*
* @param request 查询条件
* @return 1=存在,null=不存在
*/
Integer existsLimitStore(IntegralTransferOutDTO request);
} }
\ No newline at end of file \ No newline at end of file
...@@ -342,6 +342,21 @@ public class IntegralTransferOutRemoteImpl implements IntegralTransferOutRemote ...@@ -342,6 +342,21 @@ public class IntegralTransferOutRemoteImpl implements IntegralTransferOutRemote
} }
@Override @Override
public RpcResponse<Boolean> checkAllPayHasLimitStore(IntegralTransferOutDTO request) {
try {
return RpcResponse.success(integralTransferOutService.checkAllPayHasLimitStore(request));
} catch (Exception e) {
log.error("IntegralTransferOutRemote.checkAllPayHasLimitStore exception:{}", e);
if (e instanceof RpcServiceException) {
RpcServiceException rse = (RpcServiceException) e;
return RpcResponse.error(rse);
} else {
return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR);
}
}
}
@Override
public RpcResponse<IntegralTransferOutBatchCheckDTO> getAllCheckByCondition(IntegralTransferOutDTO request) { public RpcResponse<IntegralTransferOutBatchCheckDTO> getAllCheckByCondition(IntegralTransferOutDTO request) {
try { try {
return RpcResponse.success(integralTransferOutService.getAllCheckByCondition(request)); return RpcResponse.success(integralTransferOutService.getAllCheckByCondition(request));
......
...@@ -676,6 +676,28 @@ public class IntegralTransferOutService { ...@@ -676,6 +676,28 @@ public class IntegralTransferOutService {
} }
/** /**
* 一键付款前校验是否存在限制门店。
*
* @param request 查询条件(与一键付款筛选条件一致)
* @return true=存在限制门店,false=不存在
*/
public Boolean checkAllPayHasLimitStore(IntegralTransferOutDTO request) {
try {
List<String> merchantNos = listLimitStoreMerchantNos();
if (CollectionUtils.isEmpty(merchantNos)) {
return false;
}
request.setLimitStoreMerchantNos(merchantNos);
// SQL 已过滤 transfer_type != 0(排除积分提现),与一键付款逻辑一致;select 1 limit 1 仅判断是否存在
Integer exists = integralTransferOutMapper.existsLimitStore(request);
return exists != null && exists > 0;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR, e.getMessage());
}
}
/**
* 通过id查询详情 * 通过id查询详情
* *
* @param id * @param id
......
...@@ -86,6 +86,34 @@ ...@@ -86,6 +86,34 @@
from shopmall_integral_transfer_out ito from shopmall_integral_transfer_out ito
<where> <where>
ito.deleted_flag = 'N' ito.deleted_flag = 'N'
<include refid="ListWhereConditions" />
<choose>
<when test="limitStoreFlagQuery != null and limitStoreFlagQuery == 'Y'.toString()">
<if test="limitStoreMerchantNos != null and limitStoreMerchantNos.size() > 0">
and ito.merchant_id in
<foreach collection="limitStoreMerchantNos" item="merchantNo" open="(" close=")" separator=",">
#{merchantNo}
</foreach>
</if>
<if test="limitStoreMerchantNos == null or limitStoreMerchantNos.size() == 0">
and 1=0
</if>
</when>
<when test="limitStoreFlagQuery != null and limitStoreFlagQuery == 'N'.toString()">
<if test="limitStoreMerchantNos != null and limitStoreMerchantNos.size() > 0">
and (ito.merchant_id not in
<foreach collection="limitStoreMerchantNos" item="merchantNo" open="(" close=")" separator=",">
#{merchantNo}
</foreach>
or ito.merchant_id is null)
</if>
</when>
</choose>
</where>
order by ito.id desc
</select>
<sql id="ListWhereConditions">
<if test="merchantId != null"> <if test="merchantId != null">
and ito.merchant_id = #{merchantId} and ito.merchant_id = #{merchantId}
</if> </if>
...@@ -176,8 +204,16 @@ ...@@ -176,8 +204,16 @@
#{tagId} #{tagId}
</foreach> </foreach>
</if> </if>
<choose> </sql>
<when test="limitStoreFlagQuery != null and limitStoreFlagQuery == 'Y'.toString()">
<!-- 判断是否存在限制门店(仅用于一键付款前校验,不走 list 全量查询) -->
<select id="existsLimitStore" resultType="java.lang.Integer">
select 1
from shopmall_integral_transfer_out ito
<where>
ito.deleted_flag = 'N'
and ito.transfer_type != 0
<include refid="ListWhereConditions" />
<if test="limitStoreMerchantNos != null and limitStoreMerchantNos.size() > 0"> <if test="limitStoreMerchantNos != null and limitStoreMerchantNos.size() > 0">
and ito.merchant_id in and ito.merchant_id in
<foreach collection="limitStoreMerchantNos" item="merchantNo" open="(" close=")" separator=","> <foreach collection="limitStoreMerchantNos" item="merchantNo" open="(" close=")" separator=",">
...@@ -187,19 +223,8 @@ ...@@ -187,19 +223,8 @@
<if test="limitStoreMerchantNos == null or limitStoreMerchantNos.size() == 0"> <if test="limitStoreMerchantNos == null or limitStoreMerchantNos.size() == 0">
and 1=0 and 1=0
</if> </if>
</when>
<when test="limitStoreFlagQuery != null and limitStoreFlagQuery == 'N'.toString()">
<if test="limitStoreMerchantNos != null and limitStoreMerchantNos.size() > 0">
and (ito.merchant_id not in
<foreach collection="limitStoreMerchantNos" item="merchantNo" open="(" close=")" separator=",">
#{merchantNo}
</foreach>
or ito.merchant_id is null)
</if>
</when>
</choose>
</where> </where>
order by ito.id desc limit 1
</select> </select>
<select id="getDetailById" resultMap="DetailResultMap"> <select id="getDetailById" resultMap="DetailResultMap">
......
  • @wangxw1
    commented
    Owner

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

    1. 敏感信息泄露风险(安全性)

    • 问题描述:在 IntegralTransferOutService.java 的 checkAllPayHasLimitStore 方法中,捕获异常时直接将底层异常信息抛出:throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR, e.getMessage());。
    • 影响分析:如果发生数据库异常,e.getMessage() 可能会包含 SQL 语句、表名、字段名等底层敏感信息。将其封装在 RPC 异常中透传,极易导致敏感信息暴露给上游调用方甚至前端用户,存在安全隐患。
    • 优化建议:日志中保留完整堆栈用于排查问题,对外抛出异常时使用统一的、脱敏的错误提示。 java // 优化示例 catch (Exception e) { log.error("校验限制门店发生数据库异常, request: {}", request, e); // 仅抛出枚举中定义好的标准错误信息,不透传 e.getMessage() throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR); }

    2. 篡改方法入参导致副作用(最佳实践)

    • 问题描述:在 IntegralTransferOutService.java 中,直接修改了传入的 DTO 参数:request.setLimitStoreMerchantNos(merchantNos);。
    • 影响分析:修改入参是一种反模式(带来副作用)。在 RPC 场景或复杂的业务链路中,调用方可能在后续逻辑中继续使用该 request 对象,隐式地改变对象状态会导致难以排查的 Bug。
    • 优化建议:避免污染原入参。建议在 Mapper 接口中使用 @Param 注解将 request 和 merchantNos 作为两个独立参数传递,或在 Service 中构建专门的 Query 对象。 java // Mapper 接口修改示例 Integer existsLimitStore(@Param("req") IntegralTransferOutDTO request, @Param("limitNos") List<String> limitNos);

    3. XML 中存在无法触达的死代码(可维护性)

    • 问题描述:在 IntegralTransferOutMapper.xml 的 existsLimitStore 方法中,存在如下判断逻辑: <if test="limitStoreMerchantNos == null or limitStoreMerchantNos.size() == 0"> and 1=0 </if>
    • 影响分析:在 Service 层的逻辑中,已经前置判断了 if (CollectionUtils.isEmpty(merchantNos)) { return false; },因此当集合为空时根本不会发起数据库调用。XML 中的这段防御性代码实际上是死代码(Dead Code),增加了代码的阅读和维护成本。
    • 优化建议:直接移除 XML 中这块冗余的 <if> 判断,保持 SQL 模板的简洁。

    二、评分明细

    • 功能实现的正确性与健壮性(40/40分): 逻辑实现完全正确。成功规避了 MyBatis 返回单一聚合结果时可能为 null 的 NPE 风险(exists != null && exists > 0 处理得很严谨)。未触发“使用子查询”和“变更已有远程接口参数”的红线规则。

    • 安全性与潜在风险(25/30分): 扣除 5 分。SQL 层面正确使用了 MyBatis 预编译和 CONCAT 防范注入,安全性良好;但 Service 层存在透传 e.getMessage() 导致底层数据库敏感信息泄露的风险。

    • 最佳实践与可维护性(16/20分): 扣除 4 分。XML 抽取 <sql id="ListWhereConditions"> 提升了复用性,值得表扬。但 Service 层存在修改入参(副作用)的不良习惯,且 XML 中包含无用的死代码。

    • 性能与资源利用(5/5分): 满分。针对“是否存在”的校验场景,精准使用了 select 1 ... limit 1 替代了原有的全量 list 查询,避免了不必要的网络传输和内存开销,性能优化意识极佳。

    • 提交信息质量(5/5分): 满分。Commit 遵循了 Angular 规范(feat:),并且详细列出了 Controller、Mapper、Remote、Service 等各层的具体变更点及优化目的,清晰可追溯。

    总分:91分

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