Commit 379d62f3 by 王欣伟

2610指定授权数据编辑时不能将短期授权改为长期授权

1 parent 27bbf850
...@@ -22,6 +22,16 @@ public interface VehiclePermitMapper extends CommonBaseMapper<VehiclePermit> { ...@@ -22,6 +22,16 @@ public interface VehiclePermitMapper extends CommonBaseMapper<VehiclePermit> {
List<VehiclePermit> selectByReceiverIdAndContentType(@Param("receiverId") Long receiverId, @Param("contentType") Integer contentType); List<VehiclePermit> selectByReceiverIdAndContentType(@Param("receiverId") Long receiverId, @Param("contentType") Integer contentType);
/** /**
* 获取被授权人的全部未删除授权数据,包含未生效及已过期数据
*
* @param receiverId 被授权人id
* @param contentType 授权内容类型
* @return 全部未删除授权数据
*/
List<VehiclePermit> selectAllByReceiverIdAndContentType(@Param("receiverId") Long receiverId,
@Param("contentType") Integer contentType);
/**
* 获取授权数据 * 获取授权数据
* *
* @param receiverId * @param receiverId
...@@ -39,6 +49,14 @@ public interface VehiclePermitMapper extends CommonBaseMapper<VehiclePermit> { ...@@ -39,6 +49,14 @@ public interface VehiclePermitMapper extends CommonBaseMapper<VehiclePermit> {
int insertOrUpdatePermits(@Param("list") List<VehiclePermit> list); int insertOrUpdatePermits(@Param("list") List<VehiclePermit> list);
/** /**
* 批量新增长期授权,已存在的授权(包括限时授权)保持不变
*
* @param list 长期授权数据
* @return 新增数量
*/
int insertLongTermPermitsIfAbsent(@Param("list") List<VehiclePermit> list);
/**
* 删除授权数据 * 删除授权数据
* *
* @param contentIds * @param contentIds
...@@ -49,7 +67,7 @@ public interface VehiclePermitMapper extends CommonBaseMapper<VehiclePermit> { ...@@ -49,7 +67,7 @@ public interface VehiclePermitMapper extends CommonBaseMapper<VehiclePermit> {
int deleteExistedPermit(@Param("contentIds") Collection<Long> contentIds, @Param("receiverId") Long receiverId, @Param("contentType") Integer contentType); int deleteExistedPermit(@Param("contentIds") Collection<Long> contentIds, @Param("receiverId") Long receiverId, @Param("contentType") Integer contentType);
/** /**
* 删除被授权人授权数据 * 删除被授权人的授权数据
* *
* @param receiverId * @param receiverId
* @param contentType * @param contentType
......
...@@ -2499,7 +2499,10 @@ public class VehicleService extends AbstractCommonService { ...@@ -2499,7 +2499,10 @@ public class VehicleService extends AbstractCommonService {
Map<Long, Long> vehicleCreatedByMap = vehicleList.stream().collect(Collectors.toMap(Vehicle::getId, Vehicle::getCreatedBy)); Map<Long, Long> vehicleCreatedByMap = vehicleList.stream().collect(Collectors.toMap(Vehicle::getId, Vehicle::getCreatedBy));
// 2022/3/16 查出已经授权的和这次授权的比较,删除旧数据 // 2022/3/16 查出已经授权的和这次授权的比较,删除旧数据
List<VehiclePermit> vpList = vehiclePermitMapper.selectByReceiverIdAndContentType(assignBO.getReceiverId(), AssignDataTypeEnum.VEHICLE.getType()); // 已保留在提交列表中的限时授权(含已过期记录)不能被覆盖为长期授权。
// 未保留在提交列表中的记录仍按正常取消授权流程删除。
List<VehiclePermit> vpList = vehiclePermitMapper.selectAllByReceiverIdAndContentType(
assignBO.getReceiverId(), AssignDataTypeEnum.VEHICLE.getType());
Set<Long> existedVehicleIds = vpList.stream().map(VehiclePermit::getContentId).collect(Collectors.toSet()); Set<Long> existedVehicleIds = vpList.stream().map(VehiclePermit::getContentId).collect(Collectors.toSet());
...@@ -2533,7 +2536,7 @@ public class VehicleService extends AbstractCommonService { ...@@ -2533,7 +2536,7 @@ public class VehicleService extends AbstractCommonService {
int assignCnt = 0; int assignCnt = 0;
if (CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(list)) {
assignCnt = vehiclePermitMapper.insertOrUpdatePermits(list); assignCnt = vehiclePermitMapper.insertLongTermPermitsIfAbsent(list);
log.info("{}授权给用户[{}] 车辆数据 {}", ShieldContextHolder.getUsername(), assignBO.getReceiverId(), addIdList); log.info("{}授权给用户[{}] 车辆数据 {}", ShieldContextHolder.getUsername(), assignBO.getReceiverId(), addIdList);
} }
...@@ -2573,7 +2576,9 @@ public class VehicleService extends AbstractCommonService { ...@@ -2573,7 +2576,9 @@ public class VehicleService extends AbstractCommonService {
List<VehiclePermit> list = Lists.newArrayList(); List<VehiclePermit> list = Lists.newArrayList();
// 2022/3/16 查出已经授权的和这次授权的比较,删除旧数据 // 2022/3/16 查出已经授权的和这次授权的比较,删除旧数据
List<VehiclePermit> vpList = vehiclePermitMapper.selectByReceiverIdAndContentType(assignBO.getReceiverId(), AssignDataTypeEnum.ORG.getType()); // 已保留在提交列表中的限时授权不能被覆盖为长期授权,移除的记录仍允许删除。
List<VehiclePermit> vpList = vehiclePermitMapper.selectAllByReceiverIdAndContentType(
assignBO.getReceiverId(), AssignDataTypeEnum.ORG.getType());
Set<Long> existedOrgIds = vpList.stream().map(VehiclePermit::getContentId).collect(Collectors.toSet()); Set<Long> existedOrgIds = vpList.stream().map(VehiclePermit::getContentId).collect(Collectors.toSet());
...@@ -2596,7 +2601,7 @@ public class VehicleService extends AbstractCommonService { ...@@ -2596,7 +2601,7 @@ public class VehicleService extends AbstractCommonService {
int assignCnt = 0; int assignCnt = 0;
if (CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(list)) {
assignCnt = vehiclePermitMapper.insertOrUpdatePermits(list); assignCnt = vehiclePermitMapper.insertLongTermPermitsIfAbsent(list);
log.info("{}授权给用户[{}] 组织数据 {}", ShieldContextHolder.getUsername(), assignBO.getReceiverId(), addIdList); log.info("{}授权给用户[{}] 组织数据 {}", ShieldContextHolder.getUsername(), assignBO.getReceiverId(), addIdList);
} }
......
...@@ -39,6 +39,15 @@ ...@@ -39,6 +39,15 @@
and permit_expired_date &gt; now() and permit_expired_date &gt; now()
</select> </select>
<select id="selectAllByReceiverIdAndContentType" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_vehicle_permit
where del_flag = 0
and receiver_id = #{receiverId}
and content_type = #{contentType}
</select>
<select id="selectPermitVehicleByReceiverId" resultType="cn.com.glsx.admin.modules.model.view.VehiclePermitDTO"> <select id="selectPermitVehicleByReceiverId" resultType="cn.com.glsx.admin.modules.model.view.VehiclePermitDTO">
select concat('v', tvp.content_id) as id, select concat('v', tvp.content_id) as id,
tvp.accorder_id accorderId, tvp.accorder_id accorderId,
...@@ -99,6 +108,28 @@ ...@@ -99,6 +108,28 @@
updated_by = VALUES(created_by) updated_by = VALUES(created_by)
</insert> </insert>
<insert id="insertLongTermPermitsIfAbsent">
INSERT IGNORE INTO t_vehicle_permit (content_id, accorder_id, receiver_id, content_type, permit_type,
permit_days, permit_effective_date, permit_expired_date,
del_flag, created_date, created_by)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.contentId},
#{item.accorderId},
#{item.receiverId},
#{item.contentType},
#{item.permitType},
#{item.permitDays},
#{item.permitEffectiveDate},
#{item.permitExpiredDate},
0,
sysdate(),
#{item.createdBy}
)
</foreach>
</insert>
<delete id="deleteExistedPermit"> <delete id="deleteExistedPermit">
delete from t_vehicle_permit delete from t_vehicle_permit
where del_flag = 0 where del_flag = 0
......
  • @wangxw1
    commented
    Owner

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

    1. INSERT IGNORE 的使用风险及唯一索引依赖(功能正确性与健壮性)

    • 问题描述:在 VehiclePermitMapper.xml 中,insertLongTermPermitsIfAbsent 使用了 INSERT IGNORE 语法来实现“已存在的授权保持不变”。此语法强依赖于数据库表中存在正确的唯一索引(例如 content_id, receiver_id, content_type 的联合唯一索引)。如果表结构中缺少该唯一约束,INSERT IGNORE 将失效并直接插入重复数据。此外,INSERT IGNORE 会静默吞掉所有类型的错误(如字段超长、非空校验失败等),不仅限于主键/唯一键冲突,存在数据一致性隐患。
    • 优化建议:
      1. 确认 t_vehicle_permit 表是否已建立准确的联合唯一索引。
      2. 相比于 INSERT IGNORE,更推荐在 Java 内存中通过集合操作(如 removeAll)剔除掉 existedVehicleIds 中已存在的 ID,然后仅对真正需要新增的数据执行普通的批量 INSERT。这样不仅摆脱了对数据库特定语法的依赖,也避免了静默吞没异常的风险。

    2. 包含“已过期”数据导致无法重新授权的业务逻辑漏洞(功能正确性)

    • 问题描述:selectAllByReceiverIdAndContentType 方法查出了包含已过期在内的所有未删除数据。结合后续的 INSERT IGNORE 逻辑,会产生一个严重的业务 Bug:如果某用户曾经拥有一个“短期授权”且目前已经过期,当管理员试图给该用户重新发放“长期授权”时,由于数据库中存在那条已过期的记录,INSERT IGNORE 会跳过插入。结果是:管理员显示操作成功,但用户依然没有有效的授权。
    • 优化建议: 业务逻辑需要区分“有效/未生效”与“已过期”。如果短期授权已过期,应当允许新的长期授权覆盖或插入。建议在 Java 层对查询出的 vpList 进行过滤,若发现仅存的是已过期的限时授权,应当先将其逻辑删除(或更新),再执行新授权的插入;或者在查询时排除掉已过期的数据。

    3. 批量插入中使用 sysdate() 违反规范(最佳实践与可维护性)

    • 问题描述:在 insertLongTermPermitsIfAbsent 的 <foreach> 循环中,创建时间使用了 MySQL 的 sysdate() 函数。根据《阿里巴巴 Java 开发手册》及 MySQL 特性,sysdate() 在执行时是动态获取的(即每插入一行获取一次当前时间),在批量插入大量数据时,可能导致同一批次数据的 created_date 出现微小差异,不利于后续按时间维度的精确查询或排查问题。
    • 优化建议: 建议统一在 Java 代码层生成时间(如 new Date()),并赋值给 VehiclePermit 对象的 createdDate 属性,然后在 MyBatis 中通过 #{item.createdDate} 传入;或者在 SQL 中使用 NOW() 函数(NOW() 在整个 SQL 语句执行期间保持一致)。

    二、评分明细

    审查项 评分 权重 备注说明
    1. 功能实现的正确性与健壮性 28 40分 存在已过期数据阻碍新授权插入的业务逻辑漏洞;INSERT IGNORE 强依赖底层索引设计,存在插入重复数据的潜在风险。扣12分。
    2. 安全性与潜在风险 28 30分 SQL 注入防护良好(均使用了 #{})。但 INSERT IGNORE 吞没异常的特性带来了潜在的数据完整性风险。扣2分。
    3. 最佳实践与可维护性 16 20分 批量插入使用 sysdate() 不符合阿里规范;Mapper 方法名 insertLongTermPermitsIfAbsent 带有业务属性(LongTerm),但 SQL 只是通用的插入,命名不够抽象。扣4分。
    4. 性能与资源利用 5 5分 采用了 <foreach> 批量插入,避免了循环单条操作;利用内存集合过滤数据,性能表现良好。满分。
    5. 提交信息质量 4 5分 提交信息包含了需求编号(2610)和核心目的,但缺少对“为什么保留过期数据”的设计意图说明。扣1分。

    总分:81分

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