Commit 97f454ab by 章家宝

feat:专项任务添加第三方id

1 parent 2be60ffd
...@@ -23,6 +23,9 @@ public class SpecialTaskPageDTO implements Serializable { ...@@ -23,6 +23,9 @@ public class SpecialTaskPageDTO implements Serializable {
@ApiModelProperty(notes = "主键ID") @ApiModelProperty(notes = "主键ID")
private Long id; private Long id;
@ApiModelProperty(notes = "第三方ID(策略中心返回的策略ID)")
private Long thirdPartyId;
@ApiModelProperty(notes = "任务类型编码") @ApiModelProperty(notes = "任务类型编码")
private String bizType; private String bizType;
......
...@@ -105,6 +105,12 @@ public class SupervisorSpecialTask implements Serializable { ...@@ -105,6 +105,12 @@ public class SupervisorSpecialTask implements Serializable {
private String supervisorUuid; private String supervisorUuid;
/** /**
* 第三方ID(策略中心返回的策略ID)
*/
@Column(name = "third_party_id")
private Long thirdPartyId;
/**
* 创建人 * 创建人
*/ */
@Column(name = "created_by") @Column(name = "created_by")
......
...@@ -101,9 +101,17 @@ public class SupervisorSpecialTaskService { ...@@ -101,9 +101,17 @@ public class SupervisorSpecialTaskService {
} }
// 调用策略中心创建协同任务策略 // 调用策略中心创建协同任务策略
createCollaborativeStrategy(request, task.getId()); Long thirdPartyId = createCollaborativeStrategy(request, task.getId());
log.info("发起专项任务成功,taskId={}", task.getId()); // 保存第三方ID
if (thirdPartyId != null) {
SupervisorSpecialTask updateTask = new SupervisorSpecialTask();
updateTask.setId(task.getId());
updateTask.setThirdPartyId(thirdPartyId);
supervisorSpecialTaskMapper.updateByPrimaryKeySelective(updateTask);
}
log.info("发起专项任务成功,taskId={}, thirdPartyId={}", task.getId(), thirdPartyId);
return task.getId(); return task.getId();
} }
...@@ -142,8 +150,17 @@ public class SupervisorSpecialTaskService { ...@@ -142,8 +150,17 @@ public class SupervisorSpecialTaskService {
return; return;
} }
// 过滤出有第三方ID的任务
List<SpecialTaskPageDTO> tasksWithThirdPartyId = taskList.stream()
.filter(dto -> dto.getThirdPartyId() != null)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(tasksWithThirdPartyId)) {
return;
}
// 按 bizType 分组 // 按 bizType 分组
Map<String, List<SpecialTaskPageDTO>> bizTypeGroupMap = taskList.stream() Map<String, List<SpecialTaskPageDTO>> bizTypeGroupMap = tasksWithThirdPartyId.stream()
.filter(dto -> dto.getBizType() != null) .filter(dto -> dto.getBizType() != null)
.collect(Collectors.groupingBy(SpecialTaskPageDTO::getBizType)); .collect(Collectors.groupingBy(SpecialTaskPageDTO::getBizType));
...@@ -156,9 +173,9 @@ public class SupervisorSpecialTaskService { ...@@ -156,9 +173,9 @@ public class SupervisorSpecialTaskService {
String bizType = entry.getKey(); String bizType = entry.getKey();
List<SpecialTaskPageDTO> groupTasks = entry.getValue(); List<SpecialTaskPageDTO> groupTasks = entry.getValue();
// 收集该 bizType 下的所有任务ID // 收集该 bizType 下的所有第三方ID
List<String> bizInstanceIds = groupTasks.stream() List<String> bizInstanceIds = groupTasks.stream()
.map(dto -> String.valueOf(dto.getId())) .map(dto -> String.valueOf(dto.getThirdPartyId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (CollectionUtils.isEmpty(bizInstanceIds)) { if (CollectionUtils.isEmpty(bizInstanceIds)) {
...@@ -194,9 +211,9 @@ public class SupervisorSpecialTaskService { ...@@ -194,9 +211,9 @@ public class SupervisorSpecialTaskService {
.filter(task -> task.getBizInstanceId() != null) .filter(task -> task.getBizInstanceId() != null)
.collect(Collectors.toMap(TaskDTO::getBizInstanceId, task -> task, (a, b) -> a)); .collect(Collectors.toMap(TaskDTO::getBizInstanceId, task -> task, (a, b) -> a));
// 填充状态到任务列表 // 填充状态到任务列表(使用第三方ID匹配)
for (SpecialTaskPageDTO dto : groupTasks) { for (SpecialTaskPageDTO dto : groupTasks) {
String bizInstanceId = String.valueOf(dto.getId()); String bizInstanceId = String.valueOf(dto.getThirdPartyId());
if (taskMap.containsKey(bizInstanceId)) { if (taskMap.containsKey(bizInstanceId)) {
TaskDTO taskDTO = taskMap.get(bizInstanceId); TaskDTO taskDTO = taskMap.get(bizInstanceId);
// 状态转换:任务中心 status=FINISHED -> taskStatus=1(已完成),其他 -> taskStatus=0(未完成) // 状态转换:任务中心 status=FINISHED -> taskStatus=1(已完成),其他 -> taskStatus=0(未完成)
...@@ -312,14 +329,15 @@ public class SupervisorSpecialTaskService { ...@@ -312,14 +329,15 @@ public class SupervisorSpecialTaskService {
* 创建协同任务策略 * 创建协同任务策略
* @param request 专项任务请求 * @param request 专项任务请求
* @param taskId 专项任务ID * @param taskId 专项任务ID
* @return 第三方ID(策略中心返回的策略ID)
*/ */
public void createCollaborativeStrategy(SubmitSpecialTaskDTO request, Long taskId) { public Long createCollaborativeStrategy(SubmitSpecialTaskDTO request, Long taskId) {
try { try {
// 1. 映射策略类型 // 1. 映射策略类型
Integer strategyType = mapBizTypeToStrategyType(request.getBizType()); Integer strategyType = mapBizTypeToStrategyType(request.getBizType());
if (strategyType == null) { if (strategyType == null) {
log.warn("无法映射策略类型,bizType={}", request.getBizType()); log.warn("无法映射策略类型,bizType={}", request.getBizType());
return; return null;
} }
// 3. 构建策略信息 // 3. 构建策略信息
...@@ -372,11 +390,14 @@ public class SupervisorSpecialTaskService { ...@@ -372,11 +390,14 @@ public class SupervisorSpecialTaskService {
RpcResponse<Integer> response = strategyInfoRemote.create(strategyInfo); RpcResponse<Integer> response = strategyInfoRemote.create(strategyInfo);
if (response == null || !response.isSuccess()) { if (response == null || !response.isSuccess()) {
log.error("创建协同任务策略失败,taskId={}, response={}", taskId, response); log.error("创建协同任务策略失败,taskId={}, response={}", taskId, response);
return null;
} else { } else {
log.info("创建协同任务策略成功,taskId={}", taskId); log.info("创建协同任务策略成功,taskId={}, thirdPartyId={}", taskId, response.getResult());
return response.getResult() != null ? response.getResult().longValue() : null;
} }
} catch (Exception e) { } catch (Exception e) {
log.error("创建协同任务策略异常,taskId={}", taskId, e); log.error("创建协同任务策略异常,taskId={}", taskId, e);
return null;
} }
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
<result column="supervisor_phone" property="supervisorPhone" jdbcType="VARCHAR"/> <result column="supervisor_phone" property="supervisorPhone" jdbcType="VARCHAR"/>
<result column="supervisor_name" property="supervisorName" jdbcType="VARCHAR"/> <result column="supervisor_name" property="supervisorName" jdbcType="VARCHAR"/>
<result column="supervisor_uuid" property="supervisorUuid" jdbcType="VARCHAR"/> <result column="supervisor_uuid" property="supervisorUuid" jdbcType="VARCHAR"/>
<result column="third_party_id" property="thirdPartyId" jdbcType="BIGINT"/>
<result column="created_by" property="createdBy" jdbcType="VARCHAR"/> <result column="created_by" property="createdBy" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/> <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="updated_by" property="updatedBy" jdbcType="VARCHAR"/> <result column="updated_by" property="updatedBy" jdbcType="VARCHAR"/>
...@@ -26,6 +27,7 @@ ...@@ -26,6 +27,7 @@
<resultMap id="SpecialTaskPageDTOResultMap" type="cn.com.glsx.dj.supervisor.dto.SpecialTaskPageDTO"> <resultMap id="SpecialTaskPageDTOResultMap" type="cn.com.glsx.dj.supervisor.dto.SpecialTaskPageDTO">
<id column="id" property="id" jdbcType="BIGINT"/> <id column="id" property="id" jdbcType="BIGINT"/>
<result column="third_party_id" property="thirdPartyId" jdbcType="BIGINT"/>
<result column="biz_type" property="bizType" jdbcType="VARCHAR"/> <result column="biz_type" property="bizType" jdbcType="VARCHAR"/>
<result column="category_name" property="categoryName" jdbcType="VARCHAR"/> <result column="category_name" property="categoryName" jdbcType="VARCHAR"/>
<result column="merchant_id" property="merchantId" jdbcType="INTEGER"/> <result column="merchant_id" property="merchantId" jdbcType="INTEGER"/>
...@@ -43,7 +45,7 @@ ...@@ -43,7 +45,7 @@
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id, biz_type, category_name, merchant_id, merchant_name, business_type, description, id, biz_type, category_name, merchant_id, merchant_name, business_type, description,
problem_staff_phones, problem_staff_name, region_operator_phone, region_operator_name, problem_staff_phones, problem_staff_name, region_operator_phone, region_operator_name,
supervisor_phone, supervisor_name, supervisor_uuid, supervisor_phone, supervisor_name, supervisor_uuid, third_party_id,
created_by, create_time, updated_by, update_time, deleted_flag created_by, create_time, updated_by, update_time, deleted_flag
</sql> </sql>
...@@ -63,6 +65,7 @@ ...@@ -63,6 +65,7 @@
<if test="supervisorPhone != null">supervisor_phone,</if> <if test="supervisorPhone != null">supervisor_phone,</if>
<if test="supervisorName != null">supervisor_name,</if> <if test="supervisorName != null">supervisor_name,</if>
<if test="supervisorUuid != null">supervisor_uuid,</if> <if test="supervisorUuid != null">supervisor_uuid,</if>
<if test="thirdPartyId != null">third_party_id,</if>
<if test="createdBy != null">created_by,</if> <if test="createdBy != null">created_by,</if>
<if test="createTime != null">create_time,</if> <if test="createTime != null">create_time,</if>
<if test="updatedBy != null">updated_by,</if> <if test="updatedBy != null">updated_by,</if>
...@@ -83,6 +86,7 @@ ...@@ -83,6 +86,7 @@
<if test="supervisorPhone != null">#{supervisorPhone},</if> <if test="supervisorPhone != null">#{supervisorPhone},</if>
<if test="supervisorName != null">#{supervisorName},</if> <if test="supervisorName != null">#{supervisorName},</if>
<if test="supervisorUuid != null">#{supervisorUuid},</if> <if test="supervisorUuid != null">#{supervisorUuid},</if>
<if test="thirdPartyId != null">#{thirdPartyId},</if>
<if test="createdBy != null">#{createdBy},</if> <if test="createdBy != null">#{createdBy},</if>
<if test="createTime != null">#{createTime},</if> <if test="createTime != null">#{createTime},</if>
<if test="updatedBy != null">#{updatedBy},</if> <if test="updatedBy != null">#{updatedBy},</if>
...@@ -107,6 +111,7 @@ ...@@ -107,6 +111,7 @@
<if test="supervisorPhone != null">supervisor_phone = #{supervisorPhone},</if> <if test="supervisorPhone != null">supervisor_phone = #{supervisorPhone},</if>
<if test="supervisorName != null">supervisor_name = #{supervisorName},</if> <if test="supervisorName != null">supervisor_name = #{supervisorName},</if>
<if test="supervisorUuid != null">supervisor_uuid = #{supervisorUuid},</if> <if test="supervisorUuid != null">supervisor_uuid = #{supervisorUuid},</if>
<if test="thirdPartyId != null">third_party_id = #{thirdPartyId},</if>
<if test="createdBy != null">created_by = #{createdBy},</if> <if test="createdBy != null">created_by = #{createdBy},</if>
<if test="createTime != null">create_time = #{createTime},</if> <if test="createTime != null">create_time = #{createTime},</if>
<if test="updatedBy != null">updated_by = #{updatedBy},</if> <if test="updatedBy != null">updated_by = #{updatedBy},</if>
...@@ -147,7 +152,7 @@ ...@@ -147,7 +152,7 @@
</select> </select>
<select id="selectSpecialTaskPage" resultMap="SpecialTaskPageDTOResultMap" parameterType="cn.com.glsx.dj.supervisor.dto.SpecialTaskQueryDTO"> <select id="selectSpecialTaskPage" resultMap="SpecialTaskPageDTOResultMap" parameterType="cn.com.glsx.dj.supervisor.dto.SpecialTaskQueryDTO">
SELECT id, biz_type, category_name, merchant_id, merchant_name, business_type, description, SELECT id, third_party_id, biz_type, category_name, merchant_id, merchant_name, business_type, description,
problem_staff_name, region_operator_name, problem_staff_name, region_operator_name,
supervisor_phone, supervisor_name, create_time, created_by supervisor_phone, supervisor_name, create_time, created_by
FROM supervisor_special_task FROM supervisor_special_task
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 数据一致性风险与异常吞没(功能正确性与健壮性)

    • 问题描述:在 createCollaborativeStrategy 方法中,如果调用策略中心 RPC 接口失败或抛出异常,代码仅记录了日志并返回 null。而在 submitSpecialTask 方法中,如果返回 null,则直接跳过更新 thirdPartyId 并返回成功。
    • 影响分析:这会导致“本地任务创建成功,但远程策略未创建”的半成功状态(脏数据)。后续在 fillTaskStatus 方法中,由于过滤掉了 thirdPartyId == null 的任务,这些异常任务将永远无法获取状态,导致业务流程断裂,用户界面上该任务状态可能永远停滞。
    • 优化建议:根据业务要求决定强一致性还是最终一致性。如果要求强一致,RPC 调用失败时应抛出自定义业务异常,触发本地数据库事务回滚;如果允许最终一致性,应引入重试机制(如定时任务补偿或消息队列)。 java // 强一致性示例: RpcResponse<Integer> response = strategyInfoRemote.create(strategyInfo); if (response == null || !response.isSuccess()) { log.error("创建协同任务策略失败,taskId={}, response={}", taskId, response); // 抛出运行时异常,触发外层事务回滚 throw new BusinessException("调用策略中心创建策略失败"); }

    2. 数据库事务中执行 RPC 调用的隐患(性能与最佳实践)

    • 问题描述:submitSpecialTask 方法大概率是一个包含数据库写入操作的事务方法(通常带有 @Transactional 注解)。在该方法内部,直接同步调用了 createCollaborativeStrategy (RPC 网络请求)。
    • 影响分析:在数据库事务中进行网络 I/O 操作是严重的架构反模式。RPC 调用的耗时不可控,如果第三方服务响应缓慢或超时,会导致当前线程长时间占用数据库连接。在高并发场景下,极易引发数据库连接池耗尽,进而拖垮整个服务。
    • 优化建议:将 RPC 调用与数据库核心事务解耦。可以在本地任务事务提交成功后,再发起 RPC 调用并更新 thirdPartyId(可使用 Spring 的 TransactionSynchronizationManager.registerSynchronization 在事务 afterCommit 阶段执行,或拆分方法)。

    3. Commit 提交信息过于简略(提交信息质量)

    • 问题描述:提交信息为 feat:专项任务添加第三方id (by jiabao),仅说明了做了什么,缺乏业务背景和技术细节。
    • 影响分析:不利于后续的代码维护、Code Review 和问题追溯。其他开发者无法从 commit 信息中得知为什么需要这个 ID,以及它与策略中心的关联关系。
    • 优化建议:建议遵循 Angular 规范,补充 Body 描述。例如: ```text feat(supervisor): 专项任务集成策略中心第三方ID
    1. 专项任务实体及DTO新增 thirdPartyId 字段。
    2. 提交任务时同步调用策略中心,并回写 thirdPartyId。
    3. 任务列表查询时,依赖 thirdPartyId 从策略中心拉取最新任务状态。 ```

    二、评分明细

    • 功能实现的正确性与健壮性(30/40分): 扣分原因:RPC 调用失败时直接吞没异常并返回 null,导致本地数据与远程数据不一致,且产生了无法闭环的“孤儿”任务数据。未违反 MyBatis 子查询和 Dubbo 接口变更的红线规则。
    • 安全性与潜在风险(30/30分): 未发现 SQL 注入风险(MyBatis 均正确使用 #{}),无明显的越权或敏感信息泄露问题。
    • 最佳实践与可维护性(15/20分): 扣分原因:在可能存在的事务方法中同步发起 RPC 远程调用,违反了“事务应尽可能短小”的最佳实践。代码整体结构和命名尚可,Java 8 Stream API 使用规范。
    • 性能与资源利用(3/5分): 扣分原因:同上,事务中嵌套网络请求在高并发下存在严重的数据库连接资源浪费和连接池耗尽风险。
    • 提交信息质量(2/5分): 扣分原因:Commit 信息过于单薄,缺乏必要的业务上下文和改动影响说明,不利于工程化协同。

    总分:80分

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