feat: 添加批量完成LOGGED_IN任务的方法,记录完成原因与可选点数,更新相关实体和映射

This commit is contained in:
yahaozhang
2025-09-16 14:24:49 +08:00
parent 093e72d191
commit cb0ad7f25b
6 changed files with 52 additions and 5 deletions

Binary file not shown.

View File

@@ -33,6 +33,14 @@ public interface LinkTaskMapper extends BaseMapper<LinkTask> {
*/
int updatePointsIfGreater(@Param("id") Long id,
@Param("newPoints") Integer newPoints);
/**
* 将指定设备上所有 LOGGED_IN 的任务标记为 COMPLETED并写入原因与可选的完成点数。
* 返回受影响的行数。
*/
int completeLoggedInTasksByMachine(@Param("machineId") String machineId,
@Param("reason") String reason,
@Param("completedPoints") Integer completedPoints);
List<LinkTask> findByAgentId(@Param("agentId") Long agentId,
@Param("size") int size,

View File

@@ -68,6 +68,9 @@ public class LinkTask {
@TableField("completion_images")
private String completionImages; // JSON格式存储4张图片URL
@TableField("reason")
private String reason; // 状态变化原因
public Long getId() { return id; }
@@ -133,5 +136,8 @@ public class LinkTask {
public String getCompletionImages() { return completionImages; }
public void setCompletionImages(String completionImages) { this.completionImages = completionImages; }
public String getReason() { return reason; }
public void setReason(String reason) { this.reason = reason; }
}

View File

@@ -202,6 +202,8 @@ public class GameCompletionDetectionService {
try {
task.setStatus("COMPLETED");
task.setUpdatedAt(now);
// 正常检测完成:记录原因
task.setReason("状态:已完成");
// 设置完成点数
if (points != null) {

View File

@@ -71,12 +71,28 @@ public class DeviceStatusCheckService {
DeviceStatusInfo statusInfo = parseDeviceStatus(deviceStatus);
log.debug("设备 {} 状态解析结果: {}", machineId, statusInfo);
// 3. 如果设备空闲,通过完成检测服务进行判定(含缓冲与二次确认)
// 3. 如果设备空闲,通过完成检测服务进行判定(含缓冲与二次确认)
// 若未能直接判定完成,则执行兜底:将该设备上 LOGGED_IN 的任务批量置为 COMPLETED并写入原因。
if (statusInfo.isIdle()) {
String source = "TIMER_TASK";
com.gameplatform.server.util.AuditLogger.debug("IdleDetected: device={}, source={}, status={}, points={}", machineId, source, statusInfo.getStatus(), statusInfo.getPoints());
// 调用完成检测服务(包含登录缓冲与二次确认)
completionDetectionService.detectGameCompletion(machineId, statusInfo.getStatus(), source);
boolean done = completionDetectionService.detectGameCompletion(machineId, statusInfo.getStatus(), source);
if (!done) {
// 兜底完成:当设备空闲,直接将 LOGGED_IN 任务置为完成
String completionReason = "状态:已完成(空闲兜底)";
Integer points = statusInfo.getPoints();
try {
int affected = linkTaskMapper.completeLoggedInTasksByMachine(machineId, completionReason, points);
if (affected > 0) {
log.info("空闲兜底:设备 {} 完成了 {} 个任务reason='{}' points={} ", machineId, affected, completionReason, points);
} else {
log.debug("空闲兜底:设备 {} 无需更新(无 LOGGED_IN 任务)", machineId);
}
} catch (Exception e) {
log.warn("空闲兜底完成失败 device={} err={}", machineId, e.getMessage());
}
}
} else {
log.debug("设备 {} 状态为 [{}],非空闲状态,跳过检查", machineId, statusInfo.getStatus());
}

View File

@@ -23,24 +23,25 @@
<result property="firstRegionSelectAt" column="first_region_select_at" />
<result property="completedPoints" column="completed_points" />
<result property="completionImages" column="completion_images" />
<result property="reason" column="reason" />
</resultMap>
<select id="findById" parameterType="long" resultMap="LinkTaskMap">
SELECT id, batch_id, agent_id, code_no, token_hash, expire_at, status, region, machine_id, login_at, refund_at, revoked_at, created_at, updated_at, need_refresh, refresh_time, qr_created_at, qr_expire_at, first_region_select_at, completed_points, completion_images
SELECT id, batch_id, agent_id, code_no, token_hash, expire_at, status, region, machine_id, login_at, refund_at, revoked_at, created_at, updated_at, need_refresh, refresh_time, qr_created_at, qr_expire_at, first_region_select_at, completed_points, completion_images, reason
FROM link_task
WHERE id = #{id}
LIMIT 1
</select>
<select id="findByCodeNo" parameterType="string" resultMap="LinkTaskMap">
SELECT id, batch_id, agent_id, code_no, token_hash, expire_at, status, region, machine_id, login_at, refund_at, revoked_at, created_at, updated_at, need_refresh, refresh_time, qr_created_at, qr_expire_at, first_region_select_at, completed_points, completion_images
SELECT id, batch_id, agent_id, code_no, token_hash, expire_at, status, region, machine_id, login_at, refund_at, revoked_at, created_at, updated_at, need_refresh, refresh_time, qr_created_at, qr_expire_at, first_region_select_at, completed_points, completion_images, reason
FROM link_task
WHERE code_no = #{codeNo}
LIMIT 1
</select>
<select id="findByTokenHash" parameterType="string" resultMap="LinkTaskMap">
SELECT id, batch_id, agent_id, code_no, token_hash, expire_at, status, region, machine_id, login_at, refund_at, revoked_at, created_at, updated_at, need_refresh, refresh_time, qr_created_at, qr_expire_at, first_region_select_at, completed_points, completion_images
SELECT id, batch_id, agent_id, code_no, token_hash, expire_at, status, region, machine_id, login_at, refund_at, revoked_at, created_at, updated_at, need_refresh, refresh_time, qr_created_at, qr_expire_at, first_region_select_at, completed_points, completion_images, reason
FROM link_task
WHERE token_hash = #{tokenHash}
LIMIT 1
@@ -67,6 +68,7 @@
<if test="firstRegionSelectAt != null">first_region_select_at = #{firstRegionSelectAt},</if>
<if test="completedPoints != null">completed_points = #{completedPoints},</if>
<if test="completionImages != null">completion_images = #{completionImages},</if>
<if test="reason != null">reason = #{reason},</if>
updated_at = NOW()
</set>
WHERE id = #{id}
@@ -90,6 +92,19 @@
AND (completed_points IS NULL OR completed_points &lt; #{newPoints})
</update>
<!-- 将指定设备的 LOGGED_IN 任务批量置为 COMPLETED写入原因与可选点数 -->
<update id="completeLoggedInTasksByMachine">
UPDATE link_task
<set>
status = 'COMPLETED',
reason = #{reason},
<if test="completedPoints != null">completed_points = #{completedPoints},</if>
updated_at = NOW()
</set>
WHERE machine_id = #{machineId}
AND status = 'LOGGED_IN'
</update>
<select id="findByAgentId" resultMap="LinkTaskMap">
SELECT id, batch_id, agent_id, code_no, token_hash, expire_at, status, region, machine_id, login_at, refund_at, revoked_at, created_at, updated_at, need_refresh, refresh_time, qr_created_at, qr_expire_at, first_region_select_at, completed_points, completion_images
FROM link_task