fix(sync): align public change stream schema

This commit is contained in:
jiashumeng
2026-09-15 18:49:27 +09:30
parent 4cd95c61ab
commit e3b6edeb7b
9 changed files with 180 additions and 81 deletions
@@ -121,7 +121,7 @@ public class ChangesEngine {
// 4. 组装响应:中间页回传锚点 + next_cursor;最后一页签发 next_sync_token
String roundToken = codec.encodeRound(entityName, fromTs, cutoffTs, snapshotId, scopeVersion);
Map<String, Object> result = new LinkedHashMap<>();
result.put("entity", entityName);
result.put("stream", entityName);
result.put("snapshot_id", snapshotId);
result.put("data_cutoff_at", iso(new Date(cutoffTs * 1000)));
result.put("limit", pageSize);
@@ -6,6 +6,8 @@ import me.iiv.agent.entity.m0.User;
import me.iiv.agent.entity.m2.ContractNew;
import me.iiv.agent.mapper.m0.ProperytMapper;
import me.iiv.agent.mapper.m2.ContractNewMapper;
import me.iiv.agent.mcp.McpErrorCode;
import me.iiv.agent.mcp.ToolCallException;
import me.iiv.agent.permission.PermissionService;
import org.springframework.stereotype.Component;
@@ -77,22 +79,14 @@ public class ContractsSyncEntity extends AbstractSyncEntity implements SyncEntit
private Map<String, Object> toItem(ContractNew row, long mt, Map<Long, String> customerIds) {
boolean deleted = row.getSoftDelete() != null && row.getSoftDelete() == 1;
Date effective = row.getModifyTime() != null ? row.getModifyTime() : row.getCreateTime();
Map<String, Object> item = new LinkedHashMap<>();
item.put("contract_id", row.getId());
item.put("contract_no", row.getContractNo());
item.put("contract_name", row.getContractName());
item.put("status", row.getContractStatus());
item.put("start_at", ChangesEngine.iso(row.getStartDate()));
item.put("end_at", ChangesEngine.iso(row.getEndDate()));
item.put("amount_minor", toMinor(row.getAmount()));
item.put("customer_id", row.getPropertyId() == null ? null : customerIds.get(row.getPropertyId()));
item.put("property_id", row.getPropertyId());
item.put("owner_user_id", row.getSaleId());
item.put("expire_remind", row.getExpireRemind());
item.put("updated_at", ChangesEngine.iso(effective));
item.put("version", mt);
item.put("deleted_at", deleted ? ChangesEngine.iso(effective) : null);
return item;
return SyncChangeSchemas.toMap(new SyncChangeSchemas.ContractChange(
stableId(row.getId()), row.getContractNo(), row.getContractName(),
publicStatus(row.getContractStatus()), ChangesEngine.iso(row.getStartDate()),
ChangesEngine.iso(row.getEndDate()), toMinor(row.getAmount()),
row.getPropertyId() == null ? null : customerIds.get(row.getPropertyId()),
stableId(row.getPropertyId()), stableId(row.getSaleId()),
Integer.valueOf(1).equals(row.getExpireRemind()), row.getRemindDays(),
ChangesEngine.iso(effective), mt, deleted ? ChangesEngine.iso(effective) : null));
}
/** 元 → 分(amount 为 decimal(14,2),精确换算);null 原样返回 null。 */
@@ -100,6 +94,30 @@ public class ContractsSyncEntity extends AbstractSyncEntity implements SyncEntit
return amount == null ? null : amount.movePointRight(2).longValue();
}
/** Legacy database status code to the stable public contract enum. */
static String publicStatus(Integer status) {
if (status == null) {
throw invalidStatus(null);
}
return switch (status) {
case 0 -> "PENDING";
case 1 -> "REJECTED";
case 2 -> "ACTIVE";
case 3 -> "EXPIRED";
case 4 -> "TERMINATED";
default -> throw invalidStatus(status);
};
}
private static ToolCallException invalidStatus(Integer status) {
return new ToolCallException(McpErrorCode.DATA_INTEGRITY_ERROR,
"合同状态无法映射到公开契约: " + status);
}
private static String stableId(Long id) {
return id == null ? null : id.toString();
}
/**
* property_id → customerId 映射。properyt.customerId 存在 NULL 行(2026-09-14 dev 冒烟命中),
* Collectors.toMap 遇 null 值抛 NPE,必须用 put 逐条装填。
@@ -9,7 +9,6 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -59,17 +58,13 @@ public class PropertiesSyncEntity extends AbstractSyncEntity implements SyncEnti
private Map<String, Object> toItem(Properyt row, long mt) {
boolean deleted = row.getSoftDelete() != null && row.getSoftDelete() == 1;
Date effective = row.getModifyTime() != null ? row.getModifyTime() : row.getCreateTime();
Map<String, Object> item = new LinkedHashMap<>();
item.put("property_id", row.getId());
item.put("customer_id", row.getCustomerId());
item.put("property_name", row.getName());
item.put("customer_name", row.getJinideName());
item.put("unified_social_credit_code", row.getIdentificationNumber());
item.put("external_customer_no", row.getSerialnumber());
item.put("status", deleted ? "inactive" : "active");
item.put("updated_at", ChangesEngine.iso(effective));
item.put("version", mt);
item.put("deleted_at", deleted ? ChangesEngine.iso(effective) : null);
return item;
return SyncChangeSchemas.toMap(new SyncChangeSchemas.PropertyChange(
stableId(row.getId()), row.getCustomerId(), row.getName(), row.getJinideName(),
row.getIdentificationNumber(), row.getSerialnumber(), deleted ? "INACTIVE" : "ACTIVE",
ChangesEngine.iso(effective), mt, deleted ? ChangesEngine.iso(effective) : null));
}
private static String stableId(Long id) {
return id == null ? null : id.toString();
}
}
@@ -0,0 +1,73 @@
package me.iiv.agent.mcp.sync;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
/**
* Public schemas for the three change streams.
*
* <p>The source tables are legacy persistence models with numeric identifiers,
* integer flags and database-specific status codes. These DTOs form the stable
* MCP boundary so persistence details cannot leak into consumers.</p>
*/
final class SyncChangeSchemas {
private static final ObjectMapper MAPPER = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() { };
private SyncChangeSchemas() {
}
static Map<String, Object> toMap(Object value) {
return MAPPER.convertValue(value, MAP_TYPE);
}
record UserChange(
@JsonProperty("user_id") String userId,
@JsonProperty("display_name") String displayName,
String status,
@JsonProperty("employee_no") String employeeNo,
@JsonProperty("verified_mobile") String verifiedMobile,
@JsonProperty("organization_ids") List<String> organizationIds,
@JsonProperty("updated_at") String updatedAt,
long version,
@JsonProperty("deleted_at") String deletedAt) {
}
record PropertyChange(
@JsonProperty("property_id") String propertyId,
@JsonProperty("customer_id") String customerId,
@JsonProperty("property_name") String propertyName,
@JsonProperty("customer_name") String customerName,
@JsonProperty("unified_social_credit_code") String unifiedSocialCreditCode,
@JsonProperty("external_customer_no") String externalCustomerNo,
String status,
@JsonProperty("updated_at") String updatedAt,
long version,
@JsonProperty("deleted_at") String deletedAt) {
}
record ContractChange(
@JsonProperty("contract_id") String contractId,
@JsonProperty("contract_no") String contractNo,
@JsonProperty("contract_name") String contractName,
String status,
@JsonProperty("start_at") String startAt,
@JsonProperty("end_at") String endAt,
@JsonProperty("amount_minor") Long amountMinor,
@JsonProperty("customer_id") String customerId,
@JsonProperty("property_id") String propertyId,
@JsonProperty("owner_user_id") String ownerUserId,
@JsonProperty("expire_remind") boolean expireRemind,
@JsonProperty("remind_days") Integer remindDays,
@JsonProperty("updated_at") String updatedAt,
long version,
@JsonProperty("deleted_at") String deletedAt) {
}
}
@@ -7,14 +7,13 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* users 同步实体(m0.user):人员主数据增量。
* 字段口径(2026-09-14 字段核对):status 对齐 soft_delete(在职状态不用 work_status,那是考勤);
* mobile 即唯一手机号字段(verified_mobile 无对应列,按决策砍掉);organization_ids 取 dept_id;
* verified_mobile 映射唯一手机号字段 mobile;organization_ids 取 dept_id;
* 无人员合并机制,merged_into_user_id 省略。users 表无行级过滤先例,SYNC 工具以 scope 白名单为闸。
*/
@Component
@@ -50,16 +49,13 @@ public class UsersSyncEntity extends AbstractSyncEntity implements SyncEntity {
private Map<String, Object> toItem(User row, long mt) {
boolean deleted = row.getSoftDelete() != null && row.getSoftDelete() == 1;
Date effective = row.getModifyTime() != null ? row.getModifyTime() : row.getCreateTime();
Map<String, Object> item = new LinkedHashMap<>();
item.put("user_id", row.getId());
item.put("display_name", row.getName());
item.put("status", deleted ? "inactive" : "active");
item.put("employee_no", row.getNo());
item.put("mobile", row.getMobile());
item.put("organization_ids", row.getDeptId() == null ? List.of() : List.of(row.getDeptId()));
item.put("updated_at", ChangesEngine.iso(effective));
item.put("version", mt);
item.put("deleted_at", deleted ? ChangesEngine.iso(effective) : null);
return item;
return SyncChangeSchemas.toMap(new SyncChangeSchemas.UserChange(
stableId(row.getId()), row.getName(), deleted ? "INACTIVE" : "ACTIVE", row.getNo(),
row.getMobile(), row.getDeptId() == null ? List.of() : List.of(stableId(row.getDeptId())),
ChangesEngine.iso(effective), mt, deleted ? ChangesEngine.iso(effective) : null));
}
private static String stableId(Long id) {
return id == null ? null : id.toString();
}
}
@@ -43,7 +43,7 @@ public class ChangesTools implements ToolObject {
@XqTool(category = "sync", kind = "read")
@Tool(name = "users.changes.list", description = "增量同步人员主数据(m0.user)。" + PROTOCOL_DESC
+ "items 字段:user_id/display_name/status(active=在职 inactive=删除,对齐软删标记)/"
+ "employee_no/mobile/organization_ids(部门 id 列表)/updated_at(带时区)/version/deleted_at。")
+ "employee_no/verified_mobile/organization_ids(部门 id 列表)/updated_at(带时区)/version/deleted_at。")
public Map<String, Object> usersChangesList(
@ToolParam(required = false, description = SYNC_TOKEN_DESC) String syncToken,
@ToolParam(required = false, description = CURSOR_DESC) String cursor,
@@ -65,9 +65,9 @@ public class ChangesTools implements ToolObject {
@XqTool(category = "sync", kind = "read")
@Tool(name = "contracts.changes.list", description = "增量同步合同主数据(m2.contract_new)。" + PROTOCOL_DESC
+ "items 字段:contract_id/contract_no/contract_name/status(0=待审 1=驳回 2=正常 3=过期 4=终止)/"
+ "items 字段:contract_id/contract_no/contract_name/status(PENDING/REJECTED/ACTIVE/EXPIRED/TERMINATED)/"
+ "start_at/end_at(带时区)/amount_minor(金额,分)/customer_id/property_id/"
+ "owner_user_id(负责人稳定 user.id)/expire_remind/updated_at/version/deleted_at。"
+ "owner_user_id(负责人稳定 user.id)/expire_remind/remind_days/updated_at/version/deleted_at。"
+ "行级权限跟随 Token 绑定的人(与 contract.list.query 同一 ACL)。")
public Map<String, Object> contractsChangesList(
@ToolParam(required = false, description = SYNC_TOKEN_DESC) String syncToken,