package schema import ( "entgo.io/ent" "entgo.io/ent/dialect/entsql" "entgo.io/ent/schema" "entgo.io/ent/schema/field" "entgo.io/ent/schema/index" ) // SyncFailure 跨系统调用失败台账:只记录「本地事务已提交、对端通知失败」的回调, // 供人工在「同步失败台账」页确认对端状态后重试;系统不自动重试(2026-11-05 拍板)。 // // 本表由 internal/syncfailure 用原生 SQL 读写,故列名必须与 // sql/pending/2026-11-09_sync_failure.sql 严格一致(全部 snake_case)。 // 加进 ent schema 的目的仅是新装库 / reset-all 时由 Schema.Create 自动建表, // 不再依赖部署时手工执行迁移 SQL;运行库既有表的读写方式不变。 type SyncFailure struct { ent.Schema } func (SyncFailure) Annotations() []schema.Annotation { return []schema.Annotation{ entsql.Annotation{Table: "sync_failure"}, entsql.WithComments(true), schema.Comment("SyncFailure 跨系统调用失败台账:本地已提交、对端通知失败,人工确认后重试"), } } func (SyncFailure) Fields() []ent.Field { return []ent.Field{ field.Int("id").Positive(), field.Text("direction").Default("").Comment("调用方向,如 mes>wms / wms>mes"), field.Text("biz_type").Default("").Comment("业务类型,如 material.sync"), field.Text("biz_key").Default("").Comment("业务主键"), field.Text("endpoint").Default("").Comment("重试时原样重放的对端地址"), field.Text("payload_json").Default("{}").Comment("通知载荷(JSON)"), field.Text("error_msg").Default("").Comment("失败原因"), // 非空且无默认值,与运行库结构一致;Record 的 ON CONFLICT 依赖其唯一索引。 field.Text("idempotency_key").Comment("幂等键 direction|biz_type|biz_key"), field.Text("status").Default("PENDING").Comment("PENDING/SUCCESS/DEAD"), field.Int32("retry_count").Default(0).Comment("重试次数"), field.Int64("created_at").Default(0).Comment("首次失败时刻(unix秒)"), field.Int64("last_error_at").Default(0).Comment("最近失败时刻(unix秒)"), field.Text("handled_by").Default("").Comment("人工置 DEAD 的操作人"), } } func (SyncFailure) Indexes() []ent.Index { return []ent.Index{ index.Fields("idempotency_key").Unique(), index.Fields("status"), } }