feat(cache): 建立字典缓存快照(T-103)
This commit is contained in:
@@ -10,8 +10,11 @@ type PHISHealthRecord struct {
|
||||
Birthday string
|
||||
MobileNumber string
|
||||
RegionCode string
|
||||
RegionName string
|
||||
ManaDoctorID string
|
||||
ManaDoctorName string
|
||||
ManaUnitID string
|
||||
ManaUnitName string
|
||||
NationCode string
|
||||
EducationCode string
|
||||
WorkCode string
|
||||
@@ -51,8 +54,15 @@ type DictSnapshot struct {
|
||||
Insurance Dict
|
||||
}
|
||||
|
||||
type MasterDataLookup interface {
|
||||
RegionCodeByName(name string) (string, bool)
|
||||
DoctorIDByName(name string) (string, bool)
|
||||
ManaUnitIDByName(name string) (string, bool)
|
||||
}
|
||||
|
||||
type MapContext struct {
|
||||
Dicts DictSnapshot
|
||||
Dicts DictSnapshot
|
||||
MasterData MasterDataLookup
|
||||
}
|
||||
|
||||
func DefaultDictSnapshot() DictSnapshot {
|
||||
@@ -72,6 +82,7 @@ func MapHealthRecord(src PHISHealthRecord, ctx MapContext) (MappedHealthRecord,
|
||||
if ctx.Dicts.Sex.Name == "" {
|
||||
ctx.Dicts = DefaultDictSnapshot()
|
||||
}
|
||||
resolveMasterData(&src, ctx.MasterData)
|
||||
var errs []ValidationError
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
@@ -112,6 +123,26 @@ func MapHealthRecord(src PHISHealthRecord, ctx MapContext) (MappedHealthRecord,
|
||||
return mapped, errs
|
||||
}
|
||||
|
||||
func resolveMasterData(src *PHISHealthRecord, lookup MasterDataLookup) {
|
||||
if lookup == nil {
|
||||
return
|
||||
}
|
||||
if src.RegionCode == "" && src.RegionName != "" {
|
||||
if code, ok := lookup.RegionCodeByName(src.RegionName); ok {
|
||||
src.RegionCode = code
|
||||
}
|
||||
}
|
||||
if src.ManaDoctorID == "" && src.ManaDoctorName != "" {
|
||||
if code, ok := lookup.DoctorIDByName(src.ManaDoctorName); ok {
|
||||
src.ManaDoctorID = code
|
||||
}
|
||||
}
|
||||
if src.ManaUnitID == "" && src.ManaUnitName != "" {
|
||||
if code, ok := lookup.ManaUnitIDByName(src.ManaUnitName); ok {
|
||||
src.ManaUnitID = code
|
||||
}
|
||||
}
|
||||
}
|
||||
func mapCode(dict Dict, code string, errs *[]ValidationError) string {
|
||||
if code == "" {
|
||||
return ""
|
||||
|
||||
@@ -68,6 +68,26 @@ func TestMapHealthRecordUsesInjectedDictSnapshot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapHealthRecordUsesMasterDataLookupWhenCodesMissing(t *testing.T) {
|
||||
src := validPHISHealthRecord()
|
||||
src.RegionCode = ""
|
||||
src.ManaDoctorID = ""
|
||||
src.ManaUnitID = ""
|
||||
src.RegionName = "测试网格"
|
||||
src.ManaDoctorName = "测试医生"
|
||||
src.ManaUnitName = "测试机构"
|
||||
|
||||
got, errs := MapHealthRecord(src, MapContext{
|
||||
Dicts: DefaultDictSnapshot(),
|
||||
MasterData: fakeMasterDataLookup{},
|
||||
})
|
||||
if len(errs) != 0 {
|
||||
t.Fatalf("errors = %#v", errs)
|
||||
}
|
||||
if got.RegionCode != "441625000000" || got.ManaDoctorID != "doc-001" || got.ManaUnitID != "123456789" {
|
||||
t.Fatalf("master data not resolved: %#v", got)
|
||||
}
|
||||
}
|
||||
func TestGenerateCheckIDIsDeterministicAndVersionSensitive(t *testing.T) {
|
||||
first := GenerateCheckID("PHIS", "JKDA", "record-001", "20260707010101")
|
||||
second := GenerateCheckID("PHIS", "JKDA", "record-001", "20260707010101")
|
||||
@@ -83,6 +103,19 @@ func TestGenerateCheckIDIsDeterministicAndVersionSensitive(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type fakeMasterDataLookup struct{}
|
||||
|
||||
func (fakeMasterDataLookup) RegionCodeByName(name string) (string, bool) {
|
||||
return map[string]string{"测试网格": "441625000000"}[name], name == "测试网格"
|
||||
}
|
||||
|
||||
func (fakeMasterDataLookup) DoctorIDByName(name string) (string, bool) {
|
||||
return map[string]string{"测试医生": "doc-001"}[name], name == "测试医生"
|
||||
}
|
||||
|
||||
func (fakeMasterDataLookup) ManaUnitIDByName(name string) (string, bool) {
|
||||
return map[string]string{"测试机构": "123456789"}[name], name == "测试机构"
|
||||
}
|
||||
func validPHISHealthRecord() PHISHealthRecord {
|
||||
return PHISHealthRecord{
|
||||
SourceSystem: "PHIS",
|
||||
|
||||
Reference in New Issue
Block a user