代码
public class HL7Base<T>
{
public static string SplitChar = string.Empty;
public List<T> Childs { get; set; } = new List<T>();
}
public class HL7Message : HL7Base<Segment>
{
public static new string SplitChar = "\r\n";
public static HL7Message Decode(string msg)
{
var tlist = msg.Split("\r");
if (tlist.Count() <= 1)
{
tlist = msg.Split("\n");
}
return new HL7Message
{
Childs = tlist.Select(x => x.Trim('\n').Trim('\r'))
.Select(x => Segment.Decode(x))
.ToList()
};
}
public override string ToString() => string.Join(SplitChar, Childs);
public string GetValue(string header, int index, int? index1 = null, int? index2 = null)
{
var seg = Childs.FirstOrDefault(x => x.GetValue(0) == header);
if (seg != null && seg.Childs.Count >= index)
{
var val = seg.Childs[index];
if (index1.HasValue) return val.GetValue(index1.Value, index2);
return seg.Childs[index].ToString();
}
return string.Empty;
}
}
public class Segment : HL7Base<Field>
{
public static new string SplitChar = "|";
public static Segment Decode(string value) => new Segment
{
Childs = value.Split(SplitChar)
.Select(x => Field.Decode(x)).ToList()
};
public override string ToString() => string.Join(SplitChar, Childs);
public string GetValue(int index, int? index1 = null, int? index2 = null)
{
if (Childs != null && Childs.Count >= index)
{
var val = Childs[index];
if (index1.HasValue) return val.GetValue(index1.Value, index2);
return Childs[index].ToString();
}
return string.Empty;
}
}
public class Field : HL7Base<Component>
{
public static new string SplitChar = "^";
public static Field Decode(string value) => new Field
{
Childs = value.Split(SplitChar)
.Select(x => Component.Decode(x)).ToList()
};
public override string ToString() => string.Join(SplitChar, Childs);
public string GetValue(int index, int? index1 = null)
{
if (Childs != null && Childs.Count >= index)
{
var val = Childs[index];
if (index1.HasValue) return val.GetValue(index1.Value);
return Childs[index].ToString();
}
return string.Empty;
}
}
public class Component : HL7Base<string>
{
public static new string SplitChar = "&";
public static Component Decode(string value) => new Component { Childs = value.Split(SplitChar).ToList() };
public override string ToString() => string.Join(SplitChar, Childs);
public string GetValue(int index)
{
if (Childs != null && Childs.Count >= index) return Childs[index].ToString();
return string.Empty;
}
}
调用示例:
var msg = @"MSH|^~\&|MediII|MediInfo|CCTV|YN|20230919094709||ADT^A10^ADT_A09|64288b2a9b244c06a575d6fa9978d495|P|2.4
EVN|A10|20230919094709||||20230919094709
PID||2296254^0|2296254|0|YuanBaiLong^张三||19640805000000|1|||江苏省镇江市句容市江苏省句容市华阳镇茅山路1900号&江苏省句容市华阳镇茅山路1900号^321183^321100^320000^212400^^H~^^^^-^^W^灵活就业人员参保~江苏省/镇江市/句容市&江苏省句容市华阳镇茅山路1900号^321183^321100^320000^^^R||^^02^^^^13862440818~^^02^^^^13862440818|^^^^^^13862440818|||||321123196406053120||||江苏省/镇江市/句容市|||^其他||^中国||0
PV1|4|I|25012^^18^25042B&(肺外科2)209西&0^^209西病区|R|||2780^^陈达^^^^^^^^^^^211&209西(陈奇勋)||||||||||1826^^徐丽伟|98|10702242||QG1101||||20230919|||||||||||||||||25042B^^^1||20230919094646||||||4|V|2780^^陈达~
OBX|0|NM|25^年龄||59|岁|||||F|||20230914090310
DG1|1|C16.000||贲门恶性肿瘤||A";
var pv1String = "PV1";
var pidString = "PID";
var msgHL7 = HL7Message.Decode(msg);
DateTime? din = null;
if (!string.IsNullOrWhiteSpace(msgHL7.GetValue(pv1String, 25)))
{
DateTime.TryParseExact(msgHL7.GetValue(pv1String, 25), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out DateTime dateIn);
din = dateIn;
}
var patient = new CreatePatientCommand(
msgHL7.GetValue(pv1String, 19),
msgHL7.GetValue(pidString, 5, 1),
msgHL7.GetValue(pidString, 8) == "1" ? "男" : "女",
msgHL7.GetValue(pidString, 11, 0, 0),
msgHL7.GetValue(pidString, 19),
$"{msgHL7.GetValue(pv1String, 3, 0)}{msgHL7.GetValue(pv1String, 3, 2)}",
msgHL7.GetValue(pv1String, 3, 0),
din,
msgHL7.GetValue(pidString, 5, 0));