Node.js 数据格式同时支持JSON和HL7

Node.js 数据格式同时支持JSON和HL7

一个正常的HL7格式的数据大概长这样:

MSH|^~\&|HIS|SFPH|NIS|SFPH|20140626114850.755|N|ZK~S^Z01^ZKS_Z01|NISGetHisDept-20140626114850755|P|2.7
Z01|0|部门ID|部门名称|部门性质|实有床位|开放床位数|预缴金下限,第一次预缴金的下限|催款下限,病区提示催款的下限|记账下限,病区病人实际余额的下限|允许欠费上限|对应门诊科室|对应住院科室|病区急诊床位数|科室共享床位数|拼音首码|五笔首码|操作人|操作时间

经过一番阅读HL7的标准,成功地总结出HL7就是一堆段头领着一帮段信息,一级隔断用|,二级隔断用^,三级隔断用~,别的就随意了。具体内部消息格式自己定义。于是,在当下阶段,先把HL7与JSON格式之间的互相转换做出来就好,以后遇到正式的业务逻辑再做相应更改。

let hl7Body = {};
let hl72Json = (hl7Str) => {
    let payLoadArray = hl7Str.split('\r\n');
    for (let countX = 0; countX < payLoadArray.length; countX++) {
        hl7Body[payLoadArray[countX].substring(0, 3)] = payLoadArray[countX].substring(4).split('|');
        for (let countY = 0; countY < hl7Body[payLoadArray[countX].substring(0, 3)].length; countY++) {
            // console.log(hl7Body[payLoadArray[countX].substring(0, 3)][countY] + ':' + hl7Body[payLoadArray[countX].substring(0, 3)][countY].indexOf('^'));
            if (hl7Body[payLoadArray[countX].substring(0, 3)][countY].indexOf('^') !== -1 && ((payLoadArray[countX].substring(0, 3) !== 'MSH') || (countY !== 0))) {
                hl7Body[payLoadArray[countX].substring(0, 3)][countY] = hl7Body[payLoadArray[countX].substring(0, 3)][countY].split('^');
                if (Array.isArray(hl7Body[payLoadArray[countX].substring(0, 3)][countY])) {
                    for (let countZ = 0; countZ < hl7Body[payLoadArray[countX].substring(0, 3)][countY].length; countZ++) {
                        if (hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ].indexOf('~') !== -1) {
                            hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ] = hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ].split('~');
                        }
                    }
                }
            }

        }
    }
    setTimeout(() => {
        console.log(hl7Body);
        // console.log(hl7Body.MSH);
        json2Hl7(hl7Body);
    }, 500);
}
let json2Hl7 = (jsonObj) => {
    let hl7Str = '';
    for (let x in jsonObj) {
        hl7Str = hl7Str + '\r\n' + x + '|' + getResult(jsonObj[x], 1);
    }
    setTimeout(() => {
        // console.log(hl7Str.substring(2));
    }, 500)
}

let getResult = (inputParam, level) => {
    let tmpStr = '';
    switch (level) {
        case 1:
            if (Array.isArray(inputParam)) {
                let tmpCount = 0;
                for (let tmpCell in inputParam) {
                    if (++tmpCount < inputParam.length) {
                        tmpStr = tmpStr + getResult(inputParam[tmpCell], 2) + '|';
                    } else {
                        return tmpStr + getResult(inputParam[tmpCell], 2);
                    }
                }
            } else {
                return inputParam;
            }
            break;
        case 2:
            if (Array.isArray(inputParam)) {
                let tmpCount = 0;
                for (let tmpCell in inputParam) {
                    if (++tmpCount < inputParam.length) {
                        tmpStr = tmpStr + getResult(inputParam[tmpCell], 3) + '^';
                    } else {
                        return tmpStr + getResult(inputParam[tmpCell], 3);
                    }
                }
            } else {
                return inputParam;
            }
            break;
        case 3:
            if (Array.isArray(inputParam)) {
                let tmpCount = 0;
                for (let tmpCell in inputParam) {
                    if (++tmpCount < inputParam.length) {
                        tmpStr = tmpStr + getResult(inputParam[tmpCell], 4) + '~';
                    } else {
                        return tmpStr + getResult(inputParam[tmpCell], 4);
                    }
                }
            } else {
                return inputParam;
            }
            break;
        default:
            return inputParam;
    }
}


module.exports = {
    hl72Json: hl72Json,
    json2Hl7: json2Hl7
}

调用如下:

let fs = require('fs');
let path = require('path');
let handler = require('./main_sim_4');

// -----------------------------------------------------------------------

function test(filename) {
    var filepath = path.normalize(path.join(__dirname, filename));
    fs.readFile(filepath, 'utf8', function (err, xmlStr) {
        let jsonBody = handler.hl72Json(xmlStr);
        // console.log('======================')
        //console.log(jsonBody);

        //let xml = handler.json2xml(jsonBody);
        //console.log(xml);
    });
};

test('hl7payload2.txt');

你可能感兴趣的:(Node.js 数据格式同时支持JSON和HL7)