db.js
8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const pg = require('pg');
var isNumber = require("isnumber");
const fs = require('fs');
const async = require('async');
const toCSV = require('array-to-csv')
const csvToObject = require('csv-to-object')
var Iconv = require('iconv').Iconv;
var iconv = new Iconv('EUC-KR', 'UTF-8//TRANSLIT//IGNORE');
const config = {
host: 'localhost',
user: 'postgres',
password: 'root',
database: 'khuh_local',
port: 5432,
ssl: false
};
const client = new pg.Client(config);
client.connect(err => {
if (err) throw err;
else {
getPatient("당뇨");
}
});
var allPat = [];
var presTable = [];
var normalSet={};
function getPatient(desease) {
console.log(`1. 모든 환자 리스팅중.`);
const query = 'select "ID" from "ocsDWIDATA" \
where "MCD" in \
(select "Mcd" from "ocsDWVDIS" \
where "KorName" like(\'%'+desease+'%\')) \
union \
select "ID" from "ocsDWWDATA" \
where "MCD" in \
(select "Mcd" from "ocsDWVDIS" \
where "KorName" like(\'%'+desease+'%\')) \
ORDER BY "ID"';
client.query(query)
.then(res => {
allPat = res.rows;
console.log(allPat.length+"명");
getAverageSet();
//process.exit();
})
.catch(err => {
console.log(err);
process.exit();
});
}
function getAverageSet(){
console.log("2. 정상범주 가져오는중.");
var columns = ["code", "name", "val"];
require("csv-to-array")({
file: "normal_value_delete_version_utf8.csv",
columns: columns
}, function (err, array) {
array.forEach((value)=>{
normalSet[value.name]=value.val;
});
console.log(normalSet);
buildData();
});
/*
fs.readFile('normal_value_delete_version.csv', 'utf8',function(err,data){
let result = csvToObject({
dilemeter: '\n',
string: data
});
console.log(data);
normalSet=result;
buildData();
});
*/
}
function buildData(){
console.log("3. 환자 처방목록 가져오는중.");
let i=0;
let sum =0;
allPat.forEach((patient)=>{
const query=
`SELECT "YMD", "DAY2", "SEQ", "ORDCD", "KorName","EngName", "CODE", "CDNM", "KUM", "RR", "DAN", "ARE", "BunCode" \
FROM \
(SELECT * \
FROM \
((SELECT "YMD","SEQ","ORDCD" FROM "ocsDWWORD" WHERE "ocsDWWORD"."ID"='` + patient.ID +`') \
UNION \
(SELECT "YMD","SEQ","ORDCD" FROM "ocsDWIORD" WHERE "ocsDWIORD"."ID"='` + patient.ID +`')) \
AS a \
LEFT OUTER JOIN "ocsDWVSUGA" ON "a"."ORDCD"= "ocsDWVSUGA"."SuCode" \
ORDER BY "a"."YMD",CAST("a"."SEQ" AS INTEGER)) AS b \
FULL OUTER JOIN \
(SELECT * FROM "ocsKHULAB1" WHERE "ocsKHULAB1"."ID"='` + patient.ID +`') AS c \
ON "c"."DAY"="b"."YMD" AND "c"."CODE" = "b"."SuCode" \
WHERE "BunCode" IN ('50','51','54','56','59','61')
ORDER BY "YMD",CAST("SEQ" AS INTEGER)`;
return client.query(query)
.then(res => {
let resultList = res.rows;
let prev=null;
let isVisited={};
for(let i=0;i<resultList.length;i++){
let current = resultList[i];
let inspList=[];
let presList=[];
if(current.DAY2!=null && !isVisited[current.DAY2]){ //검사면,
let found=false;
let foundDay='';
for(let j=i;j<resultList.length;j++){
//순회해서 같은 검사결과 날짜를 찾아 검사리스트에 넣는다.
if(resultList[j].DAY2!=null && current.DAY2==resultList[j].DAY2){
if(isNumber(resultList[j].RR)){
inspList.push({"code":resultList[j].CODE, "name":resultList[j].CDNM, "value":resultList[j].RR, "scale":resultList[j].DAN});
}
}
//순회해서 가장 가까운 다음 날짜를 찾는다면 처방리스트에 넣는다.
if(resultList[j].DAY2==null){
if(found && foundDay==resultList[j].YMD){
if(resultList[j].KorName){
presList.push({"code":resultList[j].ORDCD,"name":resultList[j].KorName});
}
}else{
if(resultList[j].YMD>current.YMD){
found=true;
foundDay=resultList[j].YMD;
if(resultList[j].KorName){
presList.push({"code":resultList[j].ORDCD,"name":resultList[j].KorName});
}
}
}
}
//해당 날짜는 방문 했다고 표시.
isVisited[current.DAY2]=true;
}
}
if(inspList.length!=0 && presList.length!=0){
let tmpNormal = clone(normalSet);
inspList.forEach((insp)=>{
//정상범주 row에 존재한다면 넣는다
if(tmpNormal[insp.name+'-'+insp.scale]){
tmpNormal[insp.name+'-'+insp.scale]=insp.value;
}else if(tmpNormal[insp.name]){
tmpNormal[insp.name]=insp.value;
}
});
for(let k=0;k<presList.length;k++){
for(let l=k+1;l<presList.length;l++){
if(presList[k].name==presList[l].name){
presList.splice(l,1);
l--;
}else{
}
}
}
presList.sort((a,b)=>a.name<b.name);
tmpNormal["prescription"]='\"'+presList.map(e=>e.name).join('|').replace(/\"/gi,"").replace(/,/gi,"&").replace(/\n/gi," ")+'\"';
//console.log(tmpNormal);
//console.log(Object.values(tmpNormal)[125]);
presTable.push(Object.values(tmpNormal));
//presTable.push({"inspection":inspList, "prescription":presList});
}
inspList=[];
presList=[];
}
console.log(i + "번 환자("+ patient.ID +") :" +resultList.length + "개의 처방");
i++;
if(i==allPat.length){
let tmpCSV = Object.keys(normalSet).join(',')+',prescription\n';
let tmpTable = Object.values(presTable);
for(let j=0;j<tmpTable.length;j++){
tmpCSV+=tmpTable[j].join(',');
if(j!=tmpTable.length-1){
tmpCSV+='\n';
}
}
//console.log(tmpCSV);
fs.writeFileSync("result.csv", '\ufeff'+ tmpCSV, {encoding: 'utf8'});
console.log("끝");
process.exit();
}
})
})
}
function clone(obj) {
if (obj === null || typeof(obj) !== 'object')
return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = obj[attr];
}
}
return copy;
}