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