-
Notifications
You must be signed in to change notification settings - Fork 0
/
flu_main.py
282 lines (255 loc) · 11.2 KB
/
flu_main.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import datetime
import os
import shutil
import time
import pandas
import requests
import json
import pymongo
import cx_Oracle
import pymssql
import asyncio
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
def run_flu_once():
date_str = datetime.datetime.now().strftime('%Y%m%d')
basic_path = 'C:/Users/Elysion/Desktop/流感上报/' + date_str + '/'
upload_listener_path = 'C:/Users/Elysion/Desktop/流感上报/upload/'
# 判断文件夹是否已经存在
if os.path.exists(basic_path):
shutil.rmtree(basic_path)
os.mkdir(basic_path)
else:
os.mkdir(basic_path)
print(basic_path)
if not os.path.exists(upload_listener_path):
os.mkdir(upload_listener_path)
else:
pass
print(upload_listener_path)
# ---------------------data base config----------------------
# cdr_lis_db = pymysql.connect(host="10.0.0.192", user="root", password="incitedata", database="test")
lis_db = pymssql.connect(host='192.168.50.1', user='CDRUser', password='CDRUser', database='ndlmis15',
charset='utf8')
his_db = pymssql.connect(host='10.0.0.155', user='sa', password='elysion4u', database='chisdb_nczxyy',
charset='GBK')
iih_db = cx_Oracle.connect('iih', 'avjrXzkd2m42jOBb', '10.0.66.6:1521/IIH')
mr_db = cx_Oracle.connect('system', 'manager', '10.0.0.57/hosp')
# mr_db = cx_Oracle.connect('system', 'manager', '10.0.0.57/hosp', mode=cx_Oracle.SYSDBA)
# -----------------------------local functions---------------
function_step_config = [
{
'func': 'lis',
'db': lis_db,
'sql': FLU_sql.lis_sql
},
{
'func': 'flu_mz',
'db': iih_db,
'sql': FLU_sql.iih_mz_flu_sql
},
{
'func': 'pdr_mz',
'db': iih_db,
'sql': FLU_sql.iih_mz_pdr_sql
},
{
'func': 'flu_zy',
'db': his_db,
'sql': 'exec FLU_flu_zy'
},
{
'func': 'pdr_zy',
'db': his_db,
'sql': 'exec FLU_pdr'
},
{
'func': 'hda',
'db': his_db,
'sql': 'exec FLU_hda'
},
{
'func': 'hqms',
'db': mr_db,
'sql': FLU_sql.hqms_sql
},
{
'func': 'hdr',
'db': his_db,
'sql': 'exec FLU_hdr'
}
]
# ----------------lis--------------------------------------
# 获取时间段内全部LIS结果
lis_result = pandas.read_sql(FLU_sql.lis_sql, lis_db)
print(lis_result)
print('lis_all_result')
# 获取LIS符合条件的患者列表
lis_positive_result = lis_result[lis_result['P8004'] == '1']
print(lis_positive_result)
print('lis_positive_result')
# 获取HIS符合条件的患者列表
# ===========================================================
his_patient_sql = "exec FLU_patient"
his_patient_result = pandas.read_sql(his_patient_sql, his_db)
print(his_patient_result)
print('zy_patient_list')
# ===========================================================
# 获取iih符合条件的患者列表
# ===========================================================
iih_patient_result = pandas.read_sql(FLU_sql.iih_mz_patient_list, iih_db)
print(iih_patient_result)
print('mz_patient_list')
# ===========================================================
# 合并HIS和LIS和iih符合条件的患者列表
union_patient_result = pandas.concat([lis_positive_result, his_patient_result, iih_patient_result], axis=0,
sort=True)
# ----------------lis-----------------------------------
return_msg = ''
dead_people_inpatient_no = ''
for step in function_step_config:
try:
if step['func'] == 'lis':
# 筛选LIS全部结果,获取HIS相关病人的LIS报告
result0 = lis_result[lis_result.P7502.isin(
union_patient_result['inpatient_no'])]
result1 = lis_result[lis_result.P7502.isin(
union_patient_result['patient_id'])]
result = pandas.concat([result0, result1], axis=0)
else:
result = pandas.read_sql(step['sql'], step['db'])
if result.shape[0] == 0:
print(step['func'] + '无数据')
return_msg += step['func'] + '---无数据\n'
else:
file_name = step['func'] + '_' + date_str
result.to_csv(file_name + '.csv')
shutil.move(os.getcwd() + '/' + file_name + '.csv', basic_path)
print(step['func'] + '完成')
return_msg += step['func'] + '---完成\n'
if step['func'] == 'hdr':
dead_people_inpatient_no = '今日上报的死亡患者住院号为:\n'
for row in result.itertuples():
dead_people_inpatient_no += '{},'.format(row[1])
print(dead_people_inpatient_no)
dead_people_inpatient_no += '如有新增请上传相关病例资料'
'''
with zipfile.ZipFile(file_name + '.zip', 'w') as zip:
zip.write(file_name + '.csv')
shutil.move(os.getcwd() + '/' + file_name + '.zip', basic_path)
'''
except BaseException:
print(step['func'] + '失败')
return_msg += step['func'] + '---失败\n'
# -----------------------短信提示-------------------------
for phone_number in ['15583570988', '18227329509', '15181790852']:
# 预防保健科方圆 ,'18227329509'
r = requests.post(url='http://10.0.200.1/PlatformService/platform/api', json={
'api_id': 'sendMsg',
'phone': phone_number,
'content': '\n'
+ datetime.datetime.now().strftime('%Y-%m-%d')
+ ' 流感上报情况:\n'
+ return_msg
+ dead_people_inpatient_no
})
print(r.text)
return 'finish'
def move_to_upload_folder():
for file_name in os.listdir('C:/Users/Elysion/Desktop/流感上报/' + datetime.datetime.now().strftime('%Y%m%d') + '/'):
if file_name.endswith('.csv'):
# print(file_name)
shutil.copy(
'C:/Users/Elysion/Desktop/流感上报/' + datetime.datetime.now().strftime('%Y%m%d') +
'/' + file_name,
'C:/Users/Elysion/Desktop/流感上报/upload/' + file_name)
def scheduler_upload_task():
job_defaults = {'max_instances': 2}
scheduler = BlockingScheduler()
scheduler.add_job(run_once, 'interval', seconds=15, misfire_grace_time=15)
scheduler.add_job(timer_runner, 'interval', seconds=15, misfire_grace_time=15)
scheduler.start()
def request_web_sphere(param_item):
service_id = param_item['service_id']
subscription_url = "http://10.0.12.51:8780/sdk/S116/" + service_id
response = requests.get(url=subscription_url, headers={
'hospital_id': '45218368-8', # 本院机构代码
'apply_unit_id': '0', # 默认0
'exec_unit_id': '0',
'service_id': service_id, # 服务id
'visit_type': '01', # 01 门诊,03 住院,0401 体检,0201 急诊
'order_exec_id': '0',
'send_sys_id': param_item['send_sys_id'],
'extend_sub_id': '0',
'Authorization': 'Basic Zmx1OmZsdTEyMw=='
}, data={})
raw = json.loads(response.text)
return raw
def run_once():
mongo = pymongo.MongoClient('mongodb://127.0.0.1:27017/')
db = mongo['flu']
for item in [
{'service_id': 'BS301', 'send_sys_id': 'S001'}, # BS301 诊断,S001 iih
{'service_id': 'BS302', 'send_sys_id': 'S001'}, # BS302 处方
{'service_id': 'BS311', 'send_sys_id': 'S001'}, # BS311 住院用药医嘱
{'service_id': 'BS319', 'send_sys_id': 'S008'} # BS319 普通检验,S008 LIS
]:
break_flag = True
counter = 0
while break_flag:
service_id = item['service_id']
raw = request_web_sphere(item)
if raw['status'] != 0:
result = raw['data']['body']
counter += 1
if service_id == 'BS301':
db[service_id].delete_many({'visitOrdNo': result['visitOrdNo']})
for diagnosis in result['diagnosis']:
if diagnosis['diseaseCode'][0:1] == 'J':
db['flu_patients'].insert_one({'patientId': result['patientLid'],
'visitType': result['visitType'],
'visitNo': result['visitNo'],
'visitOdrNo': result['visitOrdNo'],
'comeFrom': "BS301"})
elif service_id == 'BS302':
if result['triggerEvent'] == 'new':
db[service_id].delete_many({'visitOrdNo': result['visitOrdNo']})
for prescription in result['prescriptions']:
for drugItem in prescription:
if drugItem['drugCode'] in ['123456', '654321']:
db['flu_patients'].insert_one({'patientId': result['patientLid'],
'visitType': result['visitTypeCode'],
'visitNo': result['visitNo'],
'visitOdrNo': result['visitOrdNo'],
'comeFrom': "BS302"})
elif result['triggerEvent'] == 'renew':
pass
elif service_id == 'BS311':
pass
elif service_id == 'BS319':
'''
db['flu_patients'].insert_one({'patientId': result['data']['body']['patientLid'],
'visitType': result['data']['body']['visitType'],
'visitNo': result['data']['body']['medicalNo'],
'visitOdrNo': result['data']['body']['visitOrdNo'],
'comeFrom': "BS319"})
'''
pass
else:
pass
db[service_id].insert_one(result)
else:
if counter > 0:
print('\r收到' + str(counter) + '条' + service_id + ' --- ' + str(datetime.datetime.now()))
counter = 0
break_flag = False
break
mongo.close()
def timer_runner():
for i in range(13, -1, -1):
print('\r下次轮询间隔{}秒'.format(i), end='')
time.sleep(1)
if __name__ == '__main__':
print('v1.0 begin')
run_once()
scheduler_upload_task()