diff --git a/ntuh/cron_hourly.sh b/ntuh/cron_hourly.sh index 4f2d3c0..09db065 100755 --- a/ntuh/cron_hourly.sh +++ b/ntuh/cron_hourly.sh @@ -1,8 +1,20 @@ #!/bin/bash -. /home/ntuh/d18env/bin/activate +PATH=$PATH:/usr/local/bin + +. /opt/conda/etc/profile.d/conda.sh +conda activate 2023 locale cd $(dirname $0) -python getop.py +python get_ward.py + +# #!/bin/bash + +# . /home/ntuh/d18env/bin/activate + +# locale + +# cd $(dirname $0) +# python getop.py diff --git a/ntuh/get_ward.py b/ntuh/get_ward.py new file mode 100755 index 0000000..3034eb6 --- /dev/null +++ b/ntuh/get_ward.py @@ -0,0 +1,96 @@ +#!/usr/bin/xvfb-run python +# -*- coding: utf-8 -*- + + +import os +os.environ['DJANGO_SETTINGS_MODULE'] = 'ntuh.settings' + +import django +django.setup() + + +from dateutil.relativedelta import * + +import datetime +import json + +from django.db.models import * + +from ntuhgov.portal_selenium import Login, QueryInPatientByWard +from registry.models import * + +import logging +FORMAT = '%(asctime)s %(levelname)s %(message)s' +logging.basicConfig(format=FORMAT, level=logging.INFO) + +def ScanWard(ward='08D'): + logging.info('Scan %s' % (ward)) + list = QueryInPatientByWard(ward) + TIME = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') + + for Pat in list: + # logging.warning(json.dumps(Pat, indent=1)) + + BedID = '%s-%s-%s' % (Pat['WardLabel'], Pat['RoomLabel'], Pat['BedLabel']) + BedTime = '%s_%s' % (BedID, TIME) + # logging.info('%s %s'%(BedTime,Pat['PatChartNo'])) + # lat = InpatientLog.objects.filter(BedID=BedID).latest('Created') + # print(lat.ChartNo) + + try: + lat = InpatientLog.objects.filter(BedID=BedID).latest('Created') + except: + lat = None + + if lat is None or lat.ChartNo != Pat['PatChartNo']: + logging.info('create %s %s'%(BedTime,Pat['PatChartNo'])) + # print(len(BedTime)) + p = InpatientLog.objects.create(BedTime = BedTime) + p.Ward = Pat['WardLabel'] + p.Room = Pat['RoomLabel'] + p.Bed = Pat['BedLabel'] + p.Name = Pat['LinkPatientName'] + p.ChartNo = Pat['PatChartNo'] + p.Sex = Pat['PatientSex'] + if 'PatientAgeTitle' in Pat: # 'PatientAge': '待確認' + p.Birthday = Pat['PatientAgeTitle'][-10:].replace('_', '-') + p.Age = Pat['PatientAge'] + p.HospitalDays = Pat[u'住院總天數'][:-1] + p.Enter = Pat[u'入'].replace('/', '-') + + p.BedID = BedID + + p.save() + + else: + logging.info('skip %s %s'%(BedTime,Pat['PatChartNo'])) + + + + + + continue +# print json.dumps(Pat, ensure_ascii=False, indent=1) + # logging.info(Pat) + + + + p, created = Inpatient.objects.get_or_create(ChartNo = Pat['PatChartNo']) + p.Ward = Pat['WardLabel'] + p.Room = Pat['RoomLabel'] + p.Bed = Pat['BedLabel'] + p.Name = Pat['LinkPatientName'] +# p.ChartNo = Pat['ChartNo'] + p.Sex = Pat['PatientSex'] + if 'PatientAgeTitle' in Pat: # 'PatientAge': '待確認' + p.Birthday = Pat['PatientAgeTitle'][-10:].replace('_', '-') + p.Age = Pat['PatientAge'] + p.HospitalDays = Pat[u'住院總天數'][:-1] + p.Enter = Pat[u'入'].replace('/', '-') + p.save() + +def main(): + ScanWard() + +if __name__ == '__main__': + main() # 或是任何你想執行的函式 diff --git a/ntuh/registry/migrations/0004_inpatientlog.py b/ntuh/registry/migrations/0004_inpatientlog.py new file mode 100644 index 0000000..5386bc3 --- /dev/null +++ b/ntuh/registry/migrations/0004_inpatientlog.py @@ -0,0 +1,32 @@ +# Generated by Django 4.1 on 2024-12-12 13:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('registry', '0003_auto_20240529_1440'), + ] + + operations = [ + migrations.CreateModel( + name='InpatientLog', + fields=[ + ('Ward', models.CharField(max_length=10, null=True)), + ('Room', models.CharField(max_length=10, null=True)), + ('Bed', models.CharField(max_length=10, null=True)), + ('Name', models.CharField(max_length=50, null=True)), + ('ChartNo', models.CharField(max_length=10)), + ('Sex', models.CharField(max_length=10, null=True)), + ('Birthday', models.DateField(null=True)), + ('Age', models.CharField(max_length=10, null=True)), + ('HospitalDays', models.IntegerField(null=True)), + ('Enter', models.DateField(null=True)), + ('BedID', models.CharField(max_length=10, null=True)), + ('BedTime', models.CharField(max_length=10, primary_key=True, serialize=False)), + ('Created', models.DateTimeField(auto_now_add=True)), + ('Saved', models.DateTimeField(auto_now=True)), + ], + ), + ] diff --git a/ntuh/registry/migrations/0005_alter_inpatientlog_bedtime.py b/ntuh/registry/migrations/0005_alter_inpatientlog_bedtime.py new file mode 100644 index 0000000..e0b7fc8 --- /dev/null +++ b/ntuh/registry/migrations/0005_alter_inpatientlog_bedtime.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1 on 2024-12-12 13:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('registry', '0004_inpatientlog'), + ] + + operations = [ + migrations.AlterField( + model_name='inpatientlog', + name='BedTime', + field=models.CharField(max_length=30, primary_key=True, serialize=False), + ), + ] diff --git a/ntuh/registry/models.py b/ntuh/registry/models.py index 1a0859f..1631042 100755 --- a/ntuh/registry/models.py +++ b/ntuh/registry/models.py @@ -64,6 +64,25 @@ class Inpatient(models.Model): Created = models.DateTimeField(auto_now_add = True) Saved = models.DateTimeField(auto_now = True) +class InpatientLog(models.Model): + Ward = models.CharField(max_length=10, null=True) + Room = models.CharField(max_length=10, null=True) + Bed = models.CharField(max_length=10, null=True) + Name = models.CharField(max_length=50, null=True) + ChartNo = models.CharField(max_length=10) + Sex = models.CharField(max_length=10, null=True) + Birthday = models.DateField(null=True) + Age = models.CharField(max_length=10, null=True) + HospitalDays = models.IntegerField(null=True) + Enter = models.DateField(null=True) + + BedID = models.CharField(max_length=10, null=True) + BedTime = models.CharField(max_length=30, primary_key=True) + + Created = models.DateTimeField(auto_now_add = True) + Saved = models.DateTimeField(auto_now = True) + + class Physician(models.Model): Name = models.CharField(max_length=10) EmployeeID = models.CharField(max_length=10, primary_key=True) diff --git a/ntuh/submodule/ntuhgov b/ntuh/submodule/ntuhgov index 9794234..d7c6976 160000 --- a/ntuh/submodule/ntuhgov +++ b/ntuh/submodule/ntuhgov @@ -1 +1 @@ -Subproject commit 979423478440452fc1b7b5ffc1b5b80bc98a7f3b +Subproject commit d7c69765c6df0fd63312cb5b7a52b74f2f337272