import re

# from openpyxl import load_workbook
from pandas import read_excel

from pymongo import MongoClient


SHEETS = (
    ("神外-總院_1070920.xlsx", "總院"), 
    # ("(骨穩)總院_分科1070928.xlsx", "工作表1")
    ("(骨穩)總院_分科1071002.xlsx", "工作表1")
    )

client = MongoClient("mongodb.xiao.tw", 27017)
db = client.forteo
posts = db.posts

# print(posts.find_one())
# exit()

from ntuhgov.portal_selenium import *
from ntuhgov.myutil import *


for file_name, sheet_name in SHEETS:
    data = read_excel(file_name, sheet_name)

    for chartno in data.病歷號:
        if not re.search("\d+", str(chartno)):
            continue

        print(chartno)
        post = posts.find_one({"_id": chartno})

        if post is None:
            post = {"_id": chartno}

        if 'drug' not in post:
            post['drug'] = ShowMedicalRecordDrug(chartno)
            
            
        if 'report' not in post:
            post['report'] = ElectronicMedicalReportViewer(chartno)

        if 'op' not in post:
            post['op'] = OPNoteList(chartno)

        posts.update({"_id": chartno}, {"$set": post}, upsert=True)

