Ejemplo n.º 1
0
        private async void ParsingTables(string o)
        {
            tb.ShowBalloonTip($"讀取完成 [{current_op?.UID}]", "開始解析與寫入資料庫", BalloonIcon.Info);
            log.Info($"[Info] all HTML loaded into memory. start to analyze. [{current_op?.UID}]");

            // Count = 0 代表最後一個 tab
            // 20200504 這裡一個BUG, 漏了把F_DATA_Loadcompleted刪掉,以至於不斷重複多次. ******

            /// 確定是最後一個tab這段程式到此結束
            /// 4. Parsing & Saving to SQL: async
            ///     4-1. 多工同時處理, 快速
            ///     4-2. 依照欄位資料/位置 Parsing
            ///     4-3. 存入SQL
            ///     4-4. 製作Query
            /// 查核機制?
            log.Debug($"[Start] async process, {current_op?.UID}");
            List <Response_DataModel> rds = await VPN_Dictionary.RunWriteSQL_Async(ListRetrieved);

            log.Debug($"[End] async process, {current_op?.UID}");

            /// 1. 讀取特殊註記, 如果有的話
            ///    這是在ContentPlaceHolder1_tab02
            ///    是個table
            // 每當刷新後都要重新讀一次
            // d 是parent HTML document
            HTMLDocument d = (HTMLDocument)g.Document;

            IHTMLElement Special_remark  = d?.getElementById("ContentPlaceHolder1_tab02");
            string       _special_remark = Special_remark?.innerText;

            // 製作紀錄by rd
            try
            {
                short?med_N = (short?)(from p1 in rds
                                       where p1.SQL_Tablename == SQLTableName.Medicine
                                       select p1.Count).Sum(),
                     lab_N = (short?)(from p1 in rds
                                      where p1.SQL_Tablename == SQLTableName.Laboratory
                                      select p1.Count).Sum(),
                     schedule_N = (short?)(from p1 in rds
                                           where p1.SQL_Tablename == SQLTableName.Schedule_report
                                           select p1.Count).Sum(),
                     op_N = (short?)(from p1 in rds
                                     where p1.SQL_Tablename == SQLTableName.Operations
                                     select p1.Count).Sum(),
                     dental_N = (short?)(from p1 in rds
                                         where p1.SQL_Tablename == SQLTableName.Dental
                                         select p1.Count).Sum(),
                     allergy_N = (short?)(from p1 in rds
                                          where p1.SQL_Tablename == SQLTableName.Allergy
                                          select p1.Count).Sum(),
                     discharge_N = (short?)(from p1 in rds
                                            where p1.SQL_Tablename == SQLTableName.Discharge
                                            select p1.Count).Sum(),
                     rehab_N = (short?)(from p1 in rds
                                        where p1.SQL_Tablename == SQLTableName.Rehabilitation
                                        select p1.Count).Sum(),
                     tcm_N = (short?)(from p1 in rds
                                      where p1.SQL_Tablename == SQLTableName.TraditionalChineseMedicine_detail
                                      select p1.Count).Sum();
                Com_clDataContext dc = new Com_clDataContext();
                tbl_Query2        q  = new tbl_Query2()
                {
                    uid    = ListRetrieved.First().UID,
                    QDATE  = ListRetrieved.First().QDate,
                    EDATE  = DateTime.Now,
                    remark = _special_remark
                };
                q.cloudmed_N  = med_N;
                q.cloudlab_N  = lab_N;
                q.schedule_N  = schedule_N;
                q.op_N        = op_N;
                q.dental_N    = dental_N;
                q.allergy_N   = allergy_N;
                q.discharge_N = discharge_N;
                q.rehab_N     = rehab_N;
                q.tcm_N       = tcm_N;
                dc.tbl_Query2.InsertOnSubmit(q);
                dc.SubmitChanges();
                log.Info($"[Final Step]Successfully write into tbl_Query2. From: {ListRetrieved.First().UID}, [{ListRetrieved.First().QDate}]");
                tb.ShowBalloonTip($"寫入完成 [{current_op?.UID}]", "已寫入資料庫", BalloonIcon.Info);
            }
            catch (Exception ex)
            {
                log.Error($"[Final Step]Failed to write into tbl_Querry2. From:  {ListRetrieved.First().UID}, [{ListRetrieved.First().QDate}] Error: {ex.Message}");
            }

            // 更新顯示資料
            string tempSTR = s.m.Label1.Text;

            s.m.Label1.Text = string.Empty;
            s.m.Label1.Text = tempSTR;
            s.m.Web_refresh();

            // activate hotkeys 4
            Activate_Hotkeys();

            log.Debug(o);
            log.Info("===========================================================================");
            log.Info($" ");
        }
        private void F_LoadCompleted(object sender, FrameLoadCompleteEventArgs e)
        {
            log.Debug($"++ Enter F_LoadCompleted from {e.Message}.");
            // 20200509 因為沒有先 -= F_LoadComplted, 造成了幽靈, 因此一定要 -= F_LoadComplted 在任何Exit之前.
            // 每次作業F_LoadCompleted只會被呼叫一次,沒有被洗掉的情形
            fm.FrameLoadComplete -= F_LoadCompleted;
            log.Debug($"@@ delete delegate F_LoadCompleted.");

            // 使用DocumentLoadCompletedButNotFrame, 會造成偶而不動作的症狀, 要找別的方式來處理沒插健保卡的判別
            // 方法

            if (e.Message == FrameLoadStates.DocumentLoadCompletedButNotFrame)
            {
                // 沒有lbluserID, 例如沒插健保卡
                // activate hotkeys 1
                Activate_Hotkeys();
                log.Debug($"++ Exit F_LoadCompleted (1/5). No NHI card inserted.");
                log.Info("===========================================================================");
                log.Info($" ");
                return;
            }
            /// 會有兩次
            /// 第一次讀完檔案會再執行javascript, 在local用來認證健保卡, 沒過就不會有第二次
            /// 如果認證OK, 會再透過javascript下載個案雲端資料, 觸發第二次
            /// 這段程式的目的:
            /// 1. 取得身分證字號
            /// 2. 讀取有哪些tab

            // 20210719: mark this line to simplify logging
            // log.Info($"Entered F_LoadCompleted");

            HTMLDocument d = (HTMLDocument)g.Document;
            // 確定身分證字號
            string strUID = MakeSure_UID(d.getElementById("ContentPlaceHolder1_lbluserID").innerText);

            // if (strUID = string.Empty) 離開
            if (strUID == string.Empty)
            {
                tb.ShowBalloonTip("醫療系統資料庫查無此人", "請與杏翔系統連動, 或放棄操作", BalloonIcon.Warning);

                // activate hotkeys 2
                Activate_Hotkeys();
                log.Debug($"++ Exit F_LoadCompleted (2/5). NHI card inserted but no such person.");
                log.Info("===========================================================================");
                log.Info($" ");
                return;
            }
            else
            {
                /// 表示讀卡成功
                /// show balloon with built-in icon
                tb.ShowBalloonTip("讀卡成功", $"身分證號: {strUID}", BalloonIcon.Info);
                log.Info($"  Successful NHI card read, VPN id: {strUID}.");

                /// 讀卡成功後做三件事: 讀特殊註記, 讀提醒, 開始準備讀所有資料

                /// 2. 讀取提醒, 如果有的話
                ///    這是在ContentPlaceHolder1_GV
                ///    也是個table
                IHTMLElement List_NHI_lab_reminder = d?.getElementById("ContentPlaceHolder1_GV");
                if (List_NHI_lab_reminder != null)
                {
                    HtmlDocument Html_NHI_lab_reminder = new HtmlDocument();
                    Html_NHI_lab_reminder.LoadHtml(List_NHI_lab_reminder?.outerHTML);

                    // 寫入資料庫
                    foreach (HtmlNode tr in Html_NHI_lab_reminder.DocumentNode.SelectNodes("//table/tbody/tr"))
                    {
                        HtmlDocument h_ = new HtmlDocument();
                        h_.LoadHtml(tr.InnerHtml);
                        HtmlNodeCollection tds = h_.DocumentNode.SelectNodes("//td");

                        if ((tds == null) || (tds.Count == 0))
                        {
                            continue;
                        }
                        string Lab_Name = string.Empty, Last_Date = string.Empty;
                        // tds[0] 是檢查(驗)項目類別名稱
                        Lab_Name = tds[0]?.InnerText;
                        // tds[1] 是最近1次檢查日期
                        Last_Date = tds[1]?.InnerText;

                        using (Com_clDataContext dc = new Com_clDataContext())
                        {
                            var q1 = from p1 in dc.tbl_NHI_lab_reminder
                                     where (p1.uid == strUID) && (p1.lab_name == Lab_Name) && (p1.last_date == Last_Date)
                                     select p1;
                            if (q1.Count() == 0)
                            {
                                tbl_NHI_lab_reminder newReminder = new tbl_NHI_lab_reminder()
                                {
                                    uid       = strUID,
                                    QDATE     = DateTime.Now,
                                    lab_name  = Lab_Name,
                                    last_date = Last_Date
                                };
                                dc.tbl_NHI_lab_reminder.InsertOnSubmit(newReminder);
                                dc.SubmitChanges();
                            }
                        };
                    }
                }

                /// 準備: 初始化, 欄位基礎資料/位置, 可在windows生成時就完成

                #region 準備 - 製造QueueOperation

                // Initialization
                QueueOperation?.Clear();
                ListRetrieved?.Clear();
                current_page = total_pages = 0;

                log.Info($"  start reading Operation(s).");
                // 讀取所有要讀的tab, 這些資料位於"ContentPlaceHolder1_tab"
                // IHTMLElement 無法轉型為 HTMLDocument
                // 20200429 tested successful
                IHTMLElement List_under_investigation = d?.getElementById("ContentPlaceHolder1_tab");
                // li 之下就是 a
                List <string> balloonstring = new List <string>();
                string        BalloonTip    = string.Empty;
                foreach (IHTMLElement hTML in List_under_investigation?.all)
                {
                    if (tab_id_wanted.Contains(hTML.id))
                    {
                        // 僅將要讀入的排入, 並沒有真的讀取資料
                        VPN_Operation vOP = VPN_Dictionary.Making_new_operation(hTML.id, strUID, DateTime.Now);
                        QueueOperation.Enqueue(vOP);
                        log.Info($"    讀入operation: {vOP.Short_Name}, [{strUID}]");
                        balloonstring.Add(vOP.Short_Name);
                    }
                }

                for (int i = 0; i < balloonstring.Count; i++)
                {
                    if (i == 0)
                    {
                        BalloonTip = balloonstring[i];
                    }
                    else if ((i % 3) == 0)
                    {
                        BalloonTip += $";\r\n{balloonstring[i]}";
                    }
                    else
                    {
                        BalloonTip += $"; {balloonstring[i]}";
                    }
                }
                log.Info($"  end of Reading Operation(s), 共{QueueOperation?.Count}個Operation(s).");

                #endregion 準備 - 製造QueueOperation

                #region 執行

                if (QueueOperation.Count > 0)
                {
                    // 流程控制, fm = framemonitor

                    // 載入第一個operation
                    //log.Info($"  the first operation loaded.");
                    current_op = QueueOperation.Dequeue();
                    tb.ShowBalloonTip($"開始讀取 [{current_op.UID}]", BalloonTip, BalloonIcon.Info);

                    // 判斷第一個operation是否active, (小心起見, 其實應該不可能不active)
                    // 不active就要按鍵
                    // 要注意class這個attribute是在上一層li層, 需把它改過來
                    if (d.getElementById(current_op.TAB_ID.Replace("_a_", "_li_")).className == "active")
                    {
                        // 由於此時沒有按鍵, 因此無法觸發LoadComplete, 必須人工觸發
                        fm.FrameLoadComplete += F_Data_LoadCompleted;
                        log.Debug($"@@ Add delegate F_Data_LoadCompleted.");
                        FrameLoadCompleteEventArgs ex = new FrameLoadCompleteEventArgs()
                        {
                            Message = FrameLoadStates.DirectCall
                        };
                        log.Debug($"++ Exit F_LoadCompleted (3/5). Goto F_Data_LoadCompleted by direct call.");
                        F_Data_LoadCompleted(this, ex);
                        return;
                    }
                    else
                    {
                        // 不active反而可以用按鍵, 自動會觸發F_Data_LoadCompleted
                        fm.FrameLoadComplete += F_Data_LoadCompleted;
                        log.Debug($"@@ Add delegate F_Data_LoadCompleted.");
                        // 20210719 有時候不fire
                        Thread.Sleep(150);
                        d.getElementById(current_op.TAB_ID).click();
                        log.Info($"[Action] push TAB {current_op.TAB_ID} Button.");
                        log.Debug($"++ Exit F_LoadCompleted (4/5). Go to next tab by pushing tab button.");
                        return;
                    }
                }
                else
                {
                    // 做個紀錄
                    // 一個都沒有
                    log.Info($"  no record at all.");
                    tb.ShowBalloonTip("沒有資料", "健保資料庫裡沒有資料可讀取", BalloonIcon.Info);
                    // 製作紀錄by rd
                    tbl_Query2 q = new tbl_Query2()
                    {
                        uid         = strUID,
                        QDATE       = DateTime.Now,
                        cloudmed_N  = 0,
                        cloudlab_N  = 0,
                        schedule_N  = 0,
                        op_N        = 0,
                        dental_N    = 0,
                        allergy_N   = 0,
                        discharge_N = 0,
                        rehab_N     = 0,
                        tcm_N       = 0
                    };
                    using (Com_clDataContext dc = new Com_clDataContext())
                    {
                        dc.tbl_Query2.InsertOnSubmit(q);
                        dc.SubmitChanges();
                    };

                    // activate hotkeys 3
                    Activate_Hotkeys();
                    log.Debug($"++ Exit F_LoadCompleted (5/5). NHI inserted and verified but completey no data.");
                    log.Info("===========================================================================");
                    log.Info($" ");
                }

                #endregion 執行
            }
        }