private async Task <PTresult> ImportPT_async(object[,] data, IProgress <ProgressReportModel> progress, ProgressReportModel report) { int totalN = data.GetUpperBound(0); int add_N = 0; int change_N = 0; int all_N = 0; await Task.Run(() => { log.Info($" enter ImportPT_async."); // 要有迴路, 來讀一行一行的xls, 能夠判斷 for (int i = 0; i <= totalN; i++) { // 先判斷是否已經在資料表中, 如果不是就insert否則判斷要不要update // 如何判斷是否已經在資料表中? CSDataContext dc = new CSDataContext(); string strUID = string.Empty; // 先判斷身分證字號是否空白, 原本第8, 現在第7 if (string.IsNullOrEmpty((string)data[i, 7])) { // 寫入Error Log // 沒有身分證字號是不行的 Logging.Record_error("身分證字號是空的"); log.Error("身分證字號是空的"); return; } // 再判斷是否已在資料表中 strUID = (string)data[i, 7]; //身分證號,第7欄 var pt = from p in dc.tbl_patients where p.uid == strUID select p; // this is a querry if (pt.Count() == 0) { // insert // 沒這個人可以新增這個人 // 填入資料 try { tbl_patients newPt = new tbl_patients(); if (string.IsNullOrEmpty((string)data[i, 0])) { // 寫入Error Log Logging.Record_error($"{strUID} 沒有病歷號碼"); log.Error($"{strUID} 沒有病歷號碼"); } else { newPt.cid = long.Parse((string)data[i, 0]); // 病歷號, 第1欄 } newPt.uid = strUID; // 身分證號,第8欄 if (string.IsNullOrEmpty((string)data[i, 1])) { // 寫入Error Log Logging.Record_error($"{strUID} 沒有姓名"); log.Error($"{strUID} 沒有姓名"); } else { newPt.cname = (string)data[i, 1]; //姓名,第2欄 } newPt.mf = (string)data[i, 2]; // 性別, 第3欄 if (string.IsNullOrEmpty((string)data[i, 8])) { // 寫入Error Log Logging.Record_error($"{strUID} 沒有生日資料"); log.Error($"{strUID} 沒有生日資料"); } else { string strD = (string)data[i, 8]; // 生日, 第9欄 newPt.bd = DateTime.Parse($"{strD.Substring(0, 4)}/{strD.Substring(4, 2)}/{strD.Substring(6, 2)}"); } newPt.p01 = (string)data[i, 3]; // 市內電話, 第4欄 newPt.p02 = (string)data[i, 4]; // 手機電話, 第5欄 newPt.p03 = (string)data[i, 9]; // 地址,第10欄 newPt.p04 = (string)data[i, 10]; // 提醒,第11欄 newPt.QDATE = _qdate; // 2020026新增QDATE dc.tbl_patients.InsertOnSubmit(newPt); dc.SubmitChanges(); // 20190929 加姓名, 病歷號 Logging.Record_admin("Add a new patient", $"{data[i, 0]} {strUID} {data[i, 1]}"); log.Info($"Add a new patient: {data[i, 0]} {strUID} {data[i, 1]}"); add_N++; } catch (Exception ex) { Logging.Record_error(ex.Message); log.Error(ex.Message); } } else { // update // 有此人喔, 走update方向 // 拿pt比較ws.cells(i),如果不同就修改,並且記錄 tbl_patients oldPt = (from p in dc.tbl_patients where p.uid == strUID select p).ToList()[0]; // this is a record string strChange = string.Empty; bool bChange = false; try { // 病歷號, 20200512加上修改病歷號 if (string.IsNullOrEmpty((string)data[i, 0])) { // 寫入Error Log Logging.Record_error($"{strUID} 沒有病歷號碼"); log.Error($"{strUID} 沒有病歷號碼"); } else if (oldPt.cid != long.Parse((string)data[i, 0])) { strChange += $"改病歷號: {oldPt.cid}=>{data[i, 0]}; "; bChange = true; oldPt.cid = long.Parse((string)data[i, 0]); // 病歷號, 第1欄 } // 姓名 if (string.IsNullOrEmpty((string)data[i, 1])) { // 寫入Error Log Logging.Record_error(strUID + " 沒有姓名"); log.Error($"{strUID} 沒有姓名"); } else if (oldPt.cname != (string)data[i, 1]) { strChange += $"改名: {oldPt.cname}=>{data[i, 1]}; "; bChange = true; oldPt.cname = (string)data[i, 1]; // 姓名,第2欄 } // 性別 if (oldPt.mf != (string)data[i, 2]) { strChange += $"改性別: {oldPt.mf}=>{data[i, 2]}; "; bChange = true; oldPt.mf = (string)data[i, 2]; // 性別, 第3欄 } // 生日 if (string.IsNullOrEmpty((string)data[i, 8])) { // 寫入Error Log Logging.Record_error($"{strUID} 沒有生日資料"); log.Error($"{strUID} 沒有生日資料"); } else { string strBD = (string)data[i, 8]; // 生日, 第9欄 DateTime dBD = DateTime.Parse($"{strBD.Substring(0, 4)}/{strBD.Substring(4, 2)}/{strBD.Substring(6, 2)}"); if (oldPt.bd != dBD) { strChange += $"改生日: {oldPt.bd}=>{dBD}; "; bChange = true; oldPt.bd = dBD; // 生日,第9欄 } } // 市內電話 if ((oldPt.p01 ?? string.Empty) != ((string)data[i, 3] ?? string.Empty)) { strChange += $"改市內電話: {oldPt.p01}=>{data[i, 3]}; "; bChange = true; oldPt.p01 = (string)data[i, 3]; // 市內電話,第4欄 } // 手機電話 if ((oldPt.p02 ?? string.Empty) != ((string)data[i, 4] ?? string.Empty)) { strChange += $"改手機電話: {oldPt.p02}=>{data[i, 4]}; "; bChange = true; oldPt.p02 = (string)data[i, 4]; // 手機電話,第5欄 } // 地址 if ((oldPt.p03 ?? string.Empty) != ((string)data[i, 9] ?? string.Empty)) { strChange += $"改地址: {oldPt.p03}=>{data[i, 9]}; "; bChange = true; oldPt.p03 = (string)data[i, 9]; // 地址,第10欄 } // 提醒 if ((oldPt.p04 ?? string.Empty) != ((string)data[i, 10] ?? string.Empty)) { strChange += $"改提醒: {oldPt.p04}=>{data[i, 10]}; "; bChange = true; oldPt.p04 = (string)data[i, 10]; // 提醒,第11欄 } if (bChange) { // 做實改變 // 2020026新增QDATE oldPt.QDATE = _qdate; dc.SubmitChanges(); // 做記錄 // 20190929 加姓名, 病歷號 Logging.Record_admin("Change patient data", $"{data[i, 0]} {strUID} {data[i, 1]}: {strChange}"); log.Info($"Change patient data: {data[i, 0]} {strUID} {data[i, 1]}: {strChange}"); change_N++; } } catch (Exception ex) { Logging.Record_error(ex.Message); log.Error(ex.Message); } } all_N++; report.PercentageComeplete = all_N * 100 / totalN; progress.Report(report); } log.Info($" exit ImportPT_async."); }); return(new PTresult() { NewPT = add_N, ChangePT = change_N, AllPT = all_N }); }
private string MakeSure_UID(string vpnUID) { //log.Info($" Begin to check UID: {vpnUID}"); string thesisUID = string.Empty; string thesisNAME = string.Empty; string o = string.Empty; Com_alDataContext dc = new Com_alDataContext(); /// 找到正確的身分證號碼, 1. 從MainViewModel中的CPatient /// 絕不補中間三碼 /// 如果SQL server裡沒有資料甚至可以寫入 /// 寫入的資料來源為杏翔系統, 接口為MainWindow的strID.Content /// 杏翔的key sample 如下 /// "2020/04/25 上午 02 診 015 號 - (H223505857) 陳俞潔" /// 依照杏翔有無, 資料庫有無, VPN有, 應該有四種情形 /// VPN 有, 杏翔 有, 資料庫 有 => 理想狀況取的正確UID /// VPN 有, 杏翔 有, 資料庫 無 => 新個案, UID寫入資料庫tbl_patients, 取得正確UID /// VPN 有, 杏翔 異, 資料庫 有 => 只有一筆, 直接取得UID; 若有多筆, 跳出視窗選擇正確UID /// VPN 有, 杏翔 異, 資料庫 無 => 新個案, 不做任何動作, 絕不補中間三碼 /// VPN 有, 杏翔 無, 資料庫 有 => 只有一筆, 直接取得UID; 若有多筆, 跳出視窗選擇正確UID /// VPN 有, 杏翔 無, 資料庫 無 => 新個案, 不做任何動作, 絕不補中間三碼 // 取得thesisUID try { string[] vs = s.m.strID.Content.ToString().Split(' '); // 身分證字號在[7], 還要去掉括弧 thesisUID = vs[7].Substring(1, (vs[7].Length - 2)); thesisNAME = vs[8]; log.Info($" [{s.m.strID.Content}] being processed."); log.Info($" 杏翔系統目前UID: {thesisUID}"); } catch (Exception ex) { //log.Error(ex.Message); /// 杏翔沒開, 或是沒連動, 反正就是抓不到 /// thesisUID = string.Empty; /// thesisNAME = string.Empty; log.Error($" 杏翔系統無法取得UID, ERROR:{ex.Message}"); } if (!string.IsNullOrEmpty(thesisUID)) { // 第一, 第二種狀況, 有杏翔 if ((thesisUID.Substring(0, 4) == vpnUID.Substring(0, 4)) && (thesisUID.Substring(7, 3) == vpnUID.Substring(7, 3))) { /// 要確認不要確認? /// 在看診情況下,這是90%的狀況 /// passed first test /// 在區分兩種狀況, 如果資料庫裏面沒有, 就是新個案 try { // Single() returns the item, throws an exception if there are 0 or more than one item in the sequence. string sqlUID = (from p in dc.tbl_patients where p.uid == thesisUID select p.uid).Single(); log.Info(message: $" VPN有[{vpnUID}], 杏翔有[{thesisUID}], 資料庫有[{sqlUID}]"); // 如果沒有錯誤發生 // 此時為第一種狀況 /// VPN 有, 杏翔 有, 資料庫 有 => 理想狀況取的正確UID } catch (Exception ex) { log.Error($" {ex.Message}: 資料庫裡沒有這個病人, 新加入tbl_patients."); // Single() returns the item, throws an exception if there are 0 or more than one item in the sequence. // 因為uid是primary key, 如果有錯誤只可能是沒有此uid // 此時為第二種狀況 // VPN 有, 杏翔 有, 資料庫 無 => 新個案, UID寫入資料庫tbl_patients, 取得正確UID // 接下來就是要新增一筆資料 CultureInfo MyCultureInfo = new CultureInfo("en-US"); DateTime dummyDateTime = DateTime.ParseExact("Friday, April 10, 2009", "D", MyCultureInfo); tbl_patients newPt = new tbl_patients { cid = 0, uid = thesisUID, bd = dummyDateTime, cname = thesisNAME, QDATE = DateTime.Now }; // 20200526 加入QDATE dc.tbl_patients.InsertOnSubmit(newPt); dc.SubmitChanges(); } // 無論第一或第二種狀況, 都是回傳thesisUID o = thesisUID; } else { // 如果沒有使用companion, 或是用別人的健保卡,單獨要查詢 // 第三, 第四種狀況 var q = from p in dc.tbl_patients where (p.uid.Substring(0, 4) == vpnUID.Substring(0, 4) && p.uid.Substring(7, 3) == vpnUID.Substring(7, 3)) select new { p.uid, p.cname }; switch (q.Count()) { case 0: // 這是第四種狀況 // VPN 有, 杏翔 無, 資料庫 無 => 新個案, 不做任何動作, 絕不補中間三碼 o = string.Empty; log.Info($" VPN有[{vpnUID}], 杏翔異[{thesisUID}], 資料庫 無 => 新個案, 不做任何動作, 絕不補中間三碼"); break; case 1: // passed test // 這是第三種狀況(1/2) log.Info($" VPN有[{vpnUID}], 杏翔 異, 資料庫有, 且只有一筆 => 直接從資料庫抓"); o = q.Single().uid; break; default: // 這是第三種狀況(2/2) string qu = "請選擇 \r\n"; for (int i = 0; i < q.Count(); i++) { qu += $"{i + 1}. {q.ToList()[i].uid} {q.ToList()[i].cname} \r\n"; } string answer = "0"; while ((int.Parse(answer) < 1) || (int.Parse(answer) > q.Count())) { answer = Interaction.InputBox(qu); // 有逃脫機制了 if (answer == "q") { o = string.Empty; return(o); } if (!(int.TryParse(answer, out int result))) { answer = "0"; } } log.Info($" VPN有[{vpnUID}], 杏翔 異, 資料庫 有, 但有多筆 => 選擇後從資料庫抓"); o = q.ToList()[int.Parse(answer) - 1].uid; break; } } } else { // 如果沒有使用companion, 或是用別人的健保卡,單獨要查詢 // 第三, 第四種狀況 var q = from p in dc.tbl_patients where (p.uid.Substring(0, 4) == vpnUID.Substring(0, 4) && p.uid.Substring(7, 3) == vpnUID.Substring(7, 3)) select new { p.uid, p.cname }; switch (q.Count()) { case 0: // 這是第四種狀況 // VPN 有, 杏翔 無, 資料庫 無 => 新個案, 不做任何動作, 絕不補中間三碼 log.Info($" VPN有[{vpnUID}], 杏翔 無, 資料庫 無 => 新個案, 不做任何動作, 絕不補中間三碼"); o = string.Empty; break; case 1: // passed test // 這是第三種狀況(1/2) log.Info($" VPN有[{vpnUID}], 杏翔 無, 資料庫有, 且只有一筆 => 直接從資料庫抓"); o = q.Single().uid; break; default: // 這是第三種狀況(2/2) string qu = "請選擇 \r\n"; for (int i = 0; i < q.Count(); i++) { qu += $"{i + 1}. {q.ToList()[i].uid} {q.ToList()[i].cname} \r\n"; } string answer = "0"; while ((int.Parse(answer) < 1) || (int.Parse(answer) > q.Count())) { answer = Interaction.InputBox(qu); // 有逃脫機制了 if (answer == "q") { o = string.Empty; return(o); } if (!(int.TryParse(answer, out int result))) { answer = "0"; } } log.Info($" VPN有[{vpnUID}], 杏翔 無, 資料庫 有, 但有多筆 => 選擇後從資料庫抓"); o = q.ToList()[int.Parse(answer) - 1].uid; break; } } //log.Info($" End to check UID: {vpnUID}"); return(o); }