public static Result UpdateDaUpload(List <string> keys)
        {
            Result     result = new Result();
            MMOverride db     = null;

            try
            {
                db = new MMOverride();
                using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    foreach (string key in keys)
                    {
                        ChiTietKetQuaXetNghiem_CellDyn3200 ctkqxn = db.ChiTietKetQuaXetNghiem_CellDyn3200s.SingleOrDefault <ChiTietKetQuaXetNghiem_CellDyn3200>(c => c.ChiTietKQXN_CellDyn3200GUID.ToString() == key);
                        if (ctkqxn != null)
                        {
                            ctkqxn.UpdatedBy   = Guid.Parse(Global.UserGUID);
                            ctkqxn.UpdatedDate = DateTime.Now;
                            ctkqxn.DaUpload    = true;
                        }
                    }

                    db.SubmitChanges();
                    t.Complete();
                }
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                result.Error.Code        = (se.Message.IndexOf("Timeout expired") >= 0) ? ErrorCode.SQL_QUERY_TIMEOUT : ErrorCode.INVALID_SQL_STATEMENT;
                result.Error.Description = se.ToString();
            }
            catch (Exception e)
            {
                result.Error.Code        = ErrorCode.UNKNOWN_ERROR;
                result.Error.Description = e.ToString();
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                    db = null;
                }
            }

            return(result);
        }
示例#2
0
        private List <TestResult_CellDyn3200> ParseTestResult_CellDyn3200(string result, string portName)
        {
            List <TestResult_CellDyn3200> testResults = new List <TestResult_CellDyn3200>();

            //result = "\"   \",\"CD3200C\",\"------------\",3280,0,0,\"AVER124     \",\"BUI THI NGHIA TD            \",\"----------------\",\"F\",\"--/--/----\",\"----------------------\",\".  \",\"04/14/2012\",\"17:38\",\"--/--/----\",\"--:--\",\"----------------\",06.34,04.12,01.59,0.307,0.218,0.096,04.36,012.0,038.0,087.0,027.5,031.6,012.7,00254,06.53,0.166,016.7,065.1,025.1,04.85,03.45,01.51,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00000,00000,00000,00000,00000,00000,00000,00000,-----,06.34,1,0,0,0,0,0,0,70\"   \",\"CD3200C\",\"------------\",3280,0,0,\"AVER124     \",\"BUI THI NGHIA TD            \",\"----------------\",\"F\",\"--/--/----\",\"----------------------\",\".  \",\"04/14/2012\",\"17:38\",\"--/--/----\",\"--:--\",\"----------------\",06.34,04.12,01.59,0.307,0.218,0.096,04.36,012.0,038.0,087.0,027.5,031.6,012.7,00254,06.53,0.166,016.7,065.1,025.1,04.85,03.45,01.51,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,00000,00000,00000,00000,00000,00000,00000,00000,-----,06.34,1,0,0,0,0,0,0,70";
            result = result.Replace("\"", "");

            string[] strArr = result.Split("".ToCharArray());

            if (strArr != null && strArr.Length > 0)
            {
                string lastResult = string.Empty;
                if (_htLastResult.ContainsKey(portName))
                {
                    lastResult = _htLastResult[portName].ToString();
                }
                else
                {
                    _htLastResult.Add(portName, string.Empty);
                }

                strArr[0] = string.Format("{0}{1}", lastResult, strArr[0]);
                _htLastResult[portName] = strArr[strArr.Length - 1];

                for (int i = 0; i < strArr.Length - 1; i++)
                {
                    TestResult_CellDyn3200 testResult = new TestResult_CellDyn3200();
                    result = strArr[i];
                    string[] arrResult = result.Split(",".ToCharArray(), StringSplitOptions.None);
                    if (arrResult == null || arrResult.Count() == 0)
                    {
                        continue;
                    }

                    testResult.KetQuaXetNghiem.MessageType    = arrResult[0].Trim().Substring(1);
                    testResult.KetQuaXetNghiem.InstrumentType = arrResult[1].Trim();
                    testResult.KetQuaXetNghiem.SerialNum      = arrResult[2].Trim();
                    testResult.KetQuaXetNghiem.SequenceNum    = Convert.ToInt32(arrResult[3].Trim());
                    testResult.KetQuaXetNghiem.SpareField     = arrResult[4].Trim();
                    testResult.KetQuaXetNghiem.SpecimenType   = Convert.ToInt32(arrResult[5].Trim());
                    if (testResult.KetQuaXetNghiem.SpecimenType != 0)
                    {
                        continue;
                    }

                    testResult.KetQuaXetNghiem.SpecimenID   = arrResult[6].Trim();
                    testResult.KetQuaXetNghiem.SpecimenName = arrResult[7].Trim();
                    testResult.KetQuaXetNghiem.PatientID    = arrResult[8].Trim();
                    testResult.KetQuaXetNghiem.SpecimenSex  = arrResult[9].Trim();
                    if (testResult.KetQuaXetNghiem.SpecimenSex == "-")
                    {
                        testResult.KetQuaXetNghiem.SpecimenSex = string.Empty;
                    }

                    testResult.KetQuaXetNghiem.SpecimenDOB = arrResult[10].Trim();
                    if (testResult.KetQuaXetNghiem.SpecimenDOB == "--/--/----")
                    {
                        testResult.KetQuaXetNghiem.SpecimenDOB = string.Empty;
                    }

                    testResult.KetQuaXetNghiem.DrName     = arrResult[11].Trim();
                    testResult.KetQuaXetNghiem.OperatorID = arrResult[12].Trim();

                    testResult.KetQuaXetNghiem.NgayXN = DateTime.ParseExact(string.Format("{0} {1}", arrResult[13].Trim(), arrResult[14].Trim()),
                                                                            "MM/dd/yyyy HH:mm", null);

                    testResult.KetQuaXetNghiem.CollectionDate = arrResult[15].Trim();
                    testResult.KetQuaXetNghiem.CollectionTime = arrResult[16].Trim();
                    testResult.KetQuaXetNghiem.Comment        = arrResult[17].Trim();

                    //WBC
                    ChiTietKetQuaXetNghiem_CellDyn3200 ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "WBC";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[18].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //NEU
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Neu";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[19].Trim());
                    ctkqxn.TestPercent  = Convert.ToDouble(arrResult[35].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //NEU%
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Neu%";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[35].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //LYM
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Lym";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[20].Trim());
                    ctkqxn.TestPercent  = Convert.ToDouble(arrResult[36].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //LYM%
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Lym%";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[36].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //MONO
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Mono";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[21].Trim());
                    ctkqxn.TestPercent  = Convert.ToDouble(arrResult[37].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //MONO%
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Mono%";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[37].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //EOS
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Eos";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[22].Trim());
                    ctkqxn.TestPercent  = Convert.ToDouble(arrResult[38].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //EOS%
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Eos%";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[38].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //BASO
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Baso";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[23].Trim());
                    ctkqxn.TestPercent  = Convert.ToDouble(arrResult[39].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //BASO%
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Baso%";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[39].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //RBC
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "RBC";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[24].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //HGB
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Hb";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[25].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //HCT
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "Hct";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[26].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //MCV
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "MCV";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[27].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //MCH
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "MCH";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[28].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //MCHC
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "MCHC";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[29].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //RDW
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "RDW";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[30].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //PLT
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "PLT";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[31].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //MPV
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "MPV";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[32].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //PCT
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "PCT";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[33].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    //PDW
                    ctkqxn = new ChiTietKetQuaXetNghiem_CellDyn3200();
                    ctkqxn.TenXetNghiem = "PDW";
                    ctkqxn.TestResult   = Convert.ToDouble(arrResult[34].Trim());
                    testResult.ChiTietKetQuaXetNghiem.Add(ctkqxn);

                    testResults.Add(testResult);
                }
            }

            return(testResults);
        }
        public static Result UpdateChiSoKetQuaXetNghiem(ChiTietKetQuaXetNghiem_CellDyn3200 chiTietKQXN, ref string binhThuong)
        {
            Result     result = new Result();
            MMOverride db     = null;

            try
            {
                db = new MMOverride();
                string desc = string.Empty;
                using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    ChiTietKetQuaXetNghiem_CellDyn3200 ctkqxn = db.ChiTietKetQuaXetNghiem_CellDyn3200s.SingleOrDefault <ChiTietKetQuaXetNghiem_CellDyn3200>(c => c.ChiTietKQXN_CellDyn3200GUID == chiTietKQXN.ChiTietKQXN_CellDyn3200GUID);
                    if (ctkqxn != null)
                    {
                        ctkqxn.UpdatedDate = DateTime.Now;
                        ctkqxn.UpdatedBy   = Guid.Parse(Global.UserGUID);
                        ctkqxn.Status      = (byte)Status.Actived;
                        ctkqxn.TestResult  = chiTietKQXN.TestResult;
                        ctkqxn.TestPercent = chiTietKQXN.TestPercent;
                        ctkqxn.FromValue   = chiTietKQXN.FromValue;
                        ctkqxn.ToValue     = chiTietKQXN.ToValue;
                        ctkqxn.FromPercent = chiTietKQXN.FromPercent;
                        ctkqxn.ToPercent   = chiTietKQXN.ToPercent;
                        ctkqxn.DonVi       = chiTietKQXN.DonVi;
                        ctkqxn.LamThem     = chiTietKQXN.LamThem;

                        KetQuaXetNghiem_CellDyn3200 kqxn = ctkqxn.KetQuaXetNghiem_CellDyn3200;
                        string tenBenhNhan = string.Empty;
                        string fileNum     = string.Empty;
                        if (kqxn.PatientGUID != null)
                        {
                            PatientView patient = db.PatientViews.SingleOrDefault <PatientView>(p => p.PatientGUID == kqxn.PatientGUID);
                            if (patient != null)
                            {
                                fileNum     = patient.FileNum;
                                tenBenhNhan = patient.FullName;
                            }
                        }

                        //
                        double?fromValue = null;
                        double?toValue   = null;
                        //double? fromPercent = null;
                        //double? toPercent = null;
                        string donVi = string.Empty;

                        if (ctkqxn.FromValue.HasValue)
                        {
                            fromValue = ctkqxn.FromValue.Value;
                        }

                        if (ctkqxn.ToValue.HasValue)
                        {
                            toValue = ctkqxn.ToValue.Value;
                        }

                        //if (ctkqxn.FromPercent.HasValue)
                        //    fromPercent = ctkqxn.FromPercent.Value;

                        //if (ctkqxn.ToPercent.HasValue)
                        //    toPercent = ctkqxn.ToPercent.Value;

                        if (ctkqxn.DonVi != null && ctkqxn.DonVi != string.Empty)
                        {
                            donVi = ctkqxn.DonVi;
                        }

                        double    testResult = ctkqxn.TestResult.Value;
                        TinhTrang tinhTrang  = TinhTrang.BinhThuong;

                        if (fromValue != null && toValue != null)
                        {
                            //if (fromPercent != null || toPercent != null)
                            //    binhThuong = string.Format("({0:F2} - {1:F2})", fromValue.Value, toValue.Value);
                            //else
                            if (donVi != string.Empty)
                            {
                                binhThuong = string.Format("({0:F2}-{1:F2} {2})", fromValue.Value, toValue.Value, donVi);
                            }
                            else
                            {
                                binhThuong = string.Format("({0:F2}-{1:F2})", fromValue.Value, toValue.Value);
                            }

                            if (testResult < fromValue.Value || testResult > toValue.Value)
                            {
                                tinhTrang = TinhTrang.BatThuong;
                            }
                        }
                        else if (fromValue != null)
                        {
                            //if (fromPercent != null || toPercent != null)
                            //    binhThuong = string.Format("(> {0:F2})", fromValue.Value);
                            //else
                            if (donVi != string.Empty)
                            {
                                binhThuong = string.Format("(>{0:F2} {1})", fromValue.Value, donVi);
                            }
                            else
                            {
                                binhThuong = string.Format("(>{0:F2})", fromValue.Value);
                            }

                            if (testResult <= fromValue.Value)
                            {
                                tinhTrang = TinhTrang.BatThuong;
                            }
                        }
                        else
                        {
                            //if (fromPercent != null || toPercent != null)
                            //    binhThuong = string.Format("(< {0:F2})", toValue.Value);
                            //else
                            if (donVi != string.Empty)
                            {
                                binhThuong = string.Format("(<{0:F2} {1})", toValue.Value, donVi);
                            }
                            else
                            {
                                binhThuong = string.Format("(<{0:F2})", toValue.Value);
                            }

                            if (testResult >= toValue.Value)
                            {
                                tinhTrang = TinhTrang.BatThuong;
                            }
                        }

                        //if (fromPercent != null && toPercent != null)
                        //{
                        //    double testPercent = ctkqxn.TestPercent.Value;
                        //    percent = string.Format("{0:F2}% ({1:F2} - {2:F2} {3})", testPercent, fromPercent.Value, toPercent.Value, donVi);

                        //    if (tinhTrang == TinhTrang.BinhThuong)
                        //    {
                        //        if (testPercent < fromPercent.Value || testPercent > toPercent.Value)
                        //            tinhTrang = TinhTrang.BatThuong;
                        //    }
                        //}
                        //else if (fromPercent != null)
                        //{
                        //    double testPercent = ctkqxn.TestPercent.Value;
                        //    percent = string.Format("{0:F2}% (> {1:F2} {2})", testPercent, fromPercent.Value, donVi);

                        //    if (tinhTrang == TinhTrang.BinhThuong)
                        //    {
                        //        if (testPercent <= fromPercent.Value)
                        //            tinhTrang = TinhTrang.BatThuong;
                        //    }
                        //}
                        //else if (toPercent != null)
                        //{
                        //    double testPercent = ctkqxn.TestPercent.Value;
                        //    percent = string.Format("{0:F2}% (< {1:F2} {2})", testPercent, toPercent.Value, donVi);

                        //    if (tinhTrang == TinhTrang.BinhThuong)
                        //    {
                        //        if (testPercent >= toPercent.Value)
                        //            tinhTrang = TinhTrang.BatThuong;
                        //    }
                        //}

                        chiTietKQXN.TinhTrang = (byte)tinhTrang;

                        //

                        desc += string.Format("- GUID: '{0}', Mã bệnh nhân: '{1}', Tên bệnh nhân: '{2}', Ngày xét nghiệm: '{3}', Tên xét nghiệm: '{4}', Kết quả: '{5}', % kết quả: '{6}'",
                                              kqxn.KQXN_CellDyn3200GUID.ToString(), fileNum, tenBenhNhan, kqxn.NgayXN.Value.ToString("dd/MM/yyyy HH:mm:ss"), ctkqxn.TenXetNghiem, ctkqxn.TestResult, ctkqxn.TestPercent);

                        //Tracking
                        desc = desc.Substring(0, desc.Length - 1);
                        Tracking tk = new Tracking();
                        tk.TrackingGUID = Guid.NewGuid();
                        tk.TrackingDate = DateTime.Now;
                        tk.DocStaffGUID = Guid.Parse(Global.UserGUID);
                        tk.ActionType   = (byte)ActionType.Edit;
                        tk.Action       = "Cập nhật chỉ số kết quả xét nghiệm celldyn 3200";
                        tk.Description  = desc;
                        tk.TrackingType = (byte)TrackingType.None;
                        tk.ComputerName = Utility.GetDNSHostName();
                        db.Trackings.InsertOnSubmit(tk);

                        db.SubmitChanges();
                    }

                    t.Complete();
                }
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                result.Error.Code        = (se.Message.IndexOf("Timeout expired") >= 0) ? ErrorCode.SQL_QUERY_TIMEOUT : ErrorCode.INVALID_SQL_STATEMENT;
                result.Error.Description = se.ToString();
            }
            catch (Exception e)
            {
                result.Error.Code        = ErrorCode.UNKNOWN_ERROR;
                result.Error.Description = e.ToString();
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                    db = null;
                }
            }

            return(result);
        }
        public static Result DeleteChiTietKetQuaXetNghiem(List <string> keys)
        {
            Result     result = new Result();
            MMOverride db     = null;

            try
            {
                db = new MMOverride();
                string desc = string.Empty;
                using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    foreach (string key in keys)
                    {
                        ChiTietKetQuaXetNghiem_CellDyn3200 ctkqxn = db.ChiTietKetQuaXetNghiem_CellDyn3200s.SingleOrDefault <ChiTietKetQuaXetNghiem_CellDyn3200>(c => c.ChiTietKQXN_CellDyn3200GUID.ToString() == key);
                        if (ctkqxn != null)
                        {
                            ctkqxn.DeletedDate = DateTime.Now;
                            ctkqxn.DeletedBy   = Guid.Parse(Global.UserGUID);
                            ctkqxn.Status      = (byte)Status.Deactived;

                            KetQuaXetNghiem_CellDyn3200 kqxn = ctkqxn.KetQuaXetNghiem_CellDyn3200;
                            string tenBenhNhan = string.Empty;
                            string fileNum     = string.Empty;
                            if (kqxn.PatientGUID != null)
                            {
                                PatientView patient = db.PatientViews.SingleOrDefault <PatientView>(p => p.PatientGUID == kqxn.PatientGUID);
                                if (patient != null)
                                {
                                    fileNum     = patient.FileNum;
                                    tenBenhNhan = patient.FullName;
                                }
                            }

                            desc += string.Format("- GUID: '{0}', Mã bệnh nhân: '{1}', Tên bệnh nhân: '{2}', Ngày xét nghiệm: '{3}', Tên xét nghiệm: '{4}', Kết quả: '{5}', % kết quả: '{6}'\n",
                                                  kqxn.KQXN_CellDyn3200GUID.ToString(), fileNum, tenBenhNhan, kqxn.NgayXN.Value.ToString("dd/MM/yyyy HH:mm:ss"), ctkqxn.TenXetNghiem, ctkqxn.TestResult, ctkqxn.TestPercent);
                        }
                    }

                    //Tracking
                    desc = desc.Substring(0, desc.Length - 1);
                    Tracking tk = new Tracking();
                    tk.TrackingGUID = Guid.NewGuid();
                    tk.TrackingDate = DateTime.Now;
                    tk.DocStaffGUID = Guid.Parse(Global.UserGUID);
                    tk.ActionType   = (byte)ActionType.Delete;
                    tk.Action       = "Xóa chi tiết kết quả xét nghiệm celldyn 3200";
                    tk.Description  = desc;
                    tk.TrackingType = (byte)TrackingType.None;
                    tk.ComputerName = Utility.GetDNSHostName();
                    db.Trackings.InsertOnSubmit(tk);

                    db.SubmitChanges();
                    t.Complete();
                }
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                result.Error.Code        = (se.Message.IndexOf("Timeout expired") >= 0) ? ErrorCode.SQL_QUERY_TIMEOUT : ErrorCode.INVALID_SQL_STATEMENT;
                result.Error.Description = se.ToString();
            }
            catch (Exception e)
            {
                result.Error.Code        = ErrorCode.UNKNOWN_ERROR;
                result.Error.Description = e.ToString();
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                    db = null;
                }
            }

            return(result);
        }
        public static Result InsertKQXN(List <TestResult_CellDyn3200> testResults)
        {
            Result     result = new Result();
            MMOverride db     = null;

            try
            {
                db = new MMOverride();
                using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    foreach (TestResult_CellDyn3200 testResult in testResults)
                    {
                        PatientView patient = db.PatientViews.SingleOrDefault <PatientView>(p => p.FileNum.ToLower().Trim() == testResult.KetQuaXetNghiem.SpecimenID.Trim().ToLower());

                        KetQuaXetNghiem_CellDyn3200 kqxn = db.KetQuaXetNghiem_CellDyn3200s.SingleOrDefault <KetQuaXetNghiem_CellDyn3200>(k => k.SpecimenID.Trim().ToLower() == testResult.KetQuaXetNghiem.SpecimenID.Trim().ToLower() &&
                                                                                                                                         k.OperatorID.Trim().ToLower() == testResult.KetQuaXetNghiem.OperatorID.Trim().ToLower() && k.NgayXN == testResult.KetQuaXetNghiem.NgayXN);

                        string ketQuaXetNghiemGUID = string.Empty;
                        if (kqxn == null) //Add New
                        {
                            testResult.KetQuaXetNghiem.KQXN_CellDyn3200GUID = Guid.NewGuid();
                            testResult.KetQuaXetNghiem.CreatedDate          = DateTime.Now;
                            if (Global.UserGUID != string.Empty)
                            {
                                testResult.KetQuaXetNghiem.CreatedBy = Guid.Parse(Global.UserGUID);
                            }
                            if (patient != null)
                            {
                                testResult.KetQuaXetNghiem.PatientGUID = patient.PatientGUID;
                            }
                            db.KetQuaXetNghiem_CellDyn3200s.InsertOnSubmit(testResult.KetQuaXetNghiem);
                            db.SubmitChanges();

                            ketQuaXetNghiemGUID = testResult.KetQuaXetNghiem.KQXN_CellDyn3200GUID.ToString();
                        }
                        else //Update
                        {
                            kqxn.UpdatedDate = DateTime.Now;
                            if (Global.UserGUID != string.Empty)
                            {
                                kqxn.UpdatedBy = Guid.Parse(Global.UserGUID);
                            }
                            if (patient != null)
                            {
                                kqxn.PatientGUID = patient.PatientGUID;
                            }
                            kqxn.Status         = (byte)Status.Actived;
                            kqxn.MessageType    = testResult.KetQuaXetNghiem.MessageType;
                            kqxn.InstrumentType = testResult.KetQuaXetNghiem.InstrumentType;
                            kqxn.SerialNum      = testResult.KetQuaXetNghiem.SerialNum;
                            kqxn.SequenceNum    = testResult.KetQuaXetNghiem.SequenceNum;
                            kqxn.SpareField     = testResult.KetQuaXetNghiem.SpareField;
                            kqxn.SpecimenType   = testResult.KetQuaXetNghiem.SpecimenType;
                            kqxn.SpecimenID     = testResult.KetQuaXetNghiem.SpecimenID;
                            kqxn.SpecimenName   = testResult.KetQuaXetNghiem.SpecimenName;
                            kqxn.PatientID      = testResult.KetQuaXetNghiem.PatientID;
                            kqxn.SpecimenSex    = testResult.KetQuaXetNghiem.SpecimenSex;
                            kqxn.SpecimenDOB    = testResult.KetQuaXetNghiem.SpecimenDOB;
                            kqxn.DrName         = testResult.KetQuaXetNghiem.DrName;
                            kqxn.OperatorID     = testResult.KetQuaXetNghiem.OperatorID;
                            kqxn.NgayXN         = testResult.KetQuaXetNghiem.NgayXN;
                            kqxn.CollectionDate = testResult.KetQuaXetNghiem.CollectionDate;
                            kqxn.CollectionTime = testResult.KetQuaXetNghiem.CollectionTime;
                            kqxn.Comment        = testResult.KetQuaXetNghiem.Comment;
                            db.SubmitChanges();
                            ketQuaXetNghiemGUID = kqxn.KQXN_CellDyn3200GUID.ToString();
                        }

                        foreach (ChiTietKetQuaXetNghiem_CellDyn3200 r in testResult.ChiTietKetQuaXetNghiem)
                        {
                            ChiTietKetQuaXetNghiem_CellDyn3200 ctkqxn = db.ChiTietKetQuaXetNghiem_CellDyn3200s.SingleOrDefault <ChiTietKetQuaXetNghiem_CellDyn3200>(c => c.KQXN_CellDyn3200GUID.ToString() == ketQuaXetNghiemGUID &&
                                                                                                                                                                    c.TenXetNghiem.Trim().ToLower() == r.TenXetNghiem.Trim().ToLower());

                            if (ctkqxn == null) //Add New
                            {
                                r.ChiTietKQXN_CellDyn3200GUID = Guid.NewGuid();
                                r.KQXN_CellDyn3200GUID        = Guid.Parse(ketQuaXetNghiemGUID);
                                r.CreatedDate = DateTime.Now;
                                if (Global.UserGUID != string.Empty)
                                {
                                    r.CreatedBy = Guid.Parse(Global.UserGUID);
                                }
                                db.ChiTietKetQuaXetNghiem_CellDyn3200s.InsertOnSubmit(r);
                            }
                            else //Update
                            {
                                ctkqxn.UpdatedDate = DateTime.Now;
                                if (Global.UserGUID != string.Empty)
                                {
                                    ctkqxn.UpdatedBy = Guid.Parse(Global.UserGUID);
                                }

                                ctkqxn.TenXetNghiem = r.TenXetNghiem;
                                ctkqxn.TestResult   = r.TestResult;
                                ctkqxn.TestPercent  = r.TestPercent;
                                ctkqxn.TinhTrang    = (byte)TinhTrang.BinhThuong;
                                ctkqxn.Status       = (byte)Status.Actived;
                            }
                        }
                    }

                    db.SubmitChanges();
                    t.Complete();
                }
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                result.Error.Code        = (se.Message.IndexOf("Timeout expired") >= 0) ? ErrorCode.SQL_QUERY_TIMEOUT : ErrorCode.INVALID_SQL_STATEMENT;
                result.Error.Description = se.ToString();
            }
            catch (Exception e)
            {
                result.Error.Code        = ErrorCode.UNKNOWN_ERROR;
                result.Error.Description = e.ToString();
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                    db = null;
                }
            }

            return(result);
        }
        public static Result GetChiTietKetQuaXetNghiem(string ketQuaXetNghiemGUID)
        {
            Result result = new Result();

            try
            {
                string query = string.Format("SELECT CAST(0 AS Bit) AS Checked, *, CAST('' AS nvarchar(50)) AS BinhThuong FROM ChiTietKetQuaXetNghiem_CellDyn3200View WITH(NOLOCK) WHERE KQXN_CellDyn3200GUID = '{0}' AND Status = {1} ORDER BY GroupID, [Order]",
                                             ketQuaXetNghiemGUID, (byte)Status.Actived);

                result = ExcuteQuery(query);
                if (!result.IsOK)
                {
                    return(result);
                }

                MMOverride db = new MMOverride();
                DataTable  dt = result.QueryResult as DataTable;
                foreach (DataRow row in dt.Rows)
                {
                    double?fromValue = null;
                    double?toValue   = null;
                    //double? fromPercent = null;
                    //double? toPercent = null;
                    string donVi = string.Empty;

                    if ((row["FromValue2"] != null && row["FromValue2"] != DBNull.Value) ||
                        (row["ToValue2"] != null && row["ToValue2"] != DBNull.Value))
                    {
                        if (row["FromValue2"] != null && row["FromValue2"] != DBNull.Value)
                        {
                            fromValue = Convert.ToDouble(row["FromValue2"]);
                        }

                        if (row["ToValue2"] != null && row["ToValue2"] != DBNull.Value)
                        {
                            toValue = Convert.ToDouble(row["ToValue2"]);
                        }

                        //if (row["FromPercent2"] != null && row["FromPercent2"] != DBNull.Value)
                        //    fromPercent = Convert.ToDouble(row["FromPercent2"]);

                        //if (row["ToPercent2"] != null && row["ToPercent2"] != DBNull.Value)
                        //    toPercent = Convert.ToDouble(row["ToPercent2"]);

                        if (row["DonVi2"] != null && row["DonVi2"] != DBNull.Value)
                        {
                            donVi = row["DonVi2"].ToString().Trim();
                        }
                    }
                    else
                    {
                        string tenXetNghiem      = row["Fullname"].ToString();
                        XetNghiem_CellDyn3200 xn = db.XetNghiem_CellDyn3200s.SingleOrDefault <XetNghiem_CellDyn3200>(x => x.TenXetNghiem == tenXetNghiem);
                        if (xn == null)
                        {
                            continue;
                        }

                        if (xn.FromValue.HasValue)
                        {
                            fromValue = xn.FromValue.Value;
                        }

                        if (xn.ToValue.HasValue)
                        {
                            toValue = xn.ToValue.Value;
                        }

                        //if (xn.FromPercent.HasValue)
                        //    fromPercent = xn.FromPercent.Value;

                        //if (xn.ToPercent.HasValue)
                        //    toPercent = xn.ToPercent.Value;

                        if (xn.DonVi != null && xn.DonVi != string.Empty)
                        {
                            donVi = xn.DonVi;
                        }
                    }

                    double    testResult = Convert.ToDouble(row["TestResult"]);
                    TinhTrang tinhTrang  = TinhTrang.BinhThuong;

                    if (fromValue != null && toValue != null)
                    {
                        //if (fromPercent != null || toPercent != null)
                        //    row["BinhThuong"] = string.Format("({0:F2} - {1:F2})", fromValue.Value, toValue.Value);
                        //else
                        if (donVi != string.Empty)
                        {
                            row["BinhThuong"] = string.Format("({0:F2}-{1:F2} {2})", fromValue.Value, toValue.Value, donVi);
                        }
                        else
                        {
                            row["BinhThuong"] = string.Format("({0:F2}-{1:F2})", fromValue.Value, toValue.Value);
                        }

                        if (testResult < fromValue.Value || testResult > toValue.Value)
                        {
                            tinhTrang = TinhTrang.BatThuong;
                        }
                    }
                    else if (fromValue != null)
                    {
                        //if (fromPercent != null || toPercent != null)
                        //    row["BinhThuong"] = string.Format("(> {0:F2})", fromValue.Value);
                        //else
                        if (donVi != string.Empty)
                        {
                            row["BinhThuong"] = string.Format("(>{0:F2} {1})", fromValue.Value, donVi);
                        }
                        else
                        {
                            row["BinhThuong"] = string.Format("(>{0:F2})", fromValue.Value);
                        }

                        if (testResult <= fromValue.Value)
                        {
                            tinhTrang = TinhTrang.BatThuong;
                        }
                    }
                    else
                    {
                        //if (fromPercent != null || toPercent != null)
                        //    row["BinhThuong"] = string.Format("(< {0:F2})", toValue.Value);
                        //else
                        if (donVi != string.Empty)
                        {
                            row["BinhThuong"] = string.Format("(<{0:F2} {1})", toValue.Value, donVi);
                        }
                        else
                        {
                            row["BinhThuong"] = string.Format("(<{0:F2})", toValue.Value);
                        }

                        if (testResult >= toValue.Value)
                        {
                            tinhTrang = TinhTrang.BatThuong;
                        }
                    }

                    //if (fromPercent != null && toPercent != null)
                    //{
                    //    double testPercent = Convert.ToDouble(row["TestPercent"]);
                    //    row["Percent"] = string.Format("{0:F2}% ({1:F2} - {2:F2} {3})", testPercent, fromPercent.Value, toPercent.Value, donVi);

                    //    if (tinhTrang == TinhTrang.BinhThuong)
                    //    {
                    //        if (testPercent < fromPercent.Value || testPercent > toPercent.Value)
                    //            tinhTrang = TinhTrang.BatThuong;
                    //    }
                    //}
                    //else if (fromPercent != null)
                    //{
                    //    double testPercent = Convert.ToDouble(row["TestPercent"]);
                    //    row["Percent"] = string.Format("{0:F2}% (> {1:F2} {2})", testPercent, fromPercent.Value, donVi);

                    //    if (tinhTrang == TinhTrang.BinhThuong)
                    //    {
                    //        if (testPercent <= fromPercent.Value)
                    //            tinhTrang = TinhTrang.BatThuong;
                    //    }
                    //}
                    //else if (toPercent != null)
                    //{
                    //    double testPercent = Convert.ToDouble(row["TestPercent"]);
                    //    row["Percent"] = string.Format("{0:F2}% (< {1:F2} {2})", testPercent, toPercent.Value, donVi);

                    //    if (tinhTrang == TinhTrang.BinhThuong)
                    //    {
                    //        if (testPercent >= toPercent.Value)
                    //            tinhTrang = TinhTrang.BatThuong;
                    //    }
                    //}

                    row["TinhTrang"] = (byte)tinhTrang;

                    ChiTietKetQuaXetNghiem_CellDyn3200 ctkqxn = db.ChiTietKetQuaXetNghiem_CellDyn3200s.SingleOrDefault <ChiTietKetQuaXetNghiem_CellDyn3200>(c => c.ChiTietKQXN_CellDyn3200GUID.ToString() == row["ChiTietKQXN_CellDyn3200GUID"].ToString());
                    if (ctkqxn != null)
                    {
                        ctkqxn.FromValue = fromValue;
                        ctkqxn.ToValue   = toValue;
                        //ctkqxn.FromPercent = fromPercent;
                        //ctkqxn.ToPercent = toPercent;
                        ctkqxn.DonVi = donVi;

                        if (ctkqxn.FromValue != null && ctkqxn.FromValue.HasValue)
                        {
                            row["FromValue"] = ctkqxn.FromValue.Value;
                        }

                        if (ctkqxn.ToValue != null && ctkqxn.ToValue.HasValue)
                        {
                            row["ToValue"] = ctkqxn.ToValue.Value;
                        }

                        //if (ctkqxn.FromPercent != null && ctkqxn.FromValue.HasValue)
                        //    row["FromPercent"] = ctkqxn.FromPercent.Value;

                        //if (ctkqxn.ToPercent != null && ctkqxn.ToValue.HasValue)
                        //    row["ToPercent"] = ctkqxn.ToPercent.Value;

                        //row["DoiTuong"] = ctkqxn.DoiTuong;
                        row["DonVi"] = ctkqxn.DonVi;
                    }
                }

                db.SubmitChanges();
                db.Dispose();
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                result.Error.Code        = (se.Message.IndexOf("Timeout expired") >= 0) ? ErrorCode.SQL_QUERY_TIMEOUT : ErrorCode.INVALID_SQL_STATEMENT;
                result.Error.Description = se.ToString();
            }
            catch (Exception e)
            {
                result.Error.Code        = ErrorCode.UNKNOWN_ERROR;
                result.Error.Description = e.ToString();
            }

            return(result);
        }