/*将CaseInfo对应的Entity翻译为数据契约*/
 private void TranslateCaseInfoEntityToCaseInfoContractData(
     CaseInfoEntity caseInfoEntity,
     CaseInfo caseInfo) {
     caseInfo.ErrorMessage       = caseInfoEntity.ErrorMessage;
     if (caseInfoEntity.CaseID != null) {
         caseInfo.CaseID = caseInfoEntity.CaseID.ToString();
     }
     if (caseInfoEntity.ExaminationID != null) {
         caseInfo.ExaminationID = caseInfoEntity.ExaminationID.ToString();
     }
     if (caseInfoEntity.PrescriptionID != null) {
         caseInfo.PrescriptionID = caseInfoEntity.PrescriptionID.ToString();
     }
     caseInfo.UserID             = caseInfoEntity.UserID;
     caseInfo.DoctorID           = caseInfoEntity.DoctorID;
     caseInfo.SectionID          = caseInfoEntity.SectionID;
     caseInfo.Date               = caseInfoEntity.Date;
     caseInfo.ChiefComplaint     = caseInfoEntity.ChiefComplaint;
     caseInfo.TentativeDiagnosis = caseInfoEntity.TentativeDiagnosis;
     caseInfo.DifferentialDiagnosis = caseInfoEntity.DifferentialDiagnosis;
     caseInfo.TreatmentPlan      = caseInfoEntity.TreatmentPlan;
     caseInfo.CountercheckDate   = caseInfoEntity.CountercheckDate;
     caseInfo.IsDraft            = caseInfoEntity.IsDraft;
 }
 /*修改病历*/
 public CaseInfo ModifyCase(CaseInfo newCase) {
     if (newCase == null) {
         CaseInfo newCaseInfo = new CaseInfo();
         newCaseInfo.ErrorMessage = "Empty Case Info! @Service";
         return newCaseInfo;
     }
     else {
         newCase.ErrorMessage = null;
         CaseInfoEntity caseInfoEntity = new CaseInfoEntity();
         TranslateCaseInfoToCaseInfoEntityContractData(newCase, caseInfoEntity);
         if (caseInfoEntity.ErrorMessage != null) {
             newCase.ErrorMessage = caseInfoEntity.ErrorMessage;
         }
         else {
             CaseInfoEntity newCaseInfoEntity = doctorLogic.ModifyCase(caseInfoEntity);
             newCase = new CaseInfo();
             TranslateCaseInfoEntityToCaseInfoContractData(newCaseInfoEntity, newCase);
         }
         return newCase;
     }
 }
 /*创建病历并针对复查日期创建message*/
 public CaseInfo CreateCase(CaseInfo caseInfo) {
     if (caseInfo == null) {
         CaseInfo newCaseInfo = new CaseInfo();
         newCaseInfo.ErrorMessage = "Empty Case Info! @Service";
         return newCaseInfo;
     }
     else {
         caseInfo.ErrorMessage = null;
         CaseInfoEntity caseInfoEntity = new CaseInfoEntity();
         TranslateCaseInfoToCaseInfoEntityContractData(caseInfo, caseInfoEntity);
         if (caseInfoEntity.ErrorMessage != null) {
             caseInfo.ErrorMessage = caseInfoEntity.ErrorMessage;
         }
         else {
             CaseInfoEntity newCaseInfoEntity = doctorLogic.CreateCase(caseInfoEntity);
             caseInfo = new CaseInfo();
             TranslateCaseInfoEntityToCaseInfoContractData(newCaseInfoEntity, caseInfo);
         }
         return caseInfo;
     }
 }
 /*将CaseInfo对应的数据契约翻译为Entity*/
 private void TranslateCaseInfoToCaseInfoEntityContractData(
     CaseInfo        caseInfo,
     CaseInfoEntity  caseInfoEntity) {
     caseInfoEntity.ErrorMessage         = caseInfo.ErrorMessage;
     if (caseInfo.CaseID != null) {
         try {
             caseInfoEntity.CaseID = new Guid(caseInfo.CaseID);
         }
         catch {
             caseInfoEntity.ErrorMessage = "Invalid CaseID! @Service";
         }
     }
     if (caseInfo.ExaminationID != null) {
          try {
              caseInfoEntity.ExaminationID = new Guid(caseInfo.ExaminationID);
         }
         catch {
             caseInfoEntity.ErrorMessage = "Invalid ExaminationID! @Service";
         }
     }
     if (caseInfo.PrescriptionID != null) {
         try {
             caseInfoEntity.PrescriptionID = new Guid(caseInfo.PrescriptionID);
         }
         catch {
             caseInfoEntity.ErrorMessage = "Invalid PrescriptionID! @Service";
         }
     }
     caseInfoEntity.UserID               = caseInfo.UserID;
     caseInfoEntity.DoctorID             = caseInfo.DoctorID;
     caseInfoEntity.SectionID            = caseInfo.SectionID;
     caseInfoEntity.Date                 = caseInfo.Date;
     caseInfoEntity.ChiefComplaint       = caseInfo.ChiefComplaint;
     caseInfoEntity.TentativeDiagnosis   = caseInfo.TentativeDiagnosis;
     caseInfoEntity.DifferentialDiagnosis = caseInfo.DifferentialDiagnosis;
     caseInfoEntity.TreatmentPlan        = caseInfo.TreatmentPlan;
     caseInfoEntity.CountercheckDate     = caseInfo.CountercheckDate;
     caseInfoEntity.IsDraft              = caseInfo.IsDraft;
 }
        /*修改病历*/
        public CaseInfoEntity ModifyCase(CaseInfoEntity newCase) {
            CaseHistory oldCase = new CaseHistory();
            CaseInfoEntity modifiedCase = new CaseInfoEntity();

            if (newCase.CaseID == null) {
                modifiedCase.ErrorMessage = "Case GUID Missing! @Data";
                return modifiedCase;
            }

            DrPEDatabaseEntities DEntities = new DrPEDatabaseEntities();
            oldCase = (from c in DEntities.CaseHistories
                       where ((c.CaseID == newCase.CaseID) && (c.DoctorID == newCase.DoctorID))
                       select c).FirstOrDefault();
            if (newCase == null) {
                modifiedCase.ErrorMessage = "Invalid Case GUID! @Data";
                return modifiedCase;
            }
            if (oldCase.ModifiedDate != null) {
                modifiedCase.ErrorMessage = "Modification Denied! @Data";
                return modifiedCase;
            }

            if (oldCase.ExaminationID == null) {
                oldCase.ExaminationID = newCase.ExaminationID;
            }
            if (oldCase.PrescriptionID == null) {
                oldCase.PrescriptionID = newCase.PrescriptionID;
            }

            if (oldCase.ChiefComplaint == null) {
                oldCase.ChiefComplaint = newCase.ChiefComplaint;
            }
            if (oldCase.TentativeDiagnosis == null) {
                oldCase.TentativeDiagnosis = newCase.TentativeDiagnosis;
            }
            if (oldCase.DifferentialDiagnosis == null) {
                oldCase.DifferentialDiagnosis = newCase.DifferentialDiagnosis;
            }
            if (oldCase.TreatmentPlan == null) {
                oldCase.TreatmentPlan = newCase.TreatmentPlan;
            }

            bool IsSent = oldCase.CountercheckDate.HasValue;
            if (IsSent == false) {
                oldCase.CountercheckDate = newCase.CountercheckDate;
            }

            if (newCase.IsDraft == false) {
                oldCase.ModifiedDate = DateTime.Now;
            }

            try {
                DEntities.SaveChanges();
            }
            catch {
                modifiedCase.ErrorMessage = "Invalid Case! @Data";
                return modifiedCase;
            }

            if ((IsSent == false) && (oldCase.CountercheckDate.HasValue)) {
                Doctor doctor = (from d in DEntities.Doctors
                                 where d.DoctorID == oldCase.DoctorID
                                 select d).FirstOrDefault();
                Section section = (from s in DEntities.Sections
                                   where s.SectionID == doctor.SectionID
                                   select s).FirstOrDefault();
                User user = (from u in DEntities.Users
                             where u.UserID == oldCase.UserID
                             select u).FirstOrDefault();
                DateTime lastVisit = (DateTime)oldCase.CreatedDate;
                DateTime countercheckDate = (DateTime)oldCase.CountercheckDate;
                DateTime currentTime = DateTime.Now;

                Message message = new Message();
                message.MessageID = Guid.NewGuid();
                message.Sender = section.HospitalID;
                message.Receiver = newCase.UserID;
                message.Date = countercheckDate.AddDays(-3).AddMinutes(currentTime.Minute).AddSeconds(currentTime.Second).AddMilliseconds(currentTime.Millisecond);
                message.Type = "H2U";
                message.Text = String.Format(
                    "Dear {0}.{1} {2},\nDuring your visit on {3}, Dr.{4} {5} ({6}) suggested you pay another visit on {7}. It might be a good idea to make an appointment before it's too late.\nSincerely,\nDr.PE",
                    (user.Gender.ToLower() == "female") ? "Ms" : "Mr",
                    user.LastName, user.FirstName,
                    lastVisit.ToLongDateString(),
                    doctor.LastName, doctor.FirstName,
                    section.Name,
                    countercheckDate.ToLongDateString());

                try {
                    DEntities.Messages.AddObject(message);
                    DEntities.SaveChanges();
                }
                catch {
                    modifiedCase.ErrorMessage = "Can't Create Appointment Inform! @Data";
                    return modifiedCase;
                }
            }

            modifiedCase.CaseID = oldCase.CaseID;
            modifiedCase.ExaminationID = oldCase.ExaminationID;
            modifiedCase.PrescriptionID = oldCase.PrescriptionID;
            modifiedCase.UserID = oldCase.UserID;
            modifiedCase.DoctorID = oldCase.DoctorID;
            modifiedCase.SectionID = oldCase.SectionID;
            modifiedCase.Date = oldCase.CreatedDate;
            modifiedCase.ChiefComplaint = oldCase.ChiefComplaint;
            modifiedCase.TentativeDiagnosis = oldCase.TentativeDiagnosis;
            modifiedCase.DifferentialDiagnosis = oldCase.DifferentialDiagnosis;
            modifiedCase.TreatmentPlan = oldCase.TreatmentPlan;
            modifiedCase.CountercheckDate = oldCase.CountercheckDate;

            return modifiedCase;
        }
        /*获取指定病历的信息*/
        public CaseInfoEntity GetCaseInfo(Guid caseID, bool showICD) {

            CaseInfoEntity caseInfoEntity = new CaseInfoEntity();
            DrPEDatabaseEntities DEntities = new DrPEDatabaseEntities();

            /*根据编号查找该病历*/
            CaseHistory caseInfo = (from c in DEntities.CaseHistories
                                    where c.CaseID == caseID
                                    select c).FirstOrDefault();
            if (caseInfo == null) {
                caseInfoEntity.ErrorMessage = "Invalid Case GUID! @Data";
                return caseInfoEntity;
            }

            /*复制病历部分信息*/
            caseInfoEntity.CaseID = caseInfo.CaseID;
            caseInfoEntity.ExaminationID = caseInfo.ExaminationID;
            caseInfoEntity.PrescriptionID = caseInfo.PrescriptionID;
            caseInfoEntity.UserID = caseInfo.UserID;
            caseInfoEntity.DoctorID = caseInfo.DoctorID;
            caseInfoEntity.SectionID = caseInfo.SectionID;
            caseInfoEntity.CountercheckDate = caseInfo.CountercheckDate;

            /*判断病历是否完成*/
            caseInfoEntity.Date = caseInfo.ModifiedDate;
            if (caseInfoEntity.Date == null) {
                caseInfoEntity.IsDraft = true;
            }
            else {
                caseInfoEntity.IsDraft = false;
            }

            /*针对ICD编码进行转写*/
            if (showICD == false) {
                caseInfoEntity.ChiefComplaint = TranslateICD(caseInfo.ChiefComplaint);
                caseInfoEntity.TentativeDiagnosis = TranslateICD(caseInfo.TentativeDiagnosis);
                caseInfoEntity.DifferentialDiagnosis = TranslateICD(caseInfo.DifferentialDiagnosis);
                caseInfoEntity.TreatmentPlan = TranslateICD(caseInfo.TreatmentPlan);
            }
            else {
                caseInfoEntity.ChiefComplaint = caseInfo.ChiefComplaint;
                caseInfoEntity.TentativeDiagnosis = caseInfo.TentativeDiagnosis;
                caseInfoEntity.DifferentialDiagnosis = caseInfo.DifferentialDiagnosis;
                caseInfoEntity.TreatmentPlan = caseInfo.TreatmentPlan;
            }

            return caseInfoEntity;
        }
        /*创建病历并针对复查日期创建message*/
        public CaseInfoEntity CreateCase(CaseInfoEntity caseInfoEntity) {

            DrPEDatabaseEntities DEntities = new DrPEDatabaseEntities();
            CaseHistory newCase = new CaseHistory();
            CaseInfoEntity addedCase = new CaseInfoEntity();

            newCase.CaseID = Guid.NewGuid();
            newCase.ExaminationID = caseInfoEntity.ExaminationID;
            newCase.PrescriptionID = caseInfoEntity.PrescriptionID;
            newCase.UserID = caseInfoEntity.UserID;
            newCase.DoctorID = caseInfoEntity.DoctorID;

            Doctor doctor = (from d in DEntities.Doctors
                             where d.DoctorID == newCase.DoctorID
                             select d).FirstOrDefault();
            if (doctor == null) {
                addedCase.ErrorMessage = "Invalid DoctorID! @Data";
                return addedCase;
            }
            else {
                newCase.SectionID = doctor.SectionID;
            }

            newCase.CreatedDate = DateTime.Now;
            if (caseInfoEntity.IsDraft == false) {
                newCase.ModifiedDate = newCase.CreatedDate;
            }

            newCase.ChiefComplaint = caseInfoEntity.ChiefComplaint;
            newCase.TentativeDiagnosis = caseInfoEntity.TentativeDiagnosis;
            newCase.DifferentialDiagnosis = caseInfoEntity.DifferentialDiagnosis;
            newCase.TreatmentPlan = caseInfoEntity.TreatmentPlan;
            newCase.CountercheckDate = caseInfoEntity.CountercheckDate;

            try {
                DEntities.CaseHistories.AddObject(newCase);
                DEntities.SaveChanges();
            }
            catch {
                addedCase.ErrorMessage = "Invalid Case! @Data";
                return addedCase;
            }

            if (caseInfoEntity.CountercheckDate != null) {
                Section section = (from s in DEntities.Sections
                                   where s.SectionID == doctor.SectionID
                                   select s).FirstOrDefault();
                User user = (from u in DEntities.Users
                             where u.UserID == caseInfoEntity.UserID
                             select u).FirstOrDefault();
                DateTime lastVisit = (DateTime)newCase.CreatedDate;
                DateTime countercheckDate = (DateTime)newCase.CountercheckDate;
                DateTime currentTime = DateTime.Now;

                Message message = new Message();
                message.MessageID = Guid.NewGuid();
                message.Sender = section.HospitalID;
                message.Receiver = newCase.UserID;
                message.Date = countercheckDate.AddDays(-3).AddMinutes(currentTime.Minute).AddSeconds(currentTime.Second).AddMilliseconds(currentTime.Millisecond);
                message.Type = "H2U";
                message.Text = String.Format(
                    "Dear {0}.{1} {2},\nDuring your visit on {3}, Dr.{4} {5} ({6}) suggested you pay another visit on {7}. It might be a good idea to make an appointment before it's too late.\nSincerely,\nDr.PE",
                    (user.Gender.ToLower() == "female") ? "Ms" : "Mr",
                    user.LastName, user.FirstName,
                    lastVisit.ToLongDateString(),
                    doctor.LastName, doctor.FirstName,
                    section.Name,
                    countercheckDate.ToLongDateString());

                try {
                    DEntities.Messages.AddObject(message);
                    DEntities.SaveChanges();
                }
                catch {
                    addedCase.ErrorMessage = "Can't Create Appointment Inform! @Data";
                    return addedCase;
                }
            }

            addedCase.CaseID = newCase.CaseID;
            addedCase.ExaminationID = newCase.ExaminationID;
            addedCase.PrescriptionID = newCase.PrescriptionID;
            addedCase.UserID = newCase.UserID;
            addedCase.DoctorID = newCase.DoctorID;
            addedCase.SectionID = newCase.SectionID;
            addedCase.Date = newCase.CreatedDate;
            addedCase.ChiefComplaint = newCase.ChiefComplaint;
            addedCase.TentativeDiagnosis = newCase.TentativeDiagnosis;
            addedCase.DifferentialDiagnosis = newCase.DifferentialDiagnosis;
            addedCase.TreatmentPlan = newCase.TreatmentPlan;
            addedCase.CountercheckDate = newCase.CountercheckDate;
            if (newCase.ModifiedDate == null) {
                addedCase.IsDraft = true;
            }
            else {
                addedCase.IsDraft = false;
            }

            return addedCase;
        }
 /*获取指定病历的信息*/
 public CaseInfoEntity GetCaseInfo(Guid caseID, bool showICD) {
     if (confirmed == false) {
         CaseInfoEntity caseInfoEntity = new CaseInfoEntity();
         caseInfoEntity.ErrorMessage = "xxx Not Logged in Yet! @Logic";
         return caseInfoEntity;
     }
     else {
         return doctorDAO.GetCaseInfo(caseID, showICD);
     }
 }
 /*修改病历*/
 public CaseInfoEntity ModifyCase(CaseInfoEntity newCase) {
     if (confirmed == false) {
         CaseInfoEntity newCaseInfoEntity = new CaseInfoEntity();
         newCaseInfoEntity.ErrorMessage = "XXX Not Logged in Yet! @Logic";
         return newCaseInfoEntity;
     }
     else {
         newCase.DoctorID = confirmedDoctorID;
         return doctorDAO.ModifyCase(newCase);
     }
 }
 /*创建病历并针对复查日期创建message*/
 public CaseInfoEntity CreateCase(CaseInfoEntity caseInfoEntity) {
     if (confirmed == false) {
         CaseInfoEntity newCaseInfoEntity = new CaseInfoEntity();
         newCaseInfoEntity.ErrorMessage = "XXX Not Logged in Yet! @Logic";
         return newCaseInfoEntity;
     }
     else {
         //caseInfoEntity.ErrorMessage = null;
         caseInfoEntity.DoctorID = confirmedDoctorID;
         return doctorDAO.CreateCase(caseInfoEntity);
     }
 }
 /*获取最近一次病历*/
 public CaseInfoEntity GetLastCaseInfo(bool showICD) {
     if (confirmed == false) {
         CaseInfoEntity caseInfoEntity = new CaseInfoEntity();
         caseInfoEntity.ErrorMessage = "xxx Not Logged in Yet! @Logic";
         return caseInfoEntity;
     }
     else {
         return userDAO.GetLastCaseInfo(confirmedUserID, showICD);
     }
 }