示例#1
0
        /* ------------------------------------------------------------------------
         *  Return 0  Successful
         *        -1 fatal error
         *        -2 no continue operation
         *        -3 read only
         * ------------------------------------------------------------------------- */
        public int Create(string registryID, string archive)
        {
            int returnState = 0;

            archiveNum = archive;

            #region In offline
            if (Globals.offline)
            {
                string tmpfile = DocFile(registryID);
                emrDoc.Load(tmpfile);
                File.Delete(tmpfile);
                return(returnState);
            }
            #endregion

            using (gjtEmrService.emrServiceXml es = new gjtEmrService.emrServiceXml())
            {
                #region Get emrDocument and emrNotes
                XmlNode node     = emrDoc.DocumentElement;
                XmlNode emrNotes = emrDoc.CreateElement(EmrConstant.ElementNames.EmrNotes);
                Boolean ret;
                try
                {
                    string machineName = Globals.localMachineName;
                    string machineIP   = Globals.localMachineIP;
                    string msg         = es.SetTrafficLightToRed(registryID, ref machineIP, ref machineName);
                    if (msg != null)
                    {
                        if (machineIP.Trim() != Globals.localMachineIP.Trim())
                        {
                            string error = "病历被他人打开编辑中,一般不要同时操作同一病历\r\rIP地址:"
                                           + machineIP + "\r\r主机名:" + machineName;
                            if (!ThisAddIn.CanOption(ElementNames.ContinueAsEmrOpened))
                            {
                                error += "\r\r进入只读状态!!!";
                                MessageBox.Show(error, ErrorMessage.Warning);
                                returnState = -3;
                            }
                        }
                    }
                    ret = es.GetEmrDocument(registryID, ref node, ref emrNotes);
                }
                catch (Exception ex)
                {
                    Globals.logAdapter.Record("EX741852967", ex.Message + ">>" + ex.ToString(), true);

                    return(-1);
                }

                if (ret == EmrConstant.Return.Failed)
                {
                    MessageBox.Show(ErrorMessage.WebServiceError, ErrorMessage.Error);
                    return(-1);
                }
                #endregion

                #region Check if local documents are newer than in database
                if (Globals.autoCheckSync && !ThisAddIn.CanOption(ElementNames.ContinueAsEmrOpened))
                {
                    DateTime localLwt = DateTime.Now;
                    if (ThisAddIn.HasNotesInLocal(registryID, ref localLwt))
                    {
                        long lastWriteTime = 0;
                        if (node.Attributes[AttributeNames.LastWriteTime] != null)
                        {
                            lastWriteTime = Convert.ToInt64(node.Attributes[AttributeNames.LastWriteTime].Value);
                        }
                        DateTime remoteLwt = DateTime.FromFileTime(lastWriteTime);
                        if (localLwt.CompareTo(remoteLwt) > 0)
                        {
                            DialogResult dr = MessageBox.Show("本地病历记录比数据库的更新:\r\r本地:" + localLwt.ToString() +
                                                              "  数据库:" + remoteLwt.ToString() +
                                                              "\r\r同步为本地病历吗?\r如果选择同步应十分小心,要确认本地病历确实是最新的。" +
                                                              "\r因为有时因各机器时钟不一致,可能造成假象。一旦同步成功,不能恢复!",
                                                              ErrorMessage.Warning, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
                                                              MessageBoxDefaultButton.Button1);
                            if (dr == DialogResult.Yes)
                            {
                                #region Do synchronization
                                string msg = ThisAddIn.SynchronizeOne(registryID, archiveNum, node);
                                if (msg == null)
                                {
                                    emrDocFile = udt.MakeEmrDocumentFileName(registryID, Globals.workFolder);
                                    string tmpfile = Path.Combine(Globals.currentDirectory, ResourceName.Mytmp);
                                    if (File.Exists(tmpfile))
                                    {
                                        File.Delete(tmpfile);
                                    }
                                    udt.jj.DecodeEmrDocument(emrDocFile, tmpfile);
                                    emrDoc.Load(tmpfile);
                                    return(returnState);
                                }
                                MessageBox.Show(msg, ErrorMessage.Error);
                                return(-1);

                                #endregion
                            }
                            else if (dr == DialogResult.Cancel)
                            {
                                return(-2);
                            }
                        }
                    }
                }
                #endregion

                #region Create local storage for documents.
                string docLocation = ThisAddIn.GetDocLocation(registryID);
                foreach (XmlNode emrNote in emrNotes)
                {
                    string noteDoc =
                        udt.MakeWdDocumentFileName(docLocation, emrNote.Attributes[0].Value);
                    udt.jj.StringToWordDocument(noteDoc, emrNote);

                    if (emrNote.InnerText.Length >= 2 && emrNote.InnerText.Substring(0, 2) == "UE")
                    {
                        if (Globals.localDocumentEncode)
                        {
                            udt.jj.EncodeEmrDocument(noteDoc);
                        }
                    }
                    else
                    {
                        if (!Globals.localDocumentEncode)
                        {
                            string tmpfile = Path.Combine(Globals.currentDirectory, ResourceName.Mytmp);
                            udt.jj.DecodeEmrDocument(noteDoc, tmpfile);
                            File.Delete(noteDoc);
                            File.Move(tmpfile, noteDoc);
                        }
                    }
                }
                emrDoc.LoadXml(node.OuterXml);
                emrDocFile = udt.MakeEmrDocumentFileName(registryID, Globals.workFolder);
                emrDoc.Save(emrDocFile);
                udt.jj.EncodeEmrDocument(emrDocFile);
                return(returnState);

                #endregion
            }
        }