using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using AIMSModel;
using HelperDB;
using DrawGraph;
using AIMSExtension;
namespace AIMSBLL
{
    public class BOperationReview
    {
        public static DataTable GetOperationReviewDataTable(string MdrecNo, string PatientName, string OperationBeginTime, string OperationEndTime)
        {
            string strSql = " SELECT of1.PatientId, of1.ApplyId, of1.MdrecNo, of1.ArchivesNo, of1.PatientName,of1.ApplyDepName, of1.PatientKind," +
                            " of1.Sex, of1.BirthDay, of1.Height, of1.[Weight], of1.BloodType," +
                            " of1.RHBloodType, of1.Illdistrict, of1.SickBed, of1.OperationType," +
                            " of1.[State],od.DiagnoseInfoName, od.OperationInfoName," +
                            " od.OperationBeginTime,od.OperationEndTime," +
                            " od.AnaesthesiaMethodName, od.OperationCutTypeName," +
                            " od.OperationBodyPositionName, od.OperationPositionName," +
                            " od.OperationDoctor, od.Assistant, od.AnesthesiaDoctor," +
                            " od.Nurse, od.Remarks,of1.MedicalRecord,of1.MedicalRecordTime  FROM V_OperationDoing od" +
                            "  LEFT JOIN V_OperationFront of1 ON of1.PatientId = od.PatientId WHERE (of1.MdrecNo ='" + MdrecNo + "' OR of1.PatientName LIKE '%" + PatientName + "%')" +
                            " AND od.OperationBeginTime >='" + OperationBeginTime + "' AND  od.OperationBeginTime <'" + OperationEndTime + "' AND " +
                            "   od.[State]  IN ('手术结束','麻醉复苏结束')";
            return HelperDB.DbHelperSQL.GetDataTable(strSql.ToString());
        }
        public static DataTable GetOperationPlanDataTable(DateTime BeginDate, DateTime EndDate, string DepName)
        {
            string strSql = "SELECT of1.ApplyId,OperationRoom,PlanOrder ,OrderOperationTime  ,  of1.ApplyDepName,of1.SickBed, of1.MdrecNo, of1.PatientName, of1.Sex, of1.Age,of1.ApplyDiagnoseInfoName, of1.ApplyOperationInfoName,of1.OperationDoctor, of1.Assistant1 Assistant, " +
 " of1.AnaesthesiaMethodName , of1.AnesthesiaDoctor ,  of1.InstrumentNurse  , of1.TourNurse  , of1.Remark,of1.OperationType" +
                            " FROM V_OperationFront of1 WHERE of1.PlanOperationTime>='" + BeginDate + "' AND of1.PlanOperationTime<'" + EndDate + "'  Order By OperationRoomID,PlanOrder";
            return HelperDB.DbHelperSQL.GetDataTable(strSql);
        }
        public static DataTable GetOperationPlanDataTable(string ApplyId)
        {
            string strSql = "SELECT of1.ApplyId,OperationRoom,PlanOrder ,OrderOperationTime  ,  of1.ApplyDepName,of1.SickBed, of1.MdrecNo, of1.PatientName, of1.Sex, of1.Age,of1.ApplyDiagnoseInfoName, of1.ApplyOperationInfoName,of1.OperationDoctor, of1.Assistant1 Assistant, " +
 " of1.AnaesthesiaMethodName , of1.AnesthesiaDoctor ,  of1.InstrumentNurse  , of1.TourNurse  , of1.Remark ,of1.OperationType,of1.OperationSite " +
                            " FROM V_OperationFront of1 WHERE " + ApplyId + " ";
            return HelperDB.DbHelperSQL.GetDataTable(strSql);
        }
        public static void GetASALevel(int PatientId, OperationRecord _record)
        {
            string ASALevel = GetDocumentXmlStatic("麻醉术前访视记录单", PatientId, "ASA分级");
            if (ASALevel != "")
            {
                BasicDictionary dict = BBasicDictionary.SelectSingle(" ParentId=30 and Name='" + ASALevel + "'", null);
                if (dict != null)
                {
                    _record.ASALevel = dict.Id.Value;
                    BOperationRecord.Update(" ASALevel=@ASALevel where Id=@Id", new ParameterList("@ASALevel", _record.ASALevel, "@Id", _record.Id.Value));
                }
            }
        }
        public static void GetASALevel(int PatientId, OperationRecord _record,string ASALevel  )
        { 
            if (ASALevel != "")
            {
                ASALevel = ASALevel.Replace("级", "");
                BasicDictionary dict = BBasicDictionary.SelectSingle(" ParentId=30 and Name='" + ASALevel.Trim() + "'", null);
                if (dict != null)
                {
                    _record.ASALevel = dict.Id.Value;
                    BOperationRecord.Update(" ASALevel=@ASALevel where Id=@Id", new ParameterList("@ASALevel", _record.ASALevel, "@Id", _record.Id.Value));
                }
            }
        }
        /// 
        /// 根据模板名称查询文档
        /// 
        /// 模板名称
        /// 患者Id
        /// 
        public static string GetDocumentbyName2(string tempName, int patientId)
        {
            string XmlStatic = "";
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select ");
                sb.Append(" pd.Id,");
                sb.Append(" pd.PatientId,");
                sb.Append(" pd.TemplateId,");
                sb.Append(" pd.XmlFileName,");
                sb.Append(" pd.XmlFile,");
                sb.Append(" pd.XmlStatic,");
                sb.Append(" pd.IsValid,");
                sb.Append(" pd.OperatorNo,");
                sb.Append(" pd.OperatorDate");
                sb.Append(" from PrintDocument pd");
                sb.Append(" where ");
                sb.Append(" pd.PatientId = " + patientId);
                sb.Append(" and pd.XmlFileName = '" + tempName + "'");
                sb.Append(" and pd.IsValid = 1");
                DataTable dt = DbHelperSQL.GetDataTable(sb.ToString());
                if (dt.Rows.Count > 0)
                {
                    XmlStatic = dt.Rows[0]["XmlStatic"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return XmlStatic;
        }
        public static XmlDocument GetPrintDocumentXml(string tempName, int patientId)
        {
            string XmlStatic = GetDocumentbyName2(tempName, patientId);
            XmlDocument doc = new XmlDocument();
            if (XmlStatic != null && XmlStatic.Length > 0)
            {
                doc.LoadXml(XmlStatic);
            }
            return doc;
        }
        public static string GetDocumentXmlStatic(string tempName, int patientId, string dictName)
        {
            string Value = "";
            XmlDocument doc = GetPrintDocumentXml(tempName, patientId);
            XmlElement root = doc.DocumentElement;
            if (root != null)
            {
                foreach (XmlNode row in root.ChildNodes)
                {
                    if (row.Attributes["Name"].InnerText == dictName)
                        Value = row.InnerText.Trim();//测试方法
                }
            }
            return Value;
        }
        public static void SetDocumentXmlStatic(string tempName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select ");
            sb.Append(" pd.Id,");
            sb.Append(" pd.XmlFileName,");
            sb.Append(" pd.XmlFile,");
            sb.Append(" pd.XmlStatic,");
            sb.Append(" pd.IsValid,");
            sb.Append(" pd.OperatorNo,");
            sb.Append(" pd.OperatorDate");
            sb.Append(" from PrintDocument pd");
            sb.Append(" where pd.XmlFileName = '" + tempName + "'");
            DataTable dt = DbHelperSQL.GetDataTable(sb.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            { 
                string XmlStatic = dt.Rows[i]["XmlStatic"].ToString();
                XmlDocument doc = new XmlDocument();
                if (XmlStatic != null && XmlStatic.Length > 0)
                {
                    doc.LoadXml(XmlStatic);
                }
                XmlElement root = doc.DocumentElement;
                if (root != null)
                {
                    root.ChildNodes[root.ChildNodes.Count - 7].Attributes["Name"].InnerText = "总计";
                    UpdateTemplate(dt.Rows[i]["Id"].ToString(), doc.InnerXml.ToString());
                }
            }
        }
        public static void UpdateTemplate(string Id, string XmlStatic)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update [PrintDocument] set");
                strSql.Append(" XmlStatic='" + XmlStatic + "' ");
                strSql.Append(" where Id=" + Id + " ");
                DbHelperSQL.ExecNonQuery(strSql.ToString());
            }
            catch (Exception ex)
            {
                PublicMethod.WriteLog(ex);
            }
        }
    }
}