using System;
using AIMSDAL;
using AIMSModel;
using AIMSObjectQuery;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using HelperDB;
using AIMSExtension;
using System.Data.SqlClient;
using System.Reflection;
using System.Windows.Forms;
using DrawGraph;
namespace AIMSBLL
{
    public partial class BOperationRecord
    {
        public static int AddTitleTime(int PatientId, int ApplyId, int RecoverId, int State, DateTime Time, int RoomId)
        {
            return DOperationRecord.AddTitleTime(PatientId, ApplyId, RecoverId, State, Time, RoomId);
        }
        public static OperationRecord getRecord(OperationRecord _record, int PatientId, int RecoverId)
        {
            if (_record == null) _record = new OperationRecord();
            if (PatientId == -1)
            {
                return _record;
            }
            BOperationRecord.SelectSingle(PatientId, RecoverId, ref _record);
            _record.InstrumentList = BOperationRecordInstrumentList.SelectSingle(" OperationRecordId=" + Convert.ToInt32(_record.Id), null);
            if (_record.InstrumentList != null)
            {
                _record.InstrumentList.ApplianceRecordList = BApplianceRecord.GetApplianceRecordById(Convert.ToInt32(_record.Id), Convert.ToInt32(_record.InstrumentList.Id));
            }
            else
            {
                _record.InstrumentList = new OperationRecordInstrumentList();
                _record.InstrumentList.OperationRecordId = _record.Id;
                _record.InstrumentList.OperatorNo = PublicMethod.OperatorNo;
                _record.InstrumentList.OperatorName = PublicMethod.OperatorName;
                _record.InstrumentList.Id = BOperationRecordInstrumentList.Insert(_record.InstrumentList);
            }
            if (_record.InRoomTime != null)
                _record.OperationDate = _record.InRoomTime.Value.ToShortDateString();
            DataTable OperationFrontdt = BOperationApply.GetOperationFrontDataTableByPatientId(PatientId);
            if (OperationFrontdt.Rows.Count > 0)
            {
                _record.HISPatientId = OperationFrontdt.Rows[0]["HISPatientId"].ToString();
                _record.VisitTimes = OperationFrontdt.Rows[0]["VisitTimes"].ToString();
                _record.DepartmentId = int.Parse(OperationFrontdt.Rows[0]["DepartmentId"].ToString());
                _record.ApplyDepartmentName = OperationFrontdt.Rows[0]["ApplyDepName"].ToString() + " " + OperationFrontdt.Rows[0]["SickBed"].ToString();
                _record.ApplyDepName = OperationFrontdt.Rows[0]["ApplyDepName"].ToString();
                _record.PatientDepName = OperationFrontdt.Rows[0]["PatientDepName"].ToString();
                _record.EndemicArea = OperationFrontdt.Rows[0]["Illdistrict"].ToString();
                _record.Bed = OperationFrontdt.Rows[0]["SickBed"].ToString();
                _record.InHospitalNo = OperationFrontdt.Rows[0]["MdrecNo"].ToString();
                _record.MedicalHistoryNo = OperationFrontdt.Rows[0]["ArchivesNo"].ToString();
                _record.Name = OperationFrontdt.Rows[0]["PatientName"].ToString();
                _record.Sex = OperationFrontdt.Rows[0]["Sex"].ToString();
                _record.Age = OperationFrontdt.Rows[0]["Age"].ToString();
                if (OperationFrontdt.Rows[0]["Height"].ToString() != "")
                {
                    double Height = 0;
                    bool isNum = double.TryParse(OperationFrontdt.Rows[0]["Height"].ToString(), out Height);
                    if (isNum == true)
                    {
                        _record.Height = Height.ToString();
                    }
                    else
                    {
                        _record.Height = OperationFrontdt.Rows[0]["Height"].ToString();
                    }
                }
                if (OperationFrontdt.Rows[0]["Weight"].ToString() != "")
                {
                    double Weight = 0;
                    bool isNum = double.TryParse(OperationFrontdt.Rows[0]["Weight"].ToString(), out Weight);
                    if (isNum == true)
                    {
                        _record.Weight = Weight.ToString();
                    }
                    else
                    {
                        _record.Weight = OperationFrontdt.Rows[0]["Weight"].ToString();
                    }
                } 
                _record.Applydiagnose = OperationFrontdt.Rows[0]["ApplyDiagnoseInfoId"].ToString();
                _record.Applyoperation = OperationFrontdt.Rows[0]["ApplyOperationInfoId"].ToString();
                _record.OperationType = OperationFrontdt.Rows[0]["OperationType"].ToString();
                _record.RoomId = int.Parse(OperationFrontdt.Rows[0]["OperationRoomId"].ToString().Trim());
                _record.MedicalRecord = OperationFrontdt.Rows[0]["MedicalRecord"].ToString();
                _record.PlanOperationTime = DateTime.Parse(OperationFrontdt.Rows[0]["PlanOperationTime"].ToString());
            }
            DataTable OperationDoingdt = BOperationRecord.GetOperationDoingDataTableByPatientId(PatientId, RecoverId);
            if (OperationDoingdt.Rows.Count > 0)
            {
                _record.StateName = OperationDoingdt.Rows[0]["State"].ToString().Trim();
                _record.Operation = OperationDoingdt.Rows[0]["OperationInfoId"].ToString().Trim();
                _record.Diagnose = OperationDoingdt.Rows[0]["DiagnoseInfoId"].ToString().Trim();
                _record.AnaesthesiaMethodId = OperationDoingdt.Rows[0]["AnaesthesiaMethodId"].ToString().Trim();
                _record.OperationSiteId = OperationDoingdt.Rows[0]["OperationBodyPositionId"].ToString().Trim();
                _record.OperationPositionId = OperationDoingdt.Rows[0]["OperationPositionId"].ToString().Trim();
                _record.OperationDoctor = OperationDoingdt.Rows[0]["OperationDoctorId"].ToString().Trim();
                _record.Assistant1 = OperationDoingdt.Rows[0]["AssistantId"].ToString().Trim();
                _record.AnesthesiaDoctor = OperationDoingdt.Rows[0]["AnesthesiaDoctorId"].ToString().Trim();
                _record.InstrumentNurse = OperationDoingdt.Rows[0]["InstrumentNurseId"].ToString().Trim();
                _record.TourNurse = OperationDoingdt.Rows[0]["TourNurseId"].ToString().Trim();
                _record.OrtherDoctorId = OperationDoingdt.Rows[0]["OrtherDoctorId"].ToString().Trim();
                //_record.SpecialCase = OperationDoingdt.Rows[0]["SpecialCase"].ToString().Trim();
                //if (_record.SpecialCase == null || _record.SpecialCase == "") _record.SpecialCase = "无";
                //if (OperationDoingdt.Rows[0]["Fasting"].ToString() != "")
                //    _record.Fasting = int.Parse(OperationDoingdt.Rows[0]["Fasting"].ToString());
                //_record.BloodType = OperationDoingdt.Rows[0]["BloodType"].ToString().Trim();
                if (_record.SpecialCase == null || _record.SpecialCase == "") _record.SpecialCase = "无";
                _record.OperationRoom = OperationDoingdt.Rows[0]["OperationRoom"].ToString().Trim();
            }
            _record.OpeRecordInfo = BOperationRecordInfo.SelectSingle("OperationRecordId=" + _record.Id, null);
            if (_record.OpeRecordInfo == null || _record.OpeRecordInfo.Id == null)
            {
                _record.OpeRecordInfo = new OperationRecordInfo();
                _record.OpeRecordInfo.OperationRecordId = _record.Id;
                _record.OpeRecordInfo.PatientId = PatientId;
                _record.OpeRecordInfo.ApplyId = _record.OperationApplyId;
                if (OperationFrontdt.Rows.Count > 0)
                {
                    _record.OpeRecordInfo.OperationLevel = OperationFrontdt.Rows[0]["OperationLevelName"].ToString();
                }
                _record.OpeRecordInfo.OperatorId = PublicMethod.OperatorName;
                _record.OpeRecordInfo.OperatorTime = DateTime.Now;
                _record.OpeRecordInfo.Id = BOperationRecordInfo.Insert(_record.OpeRecordInfo);
            }
            _record.OpeRecoverInInfo = BOperationRecoverInInfo.SelectSingle("PatientId=" + _record.PatientId, null);
            if (_record.OpeRecoverInInfo == null) _record.OpeRecoverInInfo = new OperationRecoverInInfo();
            _record.OpeRecoverOutInfo = BOperationRecoverOutInfo.SelectSingle("PatientId=" + _record.PatientId, null);
            if (_record.OpeRecoverOutInfo == null) _record.OpeRecoverOutInfo = new OperationRecoverOutInfo();
            _record.FactBloodGasAnalysisList = BFactBloodGasAnalysis.Select("OperationRecordId=" + _record.Id, null);
            foreach (var item in _record.FactBloodGasAnalysisList)
            {
                item.FactBloodGasAnalysisDataList = BFactBloodGasAnalysisData.Select("RecordId=" + item.Id, null);
            }
            FillEventsDataAll(PatientId, RecoverId, _record);
            FillDrugDataAll(PatientId, RecoverId, _record);
            DrawOutFluid(PatientId, RecoverId, _record);
            GetAnaseDataQualityRecord(_record);
            _record.SpareFive = "";
            if (_record.OutRoomTime != null && _record.InRoomTime != null)
                _record.SpareFive = Convert.ToInt32(((TimeSpan)(_record.OutRoomTime - _record.InRoomTime)).TotalMinutes).ToString() + "分钟";
            _record.SpareSix = "";
            if (_record.OpeRecordInfo.AnesthesiaSucceed != null && _record.OpeRecordInfo.AnesthesiaSucceed != "")
            {
                _record.SpareSix += "麻醉接台:" + _record.OpeRecordInfo.AnesthesiaSucceed + " ";
                if (_record.OpeRecordInfo.AnesthesiaSucceedTime != null)
                    _record.SpareSix += "时间:" + _record.OpeRecordInfo.AnesthesiaSucceedTime.Value.ToString("yyyy-MM-dd HH:mm") + " ";
            }
            if (_record.OpeRecordInfo.InstrumentNurseSucceed != null && _record.OpeRecordInfo.InstrumentNurseSucceed != "")
            {
                _record.SpareSix += "巡回接台:" + _record.OpeRecordInfo.InstrumentNurseSucceed + " ";
                if (_record.OpeRecordInfo.InstrumentNurseSucceedTime != null)
                    _record.SpareSix += "时间:" + _record.OpeRecordInfo.InstrumentNurseSucceedTime.Value.ToString("yyyy-MM-dd HH:mm") + " ";
            }
            if (_record.OpeRecordInfo.TourNurseSucceed != null && _record.OpeRecordInfo.TourNurseSucceed != "")
            {
                _record.SpareSix += "器械接台:" + _record.OpeRecordInfo.TourNurseSucceed + " ";
                if (_record.OpeRecordInfo.TourNurseSucceedTime != null)
                    _record.SpareSix += "时间:" + _record.OpeRecordInfo.TourNurseSucceedTime.Value.ToString("yyyy-MM-dd HH:mm") + " ";
            }
            if (_record.OpeRecordInfo.Experts != null && _record.OpeRecordInfo.Experts != "")
            {
                _record.SpareSix += "外请专家:" + _record.OpeRecordInfo.Experts + " ";
            }
            if (_record.OpeRecordInfo.RuleAnesthesiaDoctor != null && _record.OpeRecordInfo.RuleAnesthesiaDoctor != "")
            {
                _record.SpareSix += "规培麻醉医生:" + _record.OpeRecordInfo.RuleAnesthesiaDoctor + " ";
            }
            if (_record.OpeRecordInfo.RuleNurse != null && _record.OpeRecordInfo.RuleNurse != "")
            {
                _record.SpareSix += "规培护士:" + _record.OpeRecordInfo.RuleNurse + " ";
            }
            int x = 0;
            double allDrugDose = 0, allOutDose = 0;
            _record.SpareTwo = "";
            _record.SpareThree = "";
            _record.SpareFour = "";
            foreach (var item in _record.FactDrugList)
            {
                string dose = "";
                if (dose.Trim() != "") dose += " ";
                dose += ((double)item.Dosage).ToString();
                if (item.DosageUnit != null)
                {
                    dose += item.DosageUnit;
                }
                _record.SpareFour += string.Format("({0}){1} {2} {3}{4}{5} ", x + 1, (item.DrugBeginTime == item.DrugEndTime) ? item.DrugBeginTime.ToShortTimeString() : item.DrugBeginTime.ToShortTimeString() + "->" + item.DrugEndTime.ToShortTimeString(), item.DrugName.Trim(), dose, "", item.DrugChannel == null ? "" : " " + item.DrugChannel.ToString());
                if (item.DosageUnit.Trim() == "mL" || item.DosageUnit.Trim() == "ml" || item.DosageUnit.Trim() == "ML")
                    if (item.Dosage != 0)
                    {
                        Double val = Convert.ToDouble(item.Dosage);
                        allDrugDose += val;
                    }
                x++;
            }
            _record.SpareTwo = allDrugDose.ToString();
            foreach (var item in _record.FactOutputLiquidsList)
            {
                string dose = "";
                if (dose.Trim() != "") dose += " ";
                dose += ((double)item.Dosage).ToString();
                if (item.DosageUnit != null)
                {
                    dose += item.DosageUnit;
                }
                _record.SpareFour += string.Format("({0}){1} {2} {3} ", x + 1, (item.BeginTime == item.EndTime) ? item.BeginTime.ToShortTimeString() : item.BeginTime.ToShortTimeString() + "->" + item.EndTime.ToShortTimeString(), item.OutputLiquidsName.Trim(), dose);
                if (item.DosageUnit.Trim() == "mL" || item.DosageUnit.Trim() == "ml" || item.DosageUnit.Trim() == "ML")
                {
                    if (item.Dosage != 0)
                    {
                        Double val = Convert.ToDouble(item.Dosage);
                        allOutDose += val;
                    }
                }
                x++;
            }
            _record.SpareThree = allOutDose.ToString();
            if (_record.ASALevel == null || _record.ASALevel == 0)
                BOperationReview.GetASALevel(PatientId, _record);
            return _record;
        }
        public static void FillDrugDataAll(int PatientId, int TypeId, OperationRecord _record)
        {
            //药品
            _record.FactDrugList.Clear();
            DataTable DrugAllData = BOperationRecord.FillDrugData(PatientId, TypeId);
            for (int i = 0; i < DrugAllData.Rows.Count; i++)
            {
                if (DrugAllData.Rows[i]["DrugsId"].ToString() == "" || DrugAllData.Rows[i]["DrugsId"].ToString() == "0") return;
                FactDrug FactDrug = new FactDrug();
                FactDrug.Id = int.Parse(DrugAllData.Rows[i]["Id"].ToString()); ;
                FactDrug.DrugTypeId = TypeId;
                FactDrug.PatientId = PatientId;
                FactDrug.ParentId = int.Parse(DrugAllData.Rows[i]["ParentId"].ToString()); ;
                FactDrug.DrugId = int.Parse(DrugAllData.Rows[i]["DrugsId"].ToString());
                FactDrug.DrugName = DrugAllData.Rows[i]["DrugName"].ToString();
                FactDrug.DrugBeginTime = DateTime.Parse(DateTime.Parse(DrugAllData.Rows[i]["DrugBeginTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                FactDrug.DrugEndTime = DateTime.Parse(DateTime.Parse(DrugAllData.Rows[i]["DrugEndTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                if (DrugAllData.Rows[i]["Dosage"].ToString() != "") FactDrug.Dosage = decimal.Parse(DrugAllData.Rows[i]["Dosage"].ToString());
                FactDrug.DosageUnit = DrugAllData.Rows[i]["DosageUnit"].ToString();
                FactDrug.DrugChannel = DrugAllData.Rows[i]["DrugChannel"].ToString();
                FactDrug.GiveDrugType = DrugAllData.Rows[i]["GiveDrugType"].ToString();
                FactDrug.Remark = DrugAllData.Rows[i]["Remark"].ToString();
                FactDrug.DrugKind = DrugAllData.Rows[i]["DrugKind"].ToString();
                FactDrug.IsContinue = int.Parse(DrugAllData.Rows[i]["IsContinue"].ToString());
                if (DrugAllData.Rows[i]["Density"].ToString() != "") FactDrug.Density = decimal.Parse(DrugAllData.Rows[i]["Density"].ToString());
                FactDrug.DensityUnit = DrugAllData.Rows[i]["DensityUnit"].ToString();
                if (DrugAllData.Rows[i]["Velocity"].ToString() != "") FactDrug.Velocity = decimal.Parse(DrugAllData.Rows[i]["Velocity"].ToString());
                FactDrug.VelocityUnit = DrugAllData.Rows[i]["VelocityUnit"].ToString();
                FactDrug.BloodType = DrugAllData.Rows[i]["BloodType"].ToString();
                FactDrug.OperatorNo = DrugAllData.Rows[i]["OperatorNo"].ToString();
                FactDrug.OperatorName = DrugAllData.Rows[i]["OperatorName"].ToString();
                FactDrug.OperateDate = DateTime.Parse(DrugAllData.Rows[i]["OperateDate"].ToString());
                FactDrug.AnesType = DrugAllData.Rows[i]["AnesType"].ToString();
                FactDrug.Comment = DrugAllData.Rows[i]["Comment"].ToString();
                FactDrug.Access = DrugAllData.Rows[i]["Access"].ToString();
                _record.FactDrugList.Add(FactDrug);
            }
        }
        public static void FillEventsDataAll(int PatientId, int TypeId, OperationRecord _record)
        {
            //事件
            _record.FactEventsList.Clear();
            DataTable dt = BFactEvents.GetDataTable(PatientId, TypeId);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                FactEvents FactEventsObj = new FactEvents();
                FactEventsObj.Id = int.Parse(dt.Rows[i]["Id"].ToString());
                FactEventsObj.EventName = dt.Rows[i]["EventName"].ToString();
                FactEventsObj.EventId = int.Parse(dt.Rows[i]["EventId"].ToString());
                FactEventsObj.Remark = dt.Rows[i]["Remark"].ToString();
                FactEventsObj.EventRemark = dt.Rows[i]["EventRemark"].ToString();
                FactEventsObj.EventBeginTime = DateTime.Parse(DateTime.Parse(dt.Rows[i]["EventBeginTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                FactEventsObj.EventEndTime = DateTime.Parse(DateTime.Parse(dt.Rows[i]["EventEndTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                FactEventsObj.OperatorNo = dt.Rows[i]["OperatorNo"].ToString();
                FactEventsObj.OperatorName = dt.Rows[i]["OperatorName"].ToString();
                FactEventsObj.OperateDate = DateTime.Parse(dt.Rows[i]["OperateDate"].ToString());
                _record.FactEventsList.Add(FactEventsObj);
                if (FactEventsObj.EventName == "手术开始")
                {
                    _record.OperationBeginTime = FactEventsObj.EventBeginTime;
                }
                if (FactEventsObj.EventName == "手术结束")
                {
                    _record.OperationEndTime = FactEventsObj.EventBeginTime;
                }
                if (FactEventsObj.EventName == "麻醉开始")
                {
                    _record.AnesthesiaBeginTime = FactEventsObj.EventBeginTime;
                }
                if (FactEventsObj.EventName == "麻醉完成")
                {
                    _record.AnesthesiaEndTime = FactEventsObj.EventBeginTime;
                }
                if (FactEventsObj.EventName == "插管")
                {
                    _record.InCGType = FactEventsObj.EventName;
                    _record.SpileInTime = FactEventsObj.EventBeginTime;
                }
                if (FactEventsObj.EventName == "喉罩")
                {
                    _record.InCGType = FactEventsObj.EventName;
                    _record.SpileInTime = FactEventsObj.EventBeginTime;
                }
                if (FactEventsObj.EventName == "拔管")
                {
                    _record.SpileOutTime = FactEventsObj.EventBeginTime;
                }
            }
        }
        public static void DrawOutFluid(int PatientId, int TypeId, OperationRecord _record)
        {
            //出量
            _record.FactOutputLiquidsList.Clear();
            DataTable OutputLiquidsDistinct = BOperationRecord.FillOutFluid(PatientId, TypeId);
            for (int i = 0; i < OutputLiquidsDistinct.Rows.Count; i++)
            {
                FactOutputLiquids OutputLiquidsObj = new FactOutputLiquids();
                OutputLiquidsObj.OutputLiquidsTypeId = TypeId;
                OutputLiquidsObj.PatientId = PatientId;
                OutputLiquidsObj.Id = int.Parse(OutputLiquidsDistinct.Rows[i]["Id"].ToString());
                OutputLiquidsObj.OutputLiquidsId = int.Parse(OutputLiquidsDistinct.Rows[i]["OutputLiquidsId"].ToString());
                OutputLiquidsObj.OutputLiquidsName = OutputLiquidsDistinct.Rows[i]["OutputLiquidsName"].ToString();
                OutputLiquidsObj.DosageUnit = OutputLiquidsDistinct.Rows[i]["DosageUnit"].ToString();
                OutputLiquidsObj.BeginTime = DateTime.Parse(DateTime.Parse(OutputLiquidsDistinct.Rows[i]["BeginTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                OutputLiquidsObj.EndTime = DateTime.Parse(DateTime.Parse(OutputLiquidsDistinct.Rows[i]["EndTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                OutputLiquidsObj.Dosage = decimal.Parse(OutputLiquidsDistinct.Rows[i]["Dosage"].ToString());
                OutputLiquidsObj.DosageUnit = OutputLiquidsDistinct.Rows[i]["DosageUnit"].ToString();
                OutputLiquidsObj.Remark = OutputLiquidsDistinct.Rows[i]["Remark"].ToString();
                _record.FactOutputLiquidsList.Add(OutputLiquidsObj);
            }
        }
        public static void GetAnaseDataQualityRecord(OperationRecord _record)
        {
            //出量
            _record.AnaseDataQualityRecordList.Clear();
            _record.AnaseDataQualityRecordList = BAnaseDataQualityRecord.Select("OperationRecordId=" + _record.Id, null);
        }
        /// 
        ///  判断添加的事件 是否正确
        /// 
        /// true :术中 false:恢复
        /// 手术对象
        /// 事件名称
        /// 事件时间
        /// 返回提示消息
        /// 
        public static bool IfTimeisTrue(bool OpeState, OperationRecord _record, string EventName, DateTime curTimeTemp, ref string messing)
        {
            //if (curTimeTemp > PublicMethod.ServerTime())
            //{
            //    messing = EventName + "时间不可以大于当前时间 !";
            //    return false;
            //}
            TimeSpan ifinroom = curTimeTemp - _record.InRoomTime.Value;
            if (EventName != "入室" && _record.InRoomTime == null)
            {
                messing = "请填写入室时间 !";
                return false;
            }
            else if (EventName != "入室" && (ifinroom.TotalHours > 48 || ifinroom.TotalDays < -48))
            {
                messing = EventName + "时间不能大于入室时间48小时 !";
                return false;
            }
            else if (EventName != "入室" && EventName != "麻醉开始" && _record.InRoomTime > curTimeTemp)// && OpeState == true
            {
                messing = EventName + "时间不能小于入室时间(" + _record.InRoomTime + ") !";
                return false;
            }
            if (_record.OutRoomTime != null && EventName != "出室" && EventName != "麻醉完成" && _record.OutRoomTime < curTimeTemp && OpeState == true)
            {
                messing = EventName + "时间不能大于出室时间(" + _record.OutRoomTime + ") !";
                return false;
            }
            if (EventName == "麻醉完成" && _record.AnesthesiaBeginTime == null && OpeState == true)
            {
                messing = "请先填写麻醉开始时间!";
                return false;
            }
            if (EventName == "手术结束" && _record.OperationBeginTime == null && OpeState == true)
            {
                messing = "请先填写手术开始时间!";
                return false;
            }
            if (EventName == "拔管" && _record.SpileInTime == null && OpeState == true)
            {
                messing = "请先填写插管时间!";
                return false;
            }
            if (EventName == "手术结束" && curTimeTemp < _record.OperationBeginTime && OpeState == true)
            {
                messing = "手术结束时间必须晚于手术开始时间(" + _record.OperationBeginTime + ") !";
                return false;
            }
            if (EventName == "出室" && _record.OperationEndTime == null && OpeState == true)
            {
                messing = "请先填写手术结束时间!";
                return false;
            }
            if (EventName == "出室" && curTimeTemp < _record.OperationEndTime && OpeState == true)
            {
                messing = "出室时间必须晚于手术结束时间(" + _record.OperationEndTime + ") !";
                return false;
            }
            return true;
        }
        public static DataTable getOperationRecorePhysioData(int operationId)
        {
            string sqlStr = "exec [dbo].[usp_GetOperationRecorePhysioData] " + operationId;
            return DBHelper.GetDataTable(sqlStr);
        }
        public static void SelectSingle(int PatientId, int RecoverId, ref OperationRecord operationRecord)
        {
            DOperationRecord.SelectSingleEntity("PatientId=" + PatientId + " and RecoverId=" + RecoverId, ref operationRecord);
        }
        public static void UpdateState(int PatientId, int TypeId, int State)
        {
            DOperationRecord.UpdateState(PatientId, TypeId, State);
        }
        public static void UpdateOpeState(int PatientId, int TypeId, int State, string Text)
        {
            DOperationRecord.UpdateOpeState(PatientId, TypeId, State, Text);
        }
        public static void DeleteOperationRecordData(int RecorId, int PatientId, int TypeId)
        {
            DOperationRecord.DeleteOperationRecordData(RecorId, PatientId, TypeId);
        }
        public static void DeleteOperationRecorvreData(int RecorId, int PatientId, int TypeId)
        {
            DOperationRecord.DeleteOperationRecorvreData(RecorId, PatientId, TypeId);
        }
        public static void DeleteEvent(int PatientId, int eventId)
        {
            DOperationRecord.DeleteEvent(PatientId, eventId);
        }
        public static DataTable GetOperationDoingDataTableByPatientId(int PatientId, int TypeId)
        {
            return DOperationRecord.GetOperationDoingMainInfo(PatientId, TypeId);
        }
        public static bool ValidInput(int PatientId, int TypeId)
        {
            bool result = false;
            DataTable dt = DOperationRecord.GetOperationDoingMainInfo(PatientId, TypeId);
            if (dt.Rows[0]["InRoomTime"].ToString().Length < 1)
            {
                MessageBox.Show("入室不能为空!");
            }
            else if (dt.Rows[0]["OutRoomTime"].ToString().Length < 1)
            {
                MessageBox.Show("出室不能为空!");
            }
            else if (dt.Rows[0]["OperationBeginTime"].ToString().Length < 1)
            {
                MessageBox.Show("手术开始时间不能为空!");
            }
            else if (dt.Rows[0]["OperationEndTime"].ToString().Length < 1)
            {
                MessageBox.Show("手术结束时间不能为空!");
            }
            else if (dt.Rows[0]["DiagnoseInfoName"].ToString().Length < 1)
            {
                MessageBox.Show("术后诊断不能为空!");
            }
            else if (dt.Rows[0]["OperationInfoName"].ToString().Length < 1)
            {
                MessageBox.Show("实施手术不能为空!");
            }
            else if (dt.Rows[0]["AnaesthesiaMethodName"].ToString().Length < 1)
            {
                MessageBox.Show("麻醉方式不能为空!");
            }
            else if (dt.Rows[0]["OperationDoctor"].ToString().Length < 1)
            {
                MessageBox.Show("手术医生不能为空!");
            }
            else if (dt.Rows[0]["AnesthesiaDoctor"].ToString().Length < 1)
            {
                MessageBox.Show("麻醉医生不能为空!");
            }
            else
            {
                result = true;
            }
            return result;
        }
        /// 
        /// 得到手术的最大时间点
        /// 
        /// 
        /// 
        public static DateTime lastMaxOperationDate(int PatientId, int TypeId)
        {
            string sqlStr = "select top 1 * from (SELECT s.PatientId , s.EventEndTime etime FROM FactEvents s WHERE s.EventTypeId=" + TypeId + " and s.PatientId = " + PatientId + " union all SELECT s.PatientId , s.DrugEndTime FROM[dbo].[FactDrug]  s WHERE s.DrugTypeId=" + TypeId + " and s.PatientId = " + PatientId + " ) a order by a.etime desc";
            DataTable dt = DBHelper.GetDataTable(sqlStr);
            return DateTime.Parse(dt.Rows[0][1].ToString());
        }
        public static DataTable getPhysioDataByTimeName(string IP, string name, DateTime starttime, DateTime endtime)
        {
            DataTable dt = new DataTable();
            string sqlStr = " exec [usp_GetPhysioDataByName] '" + IP + "','" + name + "','" + starttime + "','" + endtime + "'";//
            dt = DATADBHelper.GetDataTable(sqlStr);
            return dt;
        }
        public static DataTable getPhysioDataByIp(string IP, int operationId, DateTime starttime, DateTime endtime, int collectInterval)
        {
            DataTable dt = new DataTable();
            string sqlStr = " exec [usp_GetPhysioDataByIp] '" + IP + "','" + starttime + "','" + endtime + "'," + collectInterval;
            dt = DATADBHelper.GetDataTable(sqlStr);
            foreach (DataRow item in dt.Rows)
            {
                if (item[0].ToString() == "7")
                {
                    foreach (DataRow item1 in dt.Rows)
                    {
                        if (item1[0].ToString() == "5" && item1[1].ToString() == item[1].ToString())
                        {
                            item1[2] = "-1";
                        }
                    }
                }
                if (item[0].ToString() == "8")
                {
                    foreach (DataRow item1 in dt.Rows)
                    {
                        if (item1[0].ToString() == "6" && item1[1].ToString() == item[1].ToString())
                        {
                            item1[2] = "-1";
                        }
                    }
                }
            }
            return dt;
        }
        public static int DeletePhysioDataByOpeID(int operationId)
        {
            string sqlStr = "DELETE FROM PhysioData  where PatientId = " + operationId + " ";
            return DBHelper.ExecNonQuery(sqlStr);
        }
        public static int DeletePhysioDataByID(int operationId, int PhysioParameterID)
        {
            string sqlStr = "delete FROM PhysioData  where PatientId = " + operationId + " and PhysioDataConfigId=" + PhysioParameterID;
            return DBHelper.ExecNonQuery(sqlStr);
        }
        public static void UpdatePhysioDataRespByTime(OperationRecord record)
        {
            int zzhx = 3;
            int jxtq = 12;
            //限制没有插管不让点击拔管
            //插管拔管时间都有 更新插管前机械通气为自主呼吸 插管后机械通气为自主呼吸 插管开始到拔管是自主呼吸为机械通气
            //有插管时间没拔管时间 更新插管前机械通气为自主呼吸  插管后自主呼吸为机械通气 
            //没有插管拔管时间更新所有机械通气为自主呼吸
            if (record.SpileInTime != null && record.SpileOutTime != null)
            {
                string sqlStr = "Update PhysioData set PhysioDataConfigId=" + zzhx + "   where PatientId = " + record.Id + " and PhysioDataConfigId=" + jxtq + " and RecordTime<'" + record.SpileInTime + "' ";
                DBHelper.ExecNonQuery(sqlStr);
                string sqlStr2 = "Update PhysioData set  PhysioDataConfigId=" + jxtq + " where PatientId = " + record.Id + " and PhysioDataConfigId=" + zzhx + " and RecordTime>='" + record.SpileInTime
                    + "'  and RecordTime<='" + record.SpileOutTime + "' ";
                DBHelper.ExecNonQuery(sqlStr2);
                string sqlStr3 = "Update PhysioData set PhysioDataConfigId=" + zzhx + "  where PatientId = " + record.Id + " and PhysioDataConfigId=" + jxtq + " and RecordTime>'" + record.SpileOutTime + "' ";
                DBHelper.ExecNonQuery(sqlStr3);
            }
            else if (record.SpileInTime != null && record.SpileOutTime == null)
            {
                string sqlStr = "Update PhysioData set PhysioDataConfigId=" + zzhx + "   where PatientId = " + record.Id + " and PhysioDataConfigId=" + jxtq + " and RecordTime<'" + record.SpileInTime + "' ";
                DBHelper.ExecNonQuery(sqlStr);
                string sqlStr2 = "Update PhysioData set  PhysioDataConfigId=" + jxtq + " where PatientId = " + record.Id + " and PhysioDataConfigId=" + zzhx + " and RecordTime>='" + record.SpileInTime
                    + "' ";
                DBHelper.ExecNonQuery(sqlStr2);
            }
            else if (record.SpileInTime == null && record.SpileOutTime == null)
            {
                string sqlStr = "Update PhysioData set PhysioDataConfigId=" + zzhx + "   where PatientId = " + record.Id + " and PhysioDataConfigId=" + jxtq;
                DBHelper.ExecNonQuery(sqlStr);
            }
        }
        public static DataTable getByOpeIDIsPhysioData(int operationId, string PhysioDataConfigId)
        {
            string sqlStr = "SELECT [PhysioDataConfig].Name , PhysioData.RecordTime, PhysioData.[Value]   FROM [dbo].[PhysioDataConfig] INNER JOIN  PhysioData ON [PhysioDataConfig].Id = PhysioData.PhysioDataConfigId  where PatientId = " + operationId + " and PhysioDataConfigId in (" + PhysioDataConfigId + ") order by PhysioDataConfigId,PhysioData.RecordTime ASC";
            return DBHelper.GetDataTable(sqlStr);
        }
        public static DataTable getByOpeIDIsPhysioData(int operationId)
        {
            string sqlStr = "SELECT [PhysioDataConfig].Name , PhysioData.RecordTime, PhysioData.[Value]   FROM [dbo].[PhysioDataConfig] INNER JOIN  PhysioData ON [PhysioDataConfig].Id = PhysioData.PhysioDataConfigId  where PatientId = " + operationId + " order by PhysioDataConfigId,PhysioData.RecordTime ASC";
            return DBHelper.GetDataTable(sqlStr);
        }
        public static int GetDataCount(int RecorId, int PatientId, int Type)
        {
            return DOperationRecord.GetDataCount(RecorId, PatientId, Type);
        }
        public static DataTable FillDrugData(int PatientId, int DrugTypeId)
        {
            return DOperationRecord.FillDrugData(PatientId, DrugTypeId);
        }
        public static DataTable FillOutFluid(int PatientId, int DrugTypeId)
        {
            return DOperationRecord.FillOutFluid(PatientId, DrugTypeId);
        }
    }
}