using System;
using System.Collections.Generic;
using System.Text;
using DrawGraph;
using System.Drawing;
using System.Xml.Serialization;
using System.Data.SqlClient;
using HelperDB;
using System.Resources;
namespace DrawGraph
{
    public class PhysioDataConfig : EventObj
    {
        private int id;
        private string name;
        private string enname;
        private string color;
        private string imgPath;
        private int highLimit;
        private int lowLimit;
        private bool isValid;
        private string operatorNo;
        private string operatorName;
        private DateTime operateDate;
        private string configType;
        private bool showImg;
        private bool showText;
        private int warningHighLimit;
        private int warningLowLimit;
        private bool isSplit;
        private bool isDefalultShow;
        private string unit;
        private int physioOrder;
        /// 
        /// 
        /// 
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        /// 
        /// 
        /// 
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// 
        /// 
        /// 
        public string Enname
        {
            get { return enname; }
            set { enname = value; }
        }
        /// 
        /// 
        /// 
        public string Color
        {
            get { return color; }
            set { color = value; }
        }
        /// 
        /// 
        /// 
        public string ImgPath
        {
            get { return imgPath; }
            set { imgPath = value; }
        }
        /// 
        /// 
        /// 
        public int HighLimit
        {
            get { return highLimit; }
            set { highLimit = value; }
        }
        /// 
        /// 
        /// 
        public int LowLimit
        {
            get { return lowLimit; }
            set { lowLimit = value; }
        }
        /// 
        /// 
        /// 
        public bool IsValid
        {
            get { return isValid; }
            set { isValid = value; }
        }
        /// 
        /// 
        /// 
        public string OperatorNo
        {
            get { return operatorNo; }
            set { operatorNo = value; }
        }
        /// 
        /// 
        /// 
        public string OperatorName
        {
            get { return operatorName; }
            set { operatorName = value; }
        }
        /// 
        /// 
        /// 
        public DateTime OperateDate
        {
            get { return operateDate; }
            set { operateDate = value; }
        }
        /// 
        /// 
        /// 
        public string ConfigType
        {
            get { return configType; }
            set { configType = value; }
        }
        /// 
        /// 
        /// 
        public bool ShowImg
        {
            get { return showImg; }
            set { showImg = value; }
        }
        /// 
        /// 
        /// 
        public bool ShowText
        {
            get { return showText; }
            set { showText = value; }
        }
        /// 
        /// 
        /// 
        public int WarningHighLimit
        {
            get { return warningHighLimit; }
            set { warningHighLimit = value; }
        }
        /// 
        /// 
        /// 
        public int WarningLowLimit
        {
            get { return warningLowLimit; }
            set { warningLowLimit = value; }
        }
        /// 
        /// 
        /// 
        public bool IsSplit
        {
            get { return isSplit; }
            set { isSplit = value; }
        }
        /// 
        /// 
        /// 
        public bool IsDefalultShow
        {
            get { return isDefalultShow; }
            set { isDefalultShow = value; }
        }
        /// 
        /// 
        /// 
        public string Unit
        {
            get { return unit; }
            set { unit = value; }
        }
        /// 
        /// 
        /// 
        public int PhysioOrder
        {
            get { return physioOrder; }
            set { physioOrder = value; }
        }
        #region 私有变量 
        private int yAisx = 0;
        private int patientId;
        private Color conveColor;
        public string showStyle { get; set; }
        private int curPDIndex = -1;
        public double X_MINOR_GRID_STEP = 5.0; //步长
        private PointPairList aPhysioDatas;
        PhysioData pdTrue = null;
        public double showValue { get; set; }
        public string className = "";
        [XmlIgnore]
        private LineItem curve1;
        [XmlIgnore]
        private bool isClick = false;
        #endregion
        #region 属性
        [XmlIgnore]
        public LineItem curve
        {
            get { return curve1; }
            set { curve1 = value; }
        }
        [XmlIgnore]
        public bool IsClick
        {
            get { return isClick; }
            set { isClick = value; }
        }
        /// 
        /// 当前选择点的Index
        /// 
        public int CurPDIndex
        {
            get { return curPDIndex; }
            set { curPDIndex = value; }
        }
        /// 
        /// 生理数据集合
        /// 
        public PointPairList APhysioParams
        {
            get { return aPhysioDatas; }
            set { aPhysioDatas = value; }
        }
        /// 
        /// 此曲线的值以Y的第几个轴匹配
        /// 
        public int YAisx
        {
            get { return yAisx; }
            set { yAisx = value; }
        }
        /// 
        /// 手术ID
        /// 
        public int PatientId
        {
            get { return patientId; }
            set { patientId = value; }
        }
        /// 
        /// 线的着色
        /// 
        public Color ConveColor
        {
            get { return conveColor; }
            set { conveColor = value; }
        }
        #endregion
        #region 事件
        public delegate void ClickEventHandler(object sender, EventArgs e);
        public event ClickEventHandler Click;
        public void onClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }
        #endregion
        public PhysioDataConfig()
        {
            APhysioParams = new PointPairList();
            className = this.GetType().Name.ToString();
        }
        /// 
        /// 包含增加和删除,当插入位置已有生理数据时,执行删除
        /// 
        /// 
        public void AddOrDelItem(PhysioData pd, int collectInterval)
        {
            bool isHave = false;
            PhysioData curPd = null;
            foreach (PointPair pptemp in APhysioParams)
            {
                PhysioData temp1 = PointPairToPhysioData(pptemp);
                if (pd.isEquert(temp1) && temp1.Value != Double.MaxValue)
                {
                    isHave = true;
                    curPd = temp1;
                    break;
                }
            }
            if (isHave)
            {
                //DelItem(curPd);
            }
            else
            {
                AddMultipleItem(pd, pd.RecordTime, collectInterval);
                //AddItem(pd, true);
            }
        }
        public void AddFillItem(PhysioData pd)
        {
            bool isHave = false;
            foreach (PointPair pptemp in APhysioParams)
            {
                PhysioData temp1 = PointPairToPhysioData(pptemp);
                double yDiff = pd.Value - temp1.Value;
                TimeSpan ts1 = new TimeSpan(pd.RecordTime.Ticks);
                TimeSpan ts2 = new TimeSpan(temp1.RecordTime.Ticks);
                double minDiff = (ts1.Subtract(ts2).TotalMinutes);
                if (-2 < minDiff && minDiff < 2 && temp1.Value != Double.MaxValue)
                {
                    isHave = true;
                    break;
                }
            }
            if (!isHave)
            {
                try
                {
                    //如果是负数则不执行操作
                    if (pd.Value < 0) return;
                    pd = reCnterValue(pd);
                    //增加点时如果当前有点且是隐藏的则直接改值
                    Sort();
                    foreach (PointPair pptemp in APhysioParams)
                    {
                        PhysioData pdTemp = PointPairToPhysioData(pptemp);
                        if (pdTemp.isEquert(pd) && pdTemp.Value == Double.MaxValue)
                        {
                            pptemp.Y = pd.Value;
                            isHave = true;
                            break;
                        }
                        else if (pdTemp.isEquert(pd))
                        {
                            isHave = true;
                            break;
                        }
                    }
                    if (!isHave)
                    {
                        this.APhysioParams.Add(pd);
                    }
                    Sort();
                    curve.Points = APhysioParams;
                    PhysioDataService.AddPhysioData(pd);
                    if (showStyle == "显示数值")//|| pd.Value > HighLimit
                    {
                        DrawTextPhysioData(pd);
                    }
                }
                catch (Exception)
                {
                    //throw new Exception("插入生理数据结点时出错");
                }
            }
        }
        /// 
        /// 增加一点生理数据,往数据库中写入数据
        /// 
        /// 
        public void AddItem(PhysioData pd, bool isShouDong = true, bool isFillMissPoint = true)
        {
            try
            {
                //如果是负数则不执行操作
                if (pd.Value < 0) return;
                if (pdTrue == null)
                    pdTrue = pd.Clone();
                if (APhysioParams.Count > 0)
                {
                    PhysioData pdTemp = pd.Clone();
                    //当前点的前一个点加步长时间的结点
                    PhysioData pdQD = reMPhysioData(pdTemp);
                    //插入的点大于之前点5分钟以上
                    if (DateTime.Compare(pd.RecordTime, pdQD.RecordTime.AddMinutes(1)) > 0)
                    {
                        pdTemp.Value = Double.MaxValue;
                        this.APhysioParams.Add(pdTemp);
                    }
                }
                if (isShouDong)
                    pd = reCnterValue(pd);
                //增加点时如果当前有点且是隐藏的则直接改值
                Sort();
                bool isHave = false;
                foreach (PointPair pptemp in APhysioParams)
                {
                    PhysioData pdTemp = PointPairToPhysioData(pptemp);
                    if (pdTemp.isEquert(pd) && pdTemp.Value == Double.MaxValue)
                    {
                        pptemp.Y = pd.Value;
                        isHave = true;
                        break;
                    }
                    else if (pdTemp.isEquert(pd))
                    {
                        isHave = true;
                        break;
                    }
                }
                //if (pd.RecordTime >DateTime.Now) return;
                if (!isHave)
                {
                    this.APhysioParams.Add(pd);
                }
                Sort();
                curve.Points = APhysioParams;
                PhysioDataService.AddPhysioData(pd);
                if (showStyle == "显示数值")
                {
                    DrawTextPhysioData(pd);
                }
                if (isShouDong == true)
                {
                    for (int i = APhysioParams.Count - 1; i > -1; i--)
                    {
                        PhysioData maxPhysioData = PointPairToPhysioData(APhysioParams[i]);
                        if (i != 0 && i != APhysioParams.Count - 1)
                        {
                            if (maxPhysioData.Y == Double.MaxValue)
                            {
                                bool b = false;
                                for (int j = 0; j < APhysioParams.Count; j++)
                                {
                                    PhysioData pdt = PointPairToPhysioData(APhysioParams[j]);
                                    if (pdt.RecordTime == maxPhysioData.RecordTime && pdt.Value <= HighLimit)
                                    {
                                        b = true;
                                        break;
                                    }
                                }
                                if (b == true)
                                    this.APhysioParams.Remove(maxPhysioData);
                            }
                        }
                    }
                }
                if (isFillMissPoint == true && pdTrue != null)
                {
                    //返回当前曲线上最大且不为空的点
                    PhysioData maxPd = ReMaxAndGoodPhysioData();
                    if (maxPd != null && pdTrue.RecordTime > maxPd.RecordTime)
                    {
                        int timeDiff = DateTime.Compare(pdTrue.RecordTime, maxPd.RecordTime);
                        //因为插入的真实点大于曲线上最后一个点,则指增加生理数据
                        if (timeDiff > 0)
                        {
                            //如果新增加的点还小于真实的点,则批量增加生理数据
                            if (DateTime.Compare(pd.RecordTime, pdTrue.RecordTime) < 0 && pd.RecordTime.ToString("HH:mm") != pdTrue.RecordTime.ToString("HH:mm"))
                            {
                                pd.RecordTime = pd.RecordTime.AddMinutes(this.X_MINOR_GRID_STEP);
                                bool isHavechild = false;
                                foreach (PointPair pptemp in APhysioParams)
                                {
                                    PhysioData pdTemp = PointPairToPhysioData(pptemp);
                                    if (pdTemp.isEquert(pd) && pdTemp.Value != Double.MaxValue)
                                    {
                                        isHavechild = true;
                                        break;
                                    }
                                }
                                if (isHavechild == false)
                                    //pd.Value = pd.Value + new Random().Next(0, 6);
                                    AddItem(pd, isShouDong, isFillMissPoint);
                            }
                        }
                        else
                        {
                            pdTrue = null;
                        }
                    }
                    else
                    {
                        pdTrue = null;
                    }
                }
            }
            catch (Exception)
            {
                throw new Exception("插入生理数据结点时出错");
            }
        }
        /// 
        /// 增加一点生理数据,往数据库中写入数据
        /// 
        /// 
        public void AddItemByTime(PhysioData pd)
        {
            try
            {
                //如果是负数则不执行操作
                if (pd.Value <= 0) return;
                bool isHave = false;
                PhysioData curPd = null;
                foreach (PointPair pptemp in APhysioParams)
                {
                    PhysioData temp1 = PointPairToPhysioData(pptemp);
                    if (pd.isEquert(temp1) && temp1.Value != Double.MaxValue)
                    {
                        isHave = true;
                        curPd = temp1;
                        break;
                    }
                }
                if (isHave) return;
                this.APhysioParams.Add(pd);
                Sort();
                curve.Points = APhysioParams;
                PhysioDataService.AddPhysioData(pd);
                if (showStyle == "显示数值")
                {
                    DrawTextPhysioData(pd);
                }
            }
            catch (Exception)
            {
                //throw new Exception("插入生理数据结点时出错");
            }
        }
        /// 
        /// 增加一点生理数据,往数据库中写入数据
        /// 
        /// 
        public void AddMultipleItem(PhysioData pd, DateTime endtime, int collectInterval)
        {
            try
            {
                //如果是负数则不执行操作
                pd = reCnterValue(pd);
                TimeSpan tsp = endtime - pd.RecordTime;
                if (tsp.TotalMinutes >= 20 && this.IsSplit == true)
                {
                    pd.RecordTime = endtime;
                    pd = reCnterValue(pd, false);
                    AddItem(pd, false);
                }
                else
                {
                    pd.RecordTime = endtime;
                    AddItem(pd);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// 
        /// 返回当前曲线上最大且不为空的点
        /// 
        /// 
        private PhysioData ReMaxAndGoodPhysioData()
        {
            PhysioData rePd = null;
            for (int i = APhysioParams.Count - 1; i > -1; i--)
            {
                rePd = PointPairToPhysioData(APhysioParams[i]);
                //且被大于的值的点不空隐藏
                if (rePd.Value != Double.MaxValue)
                {
                    return rePd;
                }
            }
            return rePd;
        }
        public void delAddObj(PhysioData newPd)
        {
            delAddObj(this.Enname + newPd.RecordTime.ToString());
        }
        public void DrawTextPhysioData(PhysioData pd)
        {
            delAddObj(pd);
            if (pd.RecordTime <= pageEnd && pd.RecordTime >= pageBegin)
            {
                string viewValue = ((int)pd.Y).ToString();
                if (Enname == "Temp")
                    viewValue = Math.Round(pd.Y, 1).ToString();
                //如果值有效,则写文本
                if (pd.Value != Double.MaxValue)
                {
                    //if ((this.YAisx == 0 && Enname == "SPO2"))
                    //{
                    //    ZUtil.DrawText(viewValue, getXPositinByXDate(pd.RecordTime), (zgcAnas.GraphPane.Chart.Rect.Top / zgcAnas.GraphPane.Rect.Height) + 0.003, zgcAnas, this.Enname + pd.RecordTime.ToString(), 5f, true);
                    //}
                    //else
                    //{
                    if (showValue != -1)
                    {
                        //double y = 0.0135 + (5 - showValue) * 0.0103;
                        if (Enname == "MAC")
                            ZUtil.DrawText((Math.Floor(pd.Value * 10) / 10).ToString(), getXPositinByXDate(pd.RecordTime), showValue, zgcAnas, this.Enname + pd.RecordTime.ToString(), 5f, true);
                        else if (Enname == "Temp")
                        {
                            ZUtil.DrawText(viewValue, getXPositinByXDate(pd.RecordTime), showValue, zgcAnas, this.Enname + pd.RecordTime.ToString(), 4.2f, true);
                        }
                        else
                        {
                            ZUtil.DrawText(viewValue, getXPositinByXDate(pd.RecordTime), showValue, zgcAnas, this.Enname + pd.RecordTime.ToString(), 5f, true);
                        }
                    }
                    else
                    {
                        if (YAisx == 0)
                        {
                            //ZUtil.DrawText(viewValue, getXPositinByXDate(pd.RecordTime), getYPositinByValue(pd.Y, 240, 200, 20), zgcAnas, this.Enname + pd.RecordTime.ToString(), 3f, true);
                            ZUtil.DrawText(viewValue, getXPositinByXDate(pd.RecordTime), getYPositinByValue(pd.Y, 240, 0), zgcAnas, this.Enname + pd.RecordTime.ToString(), 3f, true);
                        }
                        else if (YAisx == 1)
                        {
                            ZUtil.DrawText(viewValue, getXPositinByXDate(pd.RecordTime), getYPositinByValue(pd.Y, 105, 45), zgcAnas, this.Enname + pd.RecordTime.ToString(), 3f, true);
                        }
                        else if (YAisx == 2)
                        {
                            ZUtil.DrawText(viewValue, getXPositinByXDate(pd.RecordTime), getYPositinByValue(pd.Y, 42, 18), zgcAnas, this.Enname + pd.RecordTime.ToString(), 3f, true);
                        }
                    }
                    //}
                }
            }
        }
        /// 
        /// 仅修改值不修改数据库
        /// 
        /// 
        /// 
        /// 
        public PhysioData onlyModItem(PhysioData newPd, PhysioData oldPd = null)
        {
            //如果是负数则不执行操作
            if (newPd.Value < 0)
            {
                //MessageBox.Show("输入的值不能小于0");
                return null;
            }
            if (curPDIndex != -1)
            {
                newPd = reCnterValue(newPd);
                //this.APhysioParams[CurPDIndex].X = newPd.X; 
                this.APhysioParams[CurPDIndex].Y = newPd.Y;
                Sort();
                curve.Points = APhysioParams;
                if (showStyle == "显示数值")
                {
                    onlyMoveText(newPd, oldPd);
                }
                return newPd;
            }
            return null;
        }
        private void onlyMoveText(PhysioData newPd, PhysioData oldPd = null)
        {
            TextObj text = (TextObj)zgcAnas.MasterPane.GraphObjList[this.Enname + ((oldPd == null) ? newPd.RecordTime.ToString() : oldPd.RecordTime.ToString())];
            text.Location.Y = getYPositinByValue(newPd.Y, 240, 200, 20);
            text.Text = ((int)newPd.Value).ToString();
        }
        public void ModItem(PhysioData oldPd, PhysioData newPd)
        {
            //如果是负数则不执行操作
            if (newPd.Value < 0 && oldPd.PhysioDataConfigId != newPd.PhysioDataConfigId)
                return;
            if (curPDIndex != -1)
            {
                TimeSpan tsp = newPd.RecordTime - oldPd.RecordTime;
                newPd.RecordTime = oldPd.RecordTime;
                newPd = reCnterValue(newPd, false);
                //this.APhysioParams[CurPDIndex].X = newPd.X;
                this.APhysioParams[CurPDIndex].Y = newPd.Y;
                Sort();
                curve.Points = APhysioParams;
                PhysioDataService.UpdatePhysioData(oldPd, newPd);
                if (showStyle == "显示数值")
                {
                    delAddObj(oldPd);
                    DrawTextPhysioData(newPd);
                }
            }
        }
        public void AllModItem(PhysioData oldPd, PhysioData newPd)
        {
            PhysioDataService.UpdatePhysioData(oldPd, newPd);
        }
        public int indexOf(PhysioData pd)
        {
            for (int i = 0; i < APhysioParams.Count; i++)
            {
                if (pd.isEquert(PointPairToPhysioData(APhysioParams[i])))
                {
                    return i;
                }
            }
            return -1;
        }
        /// 
        /// 仅画点,不操作数据库
        /// 
        /// 
        public void AddPoition(PhysioData pd)
        {
            //如果是负数则不执行操作
            if (pd.Value < 0) return;
            //pd.Tag = pd.Id; 
            if (APhysioParams.Count > 0)
            {
                PhysioData pdTemp = pd.Clone();
                //当前点的前一个点加步长时间的结点
                PhysioData pdQD = reMPhysioData(pdTemp);
                //插入的点大于之前点5分钟以上
                if (DateTime.Compare(pd.RecordTime, pdQD.RecordTime.AddMinutes(1)) > 0)
                {
                    pdTemp.Value = Double.MaxValue;
                    this.APhysioParams.Add(pdTemp);
                }
            }
            pd = reCnterValue(pd, false);
            this.APhysioParams.Add(pd);
            Sort();
            curve.Points = APhysioParams;
            if (showStyle == "显示数值")
            {
                DrawTextPhysioData(pd);
            }
        }
        /// 
        /// 控制画点的范围
        /// 
        /// 物理点
        /// 是否采集
        /// 
        private PhysioData reCnterValue(PhysioData pd, bool isShouDong = true)
        {
            if (pd != null)
            {
                //因为物理数据的值小于下标值或大于上标值中,取上下中间的值
                if (pd.Value < lowLimit)
                {
                    pd.Value = lowLimit;
                }
                if (pd.Value > highLimit && pd.Value < double.MaxValue)
                {
                    pd.Value = highLimit;
                }
                if (APhysioParams.Count > 0 && isShouDong)
                {
                    Sort();
                    pd = reMPhysioData(pd);
                }
            }
            return pd;
        }
        /// 
        /// 返回小于传入点里小于且最接近的点
        /// 
        /// 传入点
        /// 
        private PhysioData reMPhysioData(PhysioData pd)
        {
            PhysioData maxPhysioData = null;
            for (int i = APhysioParams.Count - 1; i > -1; i--)
            {
                maxPhysioData = PointPairToPhysioData(APhysioParams[i]);
                int inTime = DateTime.Compare(pd.RecordTime, maxPhysioData.RecordTime);
                //且被大于的值的点不空隐藏
                if (inTime > 0 && maxPhysioData.Value != Double.MaxValue)
                {
                    pd.RecordTime = maxPhysioData.RecordTime.AddMinutes(this.X_MINOR_GRID_STEP);
                    return pd;
                }
            }
            //maxPhysioData = PointPairToPhysioData(APhysioParams[0]);
            //if (!pd.isEquert(maxPhysioData))
            //{
            //    pd.RecordTime = maxPhysioData.RecordTime.AddMinutes(-this.X_MINOR_GRID_STEP);
            //}
            return pd;
        }
        /// 
        /// 删除一点生理数据
        /// 
        /// 
        public void DelItem(PhysioData pd)
        {
            try
            {
                Sort();
                //如果是两头的点就删除,如果是中间的点就设置为无效
                bool isModmaxValue = false;
                for (int i = APhysioParams.Count - 1; i > -1; i--)
                {
                    PhysioData maxPhysioData = PointPairToPhysioData(APhysioParams[i]);
                    if (maxPhysioData.isEquert(pd) && i != 0 && i != APhysioParams.Count - 1)
                    {
                        APhysioParams[i].Y = Double.MaxValue;
                        isModmaxValue = true;
                        break;
                    }
                }
                if (!isModmaxValue)
                    this.APhysioParams.Remove(pd);
                curve.Points = APhysioParams;
                Sort();
                if (showStyle == "显示数值" || pd.Value == HighLimit)
                {
                    delAddObj(pd);
                }
                PhysioDataService.DelPhysioByValueData(pd);
                for (int i = APhysioParams.Count - 1; i > -1; i--)
                {
                    PhysioData maxPhysioData = PointPairToPhysioData(APhysioParams[i]);
                    if (maxPhysioData.isEquert(pd) && maxPhysioData.Y != Double.MaxValue)
                    {
                        this.APhysioParams.Remove(APhysioParams[i]);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                throw new Exception("删除生理数据结点时出错");
            }
        }
        public void DelAreaItem(int operationId, DateTime startTime, DateTime endTime, double startValue, double endValue)
        {
            try
            {
                Sort();
                bool isModmaxValue = false;
                for (int i = APhysioParams.Count - 1; i > -1; i--)
                {
                    PhysioData maxPhysioData = PointPairToPhysioData(APhysioParams[i]);
                    int startTimeDiff = DateTime.Compare(maxPhysioData.RecordTime, startTime);
                    double startValueDiff = startValue - maxPhysioData.Value;
                    int endTimeDiff = DateTime.Compare(maxPhysioData.RecordTime, endTime);
                    double endValueDiff = endValue - maxPhysioData.Value;
                    //在选区范围内
                    if (startTimeDiff > 0 && startValueDiff > 0 && endTimeDiff <= 0 && endValueDiff <= 0)
                    {
                        APhysioParams[i].Y = Double.MaxValue;
                        isModmaxValue = true;
                    }
                    ////在选区范围内
                    //if (startTimeDiff > 0 &&  endTimeDiff <= 0  && maxPhysioData.Y == Double.MaxValue)
                    //{
                    //    APhysioParams[i].Y = Double.MaxValue;
                    //    isModmaxValue = true; 
                    //}
                    if (isModmaxValue)
                    {
                        this.APhysioParams.Remove(maxPhysioData);
                        if (showStyle == "显示数值")
                        {
                            delAddObj(maxPhysioData);
                        }
                        isModmaxValue = false;
                    }
                }
                curve.Points = APhysioParams;
                Sort();
            }
            catch (Exception)
            {
            }
        }
        public void DelItems(DateTime RecordTime, DateTime EndTime)
        {
            try
            {
                PhysioDataService.DelPhysioasDataParameterID(RecordTime, EndTime, this.Id, this.PatientId);
            }
            catch (Exception)
            {
                throw new Exception("删除生理数据结点时出错");
            }
        }
        /// 
        /// 为生理数据集合排序
        /// 
        public void Sort()
        {
            aPhysioDatas.Sort();
        }
        /// 
        /// 画生理曲线,即初始化
        /// 
        public void draw(bool isValidLine = true)
        {
            if (zgcAnas == null)
            {
                //MessageBox.Show("未传递窗体或传递的不是麻醉单窗体");
                return;
            }
            PointPairList ppl = new PointPairList();
            curve = ZUtil.AddCurve(Name, ppl, conveColor, imgPath, true, zgcAnas, TextPrefix.PI + Name + Id.ToString());
            curve.YAxisIndex = this.YAisx;
            curve.Label.IsVisible = this.isValid;
            curve.Symbol.IsVisible = this.isValid;
            curve.Line.IsVisible = isValidLine;
            //if (this.isValid && showStyle == "显示数值")
            //{
            //    curve.Symbol.IsVisible = false;
            //}
            curve.Label.FontSpec = new FontSpec("微软雅黑", 5.9f, System.Drawing.Color.Black, false, false, false);
            curve.Label.FontSpec.Border.IsVisible = false;
            curve.Label.FontSpec.Fill.IsVisible = false;
            //if (!canClick)
            //{
            //    curve.Label.FontSpec = new FontSpec("微软雅黑", 5.1f, Color.Gray, false, false, false);
            //    curve.Label.FontSpec.Border.IsVisible = false;
            //}
        }
        public PhysioData PointPairToPhysioData(PointPair pp)
        {
            if (pp == null) return null;
            PhysioData pd = new PhysioData();
            pd.Value = pp.Y;
            pd.RecordTime = new XDate(pp.X);
            pd.PatientId = PatientId;
            pd.PhysioDataConfigId = Id;
            return pd;
        }
        /// 
        /// 删除麻醉单上的曲线本身,不包含数据删除
        /// 
        /// 
        public override void clearAddObj(ZedGraphControl zgc)
        {
            if (zgcAnas == null || zgcAnas != zgc) zgcAnas = zgc;
            if (showStyle == "显示数值")
            {
                foreach (PointPair pp in APhysioParams)
                {
                    PhysioData pd = PointPairToPhysioData(pp);
                    if (pd != null)
                        delAddObj(pd);
                }
            }
            //foreach (PointPair pp in APhysioParams)
            //{
            //    PhysioData pd = PointPairToPhysioData(pp);
            //    if (pd != null && pd.Value == HighLimit) delAddObj(pd);
            //}
            if (APhysioParams == null || curve == null) return;
            APhysioParams.Clear();
            //APhysioParams = new PointPairList();
            curve.Points = APhysioParams;
        }
        /// 
        /// 删除麻醉单上的曲线
        /// 
        /// 
        public void clearAddObjCurve(ZedGraphControl zgc)
        {
            delADDCurve(TextPrefix.PI + Name + Id.ToString());
        }
        /// 
        /// 重新设置曲线属性
        /// 
        public void reSetCurve()
        {
            this.curve.Color = this.ConveColor;
            this.curve.Symbol.Fill = new Fill(new ZUtil().getImage(this.imgPath), System.Drawing.Drawing2D.WrapMode.Clamp);
            this.curve.IsVisible = this.isValid;
            this.curve.YAxisIndex = this.YAisx;
            curve.Label.IsVisible = this.isValid;
            curve.Line.IsVisible = this.isValid;
            curve.Symbol.IsVisible = this.isValid;
            if (showStyle != "显示数值")
            {
                foreach (PointPair pp in APhysioParams)
                {
                    PhysioData pd = PointPairToPhysioData(pp);
                    if (pd != null)
                        delAddObj(pd);
                }
            }
            else
            {
                foreach (PointPair pp in APhysioParams)
                {
                    PhysioData pd = PointPairToPhysioData(pp);
                    if (pd != null) DrawTextPhysioData(pd);
                }
            }
        }
        /// 
        /// 重新设置曲线属性
        /// 
        public void reSetCurveSpo2()
        {
            this.curve.Color = this.ConveColor;
            this.curve.Symbol.Fill = new Fill(new ZUtil().getImage(this.imgPath), System.Drawing.Drawing2D.WrapMode.Clamp);
            this.curve.IsVisible = this.isValid;
            this.curve.YAxisIndex = this.YAisx;
            curve.Label.IsVisible = this.isValid;
            curve.Line.IsVisible = this.isValid;
            curve.Symbol.IsVisible = this.isValid;
        }
        public PhysioDataConfig Clone()
        {
            PhysioDataConfig _PhysioParameter = new PhysioDataConfig();
            _PhysioParameter.id = id;
            _PhysioParameter.Name = Name;
            _PhysioParameter.yAisx = yAisx;
            _PhysioParameter.isValid = isValid;
            _PhysioParameter.PatientId = PatientId;
            _PhysioParameter.highLimit = highLimit;
            _PhysioParameter.lowLimit = lowLimit;
            _PhysioParameter.conveColor = conveColor;
            _PhysioParameter.imgPath = imgPath;
            _PhysioParameter.curPDIndex = curPDIndex;
            _PhysioParameter.IsClick = IsClick;
            _PhysioParameter.curve = curve;
            _PhysioParameter.zgcAnas = zgcAnas;
            _PhysioParameter.showStyle = showStyle;
            _PhysioParameter.X_MINOR_GRID_STEP = X_MINOR_GRID_STEP;
            _PhysioParameter.className = className;
            if (aPhysioDatas.Count > 0)
            {
                _PhysioParameter.aPhysioDatas.Clear();
                foreach (PointPair _pp in aPhysioDatas)
                {
                    _PhysioParameter.aPhysioDatas.Add(PointPairToPhysioData(_pp));
                }
            }
            return _PhysioParameter;
        }
        /// 
        /// 清除各种对象数据
        /// 
        /// 
        public void ClearTagstr(ZedGraphControl zgc)
        {
            if (zgcAnas == null || zgcAnas != zgc) zgcAnas = zgc;
            delAddObj("PP" + Enname);
            delAddObj("PP" + Enname + "unit");
            delAddObj("PPP" + Enname);
            delAddObj("PPP" + Enname + "unit");
        }
        public static IList GetAllList()
        {
            string sql = "Select * FROM PhysioDataConfig where ConfigType='生命体征' order by PhysioOrder asc";
            return GetListBySql(sql);
        }
        private static IList GetListBySql(string sql, params SqlParameter[] para)
        {
            IList list = new List();
            using (SqlDataReader reader = DBHelper.GetReader(sql, para))
            {
                while (reader.Read())
                {
                    PhysioDataConfig temp = new PhysioDataConfig();
                    temp.Id = DBHelper.GetInt(reader["Id"]);
                    temp.Name = DBHelper.GetString(reader["Name"]);
                    temp.Enname = DBHelper.GetString(reader["Enname"]);
                    temp.Color = DBHelper.GetString(reader["Color"]);
                    temp.ImgPath = DBHelper.GetString(reader["imgPath"]);
                    temp.IsValid = DBHelper.GetInt(reader["IsValid"]) == 1 ? true : false;
                    temp.OperatorNo = DBHelper.GetString(reader["OperatorNo"]);
                    temp.OperatorName = DBHelper.GetString(reader["OperatorName"]);
                    temp.OperateDate = DBHelper.GetDateTime(reader["OperateDate"]);
                    temp.ConfigType = DBHelper.GetString(reader["ConfigType"]);
                    temp.ShowImg = DBHelper.GetInt(reader["ShowImg"]) == 1 ? true : false;
                    temp.ShowText = DBHelper.GetInt(reader["ShowText"]) == 1 ? true : false;
                    temp.HighLimit = DBHelper.GetInt(reader["HighLimit"]);
                    temp.LowLimit = DBHelper.GetInt(reader["LowLimit"]);
                    temp.WarningHighLimit = DBHelper.GetInt(reader["WarningHighLimit"]);
                    temp.WarningLowLimit = DBHelper.GetInt(reader["WarningLowLimit"]);
                    temp.IsSplit = DBHelper.GetInt(reader["IsSplit"]) == 1 ? true : false;
                    temp.IsDefalultShow = DBHelper.GetInt(reader["IsDefalultShow"]) == 1 ? true : false;
                    temp.Unit = DBHelper.GetString(reader["Unit"]);
                    temp.PhysioOrder = DBHelper.GetInt(reader["PhysioOrder"]);
                    temp.YAisx = DBHelper.GetInt(reader["YAisx"]);
                    list.Add(temp);
                }
                reader.Close();
                return list;
            }
        }
        public static int UpdatePhysioDataConfig(PhysioDataConfig physioDataConfig)
        {
            string sql = "Update PhysioDataConfig set HighLimit=@HighLimit, LowLimit=@LowLimit,ShowImg=@ShowImg, ShowText=@ShowText, WarningHighLimit=@WarningHighLimit, WarningLowLimit=@WarningLowLimit, IsSplit=@IsSplit, IsDefalultShow=@IsDefalultShow, Unit=@Unit, PhysioOrder=@PhysioOrder, YAisx=@YAisx where Id=@Id";
            SqlParameter[] para = new SqlParameter[]
                                     {
                                        new SqlParameter("@Id",physioDataConfig.Id), new SqlParameter("@HighLimit",physioDataConfig.HighLimit), new SqlParameter("@LowLimit",physioDataConfig.LowLimit),  new SqlParameter("@ShowImg",physioDataConfig.ShowImg), new SqlParameter("@ShowText",physioDataConfig.ShowText), new SqlParameter("@WarningHighLimit",physioDataConfig.WarningHighLimit), new SqlParameter("@WarningLowLimit",physioDataConfig.WarningLowLimit), new SqlParameter("@IsSplit",physioDataConfig.IsSplit), new SqlParameter("@IsDefalultShow",physioDataConfig.IsDefalultShow), new SqlParameter("@Unit",physioDataConfig.Unit), new SqlParameter("@PhysioOrder",physioDataConfig.PhysioOrder), new SqlParameter("@YAisx",physioDataConfig.YAisx)
                                  };
            return DBHelper.ExecNonQuery(sql, para);
        }
    }
}