예제 #1
0
        /// <summary>
        /// Вызывается при запросе клиентом с веба.
        /// </summary>
        /// <param name="maq"></param>
        /// <param name="days"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        private dynamic Build0(dynamic maq, IEnumerable <DateTime> days, Guid userId)
        {
            dynamic ret = new ExpandoObject();

            ret.success = false;
            ret.error   = string.Empty;

            var infos = new List <Matrix.Common.Maquette.Maquette80020>();

            var dmaq = maq as IDictionary <string, object>;

            //// сборка макета отключена
            //if (dmaq.ContainsKey("disable") && maq.disable == true) return infos;

            var startNumber = dmaq.ContainsKey("lastNumber") ? (int)maq.lastNumber : 1;

            var tubes   = new List <dynamic>();
            var tubeIds = new List <Guid>();

            foreach (var tube in maq.Tube)
            {
                tubes.Add(tube);
                tubeIds.Add(Guid.Parse((string)tube.id));
            }

            if (tubeIds.Count() == 0)
            {
                ret.error = "Макет не связан с точками учёта";
                return(ret);
            }

            var cnt = 0;

            var hours = new List <dynamic>();

            foreach (var day in days.OrderBy(d => d))
            {
                var start = day.Date;
#if MAQ_NEXTHOUR
                var end = day.Date.AddDays(2);
#else
                var end = day.Date.AddDays(1);
#endif
                var data = Data.Cache.Instance.GetRecords(start, end, "Hour", tubeIds.ToArray());
                cnt = data.Count();
                hours.AddRange(data);
            }

            foreach (var day in days)
            {
                var m = new Matrix.Common.Maquette.Maquette80020();
                m.DateTime.DayAsDateTime       = day;
                m.DateTime.TimestampAsDateTime = DateTime.Now;
                m.Number      = startNumber++;
                m.Sender.Inn  = maq.Inn;
                m.Sender.Name = maq.organization;

                var a = new Matrix.Common.Maquette.Area();
                m.Areas.Add(a);
                a.Inn  = maq.Inn;
                a.Name = maq.organization;

                foreach (var tube in tubes)
                {
                    var dtube  = tube as IDictionary <string, object>;
                    var tubeId = Guid.Parse((string)tube.id);
                    var rows   = StructureGraph.Instance.GetRows(new Guid[] { tubeId }, userId);
                    var area   = rows.FirstOrDefault().Area[0];

                    var mp = new Matrix.Common.Maquette.MeasuringPoint();
                    mp.Code = tube.code;
                    mp.Name = string.Format("{0} {1}", area.name, tube.name);
                    var parameters = StructureGraph.Instance.GetParameters(tubeId, userId);

                    var findDuplicate = new Dictionary <string, bool>();

                    foreach (var tubeParameter in parameters)
                    {
                        var dtp = tubeParameter as IDictionary <string, object>;
                        if (!dtp.ContainsKey("tag"))
                        {
                            continue;
                        }

                        string tag = tubeParameter.tag;
                        if (findDuplicate.ContainsKey(tag))
                        {
                            continue;
                        }
                        findDuplicate[tag] = true;

                        double kiu = 1.0;
                        if (dtube.ContainsKey("KTr"))
                        {
                            double.TryParse(tube.KTr.ToString(), out kiu);
                        }

                        if (new string[] { "01", "02", "03", "04", "11", "12", "13", "14" }.Contains(tag))
                        {
                            //* Было до 6/06/2017 7:25 нет нулевого часа следующего дня
                            var dayHours = hours.Where(h => h.S1 == tubeParameter.name && h.Date.Date == day && h.ObjectId == tubeId).OrderBy(h => h.Date).ToList();
                            //var dayHours = hours.Where(h => h.S1 == tubeParameter.name && h.Date.Date >= day && h.Date.Date <= day.AddDays(2)  && h.ObjectId == tubeId).OrderBy(h => h.Date).ToList();
                            //var dayHoursNext = hours.Where(h => h.S1 == tubeParameter.name && h.Date.Date == day.AddDays(1) && h.ObjectId == tubeId).OrderBy(h => h.Date).ToList(); //z

                            //var cntHours = dayHoursNext.Count;
                            var mc = new Matrix.Common.Maquette.MeasuringChannel();
                            mp.MeasuringChannels.Add(mc);
                            mc.Code = tag;
                            mc.Name = GetChannelDesc(tag);

                            for (DateTime hour = day; hour < day.AddDays(1); hour = hour.AddHours(1))
                            {
#if MAQ_NEXTHOUR
                                var hourData = dayHours.FirstOrDefault(h => h.Date == hour.AddHours(1));    // Начинается с нуля часов, на данные надо брать за +1 час

                                //zvar tmp = "нет";
                                if (hour.Hour == 23)
                                {
                                    hourData = dayHoursNext.FirstOrDefault(h => h.Date == hour.AddHours(1));
                                    //ztmp = hour.AddHours(1).ToString("<dd.mm.yy HHmm>");
                                }
#else
                                //* Было до 6/06/2017 6:51
                                var hourData = dayHours.FirstOrDefault(h => h.Date == hour);
#endif
                                var p = new Matrix.Common.Maquette.Period();
                                //zp.Start = hour.ToString("dd.mm.yy HHmm") + tmp+" cntHours:"+ cntHours.ToString()+" ";                              //*  Начало периода
                                p.Start        = hour.ToString("HHmm");                       //*  Начало периода
                                p.End          = hour.AddHours(1).ToString("HHmm");           //*  Конец периода
                                p.Value.Data   = 0;
                                p.Value.Status = 1;

                                if (hourData != null)
                                {
                                    var val = hourData.D1 * kiu;
                                    p.Value.Data   = Math.Round(val, 5);
                                    p.Value.Status = 0;
                                }
                                mc.Periods.Add(p);
                            }
                        }
                    }

                    if (mp.MeasuringChannels.Count == 0)
                    {
                        ret.error = "Измерительные каналы не найдены";
                        return(ret);
                    }

                    a.MeasuringPoints.Add(mp);
                }
                infos.Add(m);
            }
            maq.lastNumber = startNumber;
            StructureGraph.Instance.SaveMaquette(maq);

            ret.success = true;
            ret.infos   = infos;
            return(ret);
        }
예제 #2
0
        /// <summary>
        /// Вызывается при автоматической рассылке.
        /// </summary>
        /// <param name="maquetteId"></param>
        /// <param name="days"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public dynamic Build1(Guid maquetteId, IEnumerable <DateTime> days, Guid userId)
        {
            log.Trace("Начало отправки макета [maquetteId:{0}] за {1:dd.MM.yyyy HH:mm} от [userId:{2}]", maquetteId, days.OrderBy(d => d).FirstOrDefault(), userId);

            dynamic ret = new ExpandoObject();

            ret.success = true;
            ret.error   = string.Empty;

            ret.maquetteName = string.Format("[maquetteId:{0}]", maquetteId);

            var infos = new List <Matrix.Common.Maquette.Maquette80020>();

            var maq = StructureGraph.Instance.GetMaquetteById(maquetteId, userId);

            if (maq == null)
            {
                ret.error   = "Макет не существует/не найден";
                ret.success = false;
            }
            else
            {
                var dmaq = maq as IDictionary <string, object>;

                var maquetteName = dmaq.ContainsKey("name") ? maq.name : "<макет без названия>";
                ret.maquetteName = maquetteName;

                //// сборка макета отключена
                //if (dmaq.ContainsKey("disable") && maq.disable == true) return infos;

                var startNumber = dmaq.ContainsKey("lastNumber") ? (int)maq.lastNumber : 1;

                var tubes   = new List <dynamic>();
                var tubeIds = new List <Guid>();
                foreach (var tube in maq.Tube)
                {
                    tubes.Add(tube);
                    tubeIds.Add(Guid.Parse((string)tube.id));
                }

                if (tubeIds.Count() == 0)
                {
                    ret.success = false;
                    ret.error   = "Макет не связан с точками учёта";
                }
                else
                {
                    var hours = new List <dynamic>();
                    foreach (var day in days.OrderBy(d => d))
                    {
                        var start = day.Date;
                        var end   = day.Date.AddDays(1);
                        var data  = Data.Cache.Instance.GetRecords(start, end, "Hour", tubeIds.ToArray());
                        hours.AddRange(data);
                    }

                    foreach (var day in days)
                    {
                        var m = new Matrix.Common.Maquette.Maquette80020();
                        m.DateTime.DayAsDateTime       = day;
                        m.DateTime.TimestampAsDateTime = DateTime.Now;
                        m.Number      = startNumber++;
                        m.Sender.Inn  = maq.Inn;
                        m.Sender.Name = maq.organization;

                        var a = new Matrix.Common.Maquette.Area();
                        m.Areas.Add(a);
                        a.Inn  = maq.Inn;
                        a.Name = maq.organization;

                        foreach (var tube in tubes)
                        {
                            var dtube  = tube as IDictionary <string, object>;
                            var tubeId = Guid.Parse((string)tube.id);
                            var rows   = StructureGraph.Instance.GetRows(new Guid[] { tubeId }, userId);
                            var area   = rows.FirstOrDefault().Area[0];

                            var mp = new Matrix.Common.Maquette.MeasuringPoint();
                            mp.Code = tube.code;
                            mp.Name = string.Format("{0} {1}", area.name, tube.name);
                            var parameters = StructureGraph.Instance.GetParameters(tubeId, userId);

                            var findDuplicate = new Dictionary <string, bool>();

                            foreach (var tubeParameter in parameters)
                            {
                                var dtp = tubeParameter as IDictionary <string, object>;
                                if (!dtp.ContainsKey("tag"))
                                {
                                    continue;
                                }

                                string tag = tubeParameter.tag;
                                if (findDuplicate.ContainsKey(tag))
                                {
                                    continue;
                                }
                                findDuplicate[tag] = true;

                                double kiu = 1.0;
                                if (dtube.ContainsKey("KTr"))
                                {
                                    double.TryParse(tube.KTr.ToString(), out kiu);
                                }

                                if (new string[] { "01", "02", "03", "04", "11", "12", "13", "14" }.Contains(tag))
                                {
                                    var dayHours = hours.Where(h => h.S1 == tubeParameter.name && h.Date.Date == day && h.ObjectId == tubeId).OrderBy(h => h.Date).ToList();
                                    var mc       = new Matrix.Common.Maquette.MeasuringChannel();
                                    mp.MeasuringChannels.Add(mc);
                                    mc.Code = tag;
                                    mc.Name = GetChannelDesc(tag);

                                    for (DateTime hour = day; hour < day.AddDays(1); hour = hour.AddHours(1))
                                    {
                                        var hourData = dayHours.FirstOrDefault(h => h.Date == hour);
                                        var p        = new Matrix.Common.Maquette.Period();
                                        p.Start        = hour.ToString("HHmm");
                                        p.End          = hour.AddHours(1).ToString("HHmm");
                                        p.Value.Data   = 0;
                                        p.Value.Status = 1;

                                        if (hourData != null)
                                        {
                                            var val = hourData.D1 * kiu;
                                            p.Value.Data   = Math.Round(val, 5);
                                            p.Value.Status = 0;
                                        }
                                        mc.Periods.Add(p);
                                    }
                                }
                            }

                            if (mp.MeasuringChannels.Count == 0)
                            {
                                ret.error   = "Измерительные каналы не найдены";
                                ret.success = false;
                                break;
                            }
                            else
                            {
                                a.MeasuringPoints.Add(mp);
                            }
                        }

                        if (ret.success)
                        {
                            infos.Add(m);
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (ret.success)
                    {
                        maq.lastNumber = startNumber;
                        StructureGraph.Instance.SaveMaquette(maq);
                    }
                }
            }

            log.Debug("Результат отправки макетов {0} за {1:dd.MM.yyyy} - {2}", ret.maquetteName, days.OrderBy(d => d).FirstOrDefault(), ret.success ? "успех" : ret.error);
            log.Trace("Окончание отправки макетов");

            ret.infos = infos;
            return(ret);
        }