/*
         * Методы
         */
        #region public static AverageUtilization ConvertFromByteArray(byte[] data)
        /// <summary>
        /// Конвертирует данные из БД в AverageUtilization
        /// </summary>
        /// <param name="data"></param>
        public static AverageUtilization ConvertFromByteArray(byte[] data)
        {
            AverageUtilization item = new AverageUtilization();

            if (data == null)
            {
                return(item);
            }

            byte[] binaryData = data;
            if (null == binaryData)
            {
                return(item);
            }

            if (binaryData == null || binaryData.Length != SerializedDataLength)
            {
                throw new ArgumentException("Data cannot be converted to Lifelength");
            }

            item.SelectedInterval = (UtilizationInterval)DbTypes.Int32FromByteArray(binaryData, 0);

            item._hoursPerMonth  = BitConverter.ToDouble(binaryData, 4);
            item._cyclesPerMonth = BitConverter.ToDouble(binaryData, 12);
            return(item);
        }
        /// <summary>
        /// Возвращает приблизительное количество дней, за которое наберется ресурс remains с заданной среднестатистической наработкой
        /// </summary>
        /// <param name="from"></param>
        /// <param name="remains"></param>
        /// <param name="average"></param>
        /// <param name="conditionType"></param>
        /// <returns></returns>
        public static Double?GetApproximateDays(DateTime from,
                                                Lifelength remains, AverageUtilization average,
                                                ThresholdConditionType conditionType = ThresholdConditionType.WhicheverFirst)
        {
            //
            if (remains == null || average == null)
            {
                return(null);
            }

            if (remains.CalendarValue != null && remains.CalendarValue != 0)
            {
                var to = from.AddCalendarSpan(remains.CalendarSpan);
                return((to - from).Days);
            }

            var    d1 = average.CyclesPerMonth != 0 && remains.Cycles != null ? new Double?(remains.Cycles.Value / (average.Hours / average.Cycles)) : null;
            var    d2 = average.HoursPerMonth != 0 && remains.Hours != null ? remains.Hours * 30 / average.HoursPerMonth : null;
            Double?d3 = remains.Days;

            // Whichever First vs. Whichever Later
            if (conditionType == ThresholdConditionType.WhicheverFirst)
            {
                // Выбираем минимум
                Double?min = null;
                if (d1 != null)
                {
                    min = d1;
                }
                if (d2 != null && (min == null || d2 < min))
                {
                    min = d2;
                }
                if (d3 != null && (min == null || d3 < min))
                {
                    min = d3;
                }
                // Возвращаем результат
                return(min);
            }

            // Выбираем максимум
            Double?max = null;

            if (d1 != null)
            {
                max = d1;
            }
            if (d2 != null && (max == null || d2 > max))
            {
                max = d2;
            }
            if (d3 != null && (max == null || d3 > max))
            {
                max = d3;
            }
            // Возвращаем результат
            return(max);
        }
        /// <summary>
        /// Возвращает наработку агрегата или налет воздушного судна за указанный период времени
        /// </summary>
        /// <param name="average"></param>
        /// <param name="days"></param>
        /// <returns></returns>
        public static Lifelength GetUtilization(AverageUtilization average, Int32 days)
        {
            var res = Lifelength.Zero;

            if (average != null && average.CyclesPerMonth != 0 && average.HoursPerMonth != 0)
            {
                res.Cycles       = (int)(days * average.CyclesPerMonth / 30);
                res.TotalMinutes = (int)(days * average.HoursPerMonth * 60 / 30);
            }

            res.Days = days;
            return(res);
        }
        /// <summary>
        /// Возвращает дату, когда наберется ресурс remains с заданной среднестатистической наработкой
        /// </summary>
        /// <param name="remains"></param>
        /// <param name="average"></param>
        /// <param name="conditionType"></param>
        /// <returns></returns>
        public static DateTime?GetApproximateDate(Lifelength remains, AverageUtilization average, ThresholdConditionType conditionType = ThresholdConditionType.WhicheverFirst)
        {
            //
            if (remains == null || average == null)
            {
                return(null);
            }

            // расчитываем количество дней
            var days = GetApproximateDays(DateTime.Today, remains, average, conditionType);

            if (days == null)
            {
                return(null);
            }

            // возвращаем дату
            return(DateTime.Today.AddDays(days.Value));
        }