根据时间计算价格

在项目中遇到根据时间技算价格,记录下

 最初版源码

  /// <summary>
        /// 时间计算
        /// </summary>
        /// <param name="startTime">订单创建时间</param>
        /// <param name="isFirst">首次</param>
        /// <param name="dayFirstPrice">一小时首次价格</param>
        /// <param name="dayPrice">每30分钟的价格</param>
        /// <param name="nightPrice">夜间价格</param>
        /// <returns></returns>
        public decimal GetSumPrice(DateTime startTime, bool isFirst, decimal dayFirstPrice, decimal dayPrice, decimal nightPrice)
        {
           
            //白天时间范围
            var dayStartTime = DateTime.Parse(startTime.ToLongDateString() + " 07:30:00");
            var dayEndTime = DateTime.Parse(startTime.ToLongDateString() + " 19:30:00");
            //夜间时间范围
            var nightStartTime = DateTime.Parse(startTime.ToLongDateString() + " 19:30:00");
            var nightEndTime = DateTime.Parse(startTime.AddDays(1).ToLongDateString() + " 07:30:00");
            //总价
            decimal price = 0;
            //计算白天总分钟数
            int dayMinutes = Convert.ToInt32(dayEndTime.Subtract(dayStartTime).TotalMinutes);
            //计算晚上总分钟数
            int nightMinustes = Convert.ToInt32(nightEndTime.Subtract(nightStartTime).TotalMinutes);
            //结束时间已系统当前时间为准
            var endTime = DateTime.Now;
            //停车总分钟数
            int sumMinustes = Convert.ToInt32(endTime.Subtract(startTime).TotalMinutes);
            //白天开始
            if (startTime > dayStartTime && startTime < dayEndTime)
            {
                //没有超过白天停车时间
                if (sumMinustes <= dayMinutes)
                {
                    //停车时间超过5分钟,或者不是今天第一次停车
                    if (sumMinustes > 5 || !isFirst)
                    {
                        price = GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, isFirst);
                    }
                }
                else
                {
                    //第一天白天分钟数
                    int fristDayMinutes = Convert.ToInt32(dayEndTime.Subtract(startTime).TotalMinutes);
                    //第一天金额
                    price = GetDayPrice(fristDayMinutes, dayPrice, dayFirstPrice, isFirst) + nightPrice;
                    //减去第一天已算时间
                    sumMinustes = sumMinustes - fristDayMinutes - nightMinustes;
                    while (sumMinustes > 5)
                    {
                        if (sumMinustes - dayMinutes <= 0)
                        {
                            price += GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, isFirst);
                            break;
                        }
                        else
                        {
                            price += GetDayPrice(dayMinutes, dayPrice, dayFirstPrice, isFirst) + nightPrice;
                            sumMinustes = sumMinustes - dayMinutes - nightMinustes;
                        }
                    }
                }
            }
            else
            {
                //晚上开始停
                price = nightPrice;
                if (sumMinustes > nightMinustes)
                {
                    sumMinustes -= nightMinustes;
                    while (sumMinustes > 5)
                    {
                        if (sumMinustes - dayMinutes <= 0)
                        {
                            price += GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, isFirst);
                            break;
                        }
                        else
                        {
                            price += GetDayPrice(dayMinutes, dayPrice, dayFirstPrice, isFirst) + nightPrice;
                            sumMinustes = sumMinustes - dayMinutes - nightMinustes;
                        }
                    }
                }
            }
            return price;
        }
        /// <summary>
        /// 计算价格
        /// </summary>
        /// <param name="minutes"></param>
        /// <param name="price"></param>
        /// <param name="fristPrice"></param>
        /// <param name="isFirst"></param>
        /// <returns></returns>
        public static decimal GetDayPrice(int minutes, decimal price, decimal fristPrice, bool isFirst)
        {
            //首次
            if (minutes <= 60 && isFirst)
            {
                return fristPrice;
            }
            //半小时
            else if (isFirst)
            {
                int len = (minutes - 60) / 30;
                len = (minutes - 60) % 30 != 0 ? len + 1 : len;
                return len * price + fristPrice;
            }
            //不是首次5分钟
            else
            {
                int len = minutes / 30;
                len = minutes % 30 != 0 ? len + 1 : len;
                return len * price;
            }
        }
View Code

 最新代码 相比之前的,修改了循环计算每天价格

 /// <summary>
        /// 时间计算
        /// </summary>
        /// <param name="startTime">订单创建时间</param>
        /// <param name="isFirst">首次</param>
        /// <param name="dayFirstPrice">一小时首次价格</param>
        /// <param name="dayPrice">每30分钟的价格</param>
        /// <param name="nightPrice">夜间价格</param>
        /// <returns></returns>
        public decimal GetSumPrice(DateTime startTime, bool isFirst, decimal dayFirstPrice, decimal dayPrice, decimal nightPrice)
        {
            //白天时间范围
            var dayStartTime = DateTime.Parse(startTime.ToLongDateString() + " 07:30:00");
            var dayEndTime = DateTime.Parse(startTime.ToLongDateString() + " 19:30:00");
            //夜间时间范围
            var nightStartTime = DateTime.Parse(startTime.ToLongDateString() + " 19:30:00");
            var nightEndTime = DateTime.Parse(startTime.AddDays(1).ToLongDateString() + " 07:30:00");
            //总价
            decimal price = 0;
            //计算白天总分钟数
            int dayMinutes = Convert.ToInt32(dayEndTime.Subtract(dayStartTime).TotalMinutes);
            //计算晚上总分钟数
            int nightMinustes = Convert.ToInt32(nightEndTime.Subtract(nightStartTime).TotalMinutes);
            //结束时间已系统当前时间为准
            var endTime = DateTime.Now;
            //停车总分钟数
            int sumMinustes = Convert.ToInt32(endTime.Subtract(startTime).TotalMinutes);
            //白天开始
            if (startTime > dayStartTime && startTime < dayEndTime)
            {
                if (sumMinustes <= dayMinutes)//没有超过白天停车时间
                {
                    //停车时间超过5分钟,或则不是今天第一次停车
                    if (sumMinustes > 5 || !isFirst)
                    {
                        price = GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, isFirst);
                    }
                }
                else
                {
                    //第一天白天分钟数
                    int fristDayMinutes = Convert.ToInt32(dayEndTime.Subtract(startTime).TotalMinutes);
                    //第一天金额
                    price = GetDayPrice(fristDayMinutes, dayPrice, dayFirstPrice, isFirst) + nightPrice;
                    //减去第一天已算时间
                    sumMinustes = sumMinustes - fristDayMinutes - nightMinustes;
                    price += GetBehindPrice(sumMinustes, dayMinutes, dayPrice, dayFirstPrice, nightPrice);
                }
            }
            else//晚上开始停
            {
                price = nightPrice;
                if (sumMinustes > nightMinustes)
                {
                    sumMinustes -= nightMinustes;
                    price += GetBehindPrice(sumMinustes, dayMinutes, dayPrice, dayFirstPrice, nightPrice);
                }
            }
            return price;
        }
        /// <summary>
        /// 剩下的价格
        /// </summary>
        /// <param name="sumMinustes">剩下的分钟数</param>
        /// <param name="dayMinutes">白天的分钟数</param>
        /// <param name="dayPrice">白天的价格(每30分钟)</param>
        /// <param name="dayFirstPrice">白天第一个的价格</param>
        /// <param name="nightPrice">晚上每次的价格</param>
        /// <returns></returns>
        private decimal GetBehindPrice(int sumMinustes, int dayMinutes, decimal dayPrice, decimal dayFirstPrice, decimal nightPrice)
        {
            decimal price = 0;
            int dayNum = sumMinustes / 1440;
            //一整天的价格
            decimal allDay = GetDayPrice(dayMinutes, dayPrice, dayFirstPrice, true) + nightPrice;
            price += allDay * dayNum;
            sumMinustes = sumMinustes % 1440;
            if (sumMinustes > 5)
            {
                if (sumMinustes - dayMinutes <= 0)
                {
                    price += GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, true);
                }
                else
                {
                    price += allDay;
                }
            }
            return price;
        }
        /// <summary>
        /// 计算价格
        /// </summary>
        /// <param name="minutes"></param>
        /// <param name="price"></param>
        /// <param name="fristPrice"></param>
        /// <param name="isFirst"></param>
        /// <returns></returns>
        private decimal GetDayPrice(int minutes, decimal price, decimal fristPrice, bool isFirst)
        {
            //每次停车首小时
            if (minutes <= 60)
            {
                return fristPrice;
            }
            //半小时
            else if (isFirst)
            {
                int len = (minutes - 60) / 30;
                len = (minutes - 60) % 30 != 0 ? len + 1 : len;
                return len * price + fristPrice;
            }
            //不是首次5分钟
            else
            {
                int len = minutes / 30;
                len = minutes % 30 != 0 ? len + 1 : len;
                return len * price;
            }
        }
View Code

 

posted @ 2017-08-30 14:31  ice.ko  阅读(390)  评论(0编辑  收藏  举报