美文网首页
优惠券设计

优惠券设计

作者: airron | 来源:发表于2022-08-19 13:22 被阅读0次
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CouponsMg.Coupons
    {
        public enum Period
        {
            /// <summary>
            /// 1小时
            /// </summary>
            [Description("1小时")]
            OneHour = 1,
    
            /// <summary>
            /// 2小时
            /// </summary>
            [Description("2小时")]
            TwoHours = 2,
    
            /// <summary>
            /// 3小时
            /// </summary>
            [Description("3小时")]
            ThreeHours = 3,
    
            /// <summary>
            /// 4小时
            /// </summary>
            [Description("4小时")]
            FourHours = 4,
    
            /// <summary>
            ///5小时
            /// </summary>
            [Description("5小时")]
            FiveHours = 5,
    
            /// <summary>
            /// 6小时
            /// </summary>
            [Description("6小时")]
            SixHours = 6,
    
            /// <summary>
            /// 8小时
            /// </summary>
            [Description("8小时")]
            EightHours = 8,
    
            /// <summary>
            /// 9小时
            /// </summary>
            [Description("9小时")]
            TenHours = 9,
    
            /// <summary>
            /// 12小时
            /// </summary>
            [Description("12小时")]
            TwelveHours = 12,
    
            /// <summary>
            /// 1天
            /// </summary>
            [Description("1天")]
            OneDay = 24,
    
            /// <summary>
            /// 2天
            /// </summary>
            [Description("2天")]
            TwoDays = OneDay * 2,
    
            /// <summary>
            ///3天
            /// </summary>
            [Description("3天")]
            ThreeDays = OneDay * 3,
    
            /// <summary>
            /// 5天
            /// </summary>
            [Description("5天")]
            FiveDays = OneDay * 5,
    
            /// <summary>
            /// 7天
            /// </summary>
            [Description("7天")]
            SevenDays = OneDay * 7,
    
            /// <summary>
            /// 14天
            /// </summary>
            [Description("14天")]
            FourteenDays = OneDay * 14,
    
            /// <summary>
            /// 1个月
            /// </summary>
            [Description("1个月")]
            OneMonth = OneDay * 30,
    
            /// <summary>
            /// 3个月
            /// </summary>
            [Description("3个月")]
            ThreeMonths = OneMonth *3,
    
            /// <summary>
            /// 6个月
            /// </summary>
            [Description("6个月")]
            SixMounths = OneMonth *6,
    
            /// <summary>
            /// 1年
            /// </summary>
            [Description("1年")]
            OneYear = OneMonth *12,
        }
    
        public abstract class CouponsBaseModel : INotifyPropertyChanged
        {
    
            #region Property Changed
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            #endregion
    
            #region 属性
    
            #region 领取时间段
    
            /// <summary>
            /// 可领取优惠券期限范围类型。默认为7天
            /// </summary>
            public Period CanPickupPeriodType { get; set; } = Period.SevenDays;
    
            private DateTime canPickUpStartTime = DateTime.Now;
            /// <summary>
            /// 优惠券可领取开始日期 默认为 当前日期
            /// </summary>
            public DateTime CanPickUpStartTime
            {
                get { return canPickUpStartTime; }
                set
                {
                    if (value == null)
                    {
                        return;
                    }
    
                    canPickUpStartTime = value;
                    CanPickUpEndTime = canPickUpStartTime.AddHours((int)CanPickupPeriodType);
    
                    OnPropertyChanged(nameof(CanPickUpStartTime));
                }
            }
    
            private DateTime canPickUpEndTime = DateTime.Now.AddDays(7);
            /// <summary>
            /// 优惠券可领取的截止日期 默认为当前日期后7天
            /// </summary>
            public DateTime CanPickUpEndTime
            {
                get { return canPickUpEndTime; }
                set
                {
                    if (value == null)
                    {
                        return;
                    }
    
                    if (CanPickUpStartTime > value)
                    {
                        throw new ArgumentOutOfRangeException("优惠券可领取开始日期不能大于截止日期");
                    }
    
                    canPickUpEndTime = value;
                    OnPropertyChanged(nameof(CanPickUpEndTime));
                }
            }
    
            #endregion
    
            #region 有效期时间段
    
            /// <summary>
            /// 优惠券有效期时间段类型,默认为7天
            /// </summary>
            public Period EffectivePeriodType { get; set; } = Period.SevenDays;
    
            private DateTime effectiveDate = DateTime.Now;
            /// <summary>
            /// 优惠券生效日期. 默认为当前日期
            /// </summary>
            public DateTime EffectiveDate
            {
                get { return effectiveDate; }
                set
                {
                    if (value == null)
                    {
                        return;
                    }
    
                    effectiveDate = value;
                    ExpireDate = effectiveDate.AddHours((int)EffectivePeriodType);
                    OnPropertyChanged(nameof(EffectiveDate));
                }
            }
    
            private DateTime expireDate = DateTime.Now.AddDays(7);
            /// <summary>
            /// 优惠券失效日期 默认为当前日期后7天
            /// </summary>
            public DateTime ExpireDate
            {
                get { return expireDate; }
                set
                {
                    if (value == null)
                    {
                        return;
                    }
    
                    if (effectiveDate > expireDate)
                    {
                        throw new ArgumentOutOfRangeException("优惠券生效日期不能大于失效日期");
                    }
    
                    expireDate = value;
                    OnPropertyChanged(nameof(ExpireDate));
                }
            }
    
            #endregion
    
            private double thresholdAmount = 0;
            /// <summary>
            /// 优惠券使用门槛。<=0 为无门槛. 默认为无门槛
            /// </summary>
            public double ThresholdAmount
            {
                get { return thresholdAmount; }
                set
                {
                    if (value <= 0)
                    {
                        thresholdAmount = 0;
                    }
                    else
                    {
                        thresholdAmount = value;
                    }
    
                    OnPropertyChanged(nameof(ThresholdAmount));
                }
            }
    
    
            private bool isStackable = false;
            /// <summary>
            /// 优惠券是否可叠加使用. 默认为不可叠加使用
            /// </summary>
            public bool IsStackable
            {
                get { return isStackable; }
                set
                {
                    isStackable = value;
                    OnPropertyChanged(nameof(IsStackable));
                }
            }
    
    
            private bool isAutoPickUp = false;
            /// <summary>
            /// 是否可自动领取. 默认为不可自动领取
            /// </summary>
            public bool IsAutoPickUp
            {
                get { return isAutoPickUp; }
                set
                {
                    isAutoPickUp = value;
                    if (isAutoPickUp)
                    {
                        IsPickedUp = true;
                    }
    
                    OnPropertyChanged(nameof(IsAutoPickUp));
                }
            }
    
    
            private bool isPickedUp = false;
            /// <summary>
            /// 是否已领取
            /// </summary>
            public bool IsPickedUp
            {
                get { return isPickedUp; }
                set
                {
                    isAutoPickUp = value;
                    OnPropertyChanged(nameof(IsPickedUp));
                }
            }
    
            //是否过期
            public bool IsExpired => IsPickedUp && ExpireDate < EffectiveDate;
    
    
            private bool isAvailable = true;
            /// <summary>
            /// 是否可用。 如为否,则不可领取,不可使用
            /// </summary>
            public bool IsAvailable
            {
                get { return isAvailable; }
                set
                {
                    isAvailable = value;
                    OnPropertyChanged(nameof(IsAvailable));
                }
            }
    
            /// <summary>
            /// 是否可领取
            /// </summary>
            public bool CanPickUp { get; set; }
    
            #endregion
    
            public void PickUp()
            {
                bool canPickup = CheckCouponsIsInAvailablePickupPeriod();
                if (canPickup)
                {
                    IsPickedUp = true;
                    EffectiveDate = DateTime.Now;
    
                }
    
            }
    
            /// <summary>
            /// 检查优惠券是否在可领取日期范围内
            /// </summary>
            /// <returns></returns>
            private bool CheckCouponsIsInAvailablePickupPeriod()
            {
                if (CanPickUpStartTime == null || CanPickUpEndTime == null)
                {
                    return false;
                }
    
                DateTime now = DateTime.Now;
    
                //还未到可领取事件,不可领取
                if (now < CanPickUpStartTime)
                {
                    return false;
                }
    
                //已过领取事件,不可领取
                if (now >= CanPickUpEndTime)
                {
                    return false;
                }
    
                return true;
            }
    
            /// <summary>
            /// 检查优惠券可领取状态
            /// </summary>
            /// <returns></returns>
            private bool CheckCouponsCanPickableState()
            {
                if (IsPickedUp)
                {
                    return false;
                }
    
                //是否在可领取日期范围内
                bool isInAvailablePickupPeriod = CheckCouponsIsInAvailablePickupPeriod();
                return isInAvailablePickupPeriod;
            }
    
            private bool CheckCouponsAvailableState()
            {
                //优惠券已领取
                if (IsPickedUp)
                {
                    if (EffectiveDate == null || ExpireDate == null)
                    {
                        return false;
                    }
    
                    DateTime now = DateTime.Now;
    
                    //已过有效期
                    if (now >= CanPickUpEndTime)
                    {
                        return false;
                    }
    
                    return true;
                }
                else
                {
                    //是否在可领取日期范围内
                    bool isInAvailablePickupPeriod = CheckCouponsIsInAvailablePickupPeriod();
                    //已过可领取日期
                    if (!isInAvailablePickupPeriod)
                    {
                        return false;
                    }
                }
    
                return true;
            }
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:优惠券设计

          本文链接:https://www.haomeiwen.com/subject/ffkugrtx.html