WPF優(yōu)惠券設(shè)計(jì)


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小時(shí)
        /// </summary>
        [Description("1小時(shí)")]
        OneHour = 1,

        /// <summary>
        /// 2小時(shí)
        /// </summary>
        [Description("2小時(shí)")]
        TwoHours = 2,

        /// <summary>
        /// 3小時(shí)
        /// </summary>
        [Description("3小時(shí)")]
        ThreeHours = 3,

        /// <summary>
        /// 4小時(shí)
        /// </summary>
        [Description("4小時(shí)")]
        FourHours = 4,

        /// <summary>
        ///5小時(shí)
        /// </summary>
        [Description("5小時(shí)")]
        FiveHours = 5,

        /// <summary>
        /// 6小時(shí)
        /// </summary>
        [Description("6小時(shí)")]
        SixHours = 6,

        /// <summary>
        /// 8小時(shí)
        /// </summary>
        [Description("8小時(shí)")]
        EightHours = 8,

        /// <summary>
        /// 9小時(shí)
        /// </summary>
        [Description("9小時(shí)")]
        TenHours = 9,

        /// <summary>
        /// 12小時(shí)
        /// </summary>
        [Description("12小時(shí)")]
        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個(gè)月
        /// </summary>
        [Description("1個(gè)月")]
        OneMonth = OneDay * 30,

        /// <summary>
        /// 3個(gè)月
        /// </summary>
        [Description("3個(gè)月")]
        ThreeMonths = OneMonth *3,

        /// <summary>
        /// 6個(gè)月
        /// </summary>
        [Description("6個(gè)月")]
        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 領(lǐng)取時(shí)間段

        /// <summary>
        /// 可領(lǐng)取優(yōu)惠券期限范圍類型荐类。默認(rèn)為7天
        /// </summary>
        public Period CanPickupPeriodType { get; set; } = Period.SevenDays;

        private DateTime canPickUpStartTime = DateTime.Now;
        /// <summary>
        /// 優(yōu)惠券可領(lǐng)取開(kāi)始日期 默認(rèn)為 當(dāng)前日期
        /// </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>
        /// 優(yōu)惠券可領(lǐng)取的截止日期 默認(rèn)為當(dāng)前日期后7天
        /// </summary>
        public DateTime CanPickUpEndTime
        {
            get { return canPickUpEndTime; }
            set
            {
                if (value == null)
                {
                    return;
                }

                if (CanPickUpStartTime > value)
                {
                    throw new ArgumentOutOfRangeException("優(yōu)惠券可領(lǐng)取開(kāi)始日期不能大于截止日期");
                }

                canPickUpEndTime = value;
                OnPropertyChanged(nameof(CanPickUpEndTime));
            }
        }

        #endregion

        #region 有效期時(shí)間段

        /// <summary>
        /// 優(yōu)惠券有效期時(shí)間段類型怖现,默認(rèn)為7天
        /// </summary>
        public Period EffectivePeriodType { get; set; } = Period.SevenDays;

        private DateTime effectiveDate = DateTime.Now;
        /// <summary>
        /// 優(yōu)惠券生效日期. 默認(rèn)為當(dāng)前日期
        /// </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>
        /// 優(yōu)惠券失效日期 默認(rèn)為當(dāng)前日期后7天
        /// </summary>
        public DateTime ExpireDate
        {
            get { return expireDate; }
            set
            {
                if (value == null)
                {
                    return;
                }

                if (effectiveDate > expireDate)
                {
                    throw new ArgumentOutOfRangeException("優(yōu)惠券生效日期不能大于失效日期");
                }

                expireDate = value;
                OnPropertyChanged(nameof(ExpireDate));
            }
        }

        #endregion

        private double thresholdAmount = 0;
        /// <summary>
        /// 優(yōu)惠券使用門檻。<=0 為無(wú)門檻. 默認(rèn)為無(wú)門檻
        /// </summary>
        public double ThresholdAmount
        {
            get { return thresholdAmount; }
            set
            {
                if (value <= 0)
                {
                    thresholdAmount = 0;
                }
                else
                {
                    thresholdAmount = value;
                }

                OnPropertyChanged(nameof(ThresholdAmount));
            }
        }


        private bool isStackable = false;
        /// <summary>
        /// 優(yōu)惠券是否可疊加使用. 默認(rèn)為不可疊加使用
        /// </summary>
        public bool IsStackable
        {
            get { return isStackable; }
            set
            {
                isStackable = value;
                OnPropertyChanged(nameof(IsStackable));
            }
        }


        private bool isAutoPickUp = false;
        /// <summary>
        /// 是否可自動(dòng)領(lǐng)取. 默認(rèn)為不可自動(dòng)領(lǐng)取
        /// </summary>
        public bool IsAutoPickUp
        {
            get { return isAutoPickUp; }
            set
            {
                isAutoPickUp = value;
                if (isAutoPickUp)
                {
                    IsPickedUp = true;
                }

                OnPropertyChanged(nameof(IsAutoPickUp));
            }
        }


        private bool isPickedUp = false;
        /// <summary>
        /// 是否已領(lǐng)取
        /// </summary>
        public bool IsPickedUp
        {
            get { return isPickedUp; }
            set
            {
                isAutoPickUp = value;
                OnPropertyChanged(nameof(IsPickedUp));
            }
        }

        //是否過(guò)期
        public bool IsExpired => IsPickedUp && ExpireDate < EffectiveDate;


        private bool isAvailable = true;
        /// <summary>
        /// 是否可用。 如為否屈嗤,則不可領(lǐng)取潘拨,不可使用
        /// </summary>
        public bool IsAvailable
        {
            get { return isAvailable; }
            set
            {
                isAvailable = value;
                OnPropertyChanged(nameof(IsAvailable));
            }
        }

        /// <summary>
        /// 是否可領(lǐng)取
        /// </summary>
        public bool CanPickUp { get; set; }

        #endregion

        public void PickUp()
        {
            bool canPickup = CheckCouponsIsInAvailablePickupPeriod();
            if (canPickup)
            {
                IsPickedUp = true;
                EffectiveDate = DateTime.Now;

            }

        }

        /// <summary>
        /// 檢查優(yōu)惠券是否在可領(lǐng)取日期范圍內(nèi)
        /// </summary>
        /// <returns></returns>
        private bool CheckCouponsIsInAvailablePickupPeriod()
        {
            if (CanPickUpStartTime == null || CanPickUpEndTime == null)
            {
                return false;
            }

            DateTime now = DateTime.Now;

            //還未到可領(lǐng)取事件,不可領(lǐng)取
            if (now < CanPickUpStartTime)
            {
                return false;
            }

            //已過(guò)領(lǐng)取事件饶号,不可領(lǐng)取
            if (now >= CanPickUpEndTime)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 檢查優(yōu)惠券可領(lǐng)取狀態(tài)
        /// </summary>
        /// <returns></returns>
        private bool CheckCouponsCanPickableState()
        {
            if (IsPickedUp)
            {
                return false;
            }

            //是否在可領(lǐng)取日期范圍內(nèi)
            bool isInAvailablePickupPeriod = CheckCouponsIsInAvailablePickupPeriod();
            return isInAvailablePickupPeriod;
        }

        private bool CheckCouponsAvailableState()
        {
            //優(yōu)惠券已領(lǐng)取
            if (IsPickedUp)
            {
                if (EffectiveDate == null || ExpireDate == null)
                {
                    return false;
                }

                DateTime now = DateTime.Now;

                //已過(guò)有效期
                if (now >= CanPickUpEndTime)
                {
                    return false;
                }

                return true;
            }
            else
            {
                //是否在可領(lǐng)取日期范圍內(nèi)
                bool isInAvailablePickupPeriod = CheckCouponsIsInAvailablePickupPeriod();
                //已過(guò)可領(lǐng)取日期
                if (!isInAvailablePickupPeriod)
                {
                    return false;
                }
            }

            return true;
        }
    }
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末铁追,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子茫船,更是在濱河造成了極大的恐慌琅束,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件算谈,死亡現(xiàn)場(chǎng)離奇詭異涩禀,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)然眼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門艾船,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人高每,你說(shuō)我怎么就攤上這事屿岂。” “怎么了鲸匿?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵爷怀,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我带欢,道長(zhǎng)霉撵,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任洪囤,我火速辦了婚禮,結(jié)果婚禮上撕氧,老公的妹妹穿的比我還像新娘瘤缩。我一直安慰自己,他們只是感情好伦泥,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布剥啤。 她就那樣靜靜地躺著,像睡著了一般不脯。 火紅的嫁衣襯著肌膚如雪府怯。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,144評(píng)論 1 285
  • 那天防楷,我揣著相機(jī)與錄音牺丙,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛冲簿,可吹牛的內(nèi)容都是我干的粟判。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼峦剔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼档礁!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起吝沫,我...
    開(kāi)封第一講書(shū)人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤呻澜,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后惨险,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體羹幸,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年平道,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了睹欲。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡一屋,死狀恐怖窘疮,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情冀墨,我是刑警寧澤闸衫,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站诽嘉,受9級(jí)特大地震影響蔚出,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜虫腋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一骄酗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧悦冀,春花似錦趋翻、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至历等,卻和暖如春讨惩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背寒屯。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工荐捻, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓靴患,卻偏偏與公主長(zhǎng)得像仍侥,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鸳君,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容