Ejemplo n.º 1
0
        /// <summary>
        /// 使用该学习卡
        /// </summary>
        /// <param name="card">学习卡</param>
        /// <param name="acc">学员账号</param>
        public void CardUse(LearningCard card, Accounts acc)
        {
            if (card.Lc_State != 0)
            {
                throw new Exception("该学习卡已经使用");
            }
            LearningCardSet set = this.SetSingle(card.Lcs_ID);

            if (set == null || set.Lcs_IsEnable == false)
            {
                throw new Exception("该学习卡不可使用");
            }
            //是否过期
            if (!(DateTime.Now > card.Lc_LimitStart && DateTime.Now < card.Lc_LimitEnd.Date.AddDays(1)))
            {
                throw new Exception("该学习卡已经过期");
            }
            //设置学习卡的使用信息
            card.Lc_UsedTime = DateTime.Now;
            card.Lc_State    = 1; //状态,0为初始,1为使用,-1为回滚
            card.Ac_ID       = acc.Ac_ID;
            card.Ac_AccName  = acc.Ac_AccName;
            using (DbTrans tran = Gateway.Default.BeginTrans())
            {
                //学习时间的起始时间
                DateTime start = DateTime.Now, end = DateTime.Now;
                if (set.Lcs_Unit == "日" || set.Lcs_Unit == "天")
                {
                    end = start.AddDays(set.Lcs_Span);
                }
                if (set.Lcs_Unit == "周")
                {
                    end = start.AddDays(set.Lcs_Span * 7);
                }
                if (set.Lcs_Unit == "月")
                {
                    end = start.AddMonths(set.Lcs_Span);
                }
                if (set.Lcs_Unit == "年")
                {
                    end = start.AddYears(set.Lcs_Span);
                }
                int span = (end - start).Days;
                try
                {
                    Course[] courses = this.CoursesGet(set.Lcs_RelatedCourses);
                    foreach (Course cou in courses)
                    {
                        Song.Entities.Student_Course sc = null;
                        sc = tran.From <Student_Course>().Where(Student_Course._.Ac_ID == card.Ac_ID &&
                                                                Student_Course._.Cou_ID == cou.Cou_ID).ToFirst <Student_Course>();
                        if (sc != null)
                        {
                            //如果是免费或试用
                            if (sc.Stc_IsFree || sc.Stc_IsTry)
                            {
                                sc.Stc_StartTime = start;
                                sc.Stc_EndTime   = end;
                            }
                            else
                            {
                                //已经过期,则重新设置时间
                                if (sc.Stc_EndTime < DateTime.Now)
                                {
                                    sc.Stc_StartTime = start;
                                    sc.Stc_EndTime   = end;
                                }
                                else
                                {
                                    //如果未过期,则续期
                                    sc.Stc_EndTime = sc.Stc_EndTime.AddDays(span);
                                }
                            }
                        }
                        else
                        {
                            sc               = new Student_Course();
                            sc.Stc_CrtTime   = DateTime.Now;
                            sc.Stc_StartTime = start;
                            sc.Stc_EndTime   = end;
                        }
                        sc.Ac_ID      = card.Ac_ID;
                        sc.Cou_ID     = cou.Cou_ID;
                        sc.Stc_Money  = card.Lc_Price;
                        sc.Org_ID     = card.Org_ID;
                        sc.Stc_IsFree = sc.Stc_IsTry = false;
                        tran.Save <Student_Course>(sc);
                    }
                    //使用数量加1
                    set.Lsc_UsedCount = tran.Count <LearningCard>(LearningCard._.Lcs_ID == set.Lcs_ID && LearningCard._.Lc_IsUsed == true);
                    set.Lsc_UsedCount = card.Lc_IsUsed ? set.Lsc_UsedCount : set.Lsc_UsedCount + 1;
                    tran.Save <LearningCardSet>(set);
                    //标注学习卡已经使用
                    card.Lc_IsUsed = true;
                    card.Lc_Span   = span; //记录学习卡使后,增加的学习时间(单位:天),方便回滚扣除
                    tran.Save <LearningCard>(card);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
                finally
                {
                    tran.Close();
                }
            }
        }