protected void Page_Load(object sender, EventArgs e) { Song.Entities.Accounts st = this.Master.Account; if (st == null) { return; } org = Business.Do <IOrganization>().OrganCurrent(); dtLog = Business.Do <IStudent>().StudentStudyCourseLog(org.Org_ID, this.Master.Account.Ac_ID); if (!this.IsPostBack) { BindData(null, null); } //此页面的ajax提交,全部采用了POST方式 if (Request.ServerVariables["REQUEST_METHOD"] == "POST") { string action = WeiSha.Common.Request.Form["action"].String.ToLower(); string couid = WeiSha.Common.Request.Form["couid"].String.ToLower(); string json = string.Empty; switch (action) { case "getstc": Song.Entities.Student_Course stc = GetStc(couid); if (stc == null) { json = "{\"success\":\"0\"}"; } else { json = "{\"success\":\"1\",data:" + stc.ToJson() + "}"; } break; } Response.Write(json); Response.End(); } }
/// <summary> /// 学习卡使用后的回滚,将删除学员的关联课程 /// </summary> /// <param name="entity"></param> /// <param name="isclear">是否清理学习记录</param> public void CardRollback(LearningCard entity, bool isclear) { //只是领用,但未使用 if (entity.Lc_IsUsed && entity.Lc_State == 0) { //标注状态为回滚 entity.Lc_State = -1; Gateway.Default.Save <LearningCard>(entity); } //真正使用过,回滚较复杂 if (entity.Lc_IsUsed && entity.Lc_State == 1) { LearningCardSet set = this.SetSingle(entity.Lcs_ID); //学习时间的起始时间 int day = entity.Lc_Span; if (day <= 0) { if (set.Lcs_Unit == "日" || set.Lcs_Unit == "天") { day = set.Lcs_Span; } if (set.Lcs_Unit == "周") { day = set.Lcs_Span * 7; } if (set.Lcs_Unit == "月") { day = set.Lcs_Span * 30; } if (set.Lcs_Unit == "年") { day = set.Lcs_Span * 365; } } //关联的课程 Course[] courses = this.CoursesGet(set.Lcs_RelatedCourses); using (DbTrans tran = Gateway.Default.BeginTrans()) { try { foreach (Course cou in courses) { Song.Entities.Student_Course sc = tran.From <Student_Course>().Where(Student_Course._.Ac_ID == entity.Ac_ID && Student_Course._.Cou_ID == cou.Cou_ID).ToFirst <Student_Course>(); if (sc == null) { continue; } //扣除学习卡所增加的时间,计算出学习结束时间 DateTime end = sc.Stc_EndTime.AddDays(-day); if (sc.Stc_StartTime < end) { //如果扣除学习卡增加的时间后,仍然大于开始时间,则更改 tran.Update <Student_Course>(new Field[] { Student_Course._.Stc_EndTime }, new object[] { end }, Student_Course._.Ac_ID == entity.Ac_ID && Student_Course._.Cou_ID == cou.Cou_ID); } else { //如果扣除学习卡增加的时间后,小于开始时间,则直接删除课程 tran.Delete <Student_Course>(Student_Course._.Ac_ID == entity.Ac_ID && Student_Course._.Cou_ID == cou.Cou_ID); if (isclear) { _cardRollback_clear(entity.Ac_ID, cou.Cou_ID); } tran.Update <Accounts>(new Field[] { Accounts._.Ac_CurrCourse }, new object[] { -1 }, Accounts._.Ac_ID == entity.Ac_ID && Accounts._.Ac_CurrCourse == cou.Cou_ID); } } entity.Lc_State = -1; tran.Save <LearningCard>(entity); tran.Commit(); Extend.LoginState.Accounts.Refresh(entity.Ac_ID); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } } } }
/// <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(); } } }