/// <summary> /// 修改学习卡设置项 /// </summary> /// <param name="entity">业务实体</param> public void SetSave(LearningCardSet entity) { using (DbTrans tran = Gateway.Default.BeginTrans()) { try { Song.Entities.LearningCardSet rs = tran.From <LearningCardSet>().Where(LearningCardSet._.Lcs_ID == entity.Lcs_ID).ToFirst <LearningCardSet>(); LearningCard[] cards = CardGenerate(entity, tran); if (cards != null) { foreach (LearningCard c in cards) { Gateway.Default.Save <LearningCard>(c); } } tran.Update <LearningCard>(new Field[] { LearningCard._.Lc_Price, LearningCard._.Lc_LimitStart, LearningCard._.Lc_LimitEnd }, new object[] { entity.Lcs_Price, entity.Lcs_LimitStart, entity.Lcs_LimitEnd }, LearningCard._.Lcs_ID == entity.Lcs_ID && LearningCard._.Lc_IsUsed == false); tran.Save <LearningCardSet>(entity); tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } } }
/// <summary> /// <param name="tran">事务</param> /// </summary> /// <param name="set"></param> /// <param name="tran"></param> /// <returns></returns> public LearningCard[] CardGenerate(LearningCardSet set, DbTrans tran) { int count = 0; //要生成的数量 int realcount = Gateway.Default.Count <LearningCard>(LearningCard._.Lcs_ID == set.Lcs_ID); //实际数量 //如果实际数量小于要生成的数量,则只生成差额 if (realcount < set.Lcs_Count) { count = set.Lcs_Count - realcount; } //如果实际数量小于要生成的数量,则删除多余(如果已经使用的过的,不可删除) if (realcount > set.Lcs_Count) { bool isNull = false; if (tran == null) { tran = Gateway.Default.BeginTrans(); isNull = true; } Song.Entities.LearningCard[] rcs = tran.From <LearningCard>().Where(LearningCard._.Lcs_ID == set.Lcs_ID && LearningCard._.Lc_IsUsed == false) .ToArray <LearningCard>(realcount - set.Lcs_Count); foreach (LearningCard r in rcs) { tran.Delete <LearningCard>(r); } if (isNull) { tran.Commit(); tran.Dispose(); tran.Close(); } return(null); } //生成学习卡 LearningCard[] cards = new LearningCard[count]; for (int i = 0; i < count; i++) { cards[i] = CardGenerate(set, i); } //判断是否重复 for (int i = 0; i < cards.Length; i++) { for (int j = 0; j < cards.Length; j++) { if (j <= i) { continue; } if (cards[i].Lc_Code == cards[j].Lc_Code) { cards[i] = CardGenerate(set, i * j + DateTime.Now.Millisecond); i = j = 0; } } } return(cards); }
private void _Delete(int id, DbTrans tran) { ManageMenu[] ws = tran.From <ManageMenu>().Where(ManageMenu._.MM_PatId == id).ToArray <ManageMenu>(); //删除子级 foreach (ManageMenu w in ws) { _Delete(w.MM_Id, tran); } //删除菜单与权限的关联 tran.Delete <Purview>(Purview._.MM_Id == id); //删除自身 tran.Delete <ManageMenu>(ManageMenu._.MM_Id == id); }
/// <summary> /// 修改充值码设置项 /// </summary> /// <param name="entity">业务实体</param> public void RechargeSetSave(RechargeSet entity) { using (DbTrans tran = Gateway.Default.BeginTrans()) { try { Song.Entities.RechargeSet rs = tran.From <RechargeSet>().Where(RechargeSet._.Rs_ID == entity.Rs_ID).ToFirst <RechargeSet>(); //如果新增了充值码 if (rs.Rs_Count < entity.Rs_Count) { _RechargeCodeBuilds(entity, entity.Rs_Count - rs.Rs_Count); } //如果减少的充值码 if (rs.Rs_Count > entity.Rs_Count) { Song.Entities.RechargeCode[] rcs = tran.From <RechargeCode>().Where(RechargeCode._.Rs_ID == entity.Rs_ID && RechargeCode._.Rc_IsUsed == false).ToArray <RechargeCode>(rs.Rs_Count - entity.Rs_Count); foreach (RechargeCode r in rcs) { tran.Delete <RechargeCode>(r); } } tran.Update <RechargeCode>(new Field[] { RechargeCode._.Rc_Price, RechargeCode._.Rc_LimitStart, RechargeCode._.Rc_LimitEnd }, new object[] { entity.Rs_Price, entity.Rs_LimitStart, entity.Rs_LimitEnd }, RechargeCode._.Rs_ID == entity.Rs_ID && RechargeCode._.Rc_IsUsed == false); tran.Save <RechargeSet>(entity); tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } } }
/// <summary> /// 使用该充值码 /// </summary> /// <param name="entity"></param> public void CouponUseCode(RechargeCode entity) { //是否被禁用 Song.Entities.RechargeSet set = Gateway.Default.From <RechargeSet>().Where(RechargeSet._.Rs_ID == entity.Rs_ID).ToFirst <RechargeSet>(); if (set == null || set.Rs_IsEnable == false) { throw new Exception("该充值码已经被禁用"); } //是否过期 if (!(DateTime.Now > entity.Rc_LimitStart && DateTime.Now < entity.Rc_LimitEnd.Date.AddDays(1))) { throw new Exception("该充值码已经过期"); } //标注已经使用 entity.Rc_IsUsed = true; entity.Rc_UsedTime = DateTime.Now; //产生流水 CouponAccount ca = new CouponAccount(); ca.Ca_Value = entity.Rc_Price; ca.Ac_ID = entity.Ac_ID; ca.Ca_From = 2; ca.Rc_Code = entity.Rc_Code + "-" + entity.Rc_Pw; ca.Ca_Source = "充值码充值"; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { Business.Do <IAccounts>().CouponAdd(ca); tran.Save <RechargeCode>(entity); RechargeSet setEnity = tran.From <RechargeSet>().Where(RechargeSet._.Rs_ID == entity.Rs_ID).ToFirst <RechargeSet>(); if (setEnity != null) { setEnity.Rs_UsedCount++; tran.Save <RechargeSet>(setEnity); } tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public T GetModel(object id) { Field[] pkfields = Hxj.Data.Common.EntityCache.GetPrimaryKeyFields <T>(); if (pkfields.Length == 1) { if (_trans != null) { return(_trans.From <T>().Where(pkfields[0] == id).ToFirst()); } return(DBSession.CurrentSession.From <T>().Where(pkfields[0] == id).ToFirst()); } else if (pkfields.Length > 1) { return(GetModel((id as Array))); } else { throw new Exception("数据表[" + Hxj.Data.Common.EntityCache.GetTableName <T>() + "]不存在任何主键!"); } }
/// <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(); } } }