/// <summary> /// 获取该学习卡,只是暂存在学员账户名下,并不使用 /// </summary> /// <param name="entity"></param> /// <param name="acc">学员账号</param> public void CardGet(LearningCard entity, Accounts acc) { if (entity.Lc_State != 0) { throw new Exception("该学习卡已经使用"); } LearningCardSet set = this.SetSingle(entity.Lcs_ID); if (set == null || set.Lcs_IsEnable == false) { throw new Exception("该学习卡不可使用"); } //是否过期 if (!(DateTime.Now > entity.Lc_LimitStart && DateTime.Now < entity.Lc_LimitEnd.Date.AddDays(1))) { throw new Exception("该学习卡已经过期"); } //标注已经使用 entity.Lc_IsUsed = true; entity.Lc_UsedTime = DateTime.Now; entity.Lc_State = 0; //状态,0为初始,1为使用,-1为回滚 entity.Ac_ID = acc.Ac_ID; entity.Ac_AccName = acc.Ac_AccName; //使用数量加1 int usecount = Gateway.Default.Count <LearningCard>(LearningCard._.Lcs_ID == set.Lcs_ID && LearningCard._.Lc_IsUsed == true); set.Lsc_UsedCount = usecount + 1; Gateway.Default.Save <LearningCardSet>(set); Gateway.Default.Save <LearningCard>(entity); }
/// <summary> /// 删除学习卡设置项 /// </summary> /// <param name="entity">业务实体</param> public void SetDelete(LearningCardSet entity) { int used = CardOfCount(entity.Org_ID, entity.Lcs_ID, true, true); if (used > 0) { throw new Exception("当前设置项中涉及的学习卡已经存在使用记录,不能删除!可以选择禁用。"); } using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Delete <LearningCardSet>(entity); tran.Delete <LearningCard>(LearningCard._.Lcs_ID == entity.Lcs_ID); tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } } }
/// <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); }
/// <summary> /// 学习卡关联的课程 /// </summary> /// <param name="code">学习卡编码</param> /// <param name="pw">学习卡密码</param> /// <returns></returns> public Course[] CoursesForCard(string code, string pw) { LearningCard card = this.CardSingle(code, pw); if (card != null) { LearningCardSet set = this.SetSingle(card.Lcs_ID); if (set != null) { return(this.CoursesGet(set)); } } return(null); }
/// <summary> /// 生成学习卡 /// </summary> /// <param name="set">学习卡的设置项</param> /// <param name="factor">随机因子</param> /// <returns></returns> public LearningCard CardGenerate(LearningCardSet set, int factor = -1) { Song.Entities.LearningCard card = new LearningCard(); card.Lc_Price = set.Lcs_Price; //面额 card.Lc_IsEnable = true; //是否启用 card.Lc_CrtTime = DateTime.Now; //创建时间 card.Lcs_ID = set.Lcs_ID; //设置项id card.Org_ID = set.Org_ID; //机构id card.Lc_LimitStart = set.Lcs_LimitStart; //时间效 card.Lc_LimitEnd = set.Lcs_LimitEnd; //卡值码与其密码 card.Lc_Code = _CardBuildCoder(set.Lcs_SecretKey, factor, set.Lcs_CodeLength); card.Lc_Pw = _CardBuildPw(factor, set.Lcs_PwLength); return(card); }
/// <summary> /// 设置关联的课程 /// </summary> /// <param name="set"></param> /// <param name="courses"></param> /// <returns>LearningCardSet对象中的Lcs_RelatedCourses将记录关联信息</returns> public LearningCardSet CoursesSet(LearningCardSet set, Course[] courses) { if (courses == null || courses.Length <= 0) { set.Lcs_RelatedCourses = string.Empty; set.Lcs_CoursesCount = 0; return(set); } int[] couid = new int[courses.Length]; for (int i = 0; i < courses.Length; i++) { couid[i] = courses[i].Cou_ID; } return(CoursesSet(set, couid)); }
/// <summary> /// 添加学习卡设置项 /// </summary> /// <param name="entity">业务实体</param> public void SetAdd(LearningCardSet entity) { entity.Lcs_CrtTime = DateTime.Now; Song.Entities.Organization org = Business.Do <IOrganization>().OrganCurrent(); if (org != null) { entity.Org_ID = org.Org_ID; } Gateway.Default.Save <LearningCardSet>(entity); //生成学习卡 LearningCard[] cards = CardGenerate(entity); if (cards != null) { foreach (LearningCard c in cards) { Gateway.Default.Save <LearningCard>(c); } } }
public LearningCardSet CoursesSet(LearningCardSet set, int[] couid) { if (couid == null || couid.Length < 1) { set.Lcs_RelatedCourses = string.Empty; set.Lcs_CoursesCount = 0; return(set); } XmlDocument xmlDoc = new XmlDocument(); //创建根节点 XmlNode root = xmlDoc.CreateElement("Items"); xmlDoc.AppendChild(root); for (int i = 0; i < couid.Length; i++) { XmlElement item = xmlDoc.CreateElement("item"); item.SetAttribute("Cou_ID", couid[i].ToString()); root.AppendChild(item); } set.Lcs_RelatedCourses = root.OuterXml; set.Lcs_CoursesCount = couid.Length; return(set); }
/// <summary> /// 设置关联的课程 /// </summary> /// <param name="set"></param> /// <param name="couids">课程id串,以逗号分隔</param> /// <returns></returns> public LearningCardSet CoursesSet(LearningCardSet set, string couids) { List <int> list = new List <int>(); foreach (string s in couids.Split(',')) { if (string.IsNullOrWhiteSpace(s)) { continue; } if (s.Trim() == "") { continue; } int id = 0; int.TryParse(s, out id); if (id == 0) { continue; } list.Add(id); } return(CoursesSet(set, list.ToArray())); }
/// <summary> /// 获取关联的课程 /// </summary> /// <param name="set"></param> /// <returns></returns> public Course[] CoursesGet(LearningCardSet set) { return(CoursesGet(set.Lcs_RelatedCourses)); }
/// <summary> /// 导出Excel格式的学习卡信息 /// </summary> /// <param name="path">导出文件的路径(服务器端)</param> /// <param name="orgid">机构id</param> /// <param name="lcsid">学习卡设置项的id</param> /// <returns></returns> public string Card4Excel(string path, int orgid, int lcsid) { HSSFWorkbook hssfworkbook = new HSSFWorkbook(); //xml配置文件 XmlDocument xmldoc = new XmlDocument(); string confing = WeiSha.Common.App.Get["ExcelInputConfig"].VirtualPath + "学习卡.xml"; xmldoc.Load(WeiSha.Common.Server.MapPath(confing)); XmlNodeList nodes = xmldoc.GetElementsByTagName("item"); //创建工作簿对象 LearningCardSet rs = Gateway.Default.From <LearningCardSet>().Where(LearningCardSet._.Lcs_ID == lcsid).ToFirst <LearningCardSet>(); ISheet sheet = hssfworkbook.CreateSheet(rs.Lcs_Theme); //sheet.DefaultColumnWidth = 30; //创建数据行对象 IRow rowHead = sheet.CreateRow(0); for (int i = 0; i < nodes.Count; i++) { rowHead.CreateCell(i).SetCellValue(nodes[i].Attributes["Column"].Value); } //生成数据行 ICellStyle style_size = hssfworkbook.CreateCellStyle(); style_size.WrapText = true; WhereClip wc = LearningCard._.Org_ID == orgid; if (lcsid >= 0) { wc.And(LearningCard._.Lcs_ID == lcsid); } LearningCard[] rcodes = Gateway.Default.From <LearningCard>().Where(wc).OrderBy(LearningCard._.Lc_CrtTime.Desc).ToArray <LearningCard>(); for (int i = 0; i < rcodes.Length; i++) { IRow row = sheet.CreateRow(i + 1); for (int j = 0; j < nodes.Count; j++) { Type type = rcodes[i].GetType(); System.Reflection.PropertyInfo propertyInfo = type.GetProperty(nodes[j].Attributes["Field"].Value); //获取指定名称的属性 object obj = propertyInfo.GetValue(rcodes[i], null); if (obj != null) { string format = nodes[j].Attributes["Format"] != null ? nodes[j].Attributes["Format"].Value : ""; string datatype = nodes[j].Attributes["DataType"] != null ? nodes[j].Attributes["DataType"].Value : ""; string defvalue = nodes[j].Attributes["DefautValue"] != null ? nodes[j].Attributes["DefautValue"].Value : ""; string value = ""; switch (datatype) { case "date": DateTime tm = Convert.ToDateTime(obj); value = tm > DateTime.Now.AddYears(-100) ? tm.ToString(format) : ""; break; default: value = obj.ToString(); break; } if (defvalue.Trim() != "") { foreach (string s in defvalue.Split('|')) { string h = s.Substring(0, s.IndexOf("=")); string f = s.Substring(s.LastIndexOf("=") + 1); if (value.ToLower() == h.ToLower()) { value = f; } } } row.CreateCell(j).SetCellValue(value); } } } FileStream file = new FileStream(path, FileMode.Create); hssfworkbook.Write(file); file.Close(); return(path); }
/// <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(); } } }
/// <summary> /// 批量生成学习卡 /// </summary> /// <param name="set">学习卡的设置项</param> /// <returns></returns> public LearningCard[] CardGenerate(LearningCardSet set) { return(CardGenerate(set, null)); }
/// <summary> /// 删除,按主键ID; /// </summary> /// <param name="identify">实体的主键</param> public void SetDelete(int identify) { LearningCardSet set = SetSingle(identify); SetDelete(set); }