public List <CoursesComment> GetMoveCommment_Page(int movid, int pageIndex, int pageSize, int topN) { List <CoursesComment> list = new List <CoursesComment>(); ZZULIEntities db = base.EF; #region 先获取热度topN,然后再根据时间排序 //if (pageIndex == 1) //{ // List<CoursesComment> top_list = base.EF.CoursesComment.Where(cc => cc.Mov_ID == movid).OrderByDescending(cc => cc.CCom_Likes).Skip(0).Take(topN).ToList(); // List<int> top_id = new List<int>(); // top_list.ForEach(t => { list.Add(t); top_id.Add(t.CCom_ID); }); // List<CoursesComment> then_list = base.EF.CoursesComment.Where(cc => cc.Mov_ID == movid && !top_id.Contains(cc.Cou_ID)).OrderByDescending(cc => cc.CCom_Time).Skip(0).Take(pageSize - topN).ToList(); // then_list.ForEach(t => list.Add(t)); //} //else//否则获取pagesize条数据,并按照时间先后顺序排列 //{ // List<CoursesComment> var_list = base.EF.CoursesComment.Where(cc => cc.Mov_ID == movid).OrderByDescending(cc => cc.CCom_Time).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); // var_list.ForEach(v => list.Add(v)); //} #endregion list = db.CoursesComment.Where(c => c.Mov_ID == movid).OrderByDescending(c => c.CCom_Time).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); return(list); }
public static bool Collect(int target_id, int optionType) { ZZULIEntities ef = GetEntity(); switch (optionType) { case 8: var course = ef.Courses.Where(c => c.Cou_ID == target_id).FirstOrDefault(); course.Cou_CollectNum++; break; case 9: var question = ef.Questions.Where(q => q.Q_ID == target_id).FirstOrDefault(); question.Q_CollectNum++; break; case 10: var note = ef.Notes.Where(n => n.N_ID == target_id).FirstOrDefault(); note.N_CollectNum++; break; default: break; } if (ef.SaveChanges() > 0) { return(true); } else { return(false); } }
/// <summary> /// 取消关注分类 /// </summary> /// <param name="u_id"></param> /// <param name="target_id"></param> /// <returns></returns> public static bool AbortFollow(int u_id, int target_id) { ZZULIEntities ef = GetEntity(); var data = ef.Navigations.Where(n => n.Nav_ID == target_id).FirstOrDefault(); data.Nav_FollowNum--; return(ef.SaveChanges() > 0 ? true : false); }
//-----------------------------------------------------提交课程问题 public static bool Submit_CourseQuestion(int uid, int cid, int mid, string title, string content) { ZZULIEntities ef = GetEntity(); ef.CoursesQuestions.Add(new CoursesQuestions() { User_ID = uid, Cou_ID = cid, Mov_ID = mid, CQ_Title = title, CQ_Content = content, CQ_Time = DateTime.Now }); return(ef.SaveChanges() > 0 ? true : false); }
//-----------------------------------------------------提交笔记评论 public static bool Submit_NoteComment(int uid, int nid, string content) { ZZULIEntities ef = GetEntity(); ef.NotesComments.Add(new NotesComments() { N_ID = nid, User_ID = uid, NC_Content = content, NC_Time = DateTime.Now }); return(ef.SaveChanges() > 0 ? true : false); }
//-----------------------------------------------------回复问题的回复 public static bool Submit_QuestionAnswerOfAnswer(int uid, int qid, int aid, string content) { ZZULIEntities ef = GetEntity(); ef.Answers.Add(new Answers() { User_ID = uid, Q_ID = qid, A_Target = aid, A_Content = content, A_Time = DateTime.Now }); return(ef.SaveChanges() > 0 ? true : false); }
//______________________________________________________课程评论 /// <summary> /// 提交课程评论 /// </summary> /// <param name="uid">用户ID</param> /// <param name="couid">课程ID</param> /// <param name="movid">章节ID</param> /// <param name="content">评论内容</param> /// <returns></returns> public static bool Submit_CourseComment(int uid, int couid, int movid, string content) { ZZULIEntities ef = GetEntity(); CoursesComment cc = new CoursesComment() { User_ID = uid, Cou_ID = couid, Mov_ID = movid, CCom_Content = content, CCom_Likes = 0, CCom_Time = DateTime.Now }; ef.CoursesComment.Add(cc); return(ef.SaveChanges() > 0 ? true : false); }
//______________________________________________________课程笔记 public static bool Submit_CourseNote(int uid, int couid, int movid, string content) { ZZULIEntities ef = GetEntity(); CoursesNotes cn = new CoursesNotes() { User_ID = uid, Cou_ID = couid, Mov_ID = movid, CN_Content = content, CN_Time = DateTime.Now }; ef.CoursesNotes.Add(cn); return(ef.SaveChanges() > 0 ? true : false); }
//public static List<int> GetAllTags(List<int> tag_id) //{ // List<int> res = new List<int>(); // res.AddRange(tag_id); // tag_id.ForEach(t => res.AddRange(GetAllTags(t))); // return res; //} static List <int> GetChildTag(int tag_id) { ZZULIEntities ef = GetEntity(); List <int> data = ef.Navigations.Where(n => n.Ower_ID == tag_id).Select(n => n.Nav_ID).ToList(); if (data != null && data.Count() > 0) { return(data.ToList()); } return(null); }
public static List <Navigations> GetFrom_Note(int noteid) { ZZULIEntities ef = GetEntity(); //获取笔记的标签 List <int> tagsid = ef.ObjectTags.Where(o => o.Obj_ID == noteid && o.Obj_Type == 34).Select(o => o.Obj_TagID).ToList(); ////获取标签的子标签 //List<int> all = new List<int>(); //tagsid.ForEach(a => { List<int> ch = GetAllTags(a); if (ch != null && ch.Count > 0) { all.AddRange(GetAllTags(a)); } }); return(ef.Navigations.Where(n => tagsid.Contains(n.Nav_ID)).ToList()); }
/// <summary> /// 根据目标和操作类型,判断用户是否已经执行过该操作,比如是否已经收藏某课程 /// </summary> /// <param name="u_id"></param> /// <param name="target_id"></param> /// <param name="optionType"></param> /// <returns></returns> public static bool IsCollect(int u_id, int target_id, int optionType) { ZZULIEntities ef = GetEntity(); if (ef.UserCollections.Where(u => u.User_ID == u_id && u.UC_Target == target_id && u.UC_Type == optionType).Count() > 0) { return(true); } else { return(false); } }
/// <summary> /// 根据目标和操作类型,判断用户是否已经执行过该操作,比如是否已经为某课程点赞 /// </summary> /// <param name="u_id"></param> /// <param name="target_id"></param> /// <param name="optionType"></param> /// <returns></returns> public static bool IsLike(int u_id, int target_id, int optionType) { ZZULIEntities ef = GetEntity(); var data = ef.UserLikes.Where(u => u.User_ID == u_id && u.UL_Target == target_id && u.UL_Type == optionType); if (data != null && data.Count() > 0) { return(true); } else { return(false); } }
//获取课程评论 /// <summary> /// 获取课程评论 /// </summary> /// <param name="id">课程ID</param> /// <param name="topN">topN</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页容量</param> /// <returns></returns> public List <CoursesComment> GetCourseComment_Page(int couid, int pageIndex, int pageSize, int topN) { List <CoursesComment> list = new List <CoursesComment>(); //CoursesComment c; ZZULIEntities db = base.EF; //1、如果是第一页,先获取赞topN if (pageIndex == 1) { //List<CoursesComment> top_list = base.EF.CoursesComment.Include("Courses").Where(cc => cc.Cou_ID == cou_id).OrderByDescending(cc => cc.CCom_Likes).Skip(0).Take(topN).ToList(); List <CoursesComment> top_list = base.EF.CoursesComment.Where(cc => cc.Cou_ID == couid).OrderByDescending(cc => cc.CCom_Likes).Skip(0).Take(topN).ToList(); List <int> top_id = new List <int>(); top_list.ForEach(t => { list.Add(t); top_id.Add(t.CCom_ID); }); //foreach (CoursesComment v in top_list) //{ // list.Add(v); // top_id.Add(v.CCom_ID); //} //2、如果是第一页再获取pagesize-topN条,且不包含topN中的数据,并按照时间先后顺序排列 //List<CoursesComment> then_list = base.EF.CoursesComment.Include("Courses").Where(cc=>cc.Cou_ID == cou_id && (cou_id !top_id.Contains(top_id))).OrderByDescending(cc=>cc.CCom_Time).Skip(0).Take(pageSize-topN); //List<CoursesComment> then_list = (from cc in db.CoursesComment join u in db.UserInfo on cc.User_ID equals u.User_ID where cc.Cou_ID == cou_id && !top_id.Contains(cc.CCom_ID) orderby cc.CCom_Time descending select cc).Skip(0).Take(pageSize - topN).ToList(); List <CoursesComment> then_list = base.EF.CoursesComment.Where(cc => cc.Cou_ID == couid && !top_id.Contains(cc.Cou_ID)).OrderByDescending(cc => cc.CCom_Time).Skip(0).Take(pageSize - topN).ToList(); //List<CoursesComment> then_list = from cc in then_list.ForEach(t => list.Add(t)); //foreach (CoursesComment v in var_list) //{ // //c = new CoursesComment() { CCom_ID = v.CCom_ID, Cou_ID = v.Cou_ID, User_ID = v.User_ID, CCom_Content = v.CCom_Content, CCom_Time = v.CCom_Time, CCom_Likes = v.CCom_Likes, User_Name = v.User_Name }; // list.Add(v); //} } else//否则获取pagesize条数据,并按照时间先后顺序排列 { //List<CoursesComment> var_list = (from cc in db.CoursesComment join u in db.UserInfo on cc.User_ID equals u.User_ID where cc.Cou_ID == cou_id orderby cc.CCom_Time descending select cc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); List <CoursesComment> var_list = base.EF.CoursesComment.Where(cc => cc.Cou_ID == couid).OrderByDescending(cc => cc.CCom_Time).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); //foreach (CoursesComment v in var_list) //{ // //c = new CourseComment() { CCom_ID = v.CCom_ID, Cou_ID = v.Cou_ID, User_ID = v.User_ID, CCom_Content = v.CCom_Content, CCom_Time = v.CCom_Time, CCom_Likes = v.CCom_Likes, User_Name = v.User_Name }; // list.Add(v); //} var_list.ForEach(v => list.Add(v)); } return(list); }
/// <summary> /// 判断标签是否存在 /// </summary> /// <param name="tag_name"></param> /// <returns></returns> static bool TagIsExist(string tag_name) { ZZULIEntities ef = GetEntity(); var all_tags = ef.Navigations.Where(n => n.Ower_ID != -1); var list = (from l in all_tags select new { Nav_ID = l.Nav_ID, Nav_Name = l.Nav_Name }).ToList(); bool isExist = false; list.ForEach(l => { if (l.Nav_Name == tag_name) { isExist = true; } }); return(isExist); }
//-----------------------------------------------------提交笔记 /// <summary> /// /// </summary> /// <param name="uid"></param> /// <param name="title"></param> /// <param name="content"></param> /// <param name="content_style"></param> /// <param name="tags"></param> /// <returns></returns> public static bool Submit_Note(int uid, string title, string content, string content_style, List <string> tags) { ZZULIEntities ef = GetEntity(); //提交笔记 Notes note = new Notes() { User_ID = uid, N_Title = title, N_Content = content, N_Content_Style = content_style, N_Time = DateTime.Now, N_IsOriginal = true }; ef.Notes.Add(note); ef.SaveChanges(); int nid = note.N_ID; //提交标签 var taglist = ef.Navigations.Where(n => n.Nav_ID > 0); List <string> exits_id = taglist.Select(n => n.Nav_Name).ToList(); tags.ForEach(t => { //如果用户提交的标签不存在,就将该标签添加到数据库 if (!exits_id.Contains(t)) { Navigations nav = new Navigations() { Nav_Name = t, Nav_Type = 3, Nav_UseNum = 1, Ower_ID = 0, Nav_Describe = "", Nav_FollowNum = 0 }; ef.Navigations.Add(nav); ef.SaveChanges(); int tid = nav.Nav_ID; ef.ObjectTags.Add(new ObjectTags() { Obj_ID = nid, Obj_TagID = tid, Obj_Type = 34 }); } else//如果用户提交的标签已经存在,就将标签的使用量加1 { var ta = taglist.Single(tag => tag.Nav_Name == t); ef.ObjectTags.Add(new ObjectTags() { Obj_ID = nid, Obj_TagID = ta.Nav_ID, Obj_Type = 34 }); ta.Nav_UseNum++; } }); //保存用户为笔记设置的标签 return(ef.SaveChanges() > 0 ? true : false); }
//长轮询 public static bool Link(string type) { ZZULIEntities ef = GetEntity(); DateTime start = DateTime.Now; switch (type) { case "cc": while (true) { var data = ef.CoursesComment.Where(c => c.CCom_Time > start); if (data != null && data.Count() > 0) { return(true); } Thread.Sleep(2000); } case "cn": while (true) { var data = ef.CoursesNotes.Where(n => n.CN_Time > start); if (data != null && data.Count() > 0) { return(true); } Thread.Sleep(2000); } case "cq": while (true) { var data = ef.CoursesQuestions.Where(n => n.CQ_Time > start); if (data != null && data.Count() > 0) { return(true); } Thread.Sleep(2000); } default: return(false); } }
/// <summary> /// 取消踩 /// </summary> /// <param name="target_id"></param> /// <param name="error"></param> /// <returns></returns> public static bool AbortNotLike(int target_id, int optionType) { ZZULIEntities ef = GetEntity(); bool res = false; switch (optionType) { case 18: //课程 var course = ef.Courses.Where(c => c.Cou_ID == target_id).FirstOrDefault(); course.Cou_NotLikes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateCourseHotByCou_ID(target_id); res = true; } break; case 19: //问题回复 var answer = ef.Answers.Where(a => a.A_ID == target_id).FirstOrDefault(); answer.A_NotLikes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateQuestionHotByQ_ID(target_id); res = true; } break; case 20: //笔记 var note = ef.Notes.Where(n => n.N_ID == target_id).FirstOrDefault(); note.N_NotLikes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateNoteHotByN_ID(target_id); res = true; } break; default: res = false; break; } return(res); }
/// <summary> /// 记录用户的浏览行为 /// </summary> /// <param name="uid">用户id,如果用户尚未登录,则为-1</param> /// <param name="target">浏览的对象</param> /// <param name="optionType">浏览对象的类型</param> public static bool PageView(int target, int optionType, int uid = 0) { ZZULIEntities ef = GetEntity(); if (uid == 0) { ef.UserPageViews.Add(new UserPageViews() { UPV_Target = target, UPV_Type = optionType, UPV_Time = DateTime.Now }); } else { ef.UserPageViews.Add(new UserPageViews() { UPV_Target = target, UPV_Type = optionType, User_ID = uid, UPV_Time = DateTime.Now }); } return(ef.SaveChanges() > 0 ? true : false); }
public static bool AbortCollect(int target_id, int optionType) { ZZULIEntities ef = GetEntity(); var data = ef.UserCollections.Where(u => u.UC_Target == target_id && u.UC_Type == optionType && u.User_ID == Common.UserInfo.User_ID).FirstOrDefault(); ef.UserCollections.Remove(data); switch (optionType) { case 8: var course = ef.Courses.Where(c => c.Cou_ID == target_id).FirstOrDefault(); course.Cou_CollectNum--; break; case 9: var question = ef.Questions.Where(q => q.Q_ID == target_id).FirstOrDefault(); question.Q_CollectNum--; break; case 10: var note = ef.Notes.Where(n => n.N_ID == target_id).FirstOrDefault(); note.N_CollectNum--; break; default: break; } if (ef.SaveChanges() > 0) { return(true); } else { return(false); } }
/// <summary> /// 修改浏览量的值 /// </summary> /// <param name="target"></param> /// <param name="optionType"></param> /// <returns></returns> public static bool ModifyPageView(int target, int optionType) { ZZULIEntities ef = GetEntity(); switch (optionType) { case 1: //浏览课程 break; case 2: //浏览问题 var question = ef.Questions.Where(q => q.Q_ID == target).FirstOrDefault(); question.Q_PageViews++; break; case 3: //浏览笔记 var note = ef.Notes.Where(n => n.N_ID == target).FirstOrDefault(); note.N_PageViews++; break; default: break; } return(ef.SaveChanges() > 0 ? true : false); }
public void Test() { ZZULIEntities ZZUL = new ZZULIEntities(); #region 重置数据库 using (ZZULIEntities ZZU = new ZZULIEntities()) { var na = ZZU.Database.ExecuteSqlCommand("truncate table UserAry"); var nl = ZZU.Database.ExecuteSqlCommand("truncate table NewAry"); ZZU.SaveChanges(); } #endregion var UID = ZZUL.PR_UserID().ToArray(); //获取所有用户ID var AID = ZZUL.PR_AllID().ToArray(); //获取浏览、推荐、收藏用户ID var LID = ZZUL.PR_learnID().ToArray(); //获取学习课程用户ID var CID = ZZUL.PR_CouID().ToArray(); //获取所有课程所属标签ID #region 原始矩阵 #region 学习 //查找用户的学习记录,如果有 5分 if (LID.Length > 0) { for (int i = 0; i < LID.Length; i++) //控制用户 { for (int j = 0; j < CID.Length; j++) //控制课程 { var num = ZZUL.PR_Learn(LID[i], CID[j]).ToArray(); //判断用户是否学习过该课程 if (num[0] > 0) { ZZUL.PR_UserAry(LID[i], CID[j], 5); //将用户ID,课程ID,分数增加到UserAry中 continue; } } } ZZUL.SaveChanges(); } #endregion #region 收藏、推荐、浏览 //先查找用户在改标签是否学习过,如果有结束,如果没在进行判断收藏4,,推荐3,浏览1 int sum; //操作类型 if (AID.Length != 0) //判断是否有数据 { for (int i = 0; i < AID.Length; i++) //控制用户 { for (int j = 0; j < CID.Length; j++) { sum = 0; var num = ZZUL.PR_Learn(AID[i], CID[j]).ToArray(); if (num[0] > 0) //判断用户是否学习过该课程 { continue; } else { var n = ZZUL.PR_ALL(AID[i], CID[j]).ToArray(); //如果用户进行过收藏、浏览、推荐,返回推荐类型 if (n.Length > 0) //判断用户是否进行过推荐收藏、浏览、推荐 { for (int p = 0; p < n.Length; p++) { sum += Convert.ToInt32(n[p]); } switch (sum) { case 20: //该用户对该课程都进行过浏览、推荐、收藏,取最高分收藏 case 19: //该用户对该课程进行过推荐、收藏,取最高分收藏 case 9: //该用户对该课程进行过浏览、收藏,取最高分收藏 case 8: ZZUL.PR_UserAry(AID[i], CID[j], 4); break; //8代表收藏 4分 case 12: //该用户对该课程进行过浏览、推荐、取最高分推荐 case 11: ZZUL.PR_UserAry(AID[i], CID[j], 3); break; //推荐 3分 case 1: ZZUL.PR_UserAry(AID[i], CID[j], 1); break; //浏览一分 } } else { ZZUL.PR_UserAry(AID[i], CID[j], 0); //用户未对该课程进行过任何操作,0 } } } } } ZZUL.SaveChanges(); //有些用户只进行过学习操作,则需要下面循环来给0分 for (int i = 0; i < LID.Length; i++) //控制用户 { for (int j = 0; j < CID.Length; j++) //控制课程 { var num = ZZUL.PR_SelAll(LID[i], CID[j]).ToArray(); if (num.Length == 0) { ZZUL.PR_UserAry(LID[i], CID[j], 0); } } } ZZUL.SaveChanges(); #endregion #endregion #region 矩阵分解 var U = ZZUL.PR_SelectAll().GroupBy(G => G.User_ID).Select(G => G.Key).ToArray();//查找原始矩阵中的所有用户ID var list = ZZUL.PR_SelectAll().ToList(); List <List <double> > shu = new List <List <double> >(); for (int p = 0; p < U.Length; p++) { List <double> a = new List <double>(); foreach (var i in list) { if (i.User_ID == U[p]) { a.Add(Convert.ToDouble(i.Ary_Sco)); } } shu.Add(a); } Matix_Decompose matrix = new Matix_Decompose(UID.Length, CID.Length); matrix.get_Matrix(shu); #endregion #region 将分解后的矩阵写会数据库 double[,] NewArr = new double[U.Length, CID.Length]; NewArr = matrix.GetR(); //得到分解后的矩阵 var ArrID = ZZUL.PR_SelectAll().GroupBy(G => G.User_ID).Select(G => G.Key).ToArray(); for (int i = 0; i < U.Length; i++) { for (int j = 0; j < CID.Length; j++) { ZZUL.PR_NewAry(U[i], CID[j], NewArr[i, j]); } } ZZUL.SaveChanges(); #endregion }
/// <summary> /// 取消点赞 /// </summary> /// <param name="target_id"></param> /// <param name="error"></param> /// <returns></returns> public static bool AbortLike(int target_id, int optionType) { ZZULIEntities ef = GetEntity(); bool res = false; switch (optionType) { case 11: //课程 var course = ef.Courses.Where(cou => cou.Cou_ID == target_id).FirstOrDefault(); course.Cou_Likes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateCourseHotByCou_ID(target_id); res = true; } break; case 12: //课程评论 var ccom = ef.CoursesComment.Where(cc => cc.CCom_ID == target_id).FirstOrDefault(); ccom.CCom_Likes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateCourseHotByCou_ID(target_id); res = true; } break; case 13: //课程问题回复 var couA = ef.CoursesAnswers.Where(ca => ca.CA_ID == target_id).FirstOrDefault(); couA.CA_Likes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateCourseQuestionHot(target_id); res = true; } break; case 14: //课程笔记 var couN = ef.CoursesNotes.Where(cn => cn.CN_ID == target_id).FirstOrDefault(); couN.CN_Likes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateCourseNoteHot(target_id); res = true; } break; case 15: //问题回复 var answer = ef.Answers.Where(a => a.A_ID == target_id).FirstOrDefault(); answer.A_Likes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateQuestionHotByQ_ID(target_id); res = true; } break; case 16: //笔记 var note = ef.Notes.Where(n => n.N_ID == target_id).FirstOrDefault(); note.N_Likes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateNoteHotByN_ID(target_id); res = true; } break; case 17: //笔记评论 var ncom = ef.NotesComments.Where(nc => nc.NC_ID == target_id).FirstOrDefault(); ncom.NC_Likes--; if (ef.SaveChanges() > 0) { ef.SP_UpdateNoteHotByN_ID(target_id); res = true; } break; default: break; } return(res); }
/// <summary> /// 根据tag字符串获取tag对应的int型id /// </summary> /// <param name="tag"></param> /// <returns></returns> static int GetTagsID(string tag) { ZZULIEntities ef = GetEntity(); return(ef.Navigations.Where(n => n.Nav_Name == tag).FirstOrDefault().Nav_ID); }