public IHttpActionResult Getxxgc(int id)      //这边传的是UserId
        {
            List <QuestionsAll> Qlists = new List <QuestionsAll>();

            var             user = db.Users.Where(x => x.id == id).FirstOrDefault();
            List <Question> ques = null;

            if (user.Role == "S")
            {
                var        sAssign       = db.StudentAssgins.Where(x => x.StudentId == user.PersonId);
                List <int> projectIdlist = new List <int>();
                projectIdlist = (from s in sAssign
                                 select s.ProjectId).ToList();
                var linq1 = from qu in db.Questions
                            join Courses in db.Courses on qu.CourseId equals Courses.id
                            join Projects in db.Projects on Courses.ProjectId equals Projects.id
                            //where Projects.id.Equals(sAssign.ProjectId)
                            where projectIdlist.Contains(Projects.id)
                            where Courses.ActiveFlag.Equals("Y")
                            where qu.Status.Contains("O")
                            where qu.QuestionType.Contains("Q")
                            select qu;
                ques = linq1.OrderByDescending(c => c.CourseId).ThenBy(c => c.QuestionNum).ToList();
            }
            if (user.Role == "M")
            {
                var linq2 = from qu in db.Questions
                            join Courses in db.Courses on qu.CourseId equals Courses.id
                            where Courses.TeacherId == user.PersonId
                            where Courses.ActiveFlag.Equals("Y")
                            where qu.Status.Contains("O")
                            where qu.QuestionType.Contains("Q")
                            select qu;
                ques = linq2.OrderByDescending(c => c.CourseId).ThenBy(c => c.QuestionNum).ToList();
            }


            if (ques.Count == 0)
            {
                Qlists = null;
                return(Ok(Qlists));    //没有发布的问题
            }

            foreach (Question q in ques)
            {
                QuestionsAll questionitem = new QuestionsAll();
                questionitem.CourseId = q.CourseId;
                var course = db.Courses.Where(x => x.id == q.CourseId).FirstOrDefault();
                questionitem.CourseName      = course.CourseName;
                questionitem.QuestionId      = q.id;
                questionitem.QuestionContent = q.QuestionDesc;
                questionitem.ReleaseTime     = q.LastUpdateDate.ToString("f");
                var answerList = from Notes in db.Notes
                                 where Notes.PublicFlag == "Y" && Notes.QuestionId == q.id
                                 select Notes;
                if (answerList.ToList().Count == 0)
                {
                    questionitem.IsAnswered = false;    //该问题暂无回答
                    questionitem.Answers    = null;
                }
                else
                {
                    questionitem.IsAnswered = true;
                }

                List <Answers> Alists = new List <Answers>();
                foreach (Note a in answerList.ToList())
                {
                    Answers answeritem = new Answers();
                    answeritem.AnswerId   = a.id;
                    answeritem.Answer     = a.Notes;
                    answeritem.AnswerTime = a.CreationDate.ToString("f");
                    Collection col = db.Collections.FirstOrDefault(p => p.NoteId == a.id && p.UserId == id);   //当前登陆者的userid !!!
                    if (col != null)
                    {
                        answeritem.IsCollected = true;
                    }
                    else
                    {
                        answeritem.IsCollected = false;
                    }

                    Like like = db.Likes.FirstOrDefault(p => p.NoteId == a.id && p.UserId == id);   //当前登陆者的userid !!!
                    if (like != null)
                    {
                        answeritem.IsLiked = true;
                    }
                    else
                    {
                        answeritem.IsLiked = false;
                    }

                    answeritem.LikeCount = db.Likes.Where(p => p.NoteId == a.id).ToList().Count;

                    answeritem.UserId = a.UserId;
                    var user1 = db.Users.Where(x => x.id == a.UserId).FirstOrDefault();
                    if (user1.Role == "S")
                    {
                        var stu = db.Students.Where(x => x.id == user1.PersonId).FirstOrDefault();
                        answeritem.HeadImg = stu.HeadImage;
                        answeritem.name    = stu.StudentName;
                    }
                    if (user1.Role == "M")
                    {
                        var man = db.Managers.Where(x => x.id == user1.PersonId).FirstOrDefault();
                        answeritem.HeadImg = man.HeadImage;
                        answeritem.name    = man.ManagerName;
                    }

                    var commentList = from Comments in db.Comments
                                      where Comments.PublicFlag == "Y" && Comments.NoteId == a.id
                                      select Comments;
                    if (commentList.ToList().Count == 0)
                    {
                        answeritem.IsCommented = false;    //该回答暂无评论
                        answeritem.Comments    = null;
                    }
                    else
                    {
                        answeritem.IsCommented = true;
                    }

                    List <AnswerComments> Clists = new List <AnswerComments>();
                    foreach (Comment c in commentList.ToList())
                    {
                        AnswerComments commentitem = new AnswerComments();
                        commentitem.CommentId   = c.id;
                        commentitem.Comment     = c.Comments;
                        commentitem.CommentTime = c.CreationDate.ToString("f");
                        Collection col2 = db.Collections.FirstOrDefault(p => p.CommentId == c.id && p.UserId == id);   //当前登陆者的userid !!!
                        if (col2 != null)
                        {
                            commentitem.IsCollected = true;
                        }
                        else
                        {
                            commentitem.IsCollected = false;
                        }

                        Like like2 = db.Likes.FirstOrDefault(p => p.CommentId == c.id && p.UserId == id);   //当前登陆者的userid !!!
                        if (like2 != null)
                        {
                            commentitem.IsLiked = true;
                        }
                        else
                        {
                            commentitem.IsLiked = false;
                        }
                        commentitem.LikeCount = db.Likes.Where(p => p.CommentId == c.id).ToList().Count;
                        commentitem.UserId    = c.CommentedBy;
                        var user2 = db.Users.Where(x => x.id == c.CommentedBy).FirstOrDefault();
                        if (user2.Role == "S")
                        {
                            var stu = db.Students.Where(x => x.id == user2.PersonId).FirstOrDefault();
                            commentitem.HeadImg = stu.HeadImage;
                            commentitem.name    = stu.StudentName;
                        }
                        if (user2.Role == "M")
                        {
                            var man = db.Managers.Where(x => x.id == user2.PersonId).FirstOrDefault();
                            commentitem.HeadImg = man.HeadImage;
                            commentitem.name    = man.ManagerName;
                        }
                        Clists.Add(commentitem);
                    }

                    answeritem.Comments = Clists;
                    Alists.Add(answeritem);
                }

                questionitem.Answers = Alists;
                Qlists.Add(questionitem);
            }
            return(Ok(Qlists));
        }
        public IHttpActionResult GetMyAnswers(int id)    //当前登陆者的id !!!
        {
            List <MyAnswer> myanswerlist = new List <MyAnswer>();
            var             myanswers    = from Notes in db.Notes
                                           join Questions in db.Questions on Notes.QuestionId equals Questions.id
                                           join Courses in db.Courses on Questions.CourseId equals Courses.id
                                           join Projects in db.Projects on Courses.ProjectId equals Projects.id
                                           where Notes.UserId == id
                                           select new MyAnswer
            {
                CourseId   = Courses.id,
                CourseName = Courses.CourseName,

                QuestionId      = Questions.id,
                QuestionContent = Questions.QuestionDesc,

                AnswerId = Notes.id,
                Answer   = Notes.Notes,

                PublicFlag = Notes.PublicFlag,
                UserId     = id,
            };

            List <MyAnswer> notelist = myanswers.OrderByDescending(c => c.CourseId).ToList();

            if (notelist.Count == 0)
            {
                myanswerlist = null;
                return(Ok(myanswerlist));    //我没有回答任何问题
            }

            foreach (MyAnswer n in notelist)
            {
                MyAnswer myansweritem = new MyAnswer();
                myansweritem.CourseId   = n.CourseId;
                myansweritem.CourseName = n.CourseName;
                var cou = db.Courses.Where(x => x.id == n.CourseId).FirstOrDefault();
                myansweritem.CouStartTime    = cou.StartDate.ToString();
                myansweritem.QuestionId      = n.QuestionId;
                myansweritem.QuestionContent = n.QuestionContent;
                var qu = db.Questions.Where(x => x.id == n.QuestionId).FirstOrDefault();
                myansweritem.QuReleaseTime = qu.LastUpdateDate.ToString("f");
                myansweritem.AnswerId      = n.AnswerId;
                myansweritem.Answer        = n.Answer;
                var an = db.Notes.Where(x => x.id == n.AnswerId).FirstOrDefault();
                myansweritem.AnswerTime = an.LastUpdateDate.ToString("f");
                myansweritem.PublicFlag = n.PublicFlag;
                myansweritem.UserId     = n.UserId;
                var user = db.Users.Where(x => x.id == n.UserId).FirstOrDefault();
                if (user.Role == "S")
                {
                    var stu = db.Students.Where(x => x.id == user.PersonId).FirstOrDefault();
                    myansweritem.HeadImg = stu.HeadImage;
                    myansweritem.name    = stu.StudentName;
                }
                if (user.Role == "M")
                {
                    var man = db.Managers.Where(x => x.id == user.PersonId).FirstOrDefault();
                    myansweritem.HeadImg = man.HeadImage;
                    myansweritem.name    = man.ManagerName;
                }

                var commentList = from Comments in db.Comments
                                  where Comments.PublicFlag == "Y" && Comments.NoteId == n.AnswerId
                                  select Comments;
                if (commentList.ToList().Count == 0)
                {
                    myansweritem.IsCommented = false;    //该回答暂无评论
                    myansweritem.Comments    = null;
                }
                else
                {
                    myansweritem.IsCommented = true;
                }

                List <AnswerComments> Clists = new List <AnswerComments>();
                foreach (Comment c in commentList.ToList())
                {
                    AnswerComments commentitem = new AnswerComments();
                    commentitem.CommentId   = c.id;
                    commentitem.Comment     = c.Comments;
                    commentitem.CommentTime = c.CreationDate.ToString("f");
                    Collection col2 = db.Collections.FirstOrDefault(p => p.CommentId == c.id && p.UserId == id);   //当前登陆者的userid !!!
                    if (col2 != null)
                    {
                        commentitem.IsCollected = true;
                    }
                    else
                    {
                        commentitem.IsCollected = false;
                    }

                    commentitem.UserId = c.CommentedBy;
                    var user2 = db.Users.Where(x => x.id == c.CommentedBy).FirstOrDefault();
                    if (user2.Role == "S")
                    {
                        var stu = db.Students.Where(x => x.id == user2.PersonId).FirstOrDefault();
                        commentitem.HeadImg = stu.HeadImage;
                        commentitem.name    = stu.StudentName;
                    }
                    if (user2.Role == "M")
                    {
                        var man = db.Managers.Where(x => x.id == user2.PersonId).FirstOrDefault();
                        commentitem.HeadImg = man.HeadImage;
                        commentitem.name    = man.ManagerName;
                    }
                    Clists.Add(commentitem);
                }
                myansweritem.Comments = Clists;
                myanswerlist.Add(myansweritem);
            }
            return(Ok(myanswerlist));
        }
 public AnswerCommentsResult(AnswerComments result)
     : base(result)
 {
 }
        public IHttpActionResult GetMyQuestions(int id)      //当前登陆者的id !!!
        {
            List <MyQuestions> Qlists = new List <MyQuestions>();
            var qus = from qu in db.Questions
                      join Courses in db.Courses on qu.CourseId equals Courses.id
                      join Projects in db.Projects on Courses.ProjectId equals Projects.id
                      where qu.CreatedBy.Equals(id)
                      where Courses.ActiveFlag.Equals("Y")
                      where qu.Status.Contains("O")
                      where qu.QuestionType.Contains("Q")
                      select qu;
            List <Question> ques = qus.OrderByDescending(c => c.CourseId).ThenBy(c => c.QuestionNum).ToList();

            if (ques.Count == 0)
            {
                Qlists = null;
                return(Ok(Qlists));    //我没有发布问题
            }

            foreach (Question q in ques)
            {
                MyQuestions questionitem = new MyQuestions();
                questionitem.CourseId = q.CourseId;
                var course = db.Courses.Where(x => x.id == q.CourseId).FirstOrDefault();
                questionitem.CourseName      = course.CourseName;
                questionitem.QuestionId      = q.id;
                questionitem.QuestionContent = q.QuestionDesc;
                questionitem.ReleaseTime     = q.LastUpdateDate.ToString("f");
                var answerList = from Notes in db.Notes
                                 //where Notes.PublicFlag == "Y" && Notes.QuestionId == q.id
                                 where Notes.QuestionId == q.id
                                 select Notes;
                if (answerList.ToList().Count == 0)
                {
                    questionitem.IsAnswered = false;    //该问题暂无回答
                    questionitem.Answers    = null;
                }
                else
                {
                    questionitem.IsAnswered = true;
                }

                List <Answers> Alists = new List <Answers>();
                foreach (Note a in answerList.ToList())
                {
                    Answers answeritem = new Answers();
                    answeritem.AnswerId   = a.id;
                    answeritem.Answer     = a.Notes;
                    answeritem.AnswerTime = a.CreationDate.ToString("f");

                    answeritem.UserId = a.UserId;
                    var user = db.Users.Where(x => x.id == a.UserId).FirstOrDefault();
                    if (user.Role == "S")
                    {
                        var stu = db.Students.Where(x => x.id == user.PersonId).FirstOrDefault();
                        answeritem.HeadImg = stu.HeadImage;
                        answeritem.name    = stu.StudentName;
                    }
                    if (user.Role == "M")
                    {
                        var man = db.Managers.Where(x => x.id == user.PersonId).FirstOrDefault();
                        answeritem.HeadImg = man.HeadImage;
                        answeritem.name    = man.ManagerName;
                    }

                    var commentList = from Comments in db.Comments
                                      where Comments.PublicFlag == "Y" && Comments.NoteId == a.id
                                      select Comments;
                    if (commentList.ToList().Count == 0)
                    {
                        answeritem.IsCommented = false;    //该回答暂无评论
                        answeritem.Comments    = null;
                    }
                    else
                    {
                        answeritem.IsCommented = true;
                    }

                    List <AnswerComments> Clists = new List <AnswerComments>();
                    foreach (Comment c in commentList.ToList())
                    {
                        AnswerComments commentitem = new AnswerComments();
                        commentitem.CommentId   = c.id;
                        commentitem.Comment     = c.Comments;
                        commentitem.CommentTime = c.CreationDate.ToString("f");

                        commentitem.UserId = c.CommentedBy;
                        var user2 = db.Users.Where(x => x.id == c.CommentedBy).FirstOrDefault();
                        if (user2.Role == "S")
                        {
                            var stu = db.Students.Where(x => x.id == user2.PersonId).FirstOrDefault();
                            commentitem.HeadImg = stu.HeadImage;
                            commentitem.name    = stu.StudentName;
                        }
                        if (user2.Role == "M")
                        {
                            var man = db.Managers.Where(x => x.id == user2.PersonId).FirstOrDefault();
                            commentitem.HeadImg = man.HeadImage;
                            commentitem.name    = man.ManagerName;
                        }
                        Clists.Add(commentitem);
                    }

                    answeritem.Comments = Clists;
                    Alists.Add(answeritem);
                }

                questionitem.Answers = Alists;
                Qlists.Add(questionitem);
            }
            return(Ok(Qlists));
        }
        public IHttpActionResult GetMyCoursesAll(int id)    //这边传的是UserId
        {
            List <CoursesAll> CoursesAllLists = new List <CoursesAll>();
            var user = db.Users.Where(x => x.id == id).FirstOrDefault();

            List <Course> courseList = null;

            if (user.Role == "S")
            {
                var        sAssign       = db.StudentAssgins.Where(x => x.StudentId == user.PersonId);
                List <int> projectIdlist = new List <int>();
                projectIdlist = (from s in sAssign
                                 select s.ProjectId).ToList();
                var linq1 = (from cou in db.Courses
                             where projectIdlist.Contains(cou.ProjectId)
                             where cou.ActiveFlag.Contains("Y")
                             select cou).ToList();
                courseList = linq1;
            }
            if (user.Role == "M")
            {
                var linq2 = (from cou in db.Courses
                             where cou.TeacherId == user.PersonId
                             where cou.ActiveFlag.Contains("Y")
                             select cou).ToList();
                courseList = linq2;
            }

            if (courseList.Count == 0)
            {
                CoursesAllLists = null;
                return(Ok(CoursesAllLists));    //没有课程
            }

            foreach (Course cou in courseList)
            {
                CoursesAll courseitem = new CoursesAll();
                courseitem.CourseId   = cou.id;
                courseitem.CourseName = cou.CourseName;

                var noticeList = from notices in db.Questions
                                 where notices.QuestionType == "N" && notices.Status.Contains("O")
                                 where notices.CourseId == cou.id
                                 select notices;
                if (noticeList.ToList().Count == 0)
                {
                    courseitem.HasNotice = false;
                    courseitem.Notices   = null;  //该课程下没有通知
                }
                else
                {
                    courseitem.HasNotice = true;
                    List <Notice> Nlists = new List <Notice>();
                    foreach (Question n in noticeList.ToList())
                    {
                        Notice noticeitem = new Notice();
                        noticeitem.NoticeId      = n.id;
                        noticeitem.NoticeContent = n.QuestionDesc;
                        noticeitem.ReleaseTime   = n.LastUpdateDate.ToString("f");
                        Nlists.Add(noticeitem);
                    }
                    courseitem.Notices = Nlists;
                }

                var questionList = from Questions in db.Questions
                                   where Questions.QuestionType == "Q" && Questions.Status.Contains("O") && Questions.CourseId == cou.id
                                   select Questions;
                List <Question> ques = questionList.OrderByDescending(c => c.CourseId).ThenBy(c => c.QuestionNum).ToList();
                if (ques.Count == 0)
                {
                    courseitem.HasQuestion  = false;   //该课程下没有问题
                    courseitem.QuestionsAll = null;
                }
                else
                {
                    courseitem.HasQuestion = true;
                }

                List <QuestionsAll> QAlists = new List <QuestionsAll>();
                foreach (Question q in ques)
                {
                    QuestionsAll questionitem = new QuestionsAll();
                    questionitem.QuestionId      = q.id;
                    questionitem.QuestionContent = q.QuestionDesc;
                    questionitem.ReleaseTime     = q.LastUpdateDate.ToString("f");
                    var answerList = from Notes in db.Notes
                                     where Notes.UserId == id && Notes.QuestionId == q.id     //当前登录用户的所有回答   id需要改成系统获取!!!
                                     select Notes;
                    if (answerList.ToList().Count == 0)
                    {
                        questionitem.IsAnswered = false;    //该问题该用户暂未回答
                        questionitem.Answers    = null;
                    }
                    else
                    {
                        questionitem.IsAnswered = true;
                    }

                    List <Answers> Alists = new List <Answers>();
                    foreach (Note a in answerList.ToList())
                    {
                        Answers answeritem = new Answers();
                        answeritem.AnswerId   = a.id;
                        answeritem.Answer     = a.Notes;
                        answeritem.PublicFlag = a.PublicFlag;
                        answeritem.AnswerTime = a.CreationDate.ToString("f");
                        answeritem.UserId     = a.UserId;
                        var user1 = db.Users.Where(x => x.id == a.UserId).FirstOrDefault();
                        if (user1.Role == "S")
                        {
                            var stu = db.Students.Where(x => x.id == user.PersonId).FirstOrDefault();
                            answeritem.HeadImg = stu.HeadImage;
                            answeritem.name    = stu.StudentName;
                        }
                        if (user1.Role == "M")
                        {
                            var man = db.Managers.Where(x => x.id == user.PersonId).FirstOrDefault();
                            answeritem.HeadImg = man.HeadImage;
                            answeritem.name    = man.ManagerName;
                        }

                        var commentList = from Comments in db.Comments
                                          where Comments.PublicFlag == "Y" && Comments.NoteId == a.id
                                          select Comments;
                        if (commentList.ToList().Count == 0)
                        {
                            answeritem.IsCommented = false;    //该回答暂无评论
                            answeritem.Comments    = null;
                        }
                        else
                        {
                            answeritem.IsCommented = true;
                        }

                        List <AnswerComments> Clists = new List <AnswerComments>();
                        foreach (Comment c in commentList.ToList())
                        {
                            AnswerComments commentitem = new AnswerComments();
                            commentitem.CommentId   = c.id;
                            commentitem.Comment     = c.Comments;
                            commentitem.CommentTime = c.CreationDate.ToString("f");
                            commentitem.UserId      = c.CommentedBy;
                            var user2 = db.Users.Where(x => x.id == c.CommentedBy).FirstOrDefault();
                            if (user2.Role == "S")
                            {
                                var stu = db.Students.Where(x => x.id == user2.PersonId).FirstOrDefault();
                                commentitem.HeadImg = stu.HeadImage;
                                commentitem.name    = stu.StudentName;
                            }
                            if (user2.Role == "M")
                            {
                                var man = db.Managers.Where(x => x.id == user2.PersonId).FirstOrDefault();
                                commentitem.HeadImg = man.HeadImage;
                                commentitem.name    = man.ManagerName;
                            }
                            Clists.Add(commentitem);
                        }

                        answeritem.Comments = Clists;
                        Alists.Add(answeritem);
                    }

                    questionitem.Answers = Alists;
                    QAlists.Add(questionitem);
                }

                courseitem.QuestionsAll = QAlists;
                CoursesAllLists.Add(courseitem);
            }

            return(Ok(CoursesAllLists));
        }