public ActionResult Index()
        {
            int courseid = int.Parse(Request.Form["course"]);
            bool succeed = true;
            LearningPlatformEntities lpfe = new LearningPlatformEntities();
            int userId;
            //if (Session["userId"] != null)
            //{
            //    userId = Convert.ToInt32(Session["userId"]);
            //}
            //else
            //{
            //    userId = 1;
            //}
            var uid = User.Identity.GetUserId();
            userId = lpfe.AspNetUsers.Single<AspNetUsers>(m => m.Id == uid).NewId;

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    var num = lpfe.Order.Count<Order>(m => m.theCourse == courseid && m.theUser == userId);
                    if (num == 0)
                    {
                        Order or = lpfe.Order.Add(new Order());
                        or.theUser = userId;
                        or.theCourse = int.Parse(Request.Form["course"]);
                        or.Totalprice = int.Parse(Request.Form["price"]);
                        or.Orderstate = 0;
                    }

                    lpfe.SaveChanges();
                    ts.Complete();
                }
            }
            catch
            {
                succeed = false;
                Response.Write("<script> alert('数据未成功保存,请重新尝试!');</script> ");
            }
            if (succeed)
            {
                string paymentUrl = "http://10.0.138.11:8801/default.aspx", paymentMethod = "0";
                string merchantId = "Team05";
                string returnUrl = "http://" + Request.Url.Authority + Url.Action("Result", "Payment");
                string amtStr = Request.Form["price"];
                var orderid = lpfe.Order.Single<Order>(m => m.theCourse == courseid && m.theUser == userId).Id;
                string merTransId = orderid.ToString();
                RemotePost.PaymentPost(paymentUrl, merchantId, returnUrl, paymentMethod, amtStr, merTransId);
            }

            pvm.courses = new List<Course>();
            var course = lpfe.Course.Single<Course>(m => m.Id == courseid);
            pvm.courses.Add(course);
            return View(pvm);
        }
        public void UpdateChaperLog(int courseid,int chapter, int section)
        {
            var uid = User.Identity.GetUserId();
            //using (TransactionScope sc = new TransactionScope())
            //{
            //    try
            //    {
            //        using (LearningPlatformEntities lpfe = new LearningPlatformEntities())
            //        {
            //            var Id = lpfe.AspNetUsers.Single(m => m.Id == uid).NewId;
            //            var pp = lpfe.ChapterLearning.Count(m => m.theLearner == Id);
            //            if (pp > 0)
            //            {
            //                ChapterLearning cl = lpfe.ChapterLearning.Single(m => m.theLearner == Id);
            //                cl.theChapter = chapter;
            //                SectionLearning sl = lpfe.SectionLearning.Single(m => m.theLearner == Id);
            //                sl.theSection = section;
            //            }
            //            else
            //            {

            //                ChapterLearning cc = lpfe.ChapterLearning.Add(new ChapterLearning());
            //                cc.theChapter = chapter;
            //                cc.theLearner = Id;
            //                cc.theLearningState = 1;
            //                lpfe.SaveChanges();

            //                SectionLearning ss = lpfe.SectionLearning.Add(new SectionLearning());
            //                ss.theSection = section;
            //                ss.theLearner = Id;
            //                ss.theLearningState = 1;
            //            }
            //            lpfe.SaveChanges();
            //        }
            //        sc.Complete();
            //    }
            //    catch (Exception ex)
            //    {
            //        throw new Exception(ex.Message);
            //    }

            //}

                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        using (LearningPlatformEntities lpfe = new LearningPlatformEntities())
                        {
                            var Id = lpfe.AspNetUsers.Single(m => m.Id == uid).NewId;
                            var pp = lpfe.ChapterLearning.Count(m => m.theLearner == Id);
                            var chap = lpfe.CourseChapter.Single(m => m.theCourse == courseid & m.SequenceType == chapter).Id;
                            var s = section.ToString();
                            var sec = lpfe.ChapterSection.Single(m => m.theCourseChapter == chap & m.SectionOrd == s).Id;
                            if (pp > 0)
                            {
                                ChapterLearning cl = lpfe.ChapterLearning.Single(m => m.theLearner == Id);
                                cl.theChapter = chap;
                                SectionLearning sl = lpfe.SectionLearning.Single(m => m.theLearner == Id);
                                sl.theSection = sec;
                            }
                            else
                            {
                                ChapterLearning cc = lpfe.ChapterLearning.Add(new ChapterLearning());
                                cc.theChapter = chap;
                                cc.theLearner = Id;
                                cc.theLearningState = 1;

                                SectionLearning ss = lpfe.SectionLearning.Add(new SectionLearning());
                                ss.theSection = sec;
                                ss.theLearner = Id;
                                ss.theLearningState = 1;
                            }
                            lpfe.SaveChanges();
                        }
                        ts.Complete();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
        }
 public void saveProblem()
 {
     LearningPlatformEntities lpfe = new LearningPlatformEntities();
     var realcontent = Request["content"];
     var realanswer = Request["ranswer"];
     var trueanswer = Request["tanswer"];
     var iscorrect = Convert.ToInt32(Request["correct"]);
     var ord = Convert.ToInt32(Request["ord"]);
     var setType = Convert.ToInt32(Request["type"]);
     var trackId = Convert.ToInt32(Request["trackId"]);
     if (setType == 0)
     {
         using (TransactionScope ts = new TransactionScope())
         {
             ExerciseProblems ep = lpfe.ExerciseProblems.Add(new ExerciseProblems());
             ep.ProblemOrd = ord;
             ep.RealContent = realcontent;
             ep.RealAnswer = realanswer;
             ep.TrueAnswer = trueanswer;
             ep.IsCorrect = iscorrect;
             ep.theExecriseSetTracking = trackId;
             lpfe.SaveChanges();
             ts.Complete();
         }
     }
     else
     {
         var tpnum = lpfe.TestProblems.Count(m => m.theTestSetTracking == trackId & m.ProblemOrd == ord);
         if (tpnum != 0)
         {
             using (TransactionScope ts = new TransactionScope())
             {
                 TestProblems tp = lpfe.TestProblems.Single(m => m.theTestSetTracking == trackId & m.ProblemOrd == ord);
                 lpfe.TestProblems.Remove(tp);
                 lpfe.SaveChanges();
                 ts.Complete();
             }
         }
         using (TransactionScope ts = new TransactionScope())
         {
             TestProblems tp = lpfe.TestProblems.Add(new TestProblems());
             tp.ProblemOrd = ord;
             tp.RealContent = realcontent;
             tp.RealAnswer = realanswer;
             tp.TrueAnswer = trueanswer;
             tp.IsCorrect = iscorrect;
             tp.theTestSetTracking = trackId;
             lpfe.SaveChanges();
             ts.Complete();
         }
     }
 }
 public int createTrack()
 {
     LearningPlatformEntities lpfe = new LearningPlatformEntities();
     var uid = User.Identity.GetUserId();
     var userId = lpfe.AspNetUsers.Single(m => m.Id == uid).NewId;
     int setType = Convert.ToInt32(Request["type"]);
     int flag = Convert.ToInt32(Request["flag"]);
     int? num = Convert.ToInt32(Request["num"]);
     int setId = Convert.ToInt32(Request["setId"]);
     int? trackId = Convert.ToInt32(Request["trackId"]);
     if (flag == 0)
     {
         if (setType == 0)
         {
             var tryord = 0;
             using (TransactionScope ts = new TransactionScope())
             {
                 //var tryord = 0;
                 if (lpfe.ExerciseSetTracking.Count(m => m.theExerciseSet == setId & m.theLearner == userId) > 0)
                 {
                     var temp = lpfe.ExerciseSetTracking.Where(m => m.theExerciseSet == setId & m.theLearner == userId).ToList();
                     tryord = temp.Max(m => m.TryOrd);
                 }
                 ExerciseSetTracking st = lpfe.ExerciseSetTracking.Add(new ExerciseSetTracking());
                 st.theLearner = userId;
                 st.theExerciseSet = setId;
                 st.theLearningState = 1;
                 st.TryOrd = tryord + 1;
                 st.CorrectNum = 0;
                 lpfe.SaveChanges();
                 ts.Complete();
                 tryord += 1;
             }
             trackId = lpfe.ExerciseSetTracking.Single(m => m.theExerciseSet == setId & m.theLearner == userId & m.TryOrd == tryord).Id;
         }
         else
         {
             var stnum = lpfe.TestSetTracking.Count(m => m.theLearner == userId & m.theTestSet == setId);
             if (stnum == 0)
             {
                 using (TransactionScope ts = new TransactionScope())
                 {
                     TestSetTracking st = lpfe.TestSetTracking.Add(new TestSetTracking());
                     st.theLearner = userId;
                     st.theTestSet = setId;
                     st.theLearningState = 1;
                     st.CorrectNum = 0;
                     lpfe.SaveChanges();
                     ts.Complete();
                 }
                 trackId = lpfe.TestSetTracking.Single(m => m.theTestSet == setId & m.theLearner == userId).Id;
             }
             else
             {
                 var stt = lpfe.TestSetTracking.Single(m => m.theTestSet == setId & m.theLearner == userId);
                 using (TransactionScope ts = new TransactionScope())
                 {
                     stt.CorrectNum = 0;
                     lpfe.SaveChanges();
                     ts.Complete();
                 }
                 trackId = stt.Id;
             }
         }
     }
     else
     {
         if (setType == 0)
         {
             using (TransactionScope ts = new TransactionScope())
             {
                 ExerciseSetTracking st = lpfe.ExerciseSetTracking.Single(m => m.Id == trackId);
                 st.CorrectNum += 1;
                 lpfe.SaveChanges();
                 ts.Complete();
             }
         }
         else
         {
             using (TransactionScope ts = new TransactionScope())
             {
                 TestSetTracking st = lpfe.TestSetTracking.Single(m => m.Id == trackId);
                 st.CorrectNum += 1;
                 lpfe.SaveChanges();
                 ts.Complete();
             }
         }
     }
     var tempId = Convert.ToInt32(trackId);
     return tempId;
 }
        public ActionResult Result()
        {
            string merId, amt, merTransId, transId, transTime;
            LearningPlatformEntities lpfe = new LearningPlatformEntities();
            if (RemotePost.PaymentVerify(Request, out merId, out amt, out merTransId, out transId, out transTime) && merId == "Team05")
            {
                int orderid = Convert.ToInt32(merTransId);
                var coursid = 0;
                var ord = lpfe.Order.Single(m => m.Id == orderid);
                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        //add learnerbookmarker
                        LearnerBookmarker lbm = lpfe.LearnerBookmarker.Add(new LearnerBookmarker());
                        lbm.theLearner = ord.theUser;
                        lbm.theCourse = ord.theCourse;
                        lbm.theLearnerCourseState = 1;
                        lbm.LearningTime = 0;
                        lbm.theCourseObject = 1;
                        lbm.theLearnerType = 1;
                        lbm.theLearnerAbility = 1;
                        lbm.RegiseterDate = DateTime.Now;

                        //update order
                        ord.Orderstate = 1;
                        coursid = ord.theCourse;

                        //initial learning

                        //var cha = lpfe.CourseChapter.Single(m => m.theCourse == coursid&m.SequenceType==1).Id;
                        //var sec = lpfe.ChapterSection.Single(m => m.theCourseChapter == cha&m.SectionOrd=="1").Id;

                        //ChapterLearning cl = lpfe.ChapterLearning.Add(new ChapterLearning());
                        //cl.theChapter = cha;
                        //cl.theLearner = ord.theUser;
                        //cl.theLearningState = 0;

                        //SectionLearning sl = lpfe.SectionLearning.Add(new SectionLearning());
                        //sl.theLearner = ord.theUser;
                        //sl.theLearningState = 0;
                        //sl.theSection = sec;

                        lpfe.SaveChanges();
                        ts.Complete();
                    }
                    ViewBag.paymentstate = 1;
                    ViewBag.paymentMsg = "付款成功!     付款号:" + merTransId.ToString() + ";   金额:" + amt.ToString() + "元。";//付款成功!显示付款信息作为测试。
                }
                catch (Exception ex)
                {
                    ViewBag.paymentstate = 0;
                    ViewBag.paymentMsg = "付款失败!请重试" + ex.Message;
                }

                ViewBag.courseid = coursid;
            }
            return View();
        }