public static AccountSession UpdateAccountAndSession(UserInfoJson u, bool?isAttend = null)
        {
            using (BaseDBContext db = new BaseDBContext())
            {
                var wa = db.WxAccount.FirstOrDefault(d => d.openid == u.openid);
                if (wa != null)
                {
                    if (isAttend == null)
                    {
                        isAttend = false;
                    }
                }
                else
                {
                    if (isAttend == null)
                    {
                        isAttend = wa.subscribe;
                    }
                }
                wa = WxDBUpdateAccount(u, db, isAttend.Value);

                wa.lastDate = DateTime.Now;
                db.SaveChanges();

                Student s = db.Student.Find(wa.studentId);

                var a = AccountHelper.CreateAccountSession(wa, s);
                AccountHelper.SetSession(a);

                //debug.log("UpdateAccountAndSession_最终AccountSession", a);
                return(a);
            }
        }
        public JsonResult Submit(int voteId, int voteItemId)
        {
            //int voteId = (int)TempData["voteId"];
            var voteItem = db.VoteItem.FirstOrDefault(d => d.Id == voteItemId && d.VoteId == voteId && !d.Vote.IsDisabled && d.Vote.IsOpen);


            if (voteItem == null)
            {
                return(myJson.error(" 投票无效"));
            }

            var vote = voteItem.Vote;

            if (db.VoteLog.Any(d => d.VoteId == voteId))
            {
                return(myJson.error("您已经投过票"));
            }
            voteItem.Count++;
            vote.Count++;
            db.VoteLog.Add(new VoteLog()
            {
                Date       = DateTime.Now,
                StudentId  = account.studentId,
                VoteId     = voteId,
                VoteItemId = voteItemId,
                ip         = Request.UserAgent
            });
            db.SaveChanges();

            return(myJson.success(new{ voteId = voteId, count = vote.Count, itemCount = vote.VoteItems.OrderByDescending(d => d.Index).Select(d => d.Count).ToArray() }));
        }
        public void SetZeroScore(int id, bool flag)
        {
            try
            {
                using (var ctx = new BaseDBContext())
                {
                    var alliance = ctx.Alliances.Where(a => a.Id == id).FirstOrDefault();
                    if (alliance != null)
                    {
                        alliance.ZeroScore = Convert.ToByte(flag);
                        ctx.SaveChanges();
                    }
                    //else
                    //{
                    //    throw new Exception("Not Fount!");
                    //}
                }

                //update sub site
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 验证并产生TestSession
        /// </summary>
        /// <param nickname="cc"></param>
        /// <param nickname="db"></param>
        /// <param nickname="studentId"></param>
        /// <param nickname="test"></param>
        /// <returns></returns>
        public static BoolAny <TestVertifyResult> Start(Controller cc, BaseDBContext db, int studentId, Test test)
        {
            var result    = Verify(cc, db, studentId, test);
            var tesresult = test.TestResults.FirstOrDefault(d => d.StudentId == studentId);

            if (result.t.Statue == TestStatue.新的)
            {
                //if (element == null)
                //{
                //    element = new TestElement();
                //    element.StudentId = studentId;
                //    element.TestResults = new List<TestResult>();
                //    test.Elements.Add(element);
                //    db.SaveChanges();
                //}

                var tr = new TestResult()
                {
                    TestId    = test.Id,
                    ExamId    = test.ExamId.Value,
                    Date      = DateTime.Now,
                    StudentId = studentId
                };
                test.TestResults.Add(tr);
                test.JoinCount++;
                test.StudentCount++;
                try {
                    db.SaveChanges();
                }catch (Exception e)
                {
                }

                setTestSession(test, cc, new TestSession()
                {
                    DateBegin    = result.t.DateBegin,
                    DateEnd      = result.t.DateEnd,
                    TestId       = test.Id,
                    studentId    = studentId,
                    openid       = "",
                    TestResultId = tr.Id
                });
                //写记录

                //var xx = test.Elements.FirstOrDefault(d => d.StudentId == studentId).TestResults;
            }
            if (result.t.Statue == TestStatue.继续)
            {
                setTestSession(test, cc, new TestSession()
                {
                    DateBegin    = result.t.DateBegin,
                    DateEnd      = result.t.DateEnd,
                    TestId       = test.Id,
                    studentId    = studentId,
                    openid       = "",
                    TestResultId = tesresult.Id,
                });
            }
            return(result);
        }
        public ActionResult detail(int id)
        {
            var data = db.News.Find(id);

            data.VisitCount++;
            db.SaveChanges();
            return(View(data));
        }
        public ActionResult Edit(WxArtcle wc, Article at)
        {
            if (string.IsNullOrWhiteSpace(at.Title))
            {
                ModelState.AddModelError("", "标题不能为空");
                return(View(wc));
            }
            wc.Description = ArticleHelper.ArticleToString(at);

            Boolean isHas = db.WxArtcle.Where(d => d.Name.ToUpper() == wc.Name.ToUpper() && d.Id != wc.Id).Any();

            if (wc.Id == 0)
            {
                /*
                 * 验证Name重复了
                 */
                if (isHas)
                {
                    ModelState.AddModelError("", "名称不能重复");
                    return(View(wc));
                }

                db.WxArtcle.Add(wc);
            }
            else
            {
                if (isHas)
                {
                    ModelState.AddModelError("", "名称不能重复");
                    return(View(wc));
                }
                db.Entry(wc).State = EntityState.Modified;
            }
            try
            {
                db.SaveChanges();
            }
            catch
            {
                return(View(wc));
            }
            //WPconfig.UpdatewxDictKeyValue<WxArtcle>();
            return(RedirectToAction("Index"));
        }
Beispiel #7
0
        public JsonResult toTop(int id)
        {
            var data = db.Votes.Find(id);

            data.Index = DateTime.Now.ToTimeStamp();
            db.SaveChanges();
            return(myJson.success());
        }
Beispiel #8
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //BundleConfig.RegisterBundles(BundleTable.Bundles);

            JULONG.TRAIN.WEB.Models.DBfun.DBInit();
            JULONG.TRAIN.WEB.Models.DBfun.WxDBInit();
            ASConsoLe.Init(Config.AS_Url, Config.AS_AppId, Config.AS_SecretKey);



            /*同步wx关注用户*/
            return;

            string accessToken = AccessTokenContainer.TryGetAccessToken(Config.AS_AppId, Config.AS_SecretKey);

            wx.OpenIdResultJson openIds;
            int count = 0;

            using (BaseDBContext db = new BaseDBContext())
            {
                string nextOpenId = "";
                do
                {
                    openIds = UserApi.Get(accessToken, nextOpenId);
                    if (openIds.count != 0)
                    {
                        foreach (var x in openIds.data.openid)
                        {
                            var o = db.WxAccount.FirstOrDefault(d => d.openid == x);
                            if (o == null)
                            {
                                db.WxAccount.Add(new WxAccount()
                                {
                                    lastDate = DateTime.Now, regDate = DateTime.Now, openid = x, subscribe = true
                                });

                                count++;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    nextOpenId = openIds.next_openid;
                } while (openIds.count != 0);
                if (count > 0)
                {
                    db.SaveChanges();
                }
            }
        }
Beispiel #9
0
        public ActionResult TestSubmit(int id, string answers)
        {
            var account = AccountHelper.account;
            //需要改进缓存式
            var test = db.Test.Find(id);

            if (test == null)
            {
                return(myJson.error("无效考试"));
            }
            if (!test.IsOpen)
            {
                return(myJson.error("该考试的活动已经结束"));
            }

            var tr = test.TestResults.FirstOrDefault(d => d.StudentId == account.studentId);

            TimeSpan time  = DateTime.Now - tr.Date;
            var      etime = (time - test.Exam.Time);

            if (etime.TotalSeconds > 0)
            {
                return(myJson.error("您已经超出最后交卷 " + etime.ToString("hh") + ":" + etime.ToString("mm") + ":" + etime.ToString("ss") + " 时间"));
            }


            TestResult _tr = default(TestResult);

            try {
                _tr = TestHelper.CalTestResult(test.Exam, answers);
            }
            catch (Exception e)
            {
                return(myJson.error(e.Message));
            }

            if (tr.SubmitDate.HasValue)
            {
                return(myJson.error("您已经答过了"));
            }

            tr.SubmitDate = DateTime.Now;
            tr.RightCount = _tr.RightCount;
            tr.Value      = _tr.Value;
            tr.Answers    = answers;
            tr.UseTime    = time;
            db.SaveChanges();


            return(myJson.success(tr.Id));
        }
 /// <summary>
 /// 取消关注
 /// </summary>
 /// <returns></returns>
 public static Boolean WxCancelAttend(string openid)
 {
     try
     {
         using (BaseDBContext wdb = new BaseDBContext())
         {
             WxAccount newWa = wdb.WxAccount.Find(openid);
             if (newWa != null)
             {
                 newWa.subscribe = false;
                 newWa.lastDate  = DateTime.Now;
                 wdb.SaveChanges();
             }
             wdb.SaveChanges();
         }
     }
     catch (Exception e)
     {
         //debug.print("cancelAttend", e.Message);
         return(false);
     }
     return(true);
 }
 public static BoolAny <AccountSession> WxUnBinding(string openid)
 {
     using (BaseDBContext db = new BaseDBContext())
     {
         var wxaccount = db.WxAccount.Find(openid);
         if (wxaccount == null || wxaccount.studentId == 0)
         {
             return(BoolAny <AccountSession> .fail("未绑定"));
         }
         wxaccount.studentId = 0;
         db.SaveChanges();
         return(BoolAny <AccountSession> .succeed());
     }
 }
        public static BoolAny <WxAccount> WxDBUpdateAccount(UserInfoJson userinfo, BaseDBContext wdb, bool isAttend)
        {
            try
            {
                WxAccount newWa = wdb.WxAccount.Find(userinfo.openid);

                if (newWa == null)
                {
                    newWa = new WxAccount()
                    {
                        subscribe = isAttend,
                        regDate   = DateTime.Now,
                        //subscribe_time = LIB.DateTimeEx.TimeStampToDateTime(userinfo.subscribe_time),
                        subscribe_time = DateTime.Now,
                        openid         = userinfo.openid,
                        lastDate       = DateTime.Now,
                        headimgurl     = userinfo.headimgurl,
                        nickname       = userinfo.nickname,
                        sex            = userinfo.sex.ToString(),
                        groupid        = userinfo.groupid.ToString(),
                        remark         = userinfo.remark,
                        unionid        = userinfo.unionid,
                        studentId      = 0
                    };
                    wdb.WxAccount.Add(newWa);
                }
                else
                {
                    newWa.subscribe = isAttend;

                    newWa.headimgurl = userinfo.headimgurl;
                    newWa.nickname   = userinfo.nickname;
                    newWa.sex        = userinfo.sex.ToString();
                    newWa.groupid    = userinfo.groupid.ToString();
                    newWa.remark     = userinfo.remark;
                }


                wdb.SaveChanges();
                //debug.log("WxDBUpdateAccount_DBSave", newWa);
                return(BoolAny <WxAccount> .succeed(newWa));
            }
            catch (DbEntityValidationException dbEx)
            {
                debug.log("WxDBUpdateAccount_DBSave_异常", dbEx.Message);
                //debug.print("addAccount_error", e.Message);
                return(BoolAny <WxAccount> .fail(dbEx.Message));
            }
        }
        /// <summary>
        /// 新增或更新关注用户信息
        /// </summary>
        /// <returns></returns>
        public static BoolAny <WxAccount> WxDBUpdateAccount(string openid, BaseDBContext wdb, bool isAttend)
        {
            try
            {
                WxAccount newWa = wdb.WxAccount.Find(openid);
                if (isAttend)
                {
                    if (newWa == null)
                    {
                        newWa = new WxAccount()
                        {
                            subscribe      = true,
                            regDate        = DateTime.Now,
                            subscribe_time = DateTime.Now,
                            openid         = openid,
                            lastDate       = DateTime.Now
                        };
                        wdb.WxAccount.Add(newWa);
                    }
                    else
                    {
                        newWa.subscribe = true;
                    }
                    //debug.log("WxDBUpdateAccount_string_Attend", newWa);
                }
                else
                {
                    //debug.log("WxDBUpdateAccount_string_UnAttend", newWa);
                    if (newWa != null)
                    {
                        newWa.subscribe = false;
                    }
                }

                wdb.SaveChanges();
                return(BoolAny <WxAccount> .succeed(newWa));
            }
            catch (Exception e)
            {
                //debug.print("addAccount_error", e.Message);
                return(BoolAny <WxAccount> .fail(e.Message));
            }
        }
        public static AccountSession UpdateAccountAndSession(string openid, bool isAttend)
        {
            using (BaseDBContext db = new BaseDBContext())
            {
                var wa = db.WxAccount.FirstOrDefault(d => d.openid == openid && isAttend);


                wa = WxDBUpdateAccount(openid, db, isAttend);


                wa.lastDate = DateTime.Now;
                db.SaveChanges();

                Student s = db.Student.Find(wa.studentId);

                var a = AccountHelper.CreateAccountSession(wa, s);
                AccountHelper.SetSession(a);

                return(a);
            }
        }
Beispiel #15
0
        public static void WxDBInit()
        {
            using (BaseDBContext db = new BaseDBContext())
            {
                if (!db.WxArtcle.Any(d => d.Name == "Welcome"))
                {
                    db.WxArtcle.Add(new WxArtcle()
                    {
                        Name = "Welcome", type = WeixinArtcleType.图文, Bak = "关注欢迎"
                    });
                }

                if (!db.WxArtcle.Any(d => d.Name == "help"))
                {
                    db.WxArtcle.Add(new WxArtcle()
                    {
                        Name = "help", type = WeixinArtcleType.图文, Bak = "帮助"
                    });
                }

                db.SaveChanges();
            }
        }
Beispiel #16
0
 /// <summary>
 /// 事务性保存
 /// </summary>
 public void Save()
 {
     _dbContext.SaveChanges();
 }
Beispiel #17
0
 public JsonResult toTop(int id)
 {
     db.Test.Find(id).Index = DateTime.Now.ToTimeStamp();
     db.SaveChanges();
     return(myJson.success());
 }
        public static BoolAny <AccountSession> WxBinding(string openid, string workId, string password)
        {
            if (string.IsNullOrWhiteSpace(workId) || string.IsNullOrWhiteSpace(password))
            {
                return(BoolAny <AccountSession> .fail("工号或密码不能为空"));
            }

            using (BaseDBContext db = new BaseDBContext())
            {
                var     wa      = db.WxAccount.Find(openid);
                Student student = null;
                ///!!!updatesession studentID未去掉
                if (wa.studentId != 0)
                {
                    student = db.Student.Find(wa.studentId);
                    if (student != null)
                    {
                        return(BoolAny <AccountSession> .fail("你已经绑定了工号:" + student.WorkID));
                    }
                }

                //var student = db.Student.FirstOrDefault(d => d.Id== wa.studentId);

                //if (student != null)
                //{
                //    return BoolAny<AccountSession>.fail("你已经绑定了工号:"+student.WorkID);
                //}



                //password = password.MD5();
                //var member = db.Student.FirstOrDefault(d => d.workId == workid);

                var origWorker = verOrigWorkerAccount(workId, password);

                if (!origWorker) //通讯失败
                {
                    return(BoolAny <AccountSession> .fail(origWorker.message));
                }
                else
                {
                    var res = JsonConvert.DeserializeObject <myJson.myJsonResultData>(origWorker);

                    if (res.code == 0)//验证成功
                    {
                        var studentwork = db.Student.FirstOrDefault(d => d.WorkID == workId);

                        if (studentwork != null)
                        {
                            wa.studentId = studentwork.Id;
                            db.SaveChanges();
                            var sa = CreateAccountSession(wa, student);
                            return(BoolAny <AccountSession> .succeed(sa, "绑定多账号"));
                        }
                        else
                        {
                            Dictionary <string, string> _s = JsonConvert.DeserializeObject <Dictionary <string, string> >(res.data.ToString());
                            try
                            {
                                var newStudent = new Student()
                                {
                                    Name          = _s["emplname"],
                                    Sex           = _s["sex"] == "False"?Sex.男:Sex.女,
                                    Phone         = "",
                                    RegDate       = DateTime.Now,
                                    WorkID        = workId,
                                    GroupName     = _s["deptname"],
                                    LastLoginDate = DateTime.Now
                                };
                                db.Student.Add(newStudent);
                                db.SaveChanges();
                                var sa = CreateAccountSession(wa, newStudent);
                                return(BoolAny <AccountSession> .succeed(sa, "绑定账号"));
                            }
                            catch (Exception e)
                            {
                                return(BoolAny <AccountSession> .fail(e.Message));
                            }
                        }
                    }
                    else
                    {
                        return(BoolAny <AccountSession> .fail("账户或密码错误"));
                    }
                }
            }
        }