public static BoolAny <string> verOrigWorkerAccount(string workerID, string password)
        {
            string postString = "workid=" + workerID + "&password="******"/worker/verifiy", postString);
                return(BoolAny <string> .succeed(myjson));
            }
            catch (Exception e)
            {
                return(BoolAny <string> .fail(e.Message));
            }
        }
 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());
     }
 }
        /// <summary>
        /// 获取员工账号信息
        /// </summary>
        /// <param nickname="workerID"></param>
        /// <returns></returns>
        public static String getOrigWorkerAccount(string workerID)
        {
            string postString = "workid=" + workerID;

            try
            {
                string myjson = ASConsoLe.RequestProxy(Config.AS_Url + "/get", postString);
                return(BoolAny <string> .succeed(myjson));
            }
            catch (Exception e)
            {
                return(BoolAny <string> .fail(e.Message));
            }
        }
        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));
            }
        }
        public static BoolAny <String> SetTrail(string openid)
        {
            WeixinDBContext db = new WeixinDBContext();
            weixinAccount   wa = db.weixinAccount.FirstOrDefault(d => d.openid == openid);

            if (!wa.isTrail)
            {
                wa.trailDate = DateTime.Now;
                wa.isTrail   = false;
                db.SaveChanges();
                return(BoolAny <String> .succeed());
            }
            else
            {
                return(BoolAny <string> .fail("已经绑定过"));
            }
        }
        /// <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 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("账户或密码错误"));
                    }
                }
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param nickname="cc"></param>
        /// <param nickname="db"></param>
        /// <param nickname="studentId"></param>
        /// <param nickname="test"></param>
        /// <returns></returns>
        public static BoolAny <TestVertifyResult> Verify(Controller cc, BaseDBContext db, int studentId, Test test)
        {
            TestResult testResult = null;

            testResult = test.TestResults.FirstOrDefault(d => d.Student.Id == studentId);


            if (testResult == null) //没有记录,
            {
                var now = DateTime.Now;
                return(BoolAny <TestVertifyResult> .succeed(new TestVertifyResult()
                {
                    Statue = TestStatue.新的,
                    DateBegin = now,
                    DateEnd = now.Add(test.Exam.Time),
                    Message = "首次参于考试"
                }));
            }
            else //有记录
            {
                TestSession testSession = getTestSession(test, cc);

                if (testSession == null || testSession.TestResultId != testResult.Id) //没有session,或seesion中 指定考试的id不匹配, 从数据库中读取
                {
                    if (testResult.SubmitDate == null)
                    {//上次没有答完
                        var endate = testResult.Date.Add(test.Exam.Time);
                        if (endate < DateTime.Now)
                        { //已经超过时间了
                            return(BoolAny <TestVertifyResult> .fail(new TestVertifyResult()
                            {
                                Statue = TestStatue.超时,
                                DateBegin = testResult.Date,
                                DateEnd = testResult.Date.Add(test.Exam.Time),
                                resultID = testResult.Id,
                                Message = "上次考试(" + testResult.Date.ToString("yyyy-MM-dd HH:mm") + ")未打答完,现已超时"
                            }));
                        }
                        else //未超时 Session没
                        {
                            return(BoolAny <TestVertifyResult> .succeed(new TestVertifyResult()
                            {
                                Statue = TestStatue.继续,
                                DateBegin = testResult.Date,
                                resultID = testResult.Id,
                                DateEnd = testResult.Date.Add(test.Exam.Time),
                                Message = "上次考试(" + testResult.Date.ToString("yyyy-MM-dd HH:mm") + ")未打答完,现继续考试,剩余约" + ((testResult.Date.Add(test.Exam.Time) - DateTime.Now).TotalMinutes).ToString("0.0") + "分钟"
                            }));
                        }
                    }
                    else
                    {
                        return(BoolAny <TestVertifyResult> .fail(new TestVertifyResult()
                        {
                            Statue = TestStatue.完成,
                            DateBegin = testResult.Date,
                            resultID = testResult.Id,
                            DateEnd = testResult.SubmitDate.Value,
                            Message = "上次考试(" + testResult.Date.ToString("yyyy-MM-dd HH:mm") + "),已答完"
                        }));
                    }
                }
                else //有session 从session中读取
                {
                    if (testResult.SubmitDate == null)//上次没有答完
                    {
                        if (testSession.DateEnd > DateTime.Now)//已经超过时间了
                        {
                            return(BoolAny <TestVertifyResult> .fail(new TestVertifyResult()
                            {
                                Statue = TestStatue.超时,
                                DateBegin = testSession.DateBegin,
                                resultID = testResult.Id,
                                DateEnd = testSession.DateEnd,
                                Message = "上次考试(" + testResult.Date.ToString("yyyy-MM-dd HH:mm") + ")未打答完,现已超时"
                            }));
                        }
                        else //继续答题
                        {
                            return(BoolAny <TestVertifyResult> .succeed(new TestVertifyResult()
                            {
                                Statue = TestStatue.继续,
                                DateBegin = testSession.DateBegin,
                                DateEnd = testSession.DateEnd,
                                resultID = testResult.Id,
                                Message = "上次考试(" + testResult.Date.ToString("yyyy-MM-dd HH:mm") + ")未打答完,现继续考试,剩余约" + (testSession.DateEnd - DateTime.Now).TotalMinutes + "分钟"
                            }));
                        }
                    }
                    else //答完了
                    {
                        return(BoolAny <TestVertifyResult> .fail(new TestVertifyResult()
                        {
                            Statue = TestStatue.完成,
                            DateBegin = testResult.Date,
                            DateEnd = testResult.SubmitDate.Value,
                            resultID = testResult.Id,
                            Message = "上次考试(" + testResult.Date.ToString("yyyy-MM-dd HH:mm") + "),已答完"
                        }));
                    }
                }
            }
        }
 public static BoolAny <string> Clear()
 {
     return(BoolAny <string> .succeed());
 }