// GET: Teacher
        public ActionResult Index()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToRoute(new { controller = "Login", action = "LogOut" }));
            }
            int userid = PageValidate.FilterParam(User.Identity.Name);

            if (!RoleCheck.CheckHasAuthority(userid, db, "用户查询", "用户管理"))
            {
                return(RedirectToRoute(new { controller = "Error", action = "Index", err = "没有权限。" }));
            }
            int id      = 3;
            int role_id = (from u in db.User_Infos
                           join ur in db.User_vs_Roles on u.user_id equals ur.uvr_user_id
                           where u.user_id == id
                           select ur.uvr_role_id
                           ).Min();

            if (RoleCheck.CheckIsSuperAdmin(userid, db))
            {
                ViewBag.isdelete = 1;
            }
            else
            {
                ViewBag.isdelete = 0;
            }
            if (RoleCheck.CheckHasAuthority(userid, db, "用户管理"))
            {
                ViewBag.isedit = 1;
            }
            else
            {
                ViewBag.isedit = 0;
            }
            IQueryable <UserModel> list = null;

            if (role_id == 1)
            {
                ViewBag.isdelete = 1;
            }
            if (role_id == 5)
            {
                ViewBag.isedit = 0;
            }
            list = from user in db.User_Infos
                   join uvr in db.User_vs_Roles
                   on user.user_id equals uvr.uvr_user_id into UVR
                   from ur in UVR.DefaultIfEmpty()
                   join role in db.Sys_Roles
                   on ur.uvr_role_id equals role.role_id into R
                   from r in R.DefaultIfEmpty()
                   where user.user_is_teacher == true
                   select new UserModel
            {
                user_id          = user.user_id,
                role_name        = r.role_name,
                role_id          = r.role_id,
                user_name        = user.user_name,
                user_phone       = user.user_phone,
                user_photo_path  = user.user_photo_path,
                user_login_times = user.user_login_times,
                real_name        = user.real_name,
                gender           = user.user_gender
            };

            if (list != null)
            {
                return(View(list.OrderBy(x => new { x.role_id, x.user_name }).ToList()));
            }
            else
            {
                return(View(list));
            }
        }
        public ActionResult Edit([Bind(Include = "user_id,user_name,real_name,gender,user_phone,user_info,user_email,user_password,user_password2,user_home_address,user_photo_path,role_id,state")] TeacherEditModel model)
        {
            setSelect();
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToRoute(new { controller = "Login", action = "LogOut" }));
            }
            if (ModelState.IsValid)
            {
                //if (Session["token"] == null || Session["token"].ToString() != model.token)
                //{
                //    ViewBag.msg = "异常操作,请退出当前页面后重新进入操作。";
                //    return View(model);
                //}
                int userid = PageValidate.FilterParam(User.Identity.Name);
                if (!RoleCheck.CheckHasAuthority(userid, db, "用户管理") && model.user_id != userid)
                {
                    return(RedirectToRoute(new { controller = "Error", action = "Index", err = "没有权限。" }));
                }
                User_Info user_Info = db.User_Infos.Find(model.user_id);
                if (user_Info == null)
                {
                    ViewBag.msg = "没有找到相关信息,资料可能被删除。";
                    return(View(model));
                }

                if (db.User_Infos.Where(x => x.user_id != model.user_id && x.user_phone == model.user_phone).Count() > 0)
                {
                    ViewBag.msg = "该手机号码已存在。";
                    return(View(model));
                }
                if (!string.IsNullOrEmpty(model.user_password))
                {
                    if (model.user_password != model.user_password2)
                    {
                        ViewBag.msg = "两次输入的密码不匹配。";
                        return(View(model));
                    }
                    var salt = Guid.NewGuid().ToString("N").Substring(0, 10).ToUpper();
                    user_Info.user_password = AESEncrypt.Encrypt(PasswordUnit.getPassword(model.user_password.ToUpper(), salt));
                    user_Info.user_salt     = salt;
                }
                string err = "";
                if (!string.IsNullOrEmpty(model.user_photo_path) && model.user_photo_path != user_Info.user_photo_path)
                {
                    string photoDir = MyConfiguration.GetPhotoPath();
                    if (!Directory.Exists(photoDir))
                    {
                        Directory.CreateDirectory(photoDir);
                    }
                    string photoTempDir   = MyConfiguration.GetTempPhotoPath();
                    string file_name      = string.Format("{0}{1}", photoDir, model.user_photo_path).Replace("_temp", "");
                    string temp_file_name = string.Format("{0}{1}", photoTempDir, model.user_photo_path);
                    if (System.IO.File.Exists(temp_file_name))
                    {
                        FileInfo fi = new FileInfo(temp_file_name);
                        fi.CopyTo(file_name, true);
                        model.user_photo_path     = Path.GetFileName(file_name);
                        user_Info.user_photo_path = model.user_photo_path;
                    }
                    else
                    {
                        err = "图片保存失败。";
                    }
                }
                user_Info.user_name         = model.user_name;
                user_Info.user_phone        = model.user_phone;
                user_Info.user_info         = model.user_info;
                user_Info.user_email        = model.user_email;
                user_Info.user_home_address = model.user_home_address;
                user_Info.user_update_time  = DateTime.Now;
                user_Info.user_update_user  = userid;
                user_Info.user_gender       = model.gender;
                user_Info.real_name         = model.real_name;
                user_Info.user_is_teacher   = true;
                if (string.IsNullOrEmpty(user_Info.user_bindCode))
                {
                    user_Info.user_bindCode = Guid.NewGuid().ToString("N").Substring(0, 8);
                }
                db.Entry(user_Info).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                }catch (Exception e)
                {
                    err = "资料保存失败。";
                    ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString());
                }
                //权限设置
                if (RoleCheck.CheckIsSuperAdmin(model.user_id, db))
                {
                    if (model.role_id != 1)
                    {
                        err = "系统管理员权限不允许更改。";
                    }
                    goto next;
                }
                if (model.role_id == 1 && !RoleCheck.CheckIsSuperAdmin(userid, db))//添加系统管理员权限
                {
                    err = "只有系统管理员才可以添加系统管理员权限。";
                }
                else
                {
                    var uvr = db.User_vs_Roles.Where(x => x.uvr_user_id == model.user_id);
                    db.User_vs_Roles.RemoveRange(uvr);
                    User_vs_Role Nuvr = new User_vs_Role
                    {
                        uvr_user_id = model.user_id,
                        uvr_role_id = model.role_id
                    };
                    db.User_vs_Roles.Add(Nuvr);
                    try
                    {
                        db.SaveChanges();
                    }catch (Exception e)
                    {
                        err = "角色添加失败。";
                    }
                }
next:
                if (err == "")
                {
                    ViewBag.msg = "修改成功。";
                }
                else
                {
                    ViewBag.msg = err;
                }
            }
            return(View(model));
        }