コード例 #1
0
        /// <summary>
        /// 获取教职工信息
        /// </summary>
        /// <param name="id">教职工编号</param>
        /// <param name="context">数据库连接上下文对象</param>
        /// <returns></returns>
        public async Task <StaffEditViewModel> GetStaffAsync(long id, ApplicationDbContext context)
        {
            var webModel = new StaffEditViewModel();

            try
            {
                var model = await PSURepository.GetUserByIdAsync(id, context);

                webModel.Id             = model.Id.ToString();
                webModel.IsEnabled      = (Enable)(model.IsEnabled ? 1 : 0);
                webModel.Name           = model.Name;
                webModel.QQ             = model.QQ.ToString();
                webModel.Wechat         = model.Wechat;
                webModel.DepartmentId   = model.DepartmentId;
                webModel.DepartmentName = model.Department;
                webModel.Account        = model.Account;
                webModel.Address        = model.Address;
                webModel.Age            = model.Age;
                webModel.Email          = model.Email;
                webModel.Gender         = model.Gender;
                webModel.IdNumber       = model.IdNumber;
                webModel.IsMaster       = model.IsMaster;
                webModel.Password       = model.Password;
                webModel.Phone          = model.Phone;
            }
            catch (Exception ex)
            {
                _logger.LogError("获取教职工数据失败:{0},\r\n内部错误信息:{1}", ex.Message, ex.InnerException.Message);
            }
            return(webModel);
        }
コード例 #2
0
        public async Task <IActionResult> EditStaff(StaffEditViewModel webModel)
        {
            if (ModelState.IsValid)
            {
                bool flag;
                if (string.IsNullOrEmpty(webModel.Id))
                {
                    //Add Staff
                    flag = await _service.InsertStaffAsync(webModel, _context);
                }
                else
                {
                    //Update Staff
                    flag = await _service.UpdateStaffAsync(webModel, _context);
                }

                return(Json(new
                {
                    success = flag,
                    msg = flag ? "教职工信息编辑成功" : "教职工信息编辑失败"
                }));
            }

            return(Json(new
            {
                success = false,
                msg = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors).FirstOrDefault().ErrorMessage
            }));
        }
コード例 #3
0
        /// <summary>
        /// Insert Identity Entity
        /// </summary>
        /// <param name="webModel"></param>
        /// <returns></returns>
        private static IdentityUser InsertModel(StaffEditViewModel webModel)
        {
            var salt = Guid.NewGuid().ToString();

            return(new IdentityUser
            {
                Account = webModel.Account,
                AccountType = 1,
                Salt = salt,
                Password = MD5Utility.Sign(webModel.Password, salt),
                Email = webModel.Email,
                Phone = webModel.Phone,
                Name = webModel.Name,
                Gender = webModel.Gender,
                IdNumber = webModel.IdNumber,
                Wechat = webModel.Wechat,
                QQ = Convert.ToInt64(webModel.QQ),
                IsEnabled = (int)webModel.IsEnabled == 1,
                IsMaster = webModel.IsMaster,
                Address = webModel.Address,
                Age = webModel.Age,
                HomePage = "Instructor",
                CreatedId = CurrentUser.UserId,
                CreatedBy = CurrentUser.UserOID,
                CreatedName = CurrentUser.UserName
            });
        }
コード例 #4
0
        public async Task <ViewResult> EditAsync(int id)
        {
            Staff staff = await _context.Staffs.FindAsync(id);

            StaffEditViewModel staffEditViewModel = new StaffEditViewModel
            {
                Id = staff.Id,
                ApplicationUser      = staff.ApplicationUser,
                FirstName            = staff.FirstName,
                LastName             = staff.LastName,
                DOB                  = staff.DOB,
                Address              = staff.Address,
                Gender               = staff.Gender,
                LocalGovt            = staff.LocalGovt,
                Town                 = staff.Town,
                State                = staff.State,
                Position             = staff.Position,
                Salary               = staff.Salary,
                Loan                 = staff.Loan,
                Debt                 = staff.Debt,
                ExistingProfilePhoto = staff.ProfilePicture,
                UpdateDate           = DateTime.Now.ToString()
            };

            return(View(staffEditViewModel));
        }
コード例 #5
0
        public async Task <IActionResult> EditStaff(string id)
        {
            StaffEditViewModel webModel = new StaffEditViewModel();

            if (!string.IsNullOrEmpty(id))
            {
                webModel = await _service.GetStaffAsync(Convert.ToInt64(id), _context);
            }

            //加载下拉列表信息
            webModel = await _service.GetDropDownListAsync(webModel, _context);

            return(View(webModel));
        }
コード例 #6
0
        public async Task <ActionResult> StaffEdit(StaffEditViewModel model)
        {
            var user = await UserManager.FindByIdAsync(model.UserId);

            var userInfo = _context.UserInfoes.SingleOrDefault(p => p.UserId == model.UserId);

            user.UserName = model.UserName;
            user.Email    = model.Email;

            UserManager.Update(user);

            if (model.Password != null)
            {
                var token = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

                var result = await UserManager.ResetPasswordAsync(user.Id, token, model.Password);
            }

            switch ((UserRole)model.RoleId)
            {
            case UserRole.Trainer:
                var trainerInfo = (TrainerInfo)userInfo;

                trainerInfo.FullName     = model.TrainerInfo.FullName;
                trainerInfo.Address      = model.TrainerInfo.Address;
                trainerInfo.PhoneNumber  = model.TrainerInfo.PhoneNumber;
                trainerInfo.WorkingPlace = model.TrainerInfo.WorkingPlace;
                break;

            case UserRole.Trainee:
                var traineeInfo = (TraineeInfo)userInfo;

                traineeInfo.FullName    = model.TraineeInfo.FullName;
                traineeInfo.Address     = model.TraineeInfo.Address;
                traineeInfo.PhoneNumber = model.TraineeInfo.PhoneNumber;
                traineeInfo.TOEICScore  = model.TraineeInfo.TOEICScore;
                break;

            default:
                break;
            }

            _context.SaveChanges();

            this.AddNotification("Account updated", NotificationType.SUCCESS);

            return(RedirectToAction("Details/" + model.UserId, "Profile"));
        }
コード例 #7
0
        /// <summary>
        /// 获取编辑页面部门下拉列表
        /// </summary>
        /// <param name="webModel">编辑页视图Model</param>
        /// <param name="context">数据库连接上下文</param>
        /// <returns></returns>
        public async Task <StaffEditViewModel> GetDropDownListAsync(StaffEditViewModel webModel, ApplicationDbContext context)
        {
            //Get Source Data
            var departmentList = await BasicRepository.GetDepartmentList(context);

            if (departmentList != null && departmentList.Any())
            {
                webModel.DepartmentList = departmentList.Select(item => new DepartmentDropDown
                {
                    Id   = item.Id.ToString(),
                    Name = item.Name
                }).ToList();
            }

            return(webModel);
        }
コード例 #8
0
        /// <summary>
        /// 更新教职工信息
        /// </summary>
        /// <param name="webModel">专业班级编辑页视图模型</param>
        /// <param name="context">数据库上下文对象</param>
        public static async void UpdateAsync(StaffEditViewModel webModel, ApplicationDbContext context)
        {
            var model = await context.IdentityUser.FirstOrDefaultAsync(i => i.Id == Convert.ToInt64(webModel.Id));

            if (model == null)
            {
                return;
            }

            model = UpdateModel(webModel, model);

            //Get Foreign Key Association Table Information
            var department = await context.Department.Where(i => i.Id == Convert.ToInt64(webModel.DepartmentId)).FirstOrDefaultAsync();

            model.DepartmentId = Convert.ToInt64(webModel.DepartmentId);
            model.Department   = department.Name;
        }
コード例 #9
0
        public IActionResult Edit(StaffEditViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }
            Staff editedStaff = new Staff()
            {
                Id       = viewModel.Id,
                FullName = viewModel.FullName,
                Sector   = viewModel.Sector,
                BioText  = viewModel.BioText,
                StatusId = viewModel.StatusId,
                CampusId = viewModel.CampusId
            };

            _staffService.Edit(editedStaff);
            return(RedirectToAction("Index"));
        }
コード例 #10
0
        public IActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index"));
            }
            var findStaff = _staffService.GetAdmin(id);

            StaffEditViewModel viewModel = new StaffEditViewModel()
            {
                Id       = findStaff.Id,
                FullName = findStaff.FullName,
                BioText  = findStaff.BioText,
                Sector   = findStaff.Sector,
                StatusId = findStaff.StatusId,
                CampusId = findStaff.CampusId
            };

            return(View(viewModel));
        }
コード例 #11
0
        /// <summary>
        /// 更新教职工信息
        /// </summary>
        /// <param name="webModel">编辑页视图Model</param>
        /// <param name="context">数据库连接上下文对象</param>
        /// <returns></returns>
        public async Task <bool> UpdateStaffAsync(StaffEditViewModel webModel, ApplicationDbContext context)
        {
            try
            {
                //Update staff Data
                BasicRepository.UpdateAsync(webModel, context);

                //Add Operate Information
                var operate = string.Format("修改教职工信息,教职工编号:{0}", webModel.Id);
                PSURepository.InsertRecordAsync("IdentityUser", "BasicDomain", "UpdateStaffAsync", operate, (short)PSURepository.OperateCode.Update, Convert.ToInt64(webModel.Id), context);

                var index = await context.SaveChangesAsync();

                return(index == 2);
            }
            catch (Exception ex)
            {
                _logger.LogError("更新教职工失败:{0},\r\n内部错误信息:{1}", ex.Message, ex.InnerException.Message);
                return(false);
            }
        }
コード例 #12
0
        /// <summary>
        /// Update Identity Entity
        /// </summary>
        /// <param name="webModel"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        private static IdentityUser UpdateModel(StaffEditViewModel webModel, IdentityUser model)
        {
            model.Account      = webModel.Account;
            model.Password     = MD5Utility.Sign(webModel.Password, model.Salt);
            model.Email        = webModel.Email;
            model.Phone        = webModel.Phone;
            model.Name         = webModel.Name;
            model.Gender       = webModel.Gender;
            model.IdNumber     = webModel.IdNumber;
            model.Wechat       = webModel.Wechat;
            model.QQ           = Convert.ToInt64(webModel.QQ);
            model.IsEnabled    = (int)webModel.IsEnabled == 1;
            model.IsMaster     = webModel.IsMaster;
            model.Address      = webModel.Address;
            model.Age          = webModel.Age;
            model.ModifiedOn   = DateTime.Now;
            model.ModifiedId   = CurrentUser.UserId;
            model.ModifiedBy   = CurrentUser.UserOID;
            model.ModifiedName = CurrentUser.UserName;

            return(model);
        }
コード例 #13
0
        public async Task <IActionResult> Edit(StaffEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                Staff staff = _context.Staffs.Find(model.Id);
                staff.ApplicationUser = model.ApplicationUser;
                staff.FirstName       = model.FirstName;
                staff.LastName        = model.LastName;
                staff.FullName        = model.FirstName + " " + model.LastName;
                staff.DOB             = model.DOB;
                staff.Address         = model.Address;
                staff.Gender          = model.Gender;
                staff.LocalGovt       = model.LocalGovt;
                staff.Town            = model.Town;
                staff.State           = model.State;
                staff.Position        = model.Position;
                staff.Salary          = model.Salary;
                staff.Loan            = model.Loan;
                staff.Debt            = model.Debt;

                if (model.ProfileImage != null)
                {
                    if (model.ExistingProfilePhoto != null)
                    {
                        string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images", model.ExistingProfilePhoto);
                        System.IO.File.Delete(filePath);
                    }
                    staff.ProfilePicture = UploadedFile(model);
                }

                staff.UpdateDate = DateTime.Now;
                _context.Update(staff);
                await _context.SaveChangesAsync().ConfigureAwait(true);

                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }
コード例 #14
0
        /// <summary>
        /// 新增教职工信息
        /// </summary>
        /// <param name="webModel">编辑页视图Model</param>
        /// <param name="context">数据库连接上下文对象</param>
        /// <returns></returns>
        public async Task <bool> InsertStaffAsync(StaffEditViewModel webModel, ApplicationDbContext context)
        {
            try
            {
                //Add the staff Data
                var model = await BasicRepository.InsertAsync(webModel, context);

                if (model.Id == -1)
                {
                    return(false);
                }

                //Make the transaction commit
                var index = await context.SaveChangesAsync();

                return(index == 1);
            }
            catch (Exception ex)
            {
                _logger.LogError("创建新职工失败:{0},\r\n内部错误详细信息:{1}", ex.Message, ex.InnerException.Message);
                return(false);
            }
        }
コード例 #15
0
        /// <summary>
        /// 新增教职工信息
        /// </summary>
        /// <param name="webModel">编辑页视图Model</param>
        /// <param name="context">数据库上下文对象</param>
        /// <returns></returns>
        public static async Task <IdentityUser> InsertAsync(StaffEditViewModel webModel, ApplicationDbContext context)
        {
            //Get Foreign Key Association Table Information
            //
            var department = await context.Department.AsNoTracking().Where(i => i.Id == Convert.ToInt64(webModel.DepartmentId)).FirstOrDefaultAsync();

            //return error
            if (department == null)
            {
                return(new IdentityUser
                {
                    Id = -1
                });
            }

            var model = InsertModel(webModel);

            model.DepartmentId = Convert.ToInt64(webModel.DepartmentId);
            model.Department   = department.Name;

            await context.IdentityUser.AddAsync(model);

            return(model);
        }
コード例 #16
0
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var user   = _context.Users.SingleOrDefault(p => p.Id == id);
            var roleId = Convert.ToInt32(user.Roles.SingleOrDefault().RoleId);

            if (user == null)
            {
                return(HttpNotFound());
            }

            if (User.IsInRole("admin"))
            {
                var roles = _context.Roles.ToList();
                roles.Remove(roles.SingleOrDefault(p => p.Name == "admin"));
                roles.Remove(roles.SingleOrDefault(p => p.Name == "trainee"));

                var viewModel = new AdminEditViewModel
                {
                    UserId     = id,
                    UserName   = user.UserName,
                    UserRoleId = Convert.ToInt32(user.Roles.FirstOrDefault().RoleId),
                    Roles      = roles
                };

                return(View("AdminEdit", viewModel));
            }
            else if (User.IsInRole("staff"))
            {
                var roleName = (UserRole)roleId;
                var userInfo = _context.UserInfoes.SingleOrDefault(p => p.UserId == id);

                var viewModel = new StaffEditViewModel
                {
                    UserId   = id,
                    UserName = user.UserName,
                    Email    = user.Email,
                    RoleName = roleName.ToString(),
                    RoleId   = roleId
                };

                switch (roleName)
                {
                case UserRole.Trainee:
                    viewModel.TraineeInfo = (TraineeInfo)userInfo;
                    break;

                case UserRole.Trainer:
                    viewModel.TrainerInfo = (TrainerInfo)userInfo;
                    break;

                default:
                    break;
                }

                return(View("StaffEdit", viewModel));
            }
            else
            {
                return(RedirectToAction("AccessDenied", "Error"));
            }
        }