Exemplo n.º 1
0
        public bool SaveModifyEmployee(EmployeeModel model)
        {
            var employeeService = new EmployeeService();
            var roleService = new RoleService();
            string result = "";
            try
            {
                //SHA512加密
                if (model.ConfirmPassWd != null)
                {
                    SHA512 sha512 = new SHA512Managed();
                    byte[] s = sha512.ComputeHash(Encoding.UTF8.GetBytes(model.ConfirmPassWd));
                    for (int i = 0; i < s.Length; i++)
                    {
                        result += s[i].ToString("X2");
                    }
                    sha512.Clear();
                }

                //修改人员信息
                employee newemployee = employeeService.Find(model.EmployeeId);
                newemployee.Name = model.EmployeeName;
                newemployee.Number = model.EmployeeNumber;
                newemployee.RFID = model.Rfid;
                newemployee.LogOn = model.Logon;
                newemployee.Sex = model.Sex;
                newemployee.State = model.state;
                newemployee.Birthday = Convert.ToDateTime(model.Birthday);
                newemployee.Memo = model.Memo;
                if (model.ConfirmPassWd != null)
                {
                    newemployee.Password = result.ToLower();
                }
                employeeService.Update(newemployee);

                //修改角色信息
                //如果增加的角色类型数据库中没有,则插入该角色
                foreach (long t in model.RoleTypeId)
                {
                    List<role> roles = roleService.FindByEmployeeId(model.EmployeeId);
                    int flag = 1;
                    foreach (role role in roles)
                    {
                        if (t == role.RoleId)
                        {
                            flag = 0;
                        }
                    }
                    if (flag == 1)
                    {
                        var role = new role
                                       {
                                           EmployeeId = model.EmployeeId,
                                           RoleId = t
                                       };
                        roleService.Insert(role);
                    }
                }

                //   如果删除了一个角色类型,则在数据库删除对应的角色类型
                List<role> anotherroles = roleService.FindByEmployeeId(model.EmployeeId);
                foreach (role anotherrole in anotherroles)
                {
                    int flag = 1;
                    long delete = anotherrole.RoleId;
                    for (int j = 0; j < model.RoleTypeId.Count; j++)
                    {
                        if (anotherrole.RoleId == model.RoleTypeId[j])
                        {
                            flag = 0;
                        }
                    }
                    if (flag == 1)
                    {
                        roleService = new RoleService();
                        roleService.Delete(
                            roleService.Find(roleService.FindByEmployeeIdAndRoleId(model.EmployeeId, delete).Id));
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }