Esempio n. 1
0
        private bool BulkSave(MemoryStream stream, out int count, out int total)
        {
            count = 0;
            var roles   = _roleService.FindAll();
            var rolDict = new Dictionary <string, RoleGroupMaster>();

            foreach (RoleGroupMaster role in roles)
            {
                rolDict.Add(role.Name, role);
            }
            using (var reader = ExcelReaderFactory.CreateReader(stream))
            {
                total = reader.RowCount - 1;
                reader.Read();

                while (reader.Read())
                {
                    string userId   = reader.GetString(0);
                    string password = reader.GetString(1);
                    string userName = reader.GetString(2);

                    string address1     = reader.GetString(3);
                    string address2     = reader.GetString(4);
                    long   pinOrZip     = (long)reader.GetDouble(5);
                    string village      = reader.GetString(6);
                    string subDistrict  = reader.GetString(7);
                    string mobileNumber = reader.GetDouble(8).ToString();
                    string roleGroup    = reader.GetString(9);
                    User   user         = new User();
                    user.UserId   = userId;
                    user.UserName = userName;
                    user.Password = password;

                    User foundUser = _userService.FindUser(userId);
                    if (foundUser != null)
                    {
                        continue;
                    }
                    Employee foundEmployee = _employeeService.FindByMobile(mobileNumber);
                    if (foundEmployee != null)
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(roleGroup) ||
                        !rolDict.ContainsKey(roleGroup))
                    {
                        continue;
                    }

                    user.RoleGroup   = null;
                    user.RoleGroupId = rolDict[roleGroup].Id;

                    user = _userService.InsertUser(user);
                    Employee employee = new Employee();

                    employee.UserId  = user.Id;
                    employee.Profile = new Database.Entity.Crm.Contact
                    {
                        ContactAddress = new Database.Entity.Crm.Address.Master
                        {
                            AddressLine1      = address1,
                            AddressLine2      = address2,
                            LocalityOrVillage = village,
                            PinOrZip          = pinOrZip,
                            SubDistrict       = subDistrict,
                        },
                        MobileNumber = mobileNumber
                    };
                    employee = baseService.Save(employee);
                    if (employee == null)
                    {
                        continue;
                    }

                    count++;
                }
            }
            return(true);
        }