private IEnumerable<EmployeeLogin> SRVCUpdateLogins(Helpers.Log.SessionInfo logSession, Repository.Logic.Repository rep,
            long employeeId, IEnumerable<string> addLogins, IEnumerable<string> removeLogins)
        {
#pragma warning disable 618
            logSession.Add($"Try to get employee with id = {employeeId}");
            var emp = rep.Get<Repository.Model.Employee>(e => e.EmployeeId == employeeId, false, new string[] { "Logins" }).FirstOrDefault();
            if (emp == null)
                throw new Exception(string.Format(Properties.Resources.STUFFINGSERVICE_EmployeeNotFound, employeeId));

            var existedLogins = emp.Logins.Select(r => r.DomainLogin);

            #region Add logins

            if (addLogins != null && addLogins.Any())
            {
                logSession.Add($"Add logins...");
                var addLoginsUpper = addLogins
                    .Except(existedLogins)
                    .ToArray()
                    .Select(r => rep.New<Repository.Model.EmployeeLogin>((er) =>
                    {
                        er.EmployeeLoginId = emp.EmployeeId;
                        er.DomainLogin = r;
                    }))
                    .ToArray();

                logSession.Add($"Add this logins {addLoginsUpper.Concat(r => r.DomainLogin, ",")} for employee id = {employeeId}");
                foreach (var r in addLoginsUpper)
                    emp.Logins.Add(r);

                rep.AddRange(addLoginsUpper, saveAfterInsert: false);
            }

            #endregion
            #region Remove rights

            if (removeLogins != null && removeLogins.Any())
            {
                logSession.Add($"Remove logins...");
                var removeLoginsUpper = removeLogins
                    .Intersect(existedLogins)
                    .ToArray()
                    .Join(emp.Logins, r => r, er => er.DomainLogin.ToUpper(), (r, er) => er)
                    .ToArray();

                logSession.Add($"Remove this logins {removeLoginsUpper.Concat(r => r.DomainLogin, ",")} for employee id = {employeeId}");
                foreach (var r in removeLoginsUpper)
                    emp.Logins.Remove(r);

                rep.RemoveRange(removeLoginsUpper, saveAfterRemove: false);
            }

            #endregion

            rep.SaveChanges();

            return emp.Logins.Select(er => AutoMapper.Mapper.Map<EmployeeLogin>(er));
#pragma warning restore 618
        }
        private void CheckBadRights(Helpers.Log.SessionInfo logSession, Repository.Logic.Repository rep, IEnumerable<long> rights)
        {
            logSession.Add($"Check rights data...");
#pragma warning disable 618
            var availableRights = rep.Get<Repository.Model.Right>(asNoTracking: true).ToArray();
            var badRights = rights
                .Distinct()
                .LeftOuterJoin(availableRights, r => r, r => r.RightId, (rName, r) => new { rName, r })
                .Where(r => r.r == null)
                .Concat(r => r.r.SystemName, ", ");
#pragma warning restore 618

            if (!string.IsNullOrWhiteSpace(badRights))
                throw new Exception(string.Format(Properties.Resources.STUFFINGSERVICE_RightNotFound, badRights));
        }
        private IEnumerable<EmployeeRight> SRVCUpdateRights(Helpers.Log.SessionInfo logSession, Repository.Logic.Repository rep,
            long employeeId, IEnumerable<long> addRights, IEnumerable<long> removeRights, bool checkRights = true)
        {
            if (checkRights)
                CheckBadRights(logSession, rep, Enumerable.Empty<long>()
                    .Union(addRights ?? Enumerable.Empty<long>())
                    .Union(removeRights ?? Enumerable.Empty<long>())
                    );

            logSession.Add($"Get available rights from database");
            var availableRights = rep.Get<Repository.Model.Right>(asNoTracking: true).ToArray();

#pragma warning disable 618
            logSession.Add($"Try to get employee with id = {employeeId}");
            var emp = rep.Get<Repository.Model.Employee>(e => e.EmployeeId == employeeId, false, new string[] { "Rights" }).FirstOrDefault();
            if (emp == null)
                throw new Exception(string.Format(Properties.Resources.STUFFINGSERVICE_EmployeeNotFound, employeeId));

            var existedRights = emp.Rights.Select(r => r.RightId);

            #region Add rights

            if (addRights != null && addRights.Any())
            {
                logSession.Add($"Add rights...");
                var addRightsUpper = addRights
                    .Except(existedRights)
                    .Join(availableRights, r => r, ar => ar.RightId, (r, ar) => ar)
                    .ToArray()
                    .Select(r => rep.New<Repository.Model.EmployeeRight>((er) =>
                    {
                        er.EmployeeId = emp.EmployeeId;
                        er.RightId = r.RightId;
                        //er.Employee = emp;
                        //er.Right = r;
                    })).ToArray();

                logSession.Add($"Add this rights {addRightsUpper.Concat(r => r.RightId.ToString(), ",")} for employee id = {employeeId}");
                foreach (var r in addRightsUpper)
                    emp.Rights.Add(r);

                rep.AddRange(addRightsUpper, saveAfterInsert: false);
            }

            #endregion
            #region Remove rights

            if (removeRights != null && removeRights.Any())
            {
                logSession.Add($"Remove rights...");
                var removeRightsUpper = removeRights
                    .Intersect(existedRights)
                    .Join(availableRights, r => r, ar => ar.RightId, (r, ar) => ar)
                    .ToArray()
                    .Join(emp.Rights, r => r.RightId, er => er.RightId, (r, er) => er)
                    .ToArray();

                logSession.Add($"Remove this rights {removeRightsUpper.Concat(r => r.RightId.ToString(), ",")} for employee id = {employeeId}");
                foreach (var r in removeRightsUpper)
                    emp.Rights.Remove(r);

                rep.RemoveRange(removeRightsUpper, saveAfterRemove: false);
            }

            #endregion

            rep.SaveChanges();

            return emp.Rights.Select(r => AutoMapper.Mapper.Map<EmployeeRight>(r));
#pragma warning restore 618
        }