Example #1
0
        private async Task ValidateLicenses(
            ImportResultLoginUser result,
            int companyId, int loginUserId, CancellationToken token)
        {
            var licenseKeysCount    = (await loginUserLicenseProcessor.GetAsync(companyId, token)).Count();
            var loginUsersUseClient = (await loginUserQueryProcessor.GetAsync(new LoginUserSearch {
                CompanyId = companyId, UseClient = 1,
            })).ToArray();

            if (loginUsersUseClient.Length > licenseKeysCount)
            {
                result.LicenseIsOrver = true;
            }

            var Ids       = new int[] { loginUserId };
            var loginUser = (await loginUserQueryProcessor.GetAsync(new LoginUserSearch {
                Ids = Ids
            })).ToList();

            if (loginUser == null || loginUser.Count < 1)
            {
                result.NotExistsLoginUser = true;
            }
            else if (loginUser[0].UseClient == 0)
            {
                result.LoginUserHasNotLoginLicense = true;
            }
        }
Example #2
0
        public async Task <ImportResult> ImportAsync(
            IEnumerable <LoginUser> InsertList,
            IEnumerable <LoginUser> UpdateList,
            IEnumerable <LoginUser> DeleteList, CancellationToken token = default(CancellationToken))
        {
            using (var scope = transactionScopeBuilder.Create())
            {
                var deleteCount = 0;
                var updateCount = 0;
                var insertCount = 0;


                var first = InsertList.FirstOrDefault() ??
                            UpdateList.FirstOrDefault() ??
                            DeleteList.FirstOrDefault();
                if (first == null)
                {
                    throw new ArgumentNullException($"{nameof(InsertList)} or {nameof(UpdateList)}");
                }

                var companyId   = first.CompanyId;
                var loginUserId = first.UpdateBy;

                var isNotUseDistribution = (await applicationControlByCompanyId.GetAsync(companyId, token)).UseDistribution == 0;

                foreach (var delete in DeleteList)
                {
                    await deleteIdenticalQueryProcessor.DeleteAsync(delete.Id, token);

                    deleteCount++;
                }

                foreach (var update in UpdateList)
                {
                    if (isNotUseDistribution)
                    {
                        update.UseClient = 1;
                    }
                    await addLoginUserQueryProcessor.SaveAsync(update);

                    updateCount++;
                }

                foreach (var add in InsertList)
                {
                    if (isNotUseDistribution)
                    {
                        add.UseClient = 1;
                    }
                    var saveResult = await addLoginUserQueryProcessor.SaveAsync(add);

                    if (saveResult != null && !string.IsNullOrEmpty(add.InitialPassword))
                    {
                        await loginUserPasswordProcessor.SaveAsync(saveResult.CompanyId, saveResult.Id, add.InitialPassword);
                    }
                    insertCount++;
                }

                var result = new ImportResultLoginUser
                {
                    ProcessResult = new ProcessResult {
                        Result = true
                    },
                    InsertCount = insertCount,
                    UpdateCount = updateCount,
                    DeleteCount = deleteCount,
                };

                await ValidateLicenses(result, companyId, loginUserId, token);

                if (!(result.LicenseIsOrver || result.NotExistsLoginUser || result.LoginUserHasNotLoginLicense))
                {
                    scope.Complete();
                }

                return(result);
            }
        }