Пример #1
0
        public async Task <string> GetNextSequenceNumber(string name)
        {
            object userCompanyId;

            TenantContext.Properties.TryGetValue("CompanyId", out userCompanyId);

            if (userCompanyId != null && typeof(ICompanyEntity <Guid?, Guid, TKey>).IsAssignableFrom(typeof(TEntity)))
            {
                var companyId = Guid.Parse(userCompanyId.ToString());
                var sequence  = await DbContext.Set <SequenceNumber>()
                                .Where(e => e.TenantId == RepositoryManager.Tenant.Id &&
                                       e.CompanyId == companyId &&
                                       e.Name == name &&
                                       e.Active &&
                                       !e.Deleted)
                                .FirstOrDefaultAsync();

                if (sequence != null)
                {
                    var generator = new SequenceNumberGenerator()
                                    .SetPrefix(sequence.Prefix)
                                    .SetSuffix(sequence.Suffix)
                                    .SetLeftPadding(sequence.LeftPadding, sequence.LeftPaddingChar)
                                    .SetRightPadding(sequence.RightPadding, sequence.RightPaddingChar)
                                    .SetEndCyclePosition(sequence.EndCyclePosition, sequence.CycleSequence)
                                    .SetResetValue(sequence.ResetValue)
                                    .SetStartingValue(sequence.StartingValue);
                    var sequenceNo = generator.GenerateSequenceNumber();
                    sequence.StartingValue = generator.StartingValue;
                    DbContext.Set <SequenceNumber>().Update(sequence);
                    return(await Task.FromResult(sequenceNo));
                }
            }

            return(null);
        }