Esempio n. 1
0
        private async Task <bool> AddToConsoleDetailsAsync(Decos.Domain.Deco deco)
        {
            var adapter = new Adapters.Oal.dbo_console_detailsAdapter();
            var id      = await adapter.ExecuteScalarAsync <string>($"SELECT [id] FROM dbo.console_details WHERE [console_no] = '{deco.ConsoleTag}'");

            var consoleTagNotInConsoleDetails = string.IsNullOrWhiteSpace(id);

            if (consoleTagNotInConsoleDetails)
            {
                var map = new Integrations.Transforms.RtsDecoOalConsoleDetails();
                var row = await map.TransformAsync(deco);

                // check for duplicate
                var dpr = await Policy.Handle <SqlException>(e => e.IsTimeout())
                          .WaitAndRetryAsync(RetryCount, WaitInterval)
                          .ExecuteAndCaptureAsync(() => adapter.ExecuteScalarAsync <int>($"SELECT COUNT(*) FROM dbo.console_details WHERE console_no ='{row.console_no}'"));

                if (dpr.Result == 0)
                {
                    var pr = Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout())
                             .WaitAndRetryAsync(RetryCount, WaitInterval)
                             .ExecuteAndCaptureAsync(() => adapter.InsertAsync(row));
                    var result = await pr;
                    if (result.FinalException != null)
                    {
                        throw result.FinalException; // send to dead letter queue
                    }
                    System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row");
                }
                return(true);
            }

            var detailsPolly = await Policy.Handle <SqlException>(e => e.IsTimeout())
                               .WaitAndRetryAsync(RetryCount, WaitInterval)
                               .ExecuteAndCaptureAsync(async() => await adapter.LoadOneAsync(id));

            if (null != detailsPolly.FinalException)
            {
                throw new Exception("Console Details Polly Error", detailsPolly.FinalException);
            }

            var details = detailsPolly.Result;

            var decoDateTime = deco.Date.AddHours(deco.Time.Hour).AddMinutes(deco.Time.Minute).AddSeconds(deco.Time.Second);

            if (details.courier_id == deco.CourierId && details.office_no == deco.LocationId && (details.date_field ?? DateTime.MinValue).AddHours(28) >= decoDateTime)
            {
                var notes = details.item_consignments.Split(new[] { ',', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                notes.AddRange(deco.AllConsignmentnNotes.Split(new[] { ',', '\t' }, StringSplitOptions.RemoveEmptyEntries));
                details.item_consignments = string.Join("\t", notes.OrderBy(x => x).Where(x => x.Length > 3 /* remove anything less*/).Distinct());
                await adapter.UpdateAsync(details);

                return(true);
            }

            await InsertConsoleDuplicationErrorAndEventExceptionAsync(deco);

            return(false);
        }
Esempio n. 2
0
        private async Task InsertConsoleDuplicationErrorAndEventExceptionAsync(Decos.Domain.Deco deco)
        {
            Console.WriteLine("insert into console_duplicate_error & event_exception");

            //
            var map         = new Integrations.Transforms.RtsDecoOalConsoleDetails();
            var decoDetails = await map.TransformAsync(deco);

            var error = new Adapters.Oal.dbo_console_duplicate_error
            {
                item_consignments         = decoDetails.item_consignments,
                date_field                = decoDetails.date_field,
                courier_id                = decoDetails.courier_id,
                batch_name                = decoDetails.batch_name,
                beat_no                   = decoDetails.beat_no,
                console_no                = decoDetails.console_no,
                console_type              = null,
                console_type_desc         = null,
                courier_name              = decoDetails.courier_name,
                dt_created_oal_date_field = decoDetails.dt_created_oal_date_field,
                event_comment             = decoDetails.event_comment,
                id                 = GenerateId(20),
                event_type         = decoDetails.event_type,
                office_dest        = null,
                version            = 0,
                office_dest_name   = null,
                office_name        = decoDetails.office_name,
                office_next_code   = decoDetails.office_next_code,
                office_no          = decoDetails.office_no,
                other_console_type = null,
                routing_code       = null
            };
            var errorAdapter = new Adapters.Oal.dbo_console_duplicate_errorAdapter();
            await errorAdapter.InsertAsync(error);

            var exc = new Adapters.Oal.dbo_event_exception
            {
                consignment_no = error.console_no,
                date_field     = error.date_field,
                batch_name     = error.batch_name,
                office_no      = error.office_no,
                version        = error.version,
                id             = GenerateId(34),
                courier_id     = error.courier_id,
                event_class    = "pos.oal.DeliveryConsoleEventNew",
                event_id       = error.id
            };
            var excAdapter = new Adapters.Oal.dbo_event_exceptionAdapter();
            await excAdapter.InsertAsync(exc);
        }